PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/sharedaddy/sharing-sources.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 1184 lines | 895 code | 243 blank | 46 comment | 163 complexity | 3b81de2b9c87c34da3d7cd4fa193beb9 MD5 | raw file
  1. <?php
  2. abstract class Sharing_Source {
  3. public $button_style;
  4. public $smart;
  5. protected $open_links;
  6. protected $id;
  7. public function __construct( $id, array $settings ) {
  8. $this->id = $id;
  9. if ( isset( $settings['button_style'] ) )
  10. $this->button_style = $settings['button_style'];
  11. if ( isset( $settings['open_links'] ) )
  12. $this->open_links = $settings['open_links'];
  13. if ( isset( $settings['smart'] ) )
  14. $this->smart = $settings['smart'];
  15. }
  16. public function http() {
  17. return is_ssl() ? 'https' : 'http';
  18. }
  19. public function get_id() {
  20. return $this->id;
  21. }
  22. public function get_class() {
  23. return $this->id;
  24. }
  25. public function get_share_url( $post_id ) {
  26. return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id );
  27. }
  28. public function has_custom_button_style() {
  29. return false;
  30. }
  31. public function get_link( $url, $text, $title, $query = '', $id = false ) {
  32. $klasses = array( 'share-'.$this->get_class(), 'sd-button' );
  33. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' )
  34. $klasses[] = 'share-icon';
  35. if ( $this->button_style == 'icon' ) {
  36. $text = '';
  37. $klasses[] = 'no-text';
  38. }
  39. if ( !empty( $query ) ) {
  40. if ( stripos( $url, '?' ) === false )
  41. $url .= '?'.$query;
  42. else
  43. $url .= '&amp;'.$query;
  44. }
  45. if ( $this->button_style == 'text' )
  46. $klasses[] = 'no-icon';
  47. return sprintf(
  48. '<a rel="nofollow" class="%s" href="%s"%s title="%s"%s><span>%s</span></a>',
  49. implode( ' ', $klasses ),
  50. $url,
  51. ( $this->open_links == 'new' ) ? ' target="_blank"' : '',
  52. $title,
  53. ( $id ? ' id="' . esc_attr( $id ) . '"' : '' ),
  54. $text
  55. );
  56. }
  57. abstract public function get_name();
  58. abstract public function get_display( $post );
  59. public function display_header() {
  60. }
  61. public function display_footer() {
  62. }
  63. public function has_advanced_options() {
  64. return false;
  65. }
  66. public function display_preview() {
  67. $text = '&nbsp;';
  68. if ( !$this->smart )
  69. if ( $this->button_style != 'icon' )
  70. $text = $this->get_name();
  71. $klasses = array( 'share-'.$this->get_class(), 'sd-button' );
  72. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' )
  73. $klasses[] = 'share-icon';
  74. if ( $this->button_style == 'icon' )
  75. $klasses[] = 'no-text';
  76. if ( $this->button_style == 'text' )
  77. $klasses[] = 'no-icon';
  78. $link = sprintf(
  79. '<a rel="nofollow" class="%s" href="javascript:void(0);return false;" title="%s"><span>%s</span></a>',
  80. implode( ' ', $klasses ),
  81. $this->get_name(),
  82. $text
  83. );
  84. ?>
  85. <div class="option option-smart-<?php echo $this->smart ? 'on' : 'off'; ?>">
  86. <?php echo $link; ?>
  87. </div><?php
  88. }
  89. public function get_total( $post = false ) {
  90. global $wpdb, $blog_id;
  91. $name = strtolower( $this->get_id() );
  92. if ( $post == false ) {
  93. // get total number of shares for service
  94. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) );
  95. }
  96. // get total shares for a post
  97. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $blog_id, $post->ID, $name ) );
  98. }
  99. public function get_posts_total() {
  100. global $wpdb, $blog_id;
  101. $totals = array();
  102. $name = strtolower( $this->get_id() );
  103. $my_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d AND share_service = %s GROUP BY post_id ORDER BY count DESC ", $blog_id, $name ) );
  104. if ( !empty( $my_data ) )
  105. foreach( $my_data as $row )
  106. $totals[] = new Sharing_Post_Total( $row->id, $row->total );
  107. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
  108. return $totals;
  109. }
  110. public function process_request( $post, array $post_data ) {
  111. do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) );
  112. }
  113. public function js_dialog( $name, $params = array() ) {
  114. $defaults = array(
  115. 'menubar' => 1,
  116. 'resizable' => 1,
  117. 'width' => 600,
  118. 'height' => 400,
  119. );
  120. $params = array_merge( $defaults, $params );
  121. $opts = array();
  122. foreach( $params as $key => $val ) {
  123. $opts[] = "$key=$val";
  124. }
  125. $opts = implode( ',', $opts );
  126. ?>
  127. <script type="text/javascript" charset="utf-8">
  128. jQuery(document).ready(function(){
  129. jQuery( '.share-<?php echo $name; ?>' ).click(function(){
  130. window.open( jQuery(this).attr( 'href' ), 'wpcom<?php echo $name; ?>', '<?php echo $opts; ?>' );
  131. return false;
  132. });
  133. });
  134. </script>
  135. <?php
  136. }
  137. }
  138. abstract class Sharing_Advanced_Source extends Sharing_Source {
  139. public function has_advanced_options() {
  140. return true;
  141. }
  142. abstract public function display_options();
  143. abstract public function update_options( array $data );
  144. abstract public function get_options();
  145. }
  146. class Share_Email extends Sharing_Source {
  147. var $shortname = 'email';
  148. public function __construct( $id, array $settings ) {
  149. parent::__construct( $id, $settings );
  150. if ( 'official' == $this->button_style )
  151. $this->smart = true;
  152. else
  153. $this->smart = false;
  154. }
  155. public function get_name() {
  156. return __( 'Email', 'jetpack' );
  157. }
  158. // Default does nothing
  159. public function process_request( $post, array $post_data ) {
  160. $ajax = false;
  161. if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' )
  162. $ajax = true;
  163. $source_email = $target_email = $source_name = false;
  164. if ( isset( $post_data['source_email'] ) && is_email( $post_data['source_email'] ) )
  165. $source_email = $post_data['source_email'];
  166. if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) )
  167. $target_email = $post_data['target_email'];
  168. if ( isset( $post_data['source_name'] ) )
  169. $source_name = $post_data['source_name'];
  170. // Test email
  171. $error = 1; // Failure in data
  172. if ( $source_email && $target_email && $source_name ) {
  173. if ( apply_filters( 'sharing_email_check', true, $post, $post_data ) ) {
  174. $data = array(
  175. 'post' => $post,
  176. 'source' => $source_email,
  177. 'target' => $target_email,
  178. 'name' => $source_name
  179. );
  180. if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) {
  181. // Record stats
  182. parent::process_request( $data['post'], $post_data );
  183. do_action( 'sharing_email_send_post', $data );
  184. }
  185. // Return a positive regardless of whether the user is subscribed or not
  186. if ( $ajax ) {
  187. ?>
  188. <div class="response">
  189. <div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div>
  190. <div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div>
  191. <div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div>
  192. </div>
  193. <?php
  194. }
  195. else
  196. wp_safe_redirect( get_permalink( $post->ID ).'?shared=email' );
  197. die();
  198. }
  199. else
  200. $error = 2; // Email check failed
  201. }
  202. if ( $ajax )
  203. echo $error;
  204. else
  205. wp_safe_redirect( get_permalink( $post->ID ).'?shared=email&msg=fail' );
  206. die();
  207. }
  208. public function get_display( $post ) {
  209. return $this->get_link( get_permalink( $post->ID ), _x( 'Email', 'share to', 'jetpack' ), __( 'Click to email this to a friend', 'jetpack' ), 'share=email' );
  210. }
  211. /**
  212. * Outputs the hidden email dialog
  213. */
  214. public function display_footer() {
  215. global $current_user;
  216. $visible = $status = false;
  217. ?>
  218. <div id="sharing_email" style="display: none;">
  219. <form action="" method="post">
  220. <label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label>
  221. <input type="text" name="target_email" id="target_email" value="" />
  222. <?php if ( is_user_logged_in() ) : ?>
  223. <input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" />
  224. <input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" />
  225. <?php else : ?>
  226. <label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label>
  227. <input type="text" name="source_name" id="source_name" value="" />
  228. <label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label>
  229. <input type="text" name="source_email" id="source_email" value="" />
  230. <?php endif; ?>
  231. <?php do_action( 'sharing_email_dialog', 'jetpack' ); ?>
  232. <img style="float: right; display: none" class="loading" src="<?php echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" />
  233. <input type="submit" value="<?php _e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" />
  234. <a href="#cancel" class="sharing_cancel"><?php _e( 'Cancel', 'jetpack' ); ?></a>
  235. <div class="errors errors-1" style="display: none;">
  236. <?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?>
  237. </div>
  238. <div class="errors errors-2" style="display: none;">
  239. <?php _e( 'Email check failed, please try again', 'jetpack' ); ?>
  240. </div>
  241. <div class="errors errors-3" style="display: none;">
  242. <?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?>
  243. </div>
  244. </form>
  245. </div>
  246. <?php
  247. }
  248. }
  249. class Share_Twitter extends Sharing_Source {
  250. var $shortname = 'twitter';
  251. public function __construct( $id, array $settings ) {
  252. parent::__construct( $id, $settings );
  253. if ( 'official' == $this->button_style )
  254. $this->smart = true;
  255. else
  256. $this->smart = false;
  257. }
  258. public function get_name() {
  259. return __( 'Twitter', 'jetpack' );
  260. }
  261. function sharing_twitter_via( $post ) {
  262. // Allow themes to customize the via
  263. return apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID );
  264. }
  265. public function get_related_accounts( $post ) {
  266. // Format is 'username' => 'Optional description'
  267. $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
  268. // Example related string: account1,account2:Account 2 description,account3
  269. $related = array();
  270. foreach ( $related_accounts as $related_account_username => $related_account_description ) {
  271. // Join the description onto the end of the username
  272. if ( $related_account_description )
  273. $related_account_username .= ':' . $related_account_description;
  274. $related[] = $related_account_username;
  275. }
  276. return implode( ',', $related );
  277. }
  278. public function get_display( $post ) {
  279. $via = $this->sharing_twitter_via( $post );
  280. if ( $via ) {
  281. $via = '&via=' . rawurlencode( $via );
  282. $related = $this->get_related_accounts( $post );
  283. if ( ! empty( $related ) && $related !== $via )
  284. $via .= '&related=' . rawurlencode( $related );
  285. } else {
  286. $via = '';
  287. }
  288. $share_url = $this->get_share_url( $post->ID );
  289. if ( $this->smart ) {
  290. return '<div class="twitter_button"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="' . esc_url( $this->http() . '://platform.twitter.com/widgets/tweet_button.html?url=' . rawurlencode( $share_url ) . '&counturl=' . rawurlencode( str_replace( 'https://', 'http://', get_permalink( $post->ID ) ) ) . '&count=horizontal&text=' . rawurlencode( $post->post_title . ':' ) . $via ) . '" style="width:101px; height:20px;"></iframe></div>';
  291. } else {
  292. if ( 'icon-text' == $this->button_style || 'text' == $this->button_style )
  293. sharing_register_post_for_share_counts( $post->ID );
  294. return $this->get_link( get_permalink( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID );
  295. }
  296. }
  297. public function process_request( $post, array $post_data ) {
  298. $post_title = $post->post_title;
  299. $post_link = $this->get_share_url( $post->ID );
  300. if ( function_exists( 'mb_stripos' ) ) {
  301. $strlen = 'mb_strlen';
  302. $substr = 'mb_substr';
  303. } else {
  304. $strlen = 'strlen';
  305. $substr = 'substr';
  306. }
  307. $via = $this->sharing_twitter_via( $post );
  308. if ( $via ) {
  309. $related = $this->get_related_accounts( $post );
  310. if ( $related === $via )
  311. $related = false;
  312. $sig = " via @$via";
  313. } else {
  314. $via = false;
  315. $related = false;
  316. $sig = '';
  317. }
  318. $suffix_length = $strlen( " {$post_link}{$sig}" );
  319. // $sig is handled by twitter in their 'via' argument.
  320. // $post_link is handled by twitter in their 'url' argument.
  321. if ( 140 < $strlen( $post_title ) + $suffix_length ) {
  322. // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
  323. $text = $substr( $post_title, 0, 140 - $suffix_length - 1 ) . "\xE2\x80\xA6";
  324. } else {
  325. $text = $post_title;
  326. }
  327. // Record stats
  328. parent::process_request( $post, $post_data );
  329. $url = $post_link;
  330. $twitter_url = add_query_arg(
  331. urlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
  332. sprintf( '%s://twitter.com/intent/tweet', $this->http() )
  333. );
  334. // Redirect to Twitter
  335. wp_redirect( $twitter_url );
  336. die();
  337. }
  338. public function has_custom_button_style() {
  339. return $this->smart;
  340. }
  341. public function display_footer() {
  342. $this->js_dialog( $this->shortname, array( 'height' => 350 ) );
  343. }
  344. }
  345. class Share_Stumbleupon extends Sharing_Source {
  346. var $shortname = 'stumbleupon';
  347. public function __construct( $id, array $settings ) {
  348. parent::__construct( $id, $settings );
  349. if ( 'official' == $this->button_style )
  350. $this->smart = true;
  351. else
  352. $this->smart = false;
  353. }
  354. public function get_name() {
  355. return __( 'StumbleUpon', 'jetpack' );
  356. }
  357. public function has_custom_button_style() {
  358. return $this->smart;
  359. }
  360. public function get_display( $post ) {
  361. if ( $this->smart )
  362. return '<div class="stumbleupon_button"><iframe src="http://www.stumbleupon.com/badge/embed/1/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $post->post_title ) . '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px;" allowTransparency="true"></iframe></div>';
  363. else
  364. return $this->get_link( get_permalink( $post->ID ), _x( 'StumbleUpon', 'share to', 'jetpack' ), __( 'Click to share on StumbleUpon', 'jetpack' ), 'share=stumbleupon' );
  365. }
  366. public function process_request( $post, array $post_data ) {
  367. $stumbleupon_url = $this->http() . '://www.stumbleupon.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title );
  368. // Record stats
  369. parent::process_request( $post, $post_data );
  370. // Redirect to Stumbleupon
  371. wp_redirect( $stumbleupon_url );
  372. die();
  373. }
  374. }
  375. class Share_Reddit extends Sharing_Source {
  376. var $shortname = 'reddit';
  377. public function __construct( $id, array $settings ) {
  378. parent::__construct( $id, $settings );
  379. if ( 'official' == $this->button_style )
  380. $this->smart = true;
  381. else
  382. $this->smart = false;
  383. }
  384. public function get_name() {
  385. return __( 'Reddit', 'jetpack' );
  386. }
  387. public function get_display( $post ) {
  388. if ( $this->smart )
  389. return '<div class="reddit_button"><iframe src="http://www.reddit.com/static/button/button1.html?width=120&amp;url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $post->post_title ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>';
  390. else
  391. return $this->get_link( get_permalink( $post->ID ), __( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
  392. }
  393. public function process_request( $post, array $post_data ) {
  394. $reddit_url = 'http://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title );
  395. // Record stats
  396. parent::process_request( $post, $post_data );
  397. // Redirect to Reddit
  398. wp_redirect( $reddit_url );
  399. die();
  400. }
  401. }
  402. class Share_Digg extends Sharing_Source {
  403. var $shortname = 'digg';
  404. public function __construct( $id, array $settings ) {
  405. parent::__construct( $id, $settings );
  406. if ( 'official' == $this->button_style )
  407. $this->smart = true;
  408. else
  409. $this->smart = false;
  410. }
  411. public function get_name() {
  412. return __( 'Digg', 'jetpack' );
  413. }
  414. public function has_custom_button_style() {
  415. return $this->smart;
  416. }
  417. public function get_display( $post ) {
  418. if ( $this->smart ) {
  419. $url = $this->get_link( 'http://digg.com/submit?url='. rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $post->post_title ), 'Digg', __( 'Click to Digg this post', 'jetpack' ) );
  420. return '<div class="digg_button">' . str_replace( 'class="', 'class="DiggThisButton DiggCompact ', $url ) . '</div>';
  421. } else {
  422. return $this->get_link( get_permalink( $post->ID ), _x( 'Digg', 'share to', 'jetpack' ), __( 'Click to Digg this post', 'jetpack' ), 'share=digg' );
  423. }
  424. }
  425. public function process_request( $post, array $post_data ) {
  426. $digg_url = 'http://digg.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title );
  427. // Record stats
  428. parent::process_request( $post, $post_data );
  429. // Redirect to Digg
  430. wp_redirect( $digg_url );
  431. die();
  432. }
  433. public function display_header() {
  434. if ( $this->smart ) {
  435. ?>
  436. <script type="text/javascript">
  437. (function() {
  438. var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
  439. s.type = 'text/javascript';
  440. s.async = true;
  441. s.src = 'http://widgets.digg.com/buttons.js';
  442. s1.parentNode.insertBefore(s, s1);
  443. })();
  444. </script>
  445. <?php
  446. }
  447. }
  448. }
  449. class Share_LinkedIn extends Sharing_Source {
  450. var $shortname = 'linkedin';
  451. public function __construct( $id, array $settings ) {
  452. parent::__construct( $id, $settings );
  453. if ( 'official' == $this->button_style )
  454. $this->smart = true;
  455. else
  456. $this->smart = false;
  457. }
  458. public function get_name() {
  459. return __( 'LinkedIn', 'jetpack' );
  460. }
  461. public function has_custom_button_style() {
  462. return $this->smart;
  463. }
  464. public function get_display( $post ) {
  465. $share_url = $this->get_share_url( $post->ID );
  466. $display = '';
  467. if ( $this->smart )
  468. $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
  469. else
  470. $display = $this->get_link( get_permalink( $post->ID ), _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID );
  471. if ( 'icon-text' == $this->button_style || 'text' == $this->button_style )
  472. sharing_register_post_for_share_counts( $post->ID );
  473. return $display;
  474. }
  475. public function process_request( $post, array $post_data ) {
  476. setup_postdata( $post );
  477. $post_link = $this->get_share_url( $post->ID );
  478. // http://www.linkedin.com/shareArticle?mini=true&url={articleUrl}&title={articleTitle}&summary={articleSummary}&source={articleSource}
  479. $encoded_title = rawurlencode( $post->post_title );
  480. if( strlen( $encoded_title ) > 200 )
  481. $encoded_title = substr( $encoded_title, 0, 197 ) . '...';
  482. $encoded_summary = rawurlencode( strip_tags( get_the_excerpt() ) );
  483. if( strlen( $encoded_summary ) > 256 )
  484. $encoded_summary = substr( $encoded_summary, 0, 253 ) . '...';
  485. $source = get_bloginfo( 'name' );
  486. $linkedin_url = add_query_arg( array(
  487. 'title' => $encoded_title,
  488. 'url' => rawurlencode( $post_link ),
  489. 'source' => rawurlencode( $source ),
  490. 'summary' => $encoded_summary,
  491. ), 'http://www.linkedin.com/shareArticle?mini=true' );
  492. // Record stats
  493. parent::process_request( $post, $post_data );
  494. // Redirect to LinkedIn
  495. wp_redirect( $linkedin_url );
  496. die();
  497. }
  498. public function display_footer() {
  499. if ( !$this->smart )
  500. $this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) );
  501. else
  502. echo '<script type="text/javascript" src="//platform.linkedin.com/in.js"></script>';
  503. }
  504. }
  505. class Share_Facebook extends Sharing_Source {
  506. var $shortname = 'facebook';
  507. private $share_type = 'default';
  508. public function __construct( $id, array $settings ) {
  509. parent::__construct( $id, $settings );
  510. if ( isset( $settings['share_type'] ) )
  511. $this->share_type = $settings['share_type'];
  512. if ( 'official' == $this->button_style )
  513. $this->smart = true;
  514. else
  515. $this->smart = false;
  516. }
  517. public function get_name() {
  518. return __( 'Facebook', 'jetpack' );
  519. }
  520. public function display_header() {
  521. }
  522. function guess_locale_from_lang( $lang ) {
  523. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) {
  524. return 'en_US';
  525. }
  526. if ( !class_exists( 'GP_Locales' ) ) {
  527. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  528. return false;
  529. }
  530. require JETPACK__GLOTPRESS_LOCALES_PATH;
  531. }
  532. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  533. // WP.com: get_locale() returns 'it'
  534. $locale = GP_Locales::by_slug( $lang );
  535. } else {
  536. // Jetpack: get_locale() returns 'it_IT';
  537. $locale = GP_Locales::by_field( 'wp_locale', $lang );
  538. }
  539. if ( !$locale || empty( $locale->facebook_locale ) ) {
  540. return false;
  541. }
  542. return $locale->facebook_locale;
  543. }
  544. public function get_display( $post ) {
  545. $share_url = $this->get_share_url( $post->ID );
  546. if ( $this->smart ) {
  547. $url = $this->http() . '://www.facebook.com/plugins/like.php?href=' . rawurlencode( $share_url ) . '&amp;layout=button_count&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=21';
  548. // Default widths to suit English
  549. $inner_w = 90;
  550. // Locale-specific widths/overrides
  551. $widths = array(
  552. 'bg_BG' => 120,
  553. 'de_DE' => 120,
  554. 'da_DK' => 120,
  555. 'es_ES' => 122,
  556. 'es_LA' => 110,
  557. 'fi_FI' => 100,
  558. 'it_IT' => 100,
  559. 'ja_JP' => 100,
  560. 'nl_NL' => 130,
  561. 'ru_RU' => 128,
  562. );
  563. $widths = apply_filters( 'sharing_facebook_like_widths', $widths );
  564. $locale = $this->guess_locale_from_lang( get_locale() );
  565. if ( $locale ) {
  566. if ( 'en_US' != $locale ) {
  567. $url .= '&amp;locale=' . $locale;
  568. }
  569. if ( isset( $widths[$locale] ) ) {
  570. $inner_w = $widths[$locale];
  571. }
  572. }
  573. $url .= '&amp;width='.$inner_w;
  574. return '<div class="like_button"><iframe src="'.$url.'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'.( $inner_w + 6 ).'px; height:21px;" allowTransparency="true"></iframe></div>';
  575. }
  576. if ( 'icon-text' == $this->button_style || 'text' == $this->button_style )
  577. sharing_register_post_for_share_counts( $post->ID );
  578. return $this->get_link( get_permalink( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID );
  579. }
  580. public function process_request( $post, array $post_data ) {
  581. $fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $post->post_title );
  582. // Record stats
  583. parent::process_request( $post, $post_data );
  584. // Redirect to Facebook
  585. wp_redirect( $fb_url );
  586. die();
  587. }
  588. public function display_footer() {
  589. $this->js_dialog( $this->shortname );
  590. }
  591. }
  592. class Share_Print extends Sharing_Source {
  593. var $shortname = 'print';
  594. public function __construct( $id, array $settings ) {
  595. parent::__construct( $id, $settings );
  596. if ( 'official' == $this->button_style )
  597. $this->smart = true;
  598. else
  599. $this->smart = false;
  600. }
  601. public function get_name() {
  602. return __( 'Print', 'jetpack' );
  603. }
  604. public function get_display( $post ) {
  605. return $this->get_link( get_permalink( $post->ID ) . ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) );
  606. }
  607. }
  608. class Share_PressThis extends Sharing_Source {
  609. var $shortname = 'pressthis';
  610. public function __construct( $id, array $settings ) {
  611. parent::__construct( $id, $settings );
  612. if ( 'official' == $this->button_style )
  613. $this->smart = true;
  614. else
  615. $this->smart = false;
  616. }
  617. public function get_name() {
  618. return __( 'Press This', 'jetpack' );
  619. }
  620. public function process_request( $post, array $post_data ) {
  621. global $current_user;
  622. $blogs = get_blogs_of_user( $current_user->ID );
  623. if ( empty( $blogs ) ) {
  624. wp_safe_redirect( get_permalink( $post->ID ) );
  625. die();
  626. }
  627. $blog = current( $blogs );
  628. $url = $blog->siteurl.'/wp-admin/press-this.php?u='.rawurlencode( $this->get_share_url( $post->ID ) ).'&t='.rawurlencode( $post->post_title ).'&v=4';
  629. if ( isset( $_GET['sel'] ) )
  630. $url .= '&s='.rawurlencode( $_GET['sel'] );
  631. // Record stats
  632. parent::process_request( $post, $post_data );
  633. // Redirect to Press This
  634. wp_safe_redirect( $url );
  635. die();
  636. }
  637. public function get_display( $post ) {
  638. return $this->get_link( get_permalink( $post->ID ), _x( 'Press This', 'share to', 'jetpack' ), __( 'Click to Press This!', 'jetpack' ), 'share=press-this' );
  639. }
  640. }
  641. class Share_GooglePlus1 extends Sharing_Source {
  642. var $shortname = 'googleplus1';
  643. private $state = false;
  644. public function __construct( $id, array $settings ) {
  645. parent::__construct( $id, $settings );
  646. if ( 'official' == $this->button_style )
  647. $this->smart = true;
  648. else
  649. $this->smart = false;
  650. }
  651. public function get_name() {
  652. return __( 'Google +1', 'jetpack' );
  653. }
  654. public function get_display( $post ) {
  655. // Smart or not, return the G+ button
  656. return '<div class="googleplus1_button"><div class="g-plusone" data-size="medium" data-callback="sharing_plusone" data-href="' . esc_url( $this->get_share_url( $post->ID ) ) . '"></div></div>';
  657. }
  658. public function display_preview() {
  659. ?>
  660. <div class="option option-smart-on">
  661. <a href="javascript:void(0);return false;" class="share-<?php echo $this->shortname; ?>">
  662. <span></span>
  663. </a>
  664. </div><?php
  665. }
  666. public function get_state() {
  667. return $this->state;
  668. }
  669. public function process_request( $post, array $post_data ) {
  670. if ( isset( $post_data['state'] ) ) {
  671. $this->state = $post_data['state'];
  672. }
  673. // Record stats
  674. parent::process_request( $post, $post_data );
  675. die();
  676. }
  677. public function display_footer() {
  678. global $post;
  679. ?>
  680. <script type="text/javascript" charset="utf-8">
  681. function sharing_plusone( obj ) {
  682. jQuery.ajax( {
  683. url: '<?php echo get_permalink( $post->ID ) . '?share=google-plus-1'; ?>',
  684. type: 'POST',
  685. data: obj
  686. } );
  687. }
  688. </script>
  689. <script type="text/javascript" src="<?php echo $this->http(); ?>://apis.google.com/js/plusone.js"></script>
  690. <?php
  691. }
  692. public function get_total( $post = false ) {
  693. global $wpdb, $blog_id;
  694. $name = strtolower( $this->get_id() );
  695. if ( $post == false ) {
  696. // get total number of shares for service
  697. return $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) );
  698. }
  699. //get total shares for a post
  700. return $wpdb->get_var( $wpdb->prepare( "SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $blog_id, $post->ID, $name ) );
  701. }
  702. }
  703. class Share_Custom extends Sharing_Advanced_Source {
  704. private $name;
  705. private $icon;
  706. private $url;
  707. public $smart = true;
  708. var $shortname;
  709. public function get_class() {
  710. return 'custom';
  711. }
  712. public function __construct( $id, array $settings ) {
  713. parent::__construct( $id, $settings );
  714. $opts = $this->get_options();
  715. if ( isset( $settings['name'] ) ) {
  716. $this->name = $settings['name'];
  717. $this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
  718. }
  719. if ( isset( $settings['icon'] ) )
  720. $this->icon = $settings['icon'];
  721. if ( isset( $settings['url'] ) )
  722. $this->url = $settings['url'];
  723. }
  724. public function get_name() {
  725. return $this->name;
  726. }
  727. public function get_display( $post ) {
  728. $str = $this->get_link( get_permalink( $post->ID ), esc_html( $this->name ), __( 'Click to share', 'jetpack' ), 'share='.$this->id );
  729. return str_replace( '<span>', '<span style="background-image:url(' . esc_url( $this->icon ) . ');">', $str );
  730. }
  731. public function process_request( $post, array $post_data ) {
  732. $url = str_replace( '&amp;', '&', $this->url );
  733. $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
  734. $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
  735. $url = str_replace( '%post_title%', rawurlencode( $post->post_title ), $url );
  736. if ( strpos( $url, '%post_tags%' ) !== false ) {
  737. $tags = get_the_tags( $post->ID );
  738. $tagged = '';
  739. if ( $tags ) {
  740. foreach ( $tags AS $tag ) {
  741. $tagged[] = rawurlencode( $tag->name );
  742. }
  743. $tagged = implode( ',', $tagged );
  744. }
  745. $url = str_replace( '%post_tags%', $tagged, $url );
  746. }
  747. if ( strpos( $url, '%post_excerpt%' ) !== false ) {
  748. $url_excerpt = $post->post_excerpt;
  749. if ( empty( $url_excerpt ) )
  750. $url_excerpt = $post->post_content;
  751. $url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) );
  752. $url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
  753. $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
  754. $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
  755. }
  756. // Record stats
  757. parent::process_request( $post, $post_data );
  758. // Redirect
  759. wp_redirect( $url );
  760. die();
  761. }
  762. public function display_options() {
  763. ?>
  764. <div class="input">
  765. <table class="form-table">
  766. <tbody>
  767. <tr>
  768. <th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th>
  769. <td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
  770. </tr>
  771. <tr>
  772. <th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th>
  773. <td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
  774. </tr>
  775. <tr>
  776. <th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th>
  777. <td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
  778. </tr>
  779. <tr>
  780. <th scope="row"></th>
  781. <td>
  782. <input class="button-secondary" type="submit" value="<?php _e( 'Save', 'jetpack' ); ?>" />
  783. <a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a>
  784. </td>
  785. </tr>
  786. </tbody>
  787. </table>
  788. </div>
  789. <?php
  790. }
  791. public function update_options( array $data ) {
  792. $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
  793. $url = trim( esc_url_raw( $data['url'] ) );
  794. $icon = trim( esc_url_raw( $data['icon'] ) );
  795. if ( $name )
  796. $this->name = $name;
  797. if ( $url )
  798. $this->url = $url;
  799. if ( $icon )
  800. $this->icon = $icon;
  801. }
  802. public function get_options() {
  803. return array(
  804. 'name' => $this->name,
  805. 'icon' => $this->icon,
  806. 'url' => $this->url,
  807. );
  808. }
  809. public function display_preview() {
  810. $opts = $this->get_options();
  811. $text = '&nbsp;';
  812. if ( !$this->smart )
  813. if ( $this->button_style != 'icon' )
  814. $text = $this->get_name();
  815. $klasses = array( 'share-'.$this->shortname );
  816. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' )
  817. $klasses[] = 'share-icon';
  818. if ( $this->button_style == 'icon' ) {
  819. $text = '';
  820. $klasses[] = 'no-text';
  821. }
  822. if ( $this->button_style == 'text' )
  823. $klasses[] = 'no-icon';
  824. $link = sprintf(
  825. '<a rel="nofollow" class="%s" href="javascript:void(0);return false;" title="%s"><span style="background-image:url(%s) !important;background-position:left center;background-repeat:no-repeat;">%s</span></a>',
  826. implode( ' ', $klasses ),
  827. $this->get_name(),
  828. esc_url( $opts['icon'] ),
  829. $text
  830. );
  831. ?>
  832. <div class="option option-smart-off">
  833. <?php echo $link ; ?>
  834. </div><?php
  835. }
  836. }
  837. class Share_Tumblr extends Sharing_Source {
  838. var $shortname = 'tumblr';
  839. public function __construct( $id, array $settings ) {
  840. parent::__construct( $id, $settings );
  841. if ( 'official' == $this->button_style )
  842. $this->smart = true;
  843. else
  844. $this->smart = false;
  845. }
  846. public function get_name() {
  847. return __( 'Tumblr', 'jetpack' );
  848. }
  849. public function get_display( $post ) {
  850. if ( $this->smart )
  851. return '<a href="http://www.tumblr.com/share/link/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&name=' . rawurlencode( $post->post_title ) . '" title="Share on Tumblr" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:62px; height:20px; background:url(\'http://platform.tumblr.com/v1/share_2.png\') top left no-repeat transparent;">Share on Tumblr</a>';
  852. else
  853. return $this->get_link( get_permalink( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
  854. }
  855. public function process_request( $post, array $post_data ) {
  856. // Record stats
  857. parent::process_request( $post, $post_data );
  858. // Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
  859. $url = 'http://www.tumblr.com/share?v=3&u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $post->post_title ) . '&s=';
  860. wp_redirect( $url );
  861. die();
  862. }
  863. // http://www.tumblr.com/share?v=3&u=URL&t=TITLE&s=
  864. public function display_footer() {
  865. if ( $this->smart ) {
  866. ?><script type="text/javascript" src="http://platform.tumblr.com/v1/share.js"></script><?php
  867. } else {
  868. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  869. }
  870. }
  871. }
  872. class Share_Pinterest extends Sharing_Source {
  873. var $shortname = 'pinterest';
  874. public function __construct( $id, array $settings ) {
  875. parent::__construct( $id, $settings );
  876. if ( 'official' == $this->button_style )
  877. $this->smart = true;
  878. else
  879. $this->smart = false;
  880. }
  881. public function get_name() {
  882. return __( 'Pinterest', 'jetpack' );
  883. }
  884. public function get_post_image( $content ) {
  885. $image = '';
  886. if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {
  887. $thumb_id = get_post_thumbnail_id();
  888. $thumb = wp_get_attachment_image_src( $thumb_id );
  889. $image = remove_query_arg( array('w', 'h'), $thumb[0] );
  890. } else if ( preg_match_all('/<img (.+?)>/', $content, $matches) ) {
  891. foreach ( $matches[1] as $attrs ) {
  892. $media = $img = array();
  893. foreach ( wp_kses_hair( $attrs, array( 'http', 'https' ) ) as $attr )
  894. $img[$attr['name']] = $attr['value'];
  895. if ( !isset( $img['src'] ) || 0 !== strpos( $img['src'], 'http' ) ) {
  896. continue;
  897. }
  898. else {
  899. $image = htmlspecialchars_decode( $img['src'] );
  900. break;
  901. }
  902. }
  903. }
  904. return $image;
  905. }
  906. public function get_display( $post ) {
  907. if ( $this->smart )
  908. return '<div class="pinterest_button"><a href="' . esc_url( 'http://pinterest.com/pin/create/button/?url='. rawurlencode( $this->get_share_url( $post->ID ) ) . '&description=' . rawurlencode( $post->post_title ) . '&media=' . rawurlencode( esc_url_raw( $this->get_post_image( $post->post_content ) ) ) ) . '" class="pin-it-button" count-layout="horizontal"> '. __( 'Pin It', 'jetpack') .'</a></div>';
  909. else
  910. return $this->get_link( get_permalink( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest' );
  911. }
  912. public function process_request( $post, array $post_data ) {
  913. $pinterest_url = esc_url_raw( 'http://pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&description=' . rawurlencode( $post->post_title ) . '&media=' . rawurlencode( esc_url_raw( $this->get_post_image( $post->post_content ) ) ) );
  914. // Record stats
  915. parent::process_request( $post, $post_data );
  916. // Redirect to Pinterest
  917. wp_redirect( $pinterest_url );
  918. die();
  919. }
  920. public function display_footer() {
  921. if ( !$this->smart ) {
  922. $this->js_dialog( $this->shortname, array( 'width' => 650, 'height' => 280 ) );
  923. } else {
  924. ?>
  925. <script type="text/javascript">
  926. function pinterest_async_load() {
  927. var s = document.createElement("script");
  928. s.type = "text/javascript";
  929. s.async = true;
  930. s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js";
  931. var x = document.getElementsByTagName("script")[0];
  932. x.parentNode.insertBefore(s, x);
  933. }
  934. jQuery(document).on('ready post-load', function() {
  935. pinterest_async_load();
  936. });
  937. </script>
  938. <?php
  939. }
  940. }
  941. }