PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/hunt9310/ras
PHP | 1617 lines | 1029 code | 254 blank | 334 comment | 191 complexity | 28df68c3ab3bf687d3804867b3b17824 MD5 | raw file
  1. <?php
  2. abstract class Sharing_Source {
  3. public $button_style;
  4. public $smart;
  5. protected $open_link_in_new;
  6. protected $id;
  7. public function __construct( $id, array $settings ) {
  8. $this->id = $id;
  9. /**
  10. * Filter the way sharing links open.
  11. *
  12. * By default, sharing links open in a new window.
  13. *
  14. * @module sharedaddy
  15. *
  16. * @since 3.4.0
  17. *
  18. * @param bool true Should Sharing links open in a new window. Default to true.
  19. */
  20. $this->open_link_in_new = apply_filters( 'jetpack_open_sharing_in_new_window', true );
  21. if ( isset( $settings['button_style'] ) )
  22. $this->button_style = $settings['button_style'];
  23. if ( isset( $settings['smart'] ) )
  24. $this->smart = $settings['smart'];
  25. }
  26. public function http() {
  27. return is_ssl() ? 'https' : 'http';
  28. }
  29. public function get_id() {
  30. return $this->id;
  31. }
  32. public function get_class() {
  33. return $this->id;
  34. }
  35. public function get_share_url( $post_id ) {
  36. /**
  37. * Filter the sharing permalink.
  38. *
  39. * @module sharedaddy
  40. *
  41. * @since 1.2.0
  42. *
  43. * @param string get_permalink( $post_id ) Post Permalink.
  44. * @param int $post_id Post ID.
  45. * @param int $this->id Sharing ID.
  46. */
  47. return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id );
  48. }
  49. public function get_share_title( $post_id ) {
  50. $post = get_post( $post_id );
  51. /**
  52. * Filter the sharing title.
  53. *
  54. * @module sharedaddy
  55. *
  56. * @since 2.8.0
  57. *
  58. * @param string $post->post_title Post Title.
  59. * @param int $post_id Post ID.
  60. * @param int $this->id Sharing ID.
  61. */
  62. $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id );
  63. return html_entity_decode( wp_kses( $title, null ) );
  64. }
  65. public function has_custom_button_style() {
  66. return false;
  67. }
  68. public function get_link( $url, $text, $title, $query = '', $id = false ) {
  69. $args = func_get_args();
  70. $klasses = array( 'share-'.$this->get_class(), 'sd-button' );
  71. if ( 'icon' == $this->button_style || 'icon-text' == $this->button_style )
  72. $klasses[] = 'share-icon';
  73. if ( 'icon' == $this->button_style ) {
  74. $text = $title;
  75. $klasses[] = 'no-text';
  76. if ( true == $this->open_link_in_new )
  77. $text .= __( ' (Opens in new window)', 'jetpack' );
  78. }
  79. /**
  80. * Filter the sharing display ID.
  81. *
  82. * @module sharedaddy
  83. *
  84. * @since 3.4.0
  85. *
  86. * @param int|false $id Sharing ID.
  87. * @param object $this Sharing service properties.
  88. * @param array $args Array of sharing service options.
  89. */
  90. $id = apply_filters( 'jetpack_sharing_display_id', $id, $this, $args );
  91. /**
  92. * Filter the sharing display link.
  93. *
  94. * @module sharedaddy
  95. *
  96. * @since 2.8.0
  97. *
  98. * @param string $url Post URL.
  99. * @param object $this Sharing service properties.
  100. * @param int|false $id Sharing ID.
  101. * @param array $args Array of sharing service options.
  102. */
  103. $url = apply_filters( 'sharing_display_link', $url, $this, $id, $args ); // backwards compatibility
  104. /**
  105. * Filter the sharing display link.
  106. *
  107. * @module sharedaddy
  108. *
  109. * @since 2.8.0
  110. *
  111. * @param string $url Post URL.
  112. * @param object $this Sharing service properties.
  113. * @param int|false $id Sharing ID.
  114. * @param array $args Array of sharing service options.
  115. */
  116. $url = apply_filters( 'jetpack_sharing_display_link', $url, $this, $id, $args );
  117. /**
  118. * Filter the sharing display query.
  119. *
  120. * @module sharedaddy
  121. *
  122. * @since 2.8.0
  123. *
  124. * @param string $query Sharing service URL parameter.
  125. * @param object $this Sharing service properties.
  126. * @param int|false $id Sharing ID.
  127. * @param array $args Array of sharing service options.
  128. */
  129. $query = apply_filters( 'jetpack_sharing_display_query', $query, $this, $id, $args );
  130. if ( !empty( $query ) ) {
  131. if ( false === stripos( $url, '?' ) )
  132. $url .= '?'.$query;
  133. else
  134. $url .= '&amp;'.$query;
  135. }
  136. if ( 'text' == $this->button_style )
  137. $klasses[] = 'no-icon';
  138. /**
  139. * Filter the sharing display classes.
  140. *
  141. * @module sharedaddy
  142. *
  143. * @since 3.4.0
  144. *
  145. * @param array $klasses Sharing service classes.
  146. * @param object $this Sharing service properties.
  147. * @param int|false $id Sharing ID.
  148. * @param array $args Array of sharing service options.
  149. */
  150. $klasses = apply_filters( 'jetpack_sharing_display_classes', $klasses, $this, $id, $args );
  151. /**
  152. * Filter the sharing display title.
  153. *
  154. * @module sharedaddy
  155. *
  156. * @since 3.4.0
  157. *
  158. * @param string $title Sharing service title.
  159. * @param object $this Sharing service properties.
  160. * @param int|false $id Sharing ID.
  161. * @param array $args Array of sharing service options.
  162. */
  163. $title = apply_filters( 'jetpack_sharing_display_title', $title, $this, $id, $args );
  164. /**
  165. * Filter the sharing display text.
  166. *
  167. * @module sharedaddy
  168. *
  169. * @since 3.4.0
  170. *
  171. * @param string $text Sharing service text.
  172. * @param object $this Sharing service properties.
  173. * @param int|false $id Sharing ID.
  174. * @param array $args Array of sharing service options.
  175. */
  176. $text = apply_filters( 'jetpack_sharing_display_text', $text, $this, $id, $args );
  177. return sprintf(
  178. '<a rel="nofollow" data-shared="%s" class="%s" href="%s"%s title="%s"><span%s>%s</span></a>',
  179. ( $id ? esc_attr( $id ) : '' ),
  180. implode( ' ', $klasses ),
  181. $url,
  182. ( true == $this->open_link_in_new ) ? ' target="_blank"' : '',
  183. $title,
  184. ( 'icon' == $this->button_style ) ? '></span><span class="sharing-screen-reader-text"' : '',
  185. $text
  186. );
  187. }
  188. /**
  189. * Get an unfiltered post permalink to use when generating a sharing URL with get_link.
  190. * Use instead of get_share_url for non-official styles as get_permalink ensures that process_request
  191. * will be executed more reliably, in the case that the filtered URL uses a service that strips query parameters.
  192. *
  193. * @since 3.7.0
  194. * @param int $post_id Post ID.
  195. * @uses get_permalink
  196. * @return string get_permalink( $post_id ) Post permalink.
  197. */
  198. public function get_process_request_url( $post_id ) {
  199. return get_permalink( $post_id );
  200. }
  201. abstract public function get_name();
  202. abstract public function get_display( $post );
  203. public function display_header() {
  204. }
  205. public function display_footer() {
  206. }
  207. public function has_advanced_options() {
  208. return false;
  209. }
  210. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
  211. $text = '&nbsp;';
  212. $button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style;
  213. if ( !$this->smart && ! $force_smart )
  214. if ( $button_style != 'icon' )
  215. $text = $this->get_name();
  216. $klasses = array( 'share-'.$this->get_class(), 'sd-button' );
  217. if ( $button_style == 'icon' || $button_style == 'icon-text' )
  218. $klasses[] = 'share-icon';
  219. if ( $button_style == 'icon' )
  220. $klasses[] = 'no-text';
  221. if ( $button_style == 'text' )
  222. $klasses[] = 'no-icon';
  223. $link = sprintf(
  224. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',
  225. implode( ' ', $klasses ),
  226. $this->get_name(),
  227. $text
  228. );
  229. $smart = ( $this->smart || $force_smart ) ? 'on' : 'off';
  230. $return = "<div class='option option-smart-$smart'>$link</div>";
  231. if ( $echo )
  232. echo $return;
  233. return $return;
  234. }
  235. public function get_total( $post = false ) {
  236. global $wpdb, $blog_id;
  237. $name = strtolower( $this->get_id() );
  238. if ( $post == false ) {
  239. // get total number of shares for service
  240. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) );
  241. }
  242. // get total shares for a post
  243. 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 ) );
  244. }
  245. public function get_posts_total() {
  246. global $wpdb, $blog_id;
  247. $totals = array();
  248. $name = strtolower( $this->get_id() );
  249. $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 ) );
  250. if ( !empty( $my_data ) )
  251. foreach( $my_data as $row )
  252. $totals[] = new Sharing_Post_Total( $row->id, $row->total );
  253. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
  254. return $totals;
  255. }
  256. public function process_request( $post, array $post_data ) {
  257. /**
  258. * Fires when a post is shared via one of the sharing buttons.
  259. *
  260. * @module sharedaddy
  261. *
  262. * @since 1.1.0
  263. *
  264. * @param array $args Aray of information about the sharing service.
  265. */
  266. do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) );
  267. }
  268. public function js_dialog( $name, $params = array() ) {
  269. if ( true !== $this->open_link_in_new )
  270. return;
  271. $defaults = array(
  272. 'menubar' => 1,
  273. 'resizable' => 1,
  274. 'width' => 600,
  275. 'height' => 400,
  276. );
  277. $params = array_merge( $defaults, $params );
  278. $opts = array();
  279. foreach( $params as $key => $val ) {
  280. $opts[] = "$key=$val";
  281. }
  282. $opts = implode( ',', $opts );
  283. // Add JS after sharing-js has been enqueued.
  284. wp_add_inline_script( 'sharing-js',
  285. "var windowOpen;
  286. jQuery( document.body ).on( 'click', 'a.share-$name', function() {
  287. // If there's another sharing window open, close it.
  288. if ( 'undefined' !== typeof windowOpen ) {
  289. windowOpen.close();
  290. }
  291. windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' );
  292. return false;
  293. });"
  294. );
  295. }
  296. }
  297. abstract class Sharing_Advanced_Source extends Sharing_Source {
  298. public function has_advanced_options() {
  299. return true;
  300. }
  301. abstract public function display_options();
  302. abstract public function update_options( array $data );
  303. abstract public function get_options();
  304. }
  305. class Share_Email extends Sharing_Source {
  306. public $shortname = 'email';
  307. public $genericon = '\f410';
  308. public function __construct( $id, array $settings ) {
  309. parent::__construct( $id, $settings );
  310. if ( 'official' == $this->button_style )
  311. $this->smart = true;
  312. else
  313. $this->smart = false;
  314. }
  315. public function get_name() {
  316. return _x( 'Email', 'as sharing source', 'jetpack' );
  317. }
  318. // Default does nothing
  319. public function process_request( $post, array $post_data ) {
  320. $ajax = false;
  321. if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' )
  322. $ajax = true;
  323. $source_email = $target_email = $source_name = false;
  324. if ( isset( $post_data['source_email'] ) && is_email( $post_data['source_email'] ) )
  325. $source_email = $post_data['source_email'];
  326. if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) )
  327. $target_email = $post_data['target_email'];
  328. if ( isset( $post_data['source_name'] ) && strlen( $post_data['source_name'] ) < 200 ) {
  329. $source_name = $post_data['source_name'];
  330. } elseif ( isset( $post_data['source_name'] ) ) {
  331. $source_name = substr( $post_data['source_name'], 0, 200 );
  332. } else {
  333. $source_name = '';
  334. }
  335. // Test email
  336. $error = 1; // Failure in data
  337. if ( empty( $post_data['source_f_name'] ) && $source_email && $target_email && $source_name ) {
  338. /**
  339. * Allow plugins to stop the email sharing button from running the shared message through Akismet.
  340. *
  341. * @module sharedaddy
  342. *
  343. * @since 1.1.0
  344. *
  345. * @param bool true Should we check if the message isn't spam?
  346. * @param object $post Post information.
  347. * @param array $post_data Information about the shared message.
  348. */
  349. if ( apply_filters( 'sharing_email_check', true, $post, $post_data ) ) {
  350. $data = array(
  351. 'post' => $post,
  352. 'source' => $source_email,
  353. 'target' => $target_email,
  354. 'name' => $source_name
  355. );
  356. // todo: implement an error message when email doesn't get sent.
  357. /**
  358. * Filter whether an email can be sent from the Email sharing button.
  359. *
  360. * @module sharedaddy
  361. *
  362. * @since 1.1.0
  363. *
  364. * @param array $data Array of information about the shared message.
  365. */
  366. if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) {
  367. // Record stats
  368. parent::process_request( $data['post'], $post_data );
  369. /**
  370. * Fires when an email is sent via the Email sharing button.
  371. *
  372. * @module sharedaddy
  373. *
  374. * @since 1.1.0
  375. *
  376. * @param array $data Array of information about the shared message.
  377. */
  378. do_action( 'sharing_email_send_post', $data );
  379. }
  380. // Return a positive regardless of whether the user is subscribed or not
  381. if ( $ajax ) {
  382. ?>
  383. <div class="response">
  384. <div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div>
  385. <div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div>
  386. <div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div>
  387. </div>
  388. <?php
  389. }
  390. else
  391. wp_safe_redirect( get_permalink( $post->ID ).'?shared=email' );
  392. die();
  393. }
  394. else
  395. $error = 2; // Email check failed
  396. }
  397. if ( $ajax )
  398. echo $error;
  399. else
  400. wp_safe_redirect( get_permalink( $post->ID ).'?shared=email&msg=fail' );
  401. die();
  402. }
  403. public function get_display( $post ) {
  404. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Email', 'share to', 'jetpack' ), __( 'Click to email this to a friend', 'jetpack' ), 'share=email' );
  405. }
  406. /**
  407. * Outputs the hidden email dialog
  408. */
  409. public function display_footer() {
  410. global $current_user;
  411. $visible = $status = false;
  412. ?>
  413. <div id="sharing_email" style="display: none;">
  414. <form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post">
  415. <label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label>
  416. <input type="email" name="target_email" id="target_email" value="" />
  417. <?php if ( is_user_logged_in() ) : ?>
  418. <input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" />
  419. <input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" />
  420. <?php else : ?>
  421. <label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label>
  422. <input type="text" name="source_name" id="source_name" value="" />
  423. <label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label>
  424. <input type="email" name="source_email" id="source_email" value="" />
  425. <?php endif; ?>
  426. <input type="text" id="jetpack-source_f_name" name="source_f_name" class="input" value="" size="25" autocomplete="off" />
  427. <script> document.getElementById('jetpack-source_f_name').value = ''; </script>
  428. <?php
  429. /**
  430. * Fires when the Email sharing dialog is loaded.
  431. *
  432. * @module sharedaddy
  433. *
  434. * @since 1.1.0
  435. *
  436. * @param string jetpack Eail sharing source.
  437. */
  438. do_action( 'sharing_email_dialog', 'jetpack' );
  439. ?>
  440. <img style="float: right; display: none" class="loading" src="<?php
  441. /** This filter is documented in modules/stats.php */
  442. echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" />
  443. <input type="submit" value="<?php esc_attr_e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" />
  444. <a rel="nofollow" href="#cancel" class="sharing_cancel"><?php _e( 'Cancel', 'jetpack' ); ?></a>
  445. <div class="errors errors-1" style="display: none;">
  446. <?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?>
  447. </div>
  448. <div class="errors errors-2" style="display: none;">
  449. <?php _e( 'Email check failed, please try again', 'jetpack' ); ?>
  450. </div>
  451. <div class="errors errors-3" style="display: none;">
  452. <?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?>
  453. </div>
  454. </form>
  455. </div>
  456. <?php
  457. }
  458. }
  459. class Share_Twitter extends Sharing_Source {
  460. public $shortname = 'twitter';
  461. public $genericon = '\f202';
  462. // 'https://dev.twitter.com/rest/reference/get/help/configuration' ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23
  463. public $short_url_length = 24;
  464. public function __construct( $id, array $settings ) {
  465. parent::__construct( $id, $settings );
  466. if ( 'official' == $this->button_style )
  467. $this->smart = true;
  468. else
  469. $this->smart = false;
  470. }
  471. public function get_name() {
  472. return __( 'Twitter', 'jetpack' );
  473. }
  474. function sharing_twitter_via( $post ) {
  475. /**
  476. * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag.
  477. *
  478. * @module sharedaddy
  479. *
  480. * @since 3.0.0
  481. *
  482. * @param string $string Twitter Username.
  483. * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags.
  484. */
  485. $twitter_site_tag_value = apply_filters( 'jetpack_twitter_cards_site_tag', '', array() );
  486. /*
  487. * Hack to remove the unwanted behavior of adding 'via @jetpack' which
  488. * was introduced with the adding of the Twitter cards.
  489. * This should be a temporary solution until a better method is setup.
  490. */
  491. if( 'jetpack' == $twitter_site_tag_value ) {
  492. $twitter_site_tag_value = '';
  493. }
  494. /**
  495. * Filters the Twitter username used as "via" in the Twitter sharing button.
  496. *
  497. * @module sharedaddy
  498. *
  499. * @since 1.7.0
  500. *
  501. * @param string $twitter_site_tag_value Twitter Username.
  502. * @param int $post->ID Post ID.
  503. */
  504. $twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID );
  505. // Strip out anything other than a letter, number, or underscore.
  506. // This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle.
  507. return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value );
  508. }
  509. public function get_related_accounts( $post ) {
  510. /**
  511. * Filter the list of related Twitter accounts added to the Twitter sharing button.
  512. *
  513. * @module sharedaddy
  514. *
  515. * @since 1.7.0
  516. *
  517. * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description'
  518. * @param int $post->ID Post ID.
  519. */
  520. $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
  521. // Example related string: account1,account2:Account 2 description,account3
  522. $related = array();
  523. foreach ( $related_accounts as $related_account_username => $related_account_description ) {
  524. // Join the description onto the end of the username
  525. if ( $related_account_description )
  526. $related_account_username .= ':' . $related_account_description;
  527. $related[] = $related_account_username;
  528. }
  529. return implode( ',', $related );
  530. }
  531. public function get_display( $post ) {
  532. $via = $this->sharing_twitter_via( $post );
  533. if ( $via ) {
  534. $via = 'data-via="' . esc_attr( $via ) . '"';
  535. } else {
  536. $via = '';
  537. }
  538. $related = $this->get_related_accounts( $post );
  539. if ( ! empty( $related ) && $related !== $via ) {
  540. $related = 'data-related="' . esc_attr( $related ) . '"';
  541. } else {
  542. $related = '';
  543. }
  544. if ( $this->smart ) {
  545. $share_url = $this->get_share_url( $post->ID );
  546. $post_title = $this->get_share_title( $post->ID );
  547. return sprintf(
  548. '<a href="https://twitter.com/share" class="twitter-share-button" data-url="%1$s" data-text="%2$s" %3$s %4$s>Tweet</a>',
  549. esc_url( $share_url ),
  550. esc_attr( $post_title ),
  551. $via,
  552. $related
  553. );
  554. } else {
  555. if (
  556. /**
  557. * Allow plugins to disable sharing counts for specific sharing services.
  558. *
  559. * @module sharedaddy
  560. *
  561. * @since 3.0.0
  562. *
  563. * @param bool true Should sharing counts be enabled for this specific service. Default to true.
  564. * @param int $post->ID Post ID.
  565. * @param string $str Sharing service name.
  566. */
  567. apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' )
  568. ) {
  569. sharing_register_post_for_share_counts( $post->ID );
  570. }
  571. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID );
  572. }
  573. }
  574. public function process_request( $post, array $post_data ) {
  575. $post_title = $this->get_share_title( $post->ID );
  576. $post_link = $this->get_share_url( $post->ID );
  577. if ( function_exists( 'mb_stripos' ) ) {
  578. $strlen = 'mb_strlen';
  579. $substr = 'mb_substr';
  580. } else {
  581. $strlen = 'strlen';
  582. $substr = 'substr';
  583. }
  584. $via = $this->sharing_twitter_via( $post );
  585. $related = $this->get_related_accounts( $post );
  586. if ( $via ) {
  587. $sig = " via @$via";
  588. if ( $related === $via ) {
  589. $related = false;
  590. }
  591. } else {
  592. $via = false;
  593. $sig = '';
  594. }
  595. $suffix_length = $this->short_url_length + $strlen( $sig );
  596. // $sig is handled by twitter in their 'via' argument.
  597. // $post_link is handled by twitter in their 'url' argument.
  598. if ( 140 < $strlen( $post_title ) + $suffix_length ) {
  599. // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
  600. $text = $substr( $post_title, 0, 140 - $suffix_length - 1 ) . "\xE2\x80\xA6";
  601. } else {
  602. $text = $post_title;
  603. }
  604. // Record stats
  605. parent::process_request( $post, $post_data );
  606. $url = $post_link;
  607. $twitter_url = add_query_arg(
  608. rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
  609. 'https://twitter.com/intent/tweet'
  610. );
  611. // Redirect to Twitter
  612. wp_redirect( $twitter_url );
  613. die();
  614. }
  615. public function has_custom_button_style() {
  616. return $this->smart;
  617. }
  618. public function display_footer() {
  619. if ( $this->smart ) {
  620. ?>
  621. <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
  622. <?php
  623. } else {
  624. $this->js_dialog( $this->shortname, array( 'height' => 350 ) );
  625. }
  626. }
  627. }
  628. class Share_Reddit extends Sharing_Source {
  629. public $shortname = 'reddit';
  630. public $genericon = '\f222';
  631. public function __construct( $id, array $settings ) {
  632. parent::__construct( $id, $settings );
  633. if ( 'official' == $this->button_style )
  634. $this->smart = true;
  635. else
  636. $this->smart = false;
  637. }
  638. public function get_name() {
  639. return __( 'Reddit', 'jetpack' );
  640. }
  641. public function get_display( $post ) {
  642. if ( $this->smart )
  643. return '<div class="reddit_button"><iframe src="' . $this->http() . '://www.reddit.com/static/button/button1.html?newwindow=true&width=120&amp;url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>';
  644. else
  645. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
  646. }
  647. public function process_request( $post, array $post_data ) {
  648. $reddit_url = $this->http() . '://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) );
  649. // Record stats
  650. parent::process_request( $post, $post_data );
  651. // Redirect to Reddit
  652. wp_redirect( $reddit_url );
  653. die();
  654. }
  655. }
  656. class Share_LinkedIn extends Sharing_Source {
  657. public $shortname = 'linkedin';
  658. public $genericon = '\f207';
  659. public function __construct( $id, array $settings ) {
  660. parent::__construct( $id, $settings );
  661. if ( 'official' == $this->button_style )
  662. $this->smart = true;
  663. else
  664. $this->smart = false;
  665. }
  666. public function get_name() {
  667. return __( 'LinkedIn', 'jetpack' );
  668. }
  669. public function has_custom_button_style() {
  670. return $this->smart;
  671. }
  672. public function get_display( $post ) {
  673. $display = '';
  674. if ( $this->smart ) {
  675. $share_url = $this->get_share_url( $post->ID );
  676. $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
  677. } else {
  678. $display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID );
  679. }
  680. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  681. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
  682. sharing_register_post_for_share_counts( $post->ID );
  683. }
  684. return $display;
  685. }
  686. public function process_request( $post, array $post_data ) {
  687. $post_link = $this->get_share_url( $post->ID );
  688. // Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
  689. // https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
  690. $linkedin_url = add_query_arg( array(
  691. 'url' => rawurlencode( $post_link ),
  692. ), 'https://www.linkedin.com/cws/share?token=&isFramed=false' );
  693. // Record stats
  694. parent::process_request( $post, $post_data );
  695. // Redirect to LinkedIn
  696. wp_redirect( $linkedin_url );
  697. die();
  698. }
  699. public function display_footer() {
  700. if ( !$this->smart ) {
  701. $this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) );
  702. } else {
  703. ?><script type="text/javascript">
  704. jQuery( document ).ready( function() {
  705. jQuery.getScript( 'https://platform.linkedin.com/in.js?async=true', function success() {
  706. IN.init();
  707. });
  708. });
  709. jQuery( document.body ).on( 'post-load', function() {
  710. if ( typeof IN != 'undefined' )
  711. IN.parse();
  712. });
  713. </script><?php
  714. }
  715. }
  716. }
  717. class Share_Facebook extends Sharing_Source {
  718. public $shortname = 'facebook';
  719. public $genericon = '\f204';
  720. private $share_type = 'default';
  721. public function __construct( $id, array $settings ) {
  722. parent::__construct( $id, $settings );
  723. if ( isset( $settings['share_type'] ) )
  724. $this->share_type = $settings['share_type'];
  725. if ( 'official' == $this->button_style )
  726. $this->smart = true;
  727. else
  728. $this->smart = false;
  729. }
  730. public function get_name() {
  731. return __( 'Facebook', 'jetpack' );
  732. }
  733. public function display_header() {
  734. }
  735. function guess_locale_from_lang( $lang ) {
  736. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) {
  737. return 'en_US';
  738. }
  739. if ( !class_exists( 'GP_Locales' ) ) {
  740. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  741. return false;
  742. }
  743. require JETPACK__GLOTPRESS_LOCALES_PATH;
  744. }
  745. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  746. // WP.com: get_locale() returns 'it'
  747. $locale = GP_Locales::by_slug( $lang );
  748. } else {
  749. // Jetpack: get_locale() returns 'it_IT';
  750. $locale = GP_Locales::by_field( 'wp_locale', $lang );
  751. }
  752. if ( ! $locale ) {
  753. return false;
  754. }
  755. if ( empty( $locale->facebook_locale ) ) {
  756. if ( empty( $locale->wp_locale ) ) {
  757. return false;
  758. } else {
  759. // Facebook SDK is smart enough to fall back to en_US if a
  760. // locale isn't supported. Since supported Facebook locales
  761. // can fall out of sync, we'll attempt to use the known
  762. // wp_locale value and rely on said fallback.
  763. return $locale->wp_locale;
  764. }
  765. }
  766. return $locale->facebook_locale;
  767. }
  768. public function get_display( $post ) {
  769. if ( $this->smart ) {
  770. $share_url = $this->get_share_url( $post->ID );
  771. $fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>';
  772. /**
  773. * Filter the output of the Facebook Sharing button.
  774. *
  775. * @module sharedaddy
  776. *
  777. * @since 3.6.0
  778. *
  779. * @param string $fb_share_html Facebook Sharing button HTML.
  780. * @param string $share_url URL of the post to share.
  781. */
  782. return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url );
  783. }
  784. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  785. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) {
  786. sharing_register_post_for_share_counts( $post->ID );
  787. }
  788. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Click to share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID );
  789. }
  790. public function process_request( $post, array $post_data ) {
  791. $fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) );
  792. // Record stats
  793. parent::process_request( $post, $post_data );
  794. // Redirect to Facebook
  795. wp_redirect( $fb_url );
  796. die();
  797. }
  798. public function display_footer() {
  799. $this->js_dialog( $this->shortname );
  800. if ( $this->smart ) {
  801. $locale = $this->guess_locale_from_lang( get_locale() );
  802. if ( ! $locale ) {
  803. $locale = 'en_US';
  804. }
  805. /**
  806. * Filter the App ID used in the official Facebook Share button.
  807. *
  808. * @since 3.8.0
  809. *
  810. * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID).
  811. */
  812. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
  813. if ( is_numeric( $fb_app_id ) ) {
  814. $fb_app_id = '&appId=' . $fb_app_id;
  815. } else {
  816. $fb_app_id = '';
  817. }
  818. ?><div id="fb-root"></div>
  819. <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = 'https://connect.facebook.net/<?php echo $locale; ?>/sdk.js#xfbml=1<?php echo $fb_app_id; ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
  820. <script>
  821. jQuery( document.body ).on( 'post-load', function() {
  822. if ( 'undefined' !== typeof FB ) {
  823. FB.XFBML.parse();
  824. }
  825. } );
  826. </script>
  827. <?php
  828. }
  829. }
  830. }
  831. class Share_Print extends Sharing_Source {
  832. public $shortname = 'print';
  833. public $genericon = '\f469';
  834. public function __construct( $id, array $settings ) {
  835. parent::__construct( $id, $settings );
  836. if ( 'official' == $this->button_style )
  837. $this->smart = true;
  838. else
  839. $this->smart = false;
  840. }
  841. public function get_name() {
  842. return __( 'Print', 'jetpack' );
  843. }
  844. public function get_display( $post ) {
  845. return $this->get_link( $this->get_process_request_url( $post->ID ) . ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) );
  846. }
  847. }
  848. class Share_PressThis extends Sharing_Source {
  849. public $shortname = 'pressthis';
  850. public $genericon = '\f205';
  851. public function __construct( $id, array $settings ) {
  852. parent::__construct( $id, $settings );
  853. if ( 'official' == $this->button_style )
  854. $this->smart = true;
  855. else
  856. $this->smart = false;
  857. }
  858. public function get_name() {
  859. return __( 'Press This', 'jetpack' );
  860. }
  861. public function process_request( $post, array $post_data ) {
  862. global $current_user;
  863. $primary_blog = (int) get_user_meta( $current_user->ID, 'primary_blog', true );
  864. if ( $primary_blog ) {
  865. $primary_blog_details = get_blog_details( $primary_blog );
  866. } else {
  867. $primary_blog_details = false;
  868. }
  869. if ( $primary_blog_details ) {
  870. $blogs = array( $primary_blog_details );
  871. } elseif ( function_exists( 'get_active_blogs_for_user' ) ) {
  872. $blogs = get_active_blogs_for_user();
  873. if ( empty( $blogs ) ) {
  874. $blogs = get_blogs_of_user( $current_user->ID );
  875. }
  876. } else {
  877. $blogs = get_blogs_of_user( $current_user->ID );
  878. }
  879. if ( empty( $blogs ) ) {
  880. wp_safe_redirect( get_permalink( $post->ID ) );
  881. die();
  882. }
  883. $blog = current( $blogs );
  884. $url = $blog->siteurl.'/wp-admin/press-this.php?u='.rawurlencode( $this->get_share_url( $post->ID ) ).'&t='.rawurlencode( $this->get_share_title( $post->ID ) );
  885. if ( isset( $_GET['sel'] ) )
  886. $url .= '&s='.rawurlencode( $_GET['sel'] );
  887. // Record stats
  888. parent::process_request( $post, $post_data );
  889. // Redirect to Press This
  890. wp_safe_redirect( $url );
  891. die();
  892. }
  893. public function get_display( $post ) {
  894. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Press This', 'share to', 'jetpack' ), __( 'Click to Press This!', 'jetpack' ), 'share=press-this' );
  895. }
  896. }
  897. class Share_GooglePlus1 extends Sharing_Source {
  898. public $shortname = 'googleplus1';
  899. public $genericon = '\f218';
  900. private $state = false;
  901. public function __construct( $id, array $settings ) {
  902. parent::__construct( $id, $settings );
  903. if ( 'official' == $this->button_style )
  904. $this->smart = true;
  905. else
  906. $this->smart = false;
  907. }
  908. public function get_name() {
  909. return __( 'Google', 'jetpack' );
  910. }
  911. public function has_custom_button_style() {
  912. return $this->smart;
  913. }
  914. public function get_display( $post ) {
  915. if ( $this->smart ) {
  916. $share_url = $this->get_share_url( $post->ID );
  917. return '<div class="googleplus1_button"><div class="g-plus" data-action="share" data-annotation="bubble" data-href="' . esc_url( $share_url ) . '"></div></div>';
  918. } else {
  919. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Google', 'share to', 'jetpack' ), __( 'Click to share on Google+', 'jetpack' ), 'share=google-plus-1', 'sharing-google-' . $post->ID );
  920. }
  921. }
  922. public function get_state() {
  923. return $this->state;
  924. }
  925. public function process_request( $post, array $post_data ) {
  926. if ( isset( $post_data['state'] ) ) {
  927. $this->state = $post_data['state'];
  928. }
  929. // Record stats
  930. parent::process_request( $post, $post_data );
  931. // Redirect to Google +'s sharing endpoint
  932. $url = 'https://plus.google.com/share?url=' . rawurlencode( $this->get_share_url( $post->ID ) );
  933. wp_redirect( $url );
  934. die();
  935. }
  936. public function display_footer() {
  937. global $post;
  938. if ( $this->smart ) { ?>
  939. <script>
  940. function renderGooglePlus1() {
  941. if ( 'undefined' === typeof gapi ) {
  942. return;
  943. }
  944. jQuery( '.g-plus' ).each(function() {
  945. var $button = jQuery( this );
  946. if ( ! $button.data( 'gplus-rendered' ) ) {
  947. gapi.plusone.render( this, {
  948. href: $button.attr( 'data-href' ),
  949. size: $button.attr( 'data-size' ),
  950. annotation: $button.attr( 'data-annotation' )
  951. });
  952. $button.data( 'gplus-rendered', true );
  953. }
  954. });
  955. }
  956. (function() {
  957. var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  958. po.src = 'https://apis.google.com/js/plusone.js';
  959. po.innerHTML = '{"parsetags": "explicit"}';
  960. po.onload = renderGooglePlus1;
  961. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  962. })();
  963. jQuery( document.body ).on( 'post-load', renderGooglePlus1 );
  964. </script>
  965. <?php
  966. } else {
  967. $this->js_dialog( 'google-plus-1', array( 'width' => 480, 'height' => 550 ) );
  968. }
  969. }
  970. public function get_total( $post = false ) {
  971. global $wpdb, $blog_id;
  972. $name = strtolower( $this->get_id() );
  973. if ( $post == false ) {
  974. // get total number of shares for service
  975. return $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) );
  976. }
  977. //get total shares for a post
  978. 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 ) );
  979. }
  980. }
  981. class Share_Custom extends Sharing_Advanced_Source {
  982. private $name;
  983. private $icon;
  984. private $url;
  985. public $smart = true;
  986. public $shortname;
  987. public function get_class() {
  988. return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) );
  989. }
  990. public function __construct( $id, array $settings ) {
  991. parent::__construct( $id, $settings );
  992. $opts = $this->get_options();
  993. if ( isset( $settings['name'] ) ) {
  994. $this->name = $settings['name'];
  995. $this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
  996. }
  997. if ( isset( $settings['icon'] ) ) {
  998. $this->icon = $settings['icon'];
  999. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
  1000. $i = 0;
  1001. while ( $new_icon != $this->icon ) {
  1002. if ( $i > 5 ) {
  1003. $this->icon = false;
  1004. break;
  1005. } else {
  1006. $this->icon = $new_icon;
  1007. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
  1008. }
  1009. $i++;
  1010. }
  1011. }
  1012. if ( isset( $settings['url'] ) )
  1013. $this->url = $settings['url'];
  1014. }
  1015. public function get_name() {
  1016. return $this->name;
  1017. }
  1018. public function get_display( $post ) {
  1019. $str = $this->get_link( $this->get_process_request_url( $post->ID ), esc_html( $this->name ), sprintf( __( 'Click to share on %s', 'jetpack' ), esc_attr( $this->name ) ), 'share='.$this->id );
  1020. return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str );
  1021. }
  1022. public function process_request( $post, array $post_data ) {
  1023. $url = str_replace( '&amp;', '&', $this->url );
  1024. $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
  1025. $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
  1026. $url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url );
  1027. if ( strpos( $url, '%post_tags%' ) !== false ) {
  1028. $tags = get_the_tags( $post->ID );
  1029. $tagged = '';
  1030. if ( $tags ) {
  1031. foreach ( $tags AS $tag ) {
  1032. $tagged[] = rawurlencode( $tag->name );
  1033. }
  1034. $tagged = implode( ',', $tagged );
  1035. }
  1036. $url = str_replace( '%post_tags%', $tagged, $url );
  1037. }
  1038. if ( strpos( $url, '%post_excerpt%' ) !== false ) {
  1039. $url_excerpt = $post->post_excerpt;
  1040. if ( empty( $url_excerpt ) )
  1041. $url_excerpt = $post->post_content;
  1042. $url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) );
  1043. $url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
  1044. $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
  1045. $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
  1046. }
  1047. // Record stats
  1048. parent::process_request( $post, $post_data );
  1049. // Redirect
  1050. wp_redirect( $url );
  1051. die();
  1052. }
  1053. public function display_options() {
  1054. ?>
  1055. <div class="input">
  1056. <table class="form-table">
  1057. <tbody>
  1058. <tr>
  1059. <th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th>
  1060. <td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
  1061. </tr>
  1062. <tr>
  1063. <th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th>
  1064. <td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
  1065. </tr>
  1066. <tr>
  1067. <th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th>
  1068. <td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
  1069. </tr>
  1070. <tr>
  1071. <th scope="row"></th>
  1072. <td>
  1073. <input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" />
  1074. <a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a>
  1075. </td>
  1076. </tr>
  1077. </tbody>
  1078. </table>
  1079. </div>
  1080. <?php
  1081. }
  1082. public function update_options( array $data ) {
  1083. $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
  1084. $url = trim( esc_url_raw( $data['url'] ) );
  1085. $icon = trim( esc_url_raw( $data['icon'] ) );
  1086. if ( $name )
  1087. $this->name = $name;
  1088. if ( $url )
  1089. $this->url = $url;
  1090. if ( $icon )
  1091. $this->icon = $icon;
  1092. }
  1093. public function get_options() {
  1094. return array(
  1095. 'name' => $this->name,
  1096. 'icon' => $this->icon,
  1097. 'url' => $this->url,
  1098. );
  1099. }
  1100. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
  1101. $opts = $this->get_options();
  1102. $text = '&nbsp;';
  1103. if ( !$this->smart )
  1104. if ( $this->button_style != 'icon' )
  1105. $text = $this->get_name();
  1106. $klasses = array( 'share-'.$this->shortname );
  1107. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' )
  1108. $klasses[] = 'share-icon';
  1109. if ( $this->button_style == 'icon' ) {
  1110. $text = '';
  1111. $klasses[] = 'no-text';
  1112. }
  1113. if ( $this->button_style == 'text' )
  1114. $klasses[] = 'no-icon';
  1115. $link = sprintf(
  1116. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span style="background-image:url(&quot;%s&quot;) !important;background-position:left center;background-repeat:no-repeat;">%s</span></a>',
  1117. implode( ' ', $klasses ),
  1118. $this->get_name(),
  1119. addcslashes( esc_url_raw( $opts['icon'] ), '"' ),
  1120. $text
  1121. );
  1122. ?>
  1123. <div class="option option-smart-off">
  1124. <?php echo $link ; ?>
  1125. </div><?php
  1126. }
  1127. }
  1128. class Share_Tumblr extends Sharing_Source {
  1129. public $shortname = 'tumblr';
  1130. public $genericon = '\f214';
  1131. public function __construct( $id, array $settings ) {
  1132. parent::__construct( $id, $settings );
  1133. if ( 'official' == $this->button_style )
  1134. $this->smart = true;
  1135. else
  1136. $this->smart = false;
  1137. }
  1138. public function get_name() {
  1139. return __( 'Tumblr', 'jetpack' );
  1140. }
  1141. public function get_display( $post ) {
  1142. if ( $this->smart ) {
  1143. $target = '';
  1144. if ( true == $this->open_link_in_new )
  1145. $target = '_blank';
  1146. return '<a target="' . $target . '" href="https://www.tumblr.com/share/link/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&name=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '" title="' . __( 'Share on Tumblr', 'jetpack' ) . '" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:62px; height:20px; background:url(\'https://platform.tumblr.com/v1/share_2.png\') top left no-repeat transparent;">' . __( 'Share on Tumblr', 'jetpack' ) . '</a>';
  1147. } else {
  1148. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
  1149. }
  1150. }
  1151. public function process_request( $post, array $post_data ) {
  1152. // Record stats
  1153. parent::process_request( $post, $post_data );
  1154. // Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
  1155. $url = 'https://www.tumblr.com/share?v=3&u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '&s=';
  1156. wp_redirect( $url );
  1157. die();
  1158. }
  1159. // http://www.tumblr.com/share?v=3&u=URL&t=TITLE&s=
  1160. public function display_footer() {
  1161. if ( $this->smart ) {
  1162. ?><script type="text/javascript" src="https://platform.tumblr.com/v1/share.js"></script><?php
  1163. } else {
  1164. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  1165. }
  1166. }
  1167. }
  1168. class Share_Pinterest extends Sharing_Source {
  1169. public $shortname = 'pinterest';
  1170. public $genericon = '\f209';
  1171. public function __construct( $id, array $settings ) {
  1172. parent::__construct( $id, $settings );
  1173. if ( 'official' == $this->button_style )
  1174. $this->smart = true;
  1175. else
  1176. $this->smart = false;
  1177. }
  1178. public function get_name() {
  1179. return __( 'Pinterest', 'jetpack' );
  1180. }
  1181. public function get_image( $post ) {
  1182. if ( class_exists( 'Jetpack_PostImages' ) ) {
  1183. $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
  1184. if ( ! empty( $image ) ) {
  1185. return $image['src'];
  1186. }
  1187. }
  1188. /**
  1189. * Filters the default image used by the Pinterest Pin It share button.
  1190. *
  1191. * @module sharedaddy
  1192. *
  1193. * @since 3.6.0
  1194. *
  1195. * @param string $url Default image URL.
  1196. */
  1197. return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' );
  1198. }
  1199. public function get_external_url( $post ) {
  1200. $url = 'https://www.pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&media=' . rawurlencode( $this->get_image( $post ) ) . '&description=' . rawurlencode( $post->post_title );
  1201. /**
  1202. * Filters the Pinterest share URL used in sharing button output.
  1203. *
  1204. * @module sharedaddy
  1205. *
  1206. * @since 3.6.0
  1207. *
  1208. * @param string $url Pinterest share URL.
  1209. */
  1210. return apply_filters( 'jetpack_sharing_pinterest_share_url', $url );
  1211. }
  1212. public function get_widget_type() {
  1213. /**
  1214. * Filters the Pinterest widget type.
  1215. *
  1216. * @see https://business.pinterest.com/en/widget-builder
  1217. *
  1218. * @module sharedaddy
  1219. *
  1220. * @since 3.6.0
  1221. *
  1222. * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal.
  1223. */
  1224. return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' );
  1225. }
  1226. public function get_display( $post ) {
  1227. $display = '';
  1228. if ( $this->smart ) {
  1229. $display = sprintf(
  1230. '<div class="pinterest_button"><a href="%s" data-pin-do="%s" data-pin-config="beside"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a></div>',
  1231. esc_url( $this->get_external_url( $post ) ),
  1232. esc_attr( $this->get_widget_type() )
  1233. );
  1234. } else {
  1235. $display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest', 'sharing-pinterest-' . $post->ID );
  1236. }
  1237. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  1238. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
  1239. sharing_register_post_for_share_counts( $post->ID );
  1240. }
  1241. return $display;
  1242. }
  1243. public function process_request( $post, array $post_data ) {
  1244. // Record stats
  1245. parent::process_request( $post, $post_data );
  1246. // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest
  1247. if ( !isset( $_GET['js_only'] ) ) {
  1248. $pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
  1249. wp_redirect( $pinterest_url );
  1250. } else {
  1251. echo '// share count bumped';
  1252. }
  1253. die();
  1254. }
  1255. public function display_footer() {
  1256. /**
  1257. * Filter the Pin it button appearing when hovering over images when using the official button style.
  1258. *
  1259. * @module sharedaddy
  1260. *
  1261. * @since 3.6.0
  1262. *
  1263. * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images.
  1264. */
  1265. $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true );
  1266. ?>
  1267. <?php if ( $this->smart ) : ?>
  1268. <script type="text/javascript">
  1269. // Pinterest shared resources
  1270. var s = document.createElement("script");
  1271. s.type = "text/javascript";
  1272. s.async = true;
  1273. <?php if ( $jetpack_pinit_over ) echo "s.setAttribute('data-pin-hover', true);"; ?>
  1274. s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js";
  1275. var x = document.getElementsByTagName("script")[0];
  1276. x.parentNode.insertBefore(s, x);
  1277. // if 'Pin it' button has 'counts' make container wider
  1278. jQuery(window).load( function(){ jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } );
  1279. </script>
  1280. <?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?>
  1281. <script type="text/javascript">
  1282. jQuery(document).ready( function(){
  1283. jQuery('body').on('click', 'a.share-pinterest', function(e){
  1284. e.preventDefault();
  1285. // Load Pinterest Bookmarklet code
  1286. var s = document.createElement("script");
  1287. s.type = "text/javascript";
  1288. s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 );
  1289. var x = document.getElementsByTagName("script")[0];
  1290. x.parentNode.insertBefore(s, x);
  1291. // Trigger Stats
  1292. var s = document.createElement("script");
  1293. s.type = "text/javascript";
  1294. s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1';
  1295. var x = document.getElementsByTagName("script")[0];
  1296. x.parentNode.insertBefore(s, x);
  1297. });
  1298. });
  1299. </script>
  1300. <?php endif;
  1301. }
  1302. }
  1303. class Share_Pocket extends Sharing_Source {
  1304. public $shortname = 'pocket';
  1305. public $genericon = '\f224';
  1306. public function __construct( $id, array $settings ) {
  1307. parent::__construct( $id, $settings );
  1308. if ( 'official' == $this->button_style )
  1309. $this->smart = true;
  1310. else
  1311. $this->smart = false;
  1312. }
  1313. public function get_name() {
  1314. return __( 'Pocket', 'jetpack' );
  1315. }
  1316. public function process_request( $post, array $post_data ) {
  1317. // Record stats
  1318. parent::process_request( $post, $post_data );
  1319. $pocket_url = esc_url_raw( 'https://getpocket.com/save/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
  1320. wp_redirect( $pocket_url );
  1321. exit;
  1322. }
  1323. public function get_display( $post ) {
  1324. if ( $this->smart ) {
  1325. $post_count = 'horizontal';
  1326. $button = '';
  1327. $button .= '<div class="pocket_button">';
  1328. $button .= sprintf( '<a href="https://getpocket.com/save" class="pocket-btn" data-lang="%s" data-save-url="%s" data-pocket-count="%s" >%s</a>', 'en', esc_attr( $this->get_share_url( $post->ID ) ), $post_count, esc_attr__( 'Pocket', 'jetpack' ) );
  1329. $button .= '</div>';
  1330. return $button;
  1331. } else {
  1332. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pocket', 'share to', 'jetpack' ), __( 'Click to share on Pocket', 'jetpack' ), 'share=pocket' );
  1333. }
  1334. }
  1335. function display_footer() {
  1336. if ( $this->smart ) :
  1337. ?>
  1338. <script>
  1339. // Don't use Pocket's default JS as it we need to force init new Pocket share buttons loaded via JS.
  1340. function jetpack_sharing_pocket_init() {
  1341. jQuery.getScript( 'https://widgets.getpocket.com/v1/j/btn.js?v=1' );
  1342. }
  1343. jQuery( document ).ready( jetpack_sharing_pocket_init );
  1344. jQuery( document.body ).on( 'post-load', jetpack_sharing_pocket_init );
  1345. </script>
  1346. <?php
  1347. else :
  1348. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  1349. endif;
  1350. }
  1351. }
  1352. class Share_Telegram extends Sharing_Source {
  1353. public $shortname = 'telegram';
  1354. public function __construct( $id, array $settings ) {
  1355. parent::__construct( $id, $settings );
  1356. }
  1357. public function get_name() {
  1358. return __( 'Telegram', 'jetpack' );
  1359. }
  1360. public function process_request( $post, array $post_data ) {
  1361. // Record stats
  1362. parent::process_request( $post, $post_data );
  1363. $telegram_url = esc_url_raw( 'htt