PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2.2
Jetpack – WP Security, Backup, Speed, & Growth v11.2.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / sharedaddy / sharing-sources.php

sharing-sources.php in Jetpack – WP Security, Backup, Speed, & Growth 11.2.2, at modules/sharedaddy/sharing-sources.php

3,047 lines 76.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Define all sharing sources.
4 *
5 * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
6 */
7
8 use Automattic\Jetpack\Device_Detection\User_Agent_Info;
9
10 /**
11 * Base class for sharing sources.
12 * See individual sharing classes below for the implementation of this class.
13 */
14 abstract class Sharing_Source {
15 /**
16 * Button style (icon, icon-text, text, or official).
17 *
18 * @var string
19 */
20 public $button_style;
21
22 /**
23 * Does the service have an official version.
24 *
25 * @var bool
26 */
27 public $smart;
28
29 /**
30 * Should the sharing link open in a new tab.
31 *
32 * @var bool
33 */
34 protected $open_link_in_new;
35
36 /**
37 * Sharing unique ID.
38 *
39 * @var int
40 */
41 protected $id;
42
43 /**
44 * Constructor.
45 *
46 * @param int $id Sharing source ID.
47 * @param array $settings Sharing settings.
48 */
49 public function __construct( $id, array $settings ) {
50 $this->id = $id;
51 /**
52 * Filter the way sharing links open.
53 *
54 * By default, sharing links open in a new window.
55 *
56 * @module sharedaddy
57 *
58 * @since 3.4.0
59 *
60 * @param bool true Should Sharing links open in a new window. Default to true.
61 */
62 $this->open_link_in_new = apply_filters( 'jetpack_open_sharing_in_new_window', true );
63
64 if ( isset( $settings['button_style'] ) ) {
65 $this->button_style = $settings['button_style'];
66 }
67
68 if ( isset( $settings['smart'] ) ) {
69 $this->smart = $settings['smart'];
70 }
71 }
72
73 /**
74 * Is a service deprecated.
75 *
76 * @return bool
77 */
78 public function is_deprecated() {
79 return false;
80 }
81
82 /**
83 * Get the protocol to use for a sharing service, based on the site settings.
84 *
85 * @return string
86 */
87 public function http() {
88 return is_ssl() ? 'https' : 'http';
89 }
90
91 /**
92 * Get unique sharing ID.
93 *
94 * @return int
95 */
96 public function get_id() {
97 return $this->id;
98 }
99
100 /**
101 * Get unique sharing ID. Similar to get_id().
102 *
103 * @return int
104 */
105 public function get_class() {
106 return $this->id;
107 }
108
109 /**
110 * Get a post's permalink to use for sharing.
111 *
112 * @param int $post_id Post ID.
113 *
114 * @return string
115 */
116 public function get_share_url( $post_id ) {
117 /**
118 * Filter the sharing permalink.
119 *
120 * @module sharedaddy
121 *
122 * @since 1.2.0
123 *
124 * @param string get_permalink( $post_id ) Post Permalink.
125 * @param int $post_id Post ID.
126 * @param int $this->id Sharing ID.
127 */
128 return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id );
129 }
130
131 /**
132 * Get a post's title to use for sharing.
133 *
134 * @param int $post_id Post ID.
135 *
136 * @return string
137 */
138 public function get_share_title( $post_id ) {
139 $post = get_post( $post_id );
140 /**
141 * Filter the sharing title.
142 *
143 * @module sharedaddy
144 *
145 * @since 2.8.0
146 *
147 * @param string $post->post_title Post Title.
148 * @param int $post_id Post ID.
149 * @param int $this->id Sharing ID.
150 */
151 $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id );
152
153 return html_entity_decode( wp_kses( $title, null ) );
154 }
155
156 /**
157 * Does this sharing source have a custom style.
158 *
159 * @return bool
160 */
161 public function has_custom_button_style() {
162 return false;
163 }
164
165 /**
166 * Get the HTML markup to display a sharing link.
167 *
168 * @param string $url Post URL to share.
169 * @param string $text Sharing display text.
170 * @param string $title The title for the link.
171 * @param string $query Additional query arguments to add to the link. They should be in 'foo=bar&baz=1' format.
172 * @param bool|string $id Sharing ID to include in the data-shared attribute.
173 * @param array $data_attributes The keys are used as additional attribute names with 'data-' prefix.
174 * The values are used as the attribute values.
175 *
176 * @return string The HTML for the link.
177 */
178 public function get_link( $url, $text, $title, $query = '', $id = false, $data_attributes = array() ) {
179 $args = func_get_args();
180 $klasses = array( 'share-' . $this->get_class(), 'sd-button' );
181
182 if ( 'icon' === $this->button_style || 'icon-text' === $this->button_style ) {
183 $klasses[] = 'share-icon';
184 }
185
186 if ( 'icon' === $this->button_style ) {
187 $text = $title;
188 $klasses[] = 'no-text';
189
190 if ( true === $this->open_link_in_new ) {
191 $text .= __( ' (Opens in new window)', 'jetpack' );
192 }
193 }
194
195 /**
196 * Filter the sharing display ID.
197 *
198 * @module sharedaddy
199 *
200 * @since 3.4.0
201 *
202 * @param string|false $id Sharing ID.
203 * @param object $this Sharing service properties.
204 * @param array $args Array of sharing service options.
205 */
206 $id = apply_filters( 'jetpack_sharing_display_id', $id, $this, $args );
207 /**
208 * Filter the sharing display link.
209 *
210 * @module sharedaddy
211 *
212 * @since 2.8.0
213 *
214 * @param string $url Post URL.
215 * @param object $this Sharing service properties.
216 * @param string|false $id Sharing ID.
217 * @param array $args Array of sharing service options.
218 */
219 $url = apply_filters( 'sharing_display_link', $url, $this, $id, $args ); // backwards compatibility
220 /**
221 * Filter the sharing display link.
222 *
223 * @module sharedaddy
224 *
225 * @since 2.8.0
226 *
227 * @param string $url Post URL.
228 * @param object $this Sharing service properties.
229 * @param string|false $id Sharing ID.
230 * @param array $args Array of sharing service options.
231 */
232 $url = apply_filters( 'jetpack_sharing_display_link', $url, $this, $id, $args );
233 /**
234 * Filter the sharing display query.
235 *
236 * @module sharedaddy
237 *
238 * @since 2.8.0
239 *
240 * @param string $query Sharing service URL parameter.
241 * @param object $this Sharing service properties.
242 * @param string|false $id Sharing ID.
243 * @param array $args Array of sharing service options.
244 */
245 $query = apply_filters( 'jetpack_sharing_display_query', $query, $this, $id, $args );
246
247 if ( ! empty( $query ) ) {
248 if ( false === stripos( $url, '?' ) ) {
249 $url .= '?' . $query;
250 } else {
251 $url .= '&amp;' . $query;
252 }
253 }
254
255 if ( 'text' === $this->button_style ) {
256 $klasses[] = 'no-icon';
257 }
258
259 /**
260 * Filter the sharing display classes.
261 *
262 * @module sharedaddy
263 *
264 * @since 3.4.0
265 *
266 * @param array $klasses Sharing service classes.
267 * @param object $this Sharing service properties.
268 * @param string|false $id Sharing ID.
269 * @param array $args Array of sharing service options.
270 */
271 $klasses = apply_filters( 'jetpack_sharing_display_classes', $klasses, $this, $id, $args );
272 /**
273 * Filter the sharing display title.
274 *
275 * @module sharedaddy
276 *
277 * @since 3.4.0
278 *
279 * @param string $title Sharing service title.
280 * @param object $this Sharing service properties.
281 * @param string|false $id Sharing ID.
282 * @param array $args Array of sharing service options.
283 */
284 $title = apply_filters( 'jetpack_sharing_display_title', $title, $this, $id, $args );
285 /**
286 * Filter the sharing display text.
287 *
288 * @module sharedaddy
289 *
290 * @since 3.4.0
291 *
292 * @param string $text Sharing service text.
293 * @param object $this Sharing service properties.
294 * @param string|false $id Sharing ID.
295 * @param array $args Array of sharing service options.
296 */
297 $text = apply_filters( 'jetpack_sharing_display_text', $text, $this, $id, $args );
298
299 /**
300 * Filter the sharing data attributes.
301 *
302 * @module sharedaddy
303 *
304 * @since 11.0
305 *
306 * @param array $data_attributes Attributes supplied from the sharing source.
307 * Note that 'data-' will be prepended to all keys.
308 * @param Sharing_Source $this Sharing source instance.
309 * @param string|false $id Sharing ID.
310 * @param array $args Array of sharing service options.
311 */
312 $data_attributes = apply_filters( 'jetpack_sharing_data_attributes', (array) $data_attributes, $this, $id, $args );
313
314 $encoded_data_attributes = '';
315 if ( ! empty( $data_attributes ) ) {
316 $encoded_data_attributes = implode(
317 ' ',
318 array_map(
319 function ( $data_key, $data_value ) {
320 return sprintf(
321 'data-%s="%s"',
322 esc_attr( str_replace( array( ' ', '"' ), '', $data_key ) ),
323 esc_attr( $data_value )
324 );
325 },
326 array_keys( $data_attributes ),
327 array_values( $data_attributes )
328 )
329 );
330 }
331
332 return sprintf(
333 '<a rel="nofollow%s" data-shared="%s" class="%s" href="%s"%s title="%s" %s><span%s>%s</span></a>',
334 ( true === $this->open_link_in_new ) ? ' noopener noreferrer' : '',
335 ( $id ? esc_attr( $id ) : '' ),
336 implode( ' ', $klasses ),
337 $url,
338 ( true === $this->open_link_in_new ) ? ' target="_blank"' : '',
339 $title,
340 $encoded_data_attributes,
341 ( 'icon' === $this->button_style ) ? '></span><span class="sharing-screen-reader-text"' : '',
342 $text
343 );
344 }
345
346 /**
347 * Get an unfiltered post permalink to use when generating a sharing URL with get_link.
348 * Use instead of get_share_url for non-official styles as get_permalink ensures that process_request
349 * will be executed more reliably, in the case that the filtered URL uses a service that strips query parameters.
350 *
351 * @since 3.7.0
352 * @param int $post_id Post ID.
353 *
354 * @uses get_permalink
355 *
356 * @return string get_permalink( $post_id ) Post permalink.
357 */
358 public function get_process_request_url( $post_id ) {
359 return get_permalink( $post_id );
360 }
361
362 /**
363 * Get sharing name.
364 */
365 abstract public function get_name();
366
367 /**
368 * Get the markup of the sharing button.
369 *
370 * @param WP_Post $post Post object.
371 */
372 abstract public function get_display( $post );
373
374 /**
375 * Add content specific to a service in the head.
376 */
377 public function display_header() {
378 }
379
380 /**
381 * Add content specific to a service in the footer.
382 */
383 public function display_footer() {
384 }
385
386 /**
387 * Does the service have advanced options.
388 *
389 * @return bool
390 */
391 public function has_advanced_options() {
392 return false;
393 }
394
395 /**
396 * Get the AMP specific markup for a sharing button.
397 *
398 * @param \WP_Post $post The current post being viewed.
399 *
400 * @return bool|string
401 */
402 public function get_amp_display( $post ) {
403 // Only display markup if we're on a post.
404 if ( empty( $post ) ) {
405 return false;
406 }
407
408 return $this->build_amp_markup();
409 }
410
411 /**
412 * Generates and returns the markup for an AMP sharing button.
413 *
414 * @param array $attrs Custom attributes for rendering the social icon.
415 *
416 * @return string
417 */
418 protected function build_amp_markup( $attrs = array() ) {
419
420 $title = sprintf(
421 /* translators: placeholder is a service name, such as "Twitter" or "Facebook". */
422 __( 'Click to share on %s', 'jetpack' ),
423 $this->get_name()
424 );
425
426 $attrs = array_merge(
427 array(
428 'type' => $this->get_id(),
429 'height' => '32px',
430 'width' => '32px',
431 'aria-label' => $title,
432 'title' => $title,
433 ),
434 $attrs
435 );
436 $sharing_link = '<amp-social-share';
437 foreach ( $attrs as $key => $value ) {
438 $sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
439 }
440 $sharing_link .= '></amp-social-share>';
441 return $sharing_link;
442 }
443
444 /**
445 * Display a preview of the sharing button.
446 *
447 * @param bool $echo Whether to echo the output or return it.
448 * @param bool $force_smart Whether to force the smart (official) services to be shown.
449 * @param null|string $button_style Button style.
450 *
451 * @return string|void
452 */
453 public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
454 $text = '&nbsp;';
455 $button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style;
456 if ( ! $this->smart && ! $force_smart ) {
457 if ( $button_style !== 'icon' ) {
458 $text = $this->get_name();
459 }
460 }
461
462 $klasses = array( 'share-' . $this->get_class(), 'sd-button' );
463
464 if ( $button_style === 'icon' || $button_style === 'icon-text' ) {
465 $klasses[] = 'share-icon';
466 }
467
468 if ( $button_style === 'icon' ) {
469 $klasses[] = 'no-text';
470 }
471
472 if ( $button_style === 'text' ) {
473 $klasses[] = 'no-icon';
474 }
475
476 $is_deprecated = $this->is_deprecated();
477
478 $link = sprintf(
479 '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',
480 implode( ' ', $klasses ),
481 esc_attr(
482 $is_deprecated
483 /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
484 ? sprintf( __( 'The %1$s service has shut down. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $this->get_name() )
485 : $this->get_name()
486 ),
487 esc_html(
488 $is_deprecated
489 /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
490 ? sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() )
491 : $text
492 )
493 );
494
495 $smart = ( $this->smart || $force_smart ) ? 'on' : 'off';
496 $return = "<div class='option option-smart-$smart'>$link</div>";
497 if ( $echo ) {
498 echo $return; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we escape things above.
499 }
500
501 return $return;
502 }
503
504 /**
505 * Get sharing stats for a specific post or sharing service.
506 *
507 * @param bool|WP_Post $post Post object.
508 *
509 * @return int
510 */
511 public function get_total( $post = false ) {
512 global $wpdb, $blog_id;
513
514 $name = strtolower( $this->get_id() );
515
516 if ( $post === false ) {
517 // get total number of shares for service
518 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $blog_id, $name ) );
519 }
520
521 // get total shares for a post
522 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 ) );
523 }
524
525 /**
526 * Get sharing stats for all posts on the site.
527 *
528 * @return array
529 */
530 public function get_posts_total() {
531 global $wpdb, $blog_id;
532
533 $totals = array();
534 $name = strtolower( $this->get_id() );
535
536 $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 ) );
537
538 if ( ! empty( $my_data ) ) {
539 foreach ( $my_data as $row ) {
540 $totals[] = new Sharing_Post_Total( $row->id, $row->total );
541 }
542 }
543
544 usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
545
546 return $totals;
547 }
548
549 /**
550 * Process sharing request. Add actions that need to happen when sharing here.
551 *
552 * @param WP_Post $post Post object.
553 * @param array $post_data Array of information about the post we're sharing.
554 *
555 * @return void
556 */
557 public function process_request( $post, array $post_data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
558 /**
559 * Fires when a post is shared via one of the sharing buttons.
560 *
561 * @module sharedaddy
562 *
563 * @since 1.1.0
564 *
565 * @param array $args Aray of information about the sharing service.
566 */
567 do_action(
568 'sharing_bump_stats',
569 array(
570 'service' => $this,
571 'post' => $post,
572 )
573 );
574 }
575
576 /**
577 * Redirect to an external social network site to finish sharing.
578 *
579 * @param string $url Sharing URL for a given service.
580 */
581 public function redirect_request( $url ) {
582 wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We allow external redirects here; we define them ourselves.
583
584 // We set up this custom header to indicate to search engines not to index this page.
585 header( 'X-Robots-Tag: noindex, nofollow' );
586 die();
587 }
588
589 /**
590 * Add extra JavaScript to a sharing service.
591 *
592 * @param string $name Sharing service name.
593 * @param array $params Array of sharing options.
594 *
595 * @return void
596 */
597 public function js_dialog( $name, $params = array() ) {
598 if ( true !== $this->open_link_in_new ) {
599 return;
600 }
601
602 $defaults = array(
603 'menubar' => 1,
604 'resizable' => 1,
605 'width' => 600,
606 'height' => 400,
607 );
608 $params = array_merge( $defaults, $params );
609 $opts = array();
610 foreach ( $params as $key => $val ) {
611 $opts[] = "$key=$val";
612 }
613 $opts = implode( ',', $opts );
614
615 // Add JS after sharing-js has been enqueued.
616 wp_add_inline_script(
617 'sharing-js',
618 "var windowOpen;
619 ( function () {
620 function matches( el, sel ) {
621 return !! (
622 el.matches && el.matches( sel ) ||
623 el.msMatchesSelector && el.msMatchesSelector( sel )
624 );
625 }
626
627 document.body.addEventListener( 'click', function ( event ) {
628 if ( ! event.target ) {
629 return;
630 }
631
632 var el;
633 if ( matches( event.target, 'a.share-$name' ) ) {
634 el = event.target;
635 } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-$name' ) ) {
636 el = event.target.parentNode;
637 }
638
639 if ( el ) {
640 event.preventDefault();
641
642 // If there's another sharing window open, close it.
643 if ( typeof windowOpen !== 'undefined' ) {
644 windowOpen.close();
645 }
646 windowOpen = window.open( el.getAttribute( 'href' ), 'wpcom$name', '$opts' );
647 return false;
648 }
649 } );
650 } )();"
651 );
652 }
653 }
654
655 /**
656 * Handle the display of deprecated sharing services.
657 */
658 abstract class Deprecated_Sharing_Source extends Sharing_Source {
659 /**
660 * Button style (icon-text, icon, or text)
661 *
662 * @var string
663 */
664 public $button_style = 'text';
665
666 /**
667 * Does the service have an official version.
668 *
669 * @var bool
670 */
671 public $smart = false;
672
673 /**
674 * Should the sharing link open in a new tab.
675 *
676 * @var bool
677 */
678 protected $open_link_in_new = false;
679
680 /**
681 * Sharing unique ID.
682 *
683 * @var int
684 */
685 protected $id;
686
687 /**
688 * Is the service deprecated.
689 *
690 * @var bool
691 */
692 protected $deprecated = true;
693
694 /**
695 * Constructor.
696 *
697 * @param int $id Sharing source ID.
698 * @param array $settings Sharing settings.
699 */
700 final public function __construct( $id, array $settings ) {
701 $this->id = $id;
702
703 if ( isset( $settings['button_style'] ) ) {
704 $this->button_style = $settings['button_style'];
705 }
706 }
707
708 /**
709 * Is the service deprecated.
710 *
711 * @return bool
712 */
713 final public function is_deprecated() {
714 return true;
715 }
716
717 /**
718 * Get a post's permalink to use for sharing.
719 *
720 * @param int $post_id Post ID.
721 *
722 * @return string
723 */
724 final public function get_share_url( $post_id ) {
725 return get_permalink( $post_id );
726 }
727
728 /**
729 * No AMP display for deprecated sources.
730 *
731 * @param \WP_Post $post The current post being viewed.
732 */
733 final public function get_amp_display( $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
734 return false;
735 }
736
737 /**
738 * Display a preview of the sharing button.
739 *
740 * @param bool $echo Whether to echo the output or return it.
741 * @param bool $force_smart Whether to force the smart (official) services to be shown.
742 * @param null|string $button_style Button style.
743 *
744 * @return string|void
745 */
746 final public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
747 return parent::display_preview( $echo, false, $button_style );
748 }
749
750 /**
751 * Get sharing stats for a specific post or sharing service.
752 *
753 * @param bool|WP_Post $post Post object.
754 *
755 * @return int
756 */
757 final public function get_total( $post = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
758 return 0;
759 }
760
761 /**
762 * Get sharing stats for all posts on the site.
763 *
764 * @return int|array
765 */
766 final public function get_posts_total() {
767 return 0;
768 }
769
770 /**
771 * Process sharing request. Add actions that need to happen when sharing here.
772 *
773 * @param WP_Post $post Post object.
774 * @param array $post_data Array of information about the post we're sharing.
775 *
776 * @return void
777 */
778 final public function process_request( $post, array $post_data ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
779 parent::process_request( $post, $post_data );
780 }
781
782 /**
783 * Get the markup of the sharing button.
784 *
785 * @param WP_Post $post Post object.
786 *
787 * @return string
788 */
789 final public function get_display( $post ) {
790 if ( current_user_can( 'manage_options' ) ) {
791 return $this->display_deprecated( $post );
792 }
793
794 return '';
795 }
796
797 /**
798 * Display a custom message for deprecated services.
799 *
800 * @param WP_Post $post Post object.
801 *
802 * @return string
803 */
804 public function display_deprecated( $post ) {
805 return $this->get_link(
806 $this->get_share_url( $post->ID ),
807 /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
808 sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() ),
809 /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
810 sprintf( __( 'The %1$s service has shut down. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $this->get_name() )
811 );
812 }
813 }
814
815 /**
816 * Handle the display of advanced sharing services.
817 * Custom sharing buttons we create ourselves will be such services.
818 */
819 abstract class Sharing_Advanced_Source extends Sharing_Source {
820 /**
821 * Does the service have advanced options.
822 *
823 * @return bool
824 */
825 public function has_advanced_options() {
826 return true;
827 }
828
829 /**
830 * Display options for our sharing buttons.
831 */
832 abstract public function display_options();
833
834 /**
835 * Sanitize and save options for our sharing buttons.
836 *
837 * @param array $data Data to be saved.
838 *
839 * @return void
840 */
841 abstract public function update_options( array $data );
842
843 /**
844 * Get array of information about the service.
845 *
846 * @return array
847 */
848 abstract public function get_options();
849 }
850
851 /**
852 * Handle the display of the email sharing button.
853 */
854 class Share_Email extends Sharing_Source {
855 /**
856 * Service short name.
857 *
858 * @var string
859 */
860 public $shortname = 'email';
861
862 /**
863 * Service icon font code.
864 *
865 * @var string
866 */
867 public $icon = '\f410';
868
869 /**
870 * Constructor.
871 *
872 * @param int $id Sharing source ID.
873 * @param array $settings Sharing settings.
874 */
875 public function __construct( $id, array $settings ) {
876 parent::__construct( $id, $settings );
877
878 if ( 'official' === $this->button_style ) {
879 $this->smart = true;
880 } else {
881 $this->smart = false;
882 }
883 }
884
885 /**
886 * Service name.
887 *
888 * @return string
889 */
890 public function get_name() {
891 return _x( 'Email', 'as sharing source', 'jetpack' );
892 }
893
894 /**
895 * Helper function to return a nonce action based on the current post.
896 *
897 * @param WP_Post|null $post The current post if it is defined.
898 * @return string The nonce action name.
899 */
900 protected function get_email_share_nonce_action( $post ) {
901 if ( ! empty( $post ) && $post instanceof WP_Post ) {
902 return 'jetpack-email-share-' . $post->ID;
903 }
904
905 return 'jetpack-email-share';
906 }
907
908 /**
909 * Process sharing request. Add actions that need to happen when sharing here.
910 *
911 * @param WP_Post $post Post object.
912 * @param array $post_data Array of information about the post we're sharing.
913 *
914 * @return void
915 */
916 public function process_request( $post, array $post_data ) {
917 $is_ajax = false;
918 if (
919 isset( $_SERVER['HTTP_X_REQUESTED_WITH'] )
920 && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest'
921 ) {
922 $is_ajax = true;
923 }
924
925 // Require an AJAX-driven submit and a valid nonce to process the request
926 if (
927 $is_ajax
928 && isset( $post_data['email-share-nonce'] )
929 && wp_verify_nonce( $post_data['email-share-nonce'], $this->get_email_share_nonce_action( $post ) )
930 ) {
931 // Ensure that we bump stats
932 parent::process_request( $post, $post_data );
933 }
934
935 if ( $is_ajax ) {
936 wp_send_json_success();
937 } else {
938 wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' );
939 exit;
940 }
941
942 wp_die();
943 }
944
945 /**
946 * Get the markup of the sharing button.
947 *
948 * @param WP_Post $post Post object.
949 *
950 * @return string The HTML for the button.
951 */
952 public function get_display( $post ) {
953 $tracking_url = $this->get_process_request_url( $post->ID );
954 if ( false === stripos( $tracking_url, '?' ) ) {
955 $tracking_url .= '?';
956 } else {
957 $tracking_url .= '&';
958 }
959 $tracking_url .= 'share=email';
960
961 $data_attributes = array(
962 'email-share-error-title' => __( 'Do you have email set up?', 'jetpack' ),
963 'email-share-error-text' => __(
964 "If you're having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.",
965 'jetpack'
966 ),
967 'email-share-nonce' => wp_create_nonce( $this->get_email_share_nonce_action( $post ) ),
968 'email-share-track-url' => $tracking_url,
969 );
970
971 $post_title = $this->get_share_title( $post->ID );
972 $post_url = $this->get_share_url( $post->ID );
973
974 /** This filter is documented in plugins/jetpack/modules/sharedaddy/sharedaddy.php */
975 $email_subject = apply_filters(
976 'wp_sharing_email_send_post_subject',
977 sprintf( '[%s] %s', __( 'Shared Post', 'jetpack' ), $post_title )
978 );
979
980 $mailto_query = sprintf(
981 'subject=%s&body=%s&share=email',
982 rawurlencode( $email_subject ),
983 rawurlencode( $post_url )
984 );
985
986 return $this->get_link(
987 'mailto:',
988 _x( 'Email', 'share to', 'jetpack' ),
989 __( 'Click to email a link to a friend', 'jetpack' ),
990 $mailto_query,
991 false,
992 $data_attributes
993 );
994 }
995
996 /**
997 * AMP display for email.
998 *
999 * @param \WP_Post $post The current post being viewed.
1000 */
1001 public function get_amp_display( $post ) { // phpcs:ignore
1002 $attrs = array(
1003 // Prevents an empty window from opening on desktop: https://github.com/ampproject/amphtml/issues/9157.
1004 'data-target' => '_self',
1005 );
1006
1007 return $this->build_amp_markup( $attrs );
1008 }
1009 }
1010
1011 /**
1012 * Twitter sharing button.
1013 */
1014 class Share_Twitter extends Sharing_Source {
1015 /**
1016 * Service short name.
1017 *
1018 * @var string
1019 */
1020 public $shortname = 'twitter';
1021
1022 /**
1023 * Service icon font code.
1024 *
1025 * @var string
1026 */
1027 public $icon = '\f202';
1028
1029 /**
1030 * Length of a URL on Twitter.
1031 * 'https://dev.twitter.com/rest/reference/get/help/configuration'
1032 * ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23
1033 *
1034 * @var int
1035 */
1036 public $short_url_length = 24;
1037
1038 /**
1039 * Constructor.
1040 *
1041 * @param int $id Sharing source ID.
1042 * @param array $settings Sharing settings.
1043 */
1044 public function __construct( $id, array $settings ) {
1045 parent::__construct( $id, $settings );
1046
1047 if ( 'official' === $this->button_style ) {
1048 $this->smart = true;
1049 } else {
1050 $this->smart = false;
1051 }
1052 }
1053
1054 /**
1055 * Service name.
1056 *
1057 * @return string
1058 */
1059 public function get_name() {
1060 return __( 'Twitter', 'jetpack' );
1061 }
1062
1063 /**
1064 * Determine the Twitter 'via' value for a post.
1065 *
1066 * @param WP_Post|int $post Post object or post ID.
1067 * @return string Twitter handle without the preceding @.
1068 **/
1069 public static function sharing_twitter_via( $post ) {
1070 $post = get_post( $post );
1071 /**
1072 * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag.
1073 *
1074 * @module sharedaddy
1075 *
1076 * @since 3.0.0
1077 *
1078 * @param string $string Twitter Username.
1079 * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags.
1080 */
1081 $twitter_site_tag_value = apply_filters(
1082 'jetpack_twitter_cards_site_tag',
1083 '',
1084 /** This action is documented in modules/sharedaddy/sharing-sources.php */
1085 array( 'twitter:creator' => apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ) )
1086 );
1087
1088 /*
1089 * Hack to remove the unwanted behavior of adding 'via @jetpack' which
1090 * was introduced with the adding of the Twitter cards.
1091 * This should be a temporary solution until a better method is setup.
1092 */
1093 if ( 'jetpack' === $twitter_site_tag_value ) {
1094 $twitter_site_tag_value = '';
1095 }
1096
1097 /**
1098 * Filters the Twitter username used as "via" in the Twitter sharing button.
1099 *
1100 * @module sharedaddy
1101 *
1102 * @since 1.7.0
1103 *
1104 * @param string $twitter_site_tag_value Twitter Username.
1105 * @param int $post->ID Post ID.
1106 */
1107 $twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID );
1108
1109 // Strip out anything other than a letter, number, or underscore.
1110 // This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle.
1111 return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value );
1112 }
1113
1114 /**
1115 * Determine the 'related' Twitter accounts for a post.
1116 *
1117 * @param WP_Post|int $post Post object or post ID.
1118 * @return string Comma-separated list of Twitter handles.
1119 **/
1120 public static function get_related_accounts( $post ) {
1121 $post = get_post( $post );
1122 /**
1123 * Filter the list of related Twitter accounts added to the Twitter sharing button.
1124 *
1125 * @module sharedaddy
1126 *
1127 * @since 1.7.0
1128 *
1129 * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description'
1130 * @param int $post->ID Post ID.
1131 */
1132 $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
1133
1134 // Example related string: account1,account2:Account 2 description,account3
1135 $related = array();
1136
1137 foreach ( $related_accounts as $related_account_username => $related_account_description ) {
1138 // Join the description onto the end of the username
1139 if ( $related_account_description ) {
1140 $related_account_username .= ':' . $related_account_description;
1141 }
1142
1143 $related[] = $related_account_username;
1144 }
1145
1146 return implode( ',', $related );
1147 }
1148
1149 /**
1150 * Get the markup of the sharing button.
1151 *
1152 * @param WP_Post $post Post object.
1153 *
1154 * @return string
1155 */
1156 public function get_display( $post ) {
1157 $via = $this->sharing_twitter_via( $post );
1158
1159 if ( $via ) {
1160 $via = 'data-via="' . esc_attr( $via ) . '"';
1161 } else {
1162 $via = '';
1163 }
1164
1165 $related = $this->get_related_accounts( $post );
1166 if ( ! empty( $related ) && $related !== $via ) {
1167 $related = 'data-related="' . esc_attr( $related ) . '"';
1168 } else {
1169 $related = '';
1170 }
1171
1172 if ( $this->smart ) {
1173 $share_url = $this->get_share_url( $post->ID );
1174 $post_title = $this->get_share_title( $post->ID );
1175 return sprintf(
1176 '<a href="https://twitter.com/share" class="twitter-share-button" data-url="%1$s" data-text="%2$s" %3$s %4$s>Tweet</a>',
1177 esc_url( $share_url ),
1178 esc_attr( $post_title ),
1179 $via,
1180 $related
1181 );
1182 } else {
1183 if (
1184 /**
1185 * Allow plugins to disable sharing counts for specific sharing services.
1186 *
1187 * @module sharedaddy
1188 *
1189 * @since 3.0.0
1190 *
1191 * @param bool true Should sharing counts be enabled for this specific service. Default to true.
1192 * @param int $post->ID Post ID.
1193 * @param string $str Sharing service name.
1194 */
1195 apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' )
1196 ) {
1197 sharing_register_post_for_share_counts( $post->ID );
1198 }
1199 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 );
1200 }
1201 }
1202
1203 /**
1204 * Process sharing request. Add actions that need to happen when sharing here.
1205 *
1206 * @param WP_Post $post Post object.
1207 * @param array $post_data Array of information about the post we're sharing.
1208 *
1209 * @return void
1210 */
1211 public function process_request( $post, array $post_data ) {
1212 $post_title = $this->get_share_title( $post->ID );
1213 $post_link = $this->get_share_url( $post->ID );
1214
1215 if ( function_exists( 'mb_stripos' ) ) {
1216 $strlen = 'mb_strlen';
1217 $substr = 'mb_substr';
1218 } else {
1219 $strlen = 'strlen';
1220 $substr = 'substr';
1221 }
1222
1223 $via = $this->sharing_twitter_via( $post );
1224 $related = $this->get_related_accounts( $post );
1225 if ( $via ) {
1226 $sig = " via @$via";
1227 if ( $related === $via ) {
1228 $related = false;
1229 }
1230 } else {
1231 $via = false;
1232 $sig = '';
1233 }
1234
1235 $suffix_length = $this->short_url_length + $strlen( $sig );
1236 // $sig is handled by twitter in their 'via' argument.
1237 // $post_link is handled by twitter in their 'url' argument.
1238 if ( 280 < $strlen( $post_title ) + $suffix_length ) {
1239 // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
1240 $text = $substr( $post_title, 0, 280 - $suffix_length - 1 ) . "\xE2\x80\xA6";
1241 } else {
1242 $text = $post_title;
1243 }
1244
1245 // Record stats
1246 parent::process_request( $post, $post_data );
1247
1248 $url = $post_link;
1249 $twitter_url = add_query_arg(
1250 rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
1251 'https://twitter.com/intent/tweet'
1252 );
1253
1254 parent::redirect_request( $twitter_url );
1255 }
1256
1257 /**
1258 * Does this sharing source have a custom style.
1259 *
1260 * @return bool
1261 */
1262 public function has_custom_button_style() {
1263 return $this->smart;
1264 }
1265
1266 /**
1267 * Add content specific to a service in the footer.
1268 */
1269 public function display_footer() {
1270 if ( $this->smart ) {
1271 ?>
1272 <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>
1273 <?php
1274 } else {
1275 $this->js_dialog( $this->shortname, array( 'height' => 350 ) );
1276 }
1277 }
1278 }
1279
1280 /**
1281 * Reddit sharing button.
1282 */
1283 class Share_Reddit extends Sharing_Source {
1284 /**
1285 * Service short name.
1286 *
1287 * @var string
1288 */
1289 public $shortname = 'reddit';
1290
1291 /**
1292 * Service icon font code.
1293 *
1294 * @var string
1295 */
1296 public $icon = '\f222';
1297
1298 /**
1299 * Constructor.
1300 *
1301 * @param int $id Sharing source ID.
1302 * @param array $settings Sharing settings.
1303 */
1304 public function __construct( $id, array $settings ) {
1305 parent::__construct( $id, $settings );
1306
1307 if ( 'official' === $this->button_style ) {
1308 $this->smart = true;
1309 } else {
1310 $this->smart = false;
1311 }
1312 }
1313
1314 /**
1315 * Service name.
1316 *
1317 * @return string
1318 */
1319 public function get_name() {
1320 return __( 'Reddit', 'jetpack' );
1321 }
1322
1323 /**
1324 * Get the markup of the sharing button.
1325 *
1326 * @param WP_Post $post Post object.
1327 *
1328 * @return string
1329 */
1330 public function get_display( $post ) {
1331 if ( $this->smart ) {
1332 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>';
1333 } else {
1334 return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
1335 }
1336 }
1337
1338 /**
1339 * AMP display for Reddit.
1340 *
1341 * @param \WP_Post $post The current post being viewed.
1342 */
1343 public function get_amp_display( $post ) {
1344 $attrs = array(
1345 'data-share-endpoint' => esc_url_raw( 'https://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
1346 );
1347
1348 return $this->build_amp_markup( $attrs );
1349 }
1350
1351 /**
1352 * Process sharing request. Add actions that need to happen when sharing here.
1353 *
1354 * @param WP_Post $post Post object.
1355 * @param array $post_data Array of information about the post we're sharing.
1356 *
1357 * @return void
1358 */
1359 public function process_request( $post, array $post_data ) {
1360 $reddit_url = $this->http() . '://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) );
1361
1362 // Record stats
1363 parent::process_request( $post, $post_data );
1364
1365 parent::redirect_request( $reddit_url );
1366 }
1367 }
1368
1369 /**
1370 * LinkedIn sharing button.
1371 */
1372 class Share_LinkedIn extends Sharing_Source {
1373 /**
1374 * Service short name.
1375 *
1376 * @var string
1377 */
1378 public $shortname = 'linkedin';
1379
1380 /**
1381 * Service icon font code.
1382 *
1383 * @var string
1384 */
1385 public $icon = '\f207';
1386
1387 /**
1388 * Constructor.
1389 *
1390 * @param int $id Sharing source ID.
1391 * @param array $settings Sharing settings.
1392 */
1393 public function __construct( $id, array $settings ) {
1394 parent::__construct( $id, $settings );
1395
1396 if ( 'official' === $this->button_style ) {
1397 $this->smart = true;
1398 } else {
1399 $this->smart = false;
1400 }
1401 }
1402
1403 /**
1404 * Service name.
1405 *
1406 * @return string
1407 */
1408 public function get_name() {
1409 return __( 'LinkedIn', 'jetpack' );
1410 }
1411
1412 /**
1413 * Does this sharing source have a custom style.
1414 *
1415 * @return bool
1416 */
1417 public function has_custom_button_style() {
1418 return $this->smart;
1419 }
1420
1421 /**
1422 * Get the markup of the sharing button.
1423 *
1424 * @param WP_Post $post Post object.
1425 *
1426 * @return string
1427 */
1428 public function get_display( $post ) {
1429 $display = '';
1430
1431 if ( $this->smart ) {
1432 $share_url = $this->get_share_url( $post->ID );
1433 $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
1434 } else {
1435 $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 );
1436 }
1437
1438 /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1439 if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
1440 sharing_register_post_for_share_counts( $post->ID );
1441 }
1442
1443 return $display;
1444 }
1445
1446 /**
1447 * Process sharing request. Add actions that need to happen when sharing here.
1448 *
1449 * @param WP_Post $post Post object.
1450 * @param array $post_data Array of information about the post we're sharing.
1451 *
1452 * @return void
1453 */
1454 public function process_request( $post, array $post_data ) {
1455
1456 $post_link = $this->get_share_url( $post->ID );
1457
1458 // Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
1459 // https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
1460 $linkedin_url = add_query_arg(
1461 array(
1462 'url' => rawurlencode( $post_link ),
1463 ),
1464 'https://www.linkedin.com/cws/share?token=&isFramed=false'
1465 );
1466
1467 // Record stats
1468 parent::process_request( $post, $post_data );
1469
1470 parent::redirect_request( $linkedin_url );
1471 }
1472
1473 /**
1474 * Add content specific to a service in the footer.
1475 */
1476 public function display_footer() {
1477 if ( ! $this->smart ) {
1478 $this->js_dialog(
1479 $this->shortname,
1480 array(
1481 'width' => 580,
1482 'height' => 450,
1483 )
1484 );
1485 } else {
1486 ?>
1487 <script type="text/javascript">
1488 ( function () {
1489 var currentScript = document.currentScript;
1490
1491 // Helper function to load an external script.
1492 function loadScript( url, cb ) {
1493 var script = document.createElement( 'script' );
1494 var prev = currentScript || document.getElementsByTagName( 'script' )[ 0 ];
1495 script.setAttribute( 'async', true );
1496 script.setAttribute( 'src', url );
1497 prev.parentNode.insertBefore( script, prev );
1498 script.addEventListener( 'load', cb );
1499 }
1500
1501 function init() {
1502 loadScript( 'https://platform.linkedin.com/in.js?async=true', function () {
1503 if ( typeof IN !== 'undefined' ) {
1504 IN.init();
1505 }
1506 } );
1507 }
1508
1509 if ( document.readyState === 'loading' ) {
1510 document.addEventListener( 'DOMContentLoaded', init );
1511 } else {
1512 init();
1513 }
1514
1515 document.body.addEventListener( 'is.post-load', function() {
1516 if ( typeof IN !== 'undefined' ) {
1517 IN.parse();
1518 }
1519 } );
1520 } )();
1521 </script>
1522 <?php
1523 }
1524 }
1525 }
1526
1527 /**
1528 * Facebook sharing button.
1529 */
1530 class Share_Facebook extends Sharing_Source {
1531 /**
1532 * Service short name.
1533 *
1534 * @var string
1535 */
1536 public $shortname = 'facebook';
1537
1538 /**
1539 * Service icon font code.
1540 *
1541 * @var string
1542 */
1543 public $icon = '\f204';
1544
1545 /**
1546 * Sharing type.
1547 *
1548 * @var string
1549 */
1550 private $share_type = 'default';
1551
1552 /**
1553 * Constructor.
1554 *
1555 * @param int $id Sharing source ID.
1556 * @param array $settings Sharing settings.
1557 */
1558 public function __construct( $id, array $settings ) {
1559 parent::__construct( $id, $settings );
1560
1561 if ( isset( $settings['share_type'] ) ) {
1562 $this->share_type = $settings['share_type'];
1563 }
1564
1565 if ( 'official' === $this->button_style ) {
1566 $this->smart = true;
1567 } else {
1568 $this->smart = false;
1569 }
1570 }
1571
1572 /**
1573 * Service name.
1574 *
1575 * @return string
1576 */
1577 public function get_name() {
1578 return __( 'Facebook', 'jetpack' );
1579 }
1580
1581 /**
1582 * Add content specific to a service in the head.
1583 */
1584 public function display_header() {
1585 }
1586
1587 /**
1588 * Guess locale from language code.
1589 *
1590 * @param string $lang Language code.
1591 *
1592 * @return string|bool
1593 */
1594 public function guess_locale_from_lang( $lang ) {
1595 if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) {
1596 return 'en_US';
1597 }
1598
1599 if ( ! class_exists( 'GP_Locales' ) ) {
1600 if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
1601 return false;
1602 }
1603
1604 require JETPACK__GLOTPRESS_LOCALES_PATH;
1605 }
1606
1607 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1608 // WP.com: get_locale() returns 'it'
1609 $locale = GP_Locales::by_slug( $lang );
1610 } else {
1611 // Jetpack: get_locale() returns 'it_IT';
1612 $locale = GP_Locales::by_field( 'wp_locale', $lang );
1613 }
1614
1615 if ( ! $locale ) {
1616 return false;
1617 }
1618
1619 if ( empty( $locale->facebook_locale ) ) {
1620 if ( empty( $locale->wp_locale ) ) {
1621 return false;
1622 } else {
1623 // Facebook SDK is smart enough to fall back to en_US if a
1624 // locale isn't supported. Since supported Facebook locales
1625 // can fall out of sync, we'll attempt to use the known
1626 // wp_locale value and rely on said fallback.
1627 return $locale->wp_locale;
1628 }
1629 }
1630
1631 return $locale->facebook_locale;
1632 }
1633
1634 /**
1635 * Get the markup of the sharing button.
1636 *
1637 * @param WP_Post $post Post object.
1638 *
1639 * @return string
1640 */
1641 public function get_display( $post ) {
1642 if ( $this->smart ) {
1643 $share_url = $this->get_share_url( $post->ID );
1644 $fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>';
1645 /**
1646 * Filter the output of the Facebook Sharing button.
1647 *
1648 * @module sharedaddy
1649 *
1650 * @since 3.6.0
1651 *
1652 * @param string $fb_share_html Facebook Sharing button HTML.
1653 * @param string $share_url URL of the post to share.
1654 */
1655 return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url );
1656 }
1657
1658 /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1659 if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) {
1660 sharing_register_post_for_share_counts( $post->ID );
1661 }
1662 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 );
1663 }
1664
1665 /**
1666 * AMP display for Facebook.
1667 *
1668 * @param \WP_Post $post The current post being viewed.
1669 */
1670 public function get_amp_display( $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1671 $attrs = array(
1672 /** This filter is documented in modules/sharedaddy/sharing-sources.php */
1673 'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
1674 );
1675
1676 return $this->build_amp_markup( $attrs );
1677 }
1678
1679 /**
1680 * Process sharing request. Add actions that need to happen when sharing here.
1681 *
1682 * @param WP_Post $post Post object.
1683 * @param array $post_data Array of information about the post we're sharing.
1684 *
1685 * @return void
1686 */
1687 public function process_request( $post, array $post_data ) {
1688 $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 ) );
1689
1690 // Record stats
1691 parent::process_request( $post, $post_data );
1692
1693 parent::redirect_request( $fb_url );
1694 }
1695
1696 /**
1697 * Add content specific to a service in the footer.
1698 */
1699 public function display_footer() {
1700 $this->js_dialog( $this->shortname );
1701 if ( $this->smart ) {
1702 $locale = $this->guess_locale_from_lang( get_locale() );
1703 if ( ! $locale ) {
1704 $locale = 'en_US';
1705 }
1706 /**
1707 * Filter the App ID used in the official Facebook Share button.
1708 *
1709 * @since 3.8.0
1710 *
1711 * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID).
1712 */
1713 $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
1714 if ( is_numeric( $fb_app_id ) ) {
1715 $fb_app_id = '&appId=' . $fb_app_id;
1716 } else {
1717 $fb_app_id = '';
1718 }
1719 ?>
1720 <div id="fb-root"></div>
1721 <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 esc_attr( $locale ); ?>/sdk.js#xfbml=1<?php echo esc_attr( $fb_app_id ); ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
1722 <script>
1723 document.body.addEventListener( 'is.post-load', function() {
1724 if ( 'undefined' !== typeof FB ) {
1725 FB.XFBML.parse();
1726 }
1727 } );
1728 </script>
1729 <?php
1730 }
1731 }
1732 }
1733
1734 /**
1735 * Print button.
1736 */
1737 class Share_Print extends Sharing_Source {
1738 /**
1739 * Service short name.
1740 *
1741 * @var string
1742 */
1743 public $shortname = 'print';
1744
1745 /**
1746 * Service icon font code.
1747 *
1748 * @var string
1749 */
1750 public $icon = '\f469';
1751
1752 /**
1753 * Constructor.
1754 *
1755 * @param int $id Sharing source ID.
1756 * @param array $settings Sharing settings.
1757 */
1758 public function __construct( $id, array $settings ) {
1759 parent::__construct( $id, $settings );
1760
1761 if ( 'official' === $this->button_style ) {
1762 $this->smart = true;
1763 } else {
1764 $this->smart = false;
1765 }
1766 }
1767
1768 /**
1769 * Service name.
1770 *
1771 * @return string
1772 */
1773 public function get_name() {
1774 return __( 'Print', 'jetpack' );
1775 }
1776
1777 /**
1778 * Get the markup of the sharing button.
1779 *
1780 * @param WP_Post $post Post object.
1781 *
1782 * @return string
1783 */
1784 public function get_display( $post ) {
1785 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' ) );
1786 }
1787
1788 /**
1789 * AMP display for Print.
1790 *
1791 * @param \WP_Post $post The current post being viewed.
1792 */
1793 public function get_amp_display( $post ) {
1794 if ( empty( $post ) ) {
1795 return false;
1796 }
1797
1798 return '<button class="amp-social-share print" on="tap:AMP.print">Print</button>';
1799 }
1800 }
1801
1802 /**
1803 * Press This Button.
1804 */
1805 class Share_PressThis extends Sharing_Source {
1806 /**
1807 * Service short name.
1808 *
1809 * @var string
1810 */
1811 public $shortname = 'pressthis';
1812
1813 /**
1814 * Service icon font code.
1815 *
1816 * @var string
1817 */
1818 public $icon = '\f205';
1819
1820 /**
1821 * Constructor.
1822 *
1823 * @param int $id Sharing source ID.
1824 * @param array $settings Sharing settings.
1825 */
1826 public function __construct( $id, array $settings ) {
1827 parent::__construct( $id, $settings );
1828
1829 if ( 'official' === $this->button_style ) {
1830 $this->smart = true;
1831 } else {
1832 $this->smart = false;
1833 }
1834 }
1835
1836 /**
1837 * Service name.
1838 *
1839 * @return string
1840 */
1841 public function get_name() {
1842 return __( 'Press This', 'jetpack' );
1843 }
1844
1845 /**
1846 * Process sharing request. Add actions that need to happen when sharing here.
1847 *
1848 * @param WP_Post $post Post object.
1849 * @param array $post_data Array of information about the post we're sharing.
1850 *
1851 * @return void
1852 */
1853 public function process_request( $post, array $post_data ) {
1854 global $current_user;
1855
1856 $primary_blog = (int) get_user_meta( $current_user->ID, 'primary_blog', true );
1857 if ( $primary_blog ) {
1858 $primary_blog_details = get_blog_details( $primary_blog );
1859 } else {
1860 $primary_blog_details = false;
1861 }
1862
1863 if ( $primary_blog_details ) {
1864 $blogs = array( $primary_blog_details );
1865 } elseif ( function_exists( 'get_active_blogs_for_user' ) ) {
1866 $blogs = get_active_blogs_for_user();
1867 if ( empty( $blogs ) ) {
1868 $blogs = get_blogs_of_user( $current_user->ID );
1869 }
1870 } else {
1871 $blogs = get_blogs_of_user( $current_user->ID );
1872 }
1873
1874 if ( empty( $blogs ) ) {
1875 wp_safe_redirect( get_permalink( $post->ID ) );
1876 die();
1877 }
1878
1879 $blog = current( $blogs );
1880
1881 $args = array(
1882 'u' => rawurlencode( $this->get_share_url( $post->ID ) ),
1883 );
1884
1885 $args['url-scan-submit'] = 'Scan';
1886 $args['_wpnonce'] = wp_create_nonce( 'scan-site' );
1887
1888 $url = $blog->siteurl . '/wp-admin/press-this.php';
1889 $url = add_query_arg( $args, $url );
1890
1891 // Record stats
1892 parent::process_request( $post, $post_data );
1893
1894 parent::redirect_request( $url );
1895 }
1896
1897 /**
1898 * Get the markup of the sharing button.
1899 *
1900 * @param WP_Post $post Post object.
1901 *
1902 * @return string
1903 */
1904 public function get_display( $post ) {
1905 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' );
1906 }
1907
1908 /**
1909 * No AMP display for PressThis.
1910 *
1911 * @param \WP_Post $post The current post being viewed.
1912 */
1913 public function get_amp_display( $post ) { // phpcs:ignore
1914 return false;
1915 }
1916 }
1917
1918 /**
1919 * Custom (user-defined) sharing button.
1920 */
1921 class Share_Custom extends Sharing_Advanced_Source {
1922 /**
1923 * Sharing service name.
1924 *
1925 * @var string
1926 */
1927 private $name;
1928
1929 /**
1930 * Sharing icon.
1931 *
1932 * @var string
1933 */
1934 private $icon;
1935
1936 /**
1937 * Sharing service URL.
1938 *
1939 * @var string
1940 */
1941 private $url;
1942
1943 /**
1944 * Does the service have an official version.
1945 *
1946 * @var bool
1947 */
1948 public $smart = true;
1949
1950 /**
1951 * Service short name.
1952 *
1953 * @var string
1954 */
1955 public $shortname;
1956
1957 /**
1958 * Custom sharing class.
1959 *
1960 * @return string
1961 */
1962 public function get_class() {
1963 return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) );
1964 }
1965
1966 /**
1967 * Constructor.
1968 *
1969 * @param int $id Sharing source ID.
1970 * @param array $settings Sharing settings.
1971 */
1972 public function __construct( $id, array $settings ) {
1973 parent::__construct( $id, $settings );
1974
1975 if ( isset( $settings['name'] ) ) {
1976 $this->name = $settings['name'];
1977 $this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
1978 }
1979
1980 if ( isset( $settings['icon'] ) ) {
1981 $this->icon = $settings['icon'];
1982
1983 $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1984 $i = 0;
1985 while ( $new_icon !== $this->icon ) {
1986 if ( $i > 5 ) {
1987 $this->icon = false;
1988 break;
1989 } else {
1990 $this->icon = $new_icon;
1991 $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1992 }
1993 $i++;
1994 }
1995 }
1996
1997 if ( isset( $settings['url'] ) ) {
1998 $this->url = $settings['url'];
1999 }
2000 }
2001
2002 /**
2003 * Service name.
2004 *
2005 * @return string
2006 */
2007 public function get_name() {
2008 return $this->name;
2009 }
2010
2011 /**
2012 * Get the markup of the sharing button.
2013 *
2014 * @param WP_Post $post Post object.
2015 *
2016 * @return string
2017 */
2018 public function get_display( $post ) {
2019 $str = $this->get_link(
2020 $this->get_process_request_url( $post->ID ),
2021 esc_html( $this->name ),
2022 sprintf(
2023 /* Translators: placeholder is the name of a social network. */
2024 __( 'Click to share on %s', 'jetpack' ),
2025 esc_attr( $this->name )
2026 ),
2027 'share=' . $this->id
2028 );
2029 return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str );
2030 }
2031
2032 /**
2033 * No AMP display for custom elements.
2034 *
2035 * @param \WP_Post $post The current post being viewed.
2036 */
2037 public function get_amp_display( $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
2038 return false;
2039 }
2040
2041 /**
2042 * Process sharing request. Add actions that need to happen when sharing here.
2043 *
2044 * @param WP_Post $post Post object.
2045 * @param array $post_data Array of information about the post we're sharing.
2046 *
2047 * @return void
2048 */
2049 public function process_request( $post, array $post_data ) {
2050 $url = str_replace( '&amp;', '&', $this->url );
2051 $url = str_replace( '%post_id%', rawurlencode( $post->ID ), $url );
2052 $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
2053 $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
2054 $url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url );
2055 $url = str_replace( '%home_url%', rawurlencode( home_url() ), $url );
2056 $url = str_replace( '%post_slug%', rawurlencode( $post->post_name ), $url );
2057
2058 if ( strpos( $url, '%post_tags%' ) !== false ) {
2059 $tags = get_the_tags( $post->ID );
2060 $tagged = '';
2061
2062 if ( $tags ) {
2063 $tagged_raw = array();
2064 foreach ( $tags as $tag ) {
2065 $tagged_raw[] = rawurlencode( $tag->name );
2066 }
2067
2068 $tagged = implode( ',', $tagged_raw );
2069 }
2070
2071 $url = str_replace( '%post_tags%', $tagged, $url );
2072 }
2073
2074 if ( strpos( $url, '%post_excerpt%' ) !== false ) {
2075 $url_excerpt = $post->post_excerpt;
2076 if ( empty( $url_excerpt ) ) {
2077 $url_excerpt = $post->post_content;
2078 }
2079
2080 $url_excerpt = wp_strip_all_tags( strip_shortcodes( $url_excerpt ) );
2081 $url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
2082 $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
2083 $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
2084 }
2085
2086 // Record stats
2087 parent::process_request( $post, $post_data );
2088
2089 parent::redirect_request( $url );
2090 }
2091
2092 /**
2093 * Display options for our sharing buttons.
2094 *
2095 * @return void
2096 */
2097 public function display_options() {
2098 ?>
2099 <div class="input">
2100 <table class="form-table">
2101 <tbody>
2102 <tr>
2103 <th scope="row"><?php esc_html_e( 'Label', 'jetpack' ); ?></th>
2104 <td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
2105 </tr>
2106
2107 <tr>
2108 <th scope="row"><?php esc_html_e( 'URL', 'jetpack' ); ?></th>
2109 <td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
2110 </tr>
2111
2112 <tr>
2113 <th scope="row"><?php esc_html_e( 'Icon', 'jetpack' ); ?></th>
2114 <td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
2115 </tr>
2116
2117 <tr>
2118 <th scope="row"></th>
2119 <td>
2120 <input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" />
2121 <a href="#" class="remove"><small><?php esc_html_e( 'Remove Service', 'jetpack' ); ?></small></a>
2122 </td>
2123 </tr>
2124 </tbody>
2125 </table>
2126 </div>
2127 <?php
2128 }
2129
2130 /**
2131 * Sanitize and save options for our sharing buttons.
2132 *
2133 * @param array $data Data to be saved.
2134 *
2135 * @return void
2136 */
2137 public function update_options( array $data ) {
2138 $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
2139 $url = trim( esc_url_raw( $data['url'] ) );
2140 $icon = trim( esc_url_raw( $data['icon'] ) );
2141
2142 if ( $name ) {
2143 $this->name = $name;
2144 }
2145
2146 if ( $url ) {
2147 $this->url = $url;
2148 }
2149
2150 if ( $icon ) {
2151 $this->icon = $icon;
2152 }
2153 }
2154
2155 /**
2156 * Get array of information about the service.
2157 *
2158 * @return array
2159 */
2160 public function get_options() {
2161 return array(
2162 'name' => $this->name,
2163 'icon' => $this->icon,
2164 'url' => $this->url,
2165 );
2166 }
2167
2168 /**
2169 * Display a preview of the sharing button.
2170 *
2171 * @param bool $echo Whether to echo the output or return it.
2172 * @param bool $force_smart Whether to force the smart (official) services to be shown.
2173 * @param null|string $button_style Button style.
2174 *
2175 * @return void
2176 */
2177 public function display_preview( $echo = true, $force_smart = false, $button_style = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
2178 $opts = $this->get_options();
2179
2180 $text = '&nbsp;';
2181 if ( ! $this->smart ) {
2182 if ( $this->button_style !== 'icon' ) {
2183 $text = $this->get_name();
2184 }
2185 }
2186
2187 $klasses = array( 'share-' . $this->shortname );
2188
2189 if ( $this->button_style === 'icon' || $this->button_style === 'icon-text' ) {
2190 $klasses[] = 'share-icon';
2191 }
2192
2193 if ( $this->button_style === 'icon' ) {
2194 $text = '';
2195 $klasses[] = 'no-text';
2196 }
2197
2198 if ( $this->button_style === 'text' ) {
2199 $klasses[] = 'no-icon';
2200 }
2201
2202 $link = sprintf(
2203 '<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>',
2204 esc_attr( implode( ' ', $klasses ) ),
2205 esc_attr( $this->get_name() ),
2206 addcslashes( esc_url_raw( $opts['icon'] ), '"' ),
2207 esc_html( $text )
2208 );
2209 ?>
2210 <div class="option option-smart-off">
2211 <?php echo $link; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped above. ?>
2212 </div>
2213 <?php
2214 }
2215 }
2216
2217 /**
2218 * Tumblr sharing service.
2219 */
2220 class Share_Tumblr extends Sharing_Source {
2221 /**
2222 * Service short name.
2223 *
2224 * @var string
2225 */
2226 public $shortname = 'tumblr';
2227
2228 /**
2229 * Service icon font code.
2230 *
2231 * @var string
2232 */
2233 public $icon = '\f214';
2234
2235 /**
2236 * Constructor.
2237 *
2238 * @param int $id Sharing source ID.
2239 * @param array $settings Sharing settings.
2240 */
2241 public function __construct( $id, array $settings ) {
2242 parent::__construct( $id, $settings );
2243 if ( 'official' === $this->button_style ) {
2244 $this->smart = true;
2245 } else {
2246 $this->smart = false;
2247 }
2248 }
2249
2250 /**
2251 * Service name.
2252 *
2253 * @return string
2254 */
2255 public function get_name() {
2256 return __( 'Tumblr', 'jetpack' );
2257 }
2258
2259 /**
2260 * Get the markup of the sharing button.
2261 *
2262 * @param WP_Post $post Post object.
2263 *
2264 * @return string
2265 */
2266 public function get_display( $post ) {
2267 if ( $this->smart ) {
2268 $target = '';
2269 if ( true === $this->open_link_in_new ) {
2270 $target = '_blank';
2271 }
2272
2273 /**
2274 * If we are looking at a single post, let Tumblr figure out the post type (text, photo, link, quote, chat, or video)
2275 * based on the content available on the page.
2276 * If we are not looking at a single post, content from other posts can appear on the page and Tumblr will pick that up.
2277 * In this case, we want Tumblr to focus on our current post, so we will limit the post type to link, where we can give Tumblr a link to our post.
2278 */
2279 if ( ! is_single() ) {
2280 $posttype = 'data-posttype="link"';
2281 } else {
2282 $posttype = '';
2283 }
2284
2285 // Documentation: https://www.tumblr.com/docs/en/share_button
2286 return sprintf(
2287 '<a class="tumblr-share-button" target="%1$s" href="%2$s" data-title="%3$s" data-content="%4$s" title="%5$s"%6$s>%5$s</a>',
2288 $target,
2289 'https://www.tumblr.com/share',
2290 $this->get_share_title( $post->ID ),
2291 $this->get_share_url( $post->ID ),
2292 __( 'Share on Tumblr', 'jetpack' ),
2293 $posttype
2294 );
2295 } else {
2296 return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
2297 }
2298 }
2299
2300 /**
2301 * Process sharing request. Add actions that need to happen when sharing here.
2302 *
2303 * @param WP_Post $post Post object.
2304 * @param array $post_data Array of information about the post we're sharing.
2305 *
2306 * @return void
2307 */
2308 public function process_request( $post, array $post_data ) {
2309 // Record stats
2310 parent::process_request( $post, $post_data );
2311
2312 // Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
2313 $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=';
2314
2315 parent::redirect_request( $url );
2316 }
2317
2318 /**
2319 * Add content specific to a service in the footer.
2320 */
2321 public function display_footer() {
2322 if ( $this->smart ) {
2323 // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
2324 ?>
2325 <script id="tumblr-js" type="text/javascript" src="https://assets.tumblr.com/share-button.js"></script>
2326 <?php
2327 // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript
2328 } else {
2329 $this->js_dialog(
2330 $this->shortname,
2331 array(
2332 'width' => 450,
2333 'height' => 450,
2334 )
2335 );
2336 }
2337 }
2338 }
2339
2340 /**
2341 * Pinterest sharing service.
2342 */
2343 class Share_Pinterest extends Sharing_Source {
2344 /**
2345 * Service short name.
2346 *
2347 * @var string
2348 */
2349 public $shortname = 'pinterest';
2350
2351 /**
2352 * Service icon font code.
2353 *
2354 * @var string
2355 */
2356 public $icon = '\f209';
2357
2358 /**
2359 * Constructor.
2360 *
2361 * @param int $id Sharing source ID.
2362 * @param array $settings Sharing settings.
2363 */
2364 public function __construct( $id, array $settings ) {
2365 parent::__construct( $id, $settings );
2366 if ( 'official' === $this->button_style ) {
2367 $this->smart = true;
2368 } else {
2369 $this->smart = false;
2370 }
2371 }
2372
2373 /**
2374 * Service name.
2375 *
2376 * @return string
2377 */
2378 public function get_name() {
2379 return __( 'Pinterest', 'jetpack' );
2380 }
2381
2382 /**
2383 * Get image representative of the post to pass on to Pinterest.
2384 *
2385 * @param WP_Post $post Post object.
2386 *
2387 * @return string
2388 */
2389 public function get_image( $post ) {
2390 if ( class_exists( 'Jetpack_PostImages' ) ) {
2391 $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
2392 if ( ! empty( $image ) ) {
2393 return $image['src'];
2394 }
2395 }
2396
2397 /**
2398 * Filters the default image used by the Pinterest Pin It share button.
2399 *
2400 * @module sharedaddy
2401 *
2402 * @since 3.6.0
2403 *
2404 * @param string $url Default image URL.
2405 */
2406 return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' );
2407 }
2408
2409 /**
2410 * Get Pinterest external sharing URL.
2411 *
2412 * @param WP_Post $post Post object.
2413 *
2414 * @return string
2415 */
2416 public function get_external_url( $post ) {
2417 $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 );
2418
2419 /**
2420 * Filters the Pinterest share URL used in sharing button output.
2421 *
2422 * @module sharedaddy
2423 *
2424 * @since 3.6.0
2425 *
2426 * @param string $url Pinterest share URL.
2427 */
2428 return apply_filters( 'jetpack_sharing_pinterest_share_url', $url );
2429 }
2430
2431 /**
2432 * Get Pinterest widget type.
2433 *
2434 * @return string
2435 */
2436 public function get_widget_type() {
2437 /**
2438 * Filters the Pinterest widget type.
2439 *
2440 * @see https://business.pinterest.com/en/widget-builder
2441 *
2442 * @module sharedaddy
2443 *
2444 * @since 3.6.0
2445 *
2446 * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal.
2447 */
2448 return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' );
2449 }
2450
2451 /**
2452 * Get the markup of the sharing button.
2453 *
2454 * @param WP_Post $post Post object.
2455 *
2456 * @return string
2457 */
2458 public function get_display( $post ) {
2459 $display = '';
2460
2461 if ( $this->smart ) {
2462 $display = sprintf(
2463 '<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>',
2464 esc_url( $this->get_external_url( $post ) ),
2465 esc_attr( $this->get_widget_type() )
2466 );
2467 } else {
2468 $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 );
2469 }
2470
2471 /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
2472 if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
2473 sharing_register_post_for_share_counts( $post->ID );
2474 }
2475
2476 return $display;
2477 }
2478
2479 /**
2480 * Process sharing request. Add actions that need to happen when sharing here.
2481 *
2482 * @param WP_Post $post Post object.
2483 * @param array $post_data Array of information about the post we're sharing.
2484 *
2485 * @return void
2486 */
2487 public function process_request( $post, array $post_data ) {
2488 // Record stats
2489 parent::process_request( $post, $post_data );
2490 // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest
2491 if ( ! isset( $_GET['js_only'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
2492 $pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
2493 parent::redirect_request( $pinterest_url );
2494 } else {
2495 echo '// share count bumped';
2496 die();
2497 }
2498 }
2499
2500 /**
2501 * Add content specific to a service in the footer.
2502 */
2503 public function display_footer() {
2504 /**
2505 * Filter the Pin it button appearing when hovering over images when using the official button style.
2506 *
2507 * @module sharedaddy
2508 *
2509 * @since 3.6.0
2510 *
2511 * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images.
2512 */
2513 $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true );
2514 ?>
2515 <?php if ( $this->smart ) : ?>
2516 <script type="text/javascript">
2517 ( function () {
2518 // Pinterest shared resources
2519 var s = document.createElement( 'script' );
2520 s.type = 'text/javascript';
2521 s.async = true;
2522 <?php
2523 if ( $jetpack_pinit_over ) {
2524 echo "s.setAttribute( 'data-pin-hover', true );";
2525 }
2526 ?>
2527 s.src = window.location.protocol + '//assets.pinterest.com/js/pinit.js';
2528 var x = document.getElementsByTagName( 'script' )[ 0 ];
2529 x.parentNode.insertBefore(s, x);
2530 // if 'Pin it' button has 'counts' make container wider
2531 function init() {
2532 var shares = document.querySelectorAll( 'li.share-pinterest' );
2533 for ( var i = 0; i < shares.length; i++ ) {
2534 var share = shares[ i ];
2535 if ( share.querySelector( 'a span:visible' ) ) {
2536 share.style.width = '80px';
2537 }
2538 }
2539 }
2540
2541 if ( document.readyState !== 'complete' ) {
2542 document.addEventListener( 'load', init );
2543 } else {
2544 init();
2545 }
2546 } )();
2547 </script>
2548 <?php elseif ( 'buttonPin' !== $this->get_widget_type() ) : ?>
2549 <script type="text/javascript">
2550 ( function () {
2551 function init() {
2552 document.body.addEventListener( 'click', function ( e ) {
2553 if ( e.target && (
2554 e.target.matches && e.target.matches( 'a.share-pinterest' ) ||
2555 e.target.msMatchesSelector && e.target.msMatchesSelector( 'a.share-pinterest' )
2556 ) ) {
2557 e.preventDefault();
2558 // Load Pinterest Bookmarklet code
2559 var s = document.createElement( 'script' );
2560 s.type = 'text/javascript';
2561 s.src = window.location.protocol + '//assets.pinterest.com/js/pinmarklet.js?r=' + ( Math.random() * 99999999 );
2562 var x = document.getElementsByTagName( 'script' )[ 0 ];
2563 x.parentNode.insertBefore( s, x );
2564 // Trigger Stats
2565 var s = document.createElement( 'script' );
2566 s.type = 'text/javascript';
2567 s.src = e.target.href + ( e.target.href.indexOf( '?' ) ? '&' : '?' ) + 'js_only=1';
2568 var x = document.getElementsByTagName( 'script' )[ '0' ];
2569 x.parentNode.insertBefore( s, x );
2570 }
2571 } );
2572 }
2573
2574 if ( document.readyState === 'loading' ) {
2575 document.addEventListener( 'DOMContentLoaded', init );
2576 } else {
2577 init();
2578 }
2579 } )();
2580 </script>
2581 <?php
2582 endif;
2583 }
2584 }
2585
2586 /**
2587 * Pocket sharing service.
2588 */
2589 class Share_Pocket extends Sharing_Source {
2590 /**
2591 * Service short name.
2592 *
2593 * @var string
2594 */
2595 public $shortname = 'pocket';
2596
2597 /**
2598 * Service icon font code.
2599 *
2600 * @var string
2601 */
2602 public $icon = '\f224';
2603
2604 /**
2605 * Constructor.
2606 *
2607 * @param int $id Sharing source ID.
2608 * @param array $settings Sharing settings.
2609 */
2610 public function __construct( $id, array $settings ) {
2611 parent::__construct( $id, $settings );
2612
2613 if ( 'official' === $this->button_style ) {
2614 $this->smart = true;
2615 } else {
2616 $this->smart = false;
2617 }
2618 }
2619
2620 /**
2621 * Service name.
2622 *
2623 * @return string
2624 */
2625 public function get_name() {
2626 return __( 'Pocket', 'jetpack' );
2627 }
2628
2629 /**
2630 * Process sharing request. Add actions that need to happen when sharing here.
2631 *
2632 * @param WP_Post $post Post object.
2633 * @param array $post_data Array of information about the post we're sharing.
2634 *
2635 * @return void
2636 */
2637 public function process_request( $post, array $post_data ) {
2638 // Record stats
2639 parent::process_request( $post, $post_data );
2640
2641 $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 ) ) );
2642
2643 parent::redirect_request( $pocket_url );
2644 }
2645
2646 /**
2647 * Get the markup of the sharing button.
2648 *
2649 * @param WP_Post $post Post object.
2650 *
2651 * @return string
2652 */
2653 public function get_display( $post ) {
2654 if ( $this->smart ) {
2655 $post_count = 'horizontal';
2656
2657 $button = '';
2658 $button .= '<div class="pocket_button">';
2659 $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' ) );
2660 $button .= '</div>';
2661
2662 return $button;
2663 } else {
2664 return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pocket', 'share to', 'jetpack' ), __( 'Click to share on Pocket', 'jetpack' ), 'share=pocket' );
2665 }
2666
2667 }
2668
2669 /**
2670 * AMP display for Pocket.
2671 *
2672 * @param \WP_Post $post The current post being viewed.
2673 */
2674 public function get_amp_display( $post ) {
2675 $attrs = array(
2676 'data-share-endpoint' => esc_url_raw( 'https://getpocket.com/save/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
2677 );
2678
2679 return $this->build_amp_markup( $attrs );
2680 }
2681
2682 /**
2683 * Add content specific to a service in the footer.
2684 */
2685 public function display_footer() {
2686 if ( $this->smart ) :
2687 ?>
2688 <script>
2689 ( function () {
2690 var currentScript = document.currentScript;
2691
2692 // Don't use Pocket's default JS as it we need to force init new Pocket share buttons loaded via JS.
2693 function jetpack_sharing_pocket_init() {
2694 var script = document.createElement( 'script' );
2695 var prev = currentScript || document.getElementsByTagName( 'script' )[ 0 ];
2696 script.setAttribute( 'async', true );
2697 script.setAttribute( 'src', 'https://widgets.getpocket.com/v1/j/btn.js?v=1' );
2698 prev.parentNode.insertBefore( script, prev );
2699 }
2700
2701 if ( document.readyState === 'loading' ) {
2702 document.addEventListener( 'DOMContentLoaded', jetpack_sharing_pocket_init );
2703 } else {
2704 jetpack_sharing_pocket_init();
2705 }
2706 document.body.addEventListener( 'is.post-load', jetpack_sharing_pocket_init );
2707 } )();
2708 </script>
2709 <?php
2710 else :
2711 $this->js_dialog(
2712 $this->shortname,
2713 array(
2714 'width' => 450,
2715 'height' => 450,
2716 )
2717 );
2718 endif;
2719
2720 }
2721
2722 }
2723
2724 /**
2725 * Telegram sharing service.
2726 */
2727 class Share_Telegram extends Sharing_Source {
2728 /**
2729 * Service short name.
2730 *
2731 * @var string
2732 */
2733 public $shortname = 'telegram';
2734
2735 /**
2736 * Constructor.
2737 *
2738 * @param int $id Sharing source ID.
2739 * @param array $settings Sharing settings.
2740 */
2741 public function __construct( $id, array $settings ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
2742 parent::__construct( $id, $settings );
2743 }
2744
2745 /**
2746 * Service name.
2747 *
2748 * @return string
2749 */
2750 public function get_name() {
2751 return __( 'Telegram', 'jetpack' );
2752 }
2753
2754 /**
2755 * Process sharing request. Add actions that need to happen when sharing here.
2756 *
2757 * @param WP_Post $post Post object.
2758 * @param array $post_data Array of information about the post we're sharing.
2759 *
2760 * @return void
2761 */
2762 public function process_request( $post, array $post_data ) {
2763 // Record stats
2764 parent::process_request( $post, $post_data );
2765
2766 $telegram_url = esc_url_raw( 'https://telegram.me/share/url?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&text=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
2767
2768 parent::redirect_request( $telegram_url );
2769 }
2770
2771 /**
2772 * Get the markup of the sharing button.
2773 *
2774 * @param WP_Post $post Post object.
2775 *
2776 * @return string
2777 */
2778 public function get_display( $post ) {
2779 return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Telegram', 'share to', 'jetpack' ), __( 'Click to share on Telegram', 'jetpack' ), 'share=telegram' );
2780 }
2781
2782 /**
2783 * AMP display for Telegram.
2784 *
2785 * @param \WP_Post $post The current post being viewed.
2786 */
2787 public function get_amp_display( $post ) {
2788 $attrs = array(
2789 'data-share-endpoint' => esc_url_raw( 'https://telegram.me/share/url?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&text=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
2790 );
2791
2792 return $this->build_amp_markup( $attrs );
2793 }
2794
2795 /**
2796 * Add content specific to a service in the footer.
2797 */
2798 public function display_footer() {
2799 $this->js_dialog(
2800 $this->shortname,
2801 array(
2802 'width' => 450,
2803 'height' => 450,
2804 )
2805 );
2806 }
2807 }
2808
2809 /**
2810 * WhatsApp sharing service.
2811 */
2812 class Jetpack_Share_WhatsApp extends Sharing_Source {
2813 /**
2814 * Service short name.
2815 *
2816 * @var string
2817 */
2818 public $shortname = 'jetpack-whatsapp';
2819
2820 /**
2821 * Constructor.
2822 *
2823 * @param int $id Sharing source ID.
2824 * @param array $settings Sharing settings.
2825 */
2826 public function __construct( $id, array $settings ) { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
2827 parent::__construct( $id, $settings );
2828 }
2829
2830 /**
2831 * Service name.
2832 *
2833 * @return string
2834 */
2835 public function get_name() {
2836 return __( 'WhatsApp', 'jetpack' );
2837 }
2838
2839 /**
2840 * Get the markup of the sharing button.
2841 *
2842 * @param WP_Post $post Post object.
2843 *
2844 * @return string
2845 */
2846 public function get_display( $post ) {
2847 return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'WhatsApp', 'share to', 'jetpack' ), __( 'Click to share on WhatsApp', 'jetpack' ), 'share=jetpack-whatsapp' );
2848 }
2849
2850 /**
2851 * AMP display for Whatsapp.
2852 *
2853 * @param \WP_Post $post The current post being viewed.
2854 */
2855 public function get_amp_display( $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
2856 $attrs = array(
2857 'type' => 'whatsapp',
2858 );
2859
2860 return $this->build_amp_markup( $attrs );
2861 }
2862
2863 /**
2864 * Process sharing request. Add actions that need to happen when sharing here.
2865 *
2866 * @param WP_Post $post Post object.
2867 * @param array $post_data Array of information about the post we're sharing.
2868 *
2869 * @return void
2870 */
2871 public function process_request( $post, array $post_data ) {
2872 // Record stats
2873 parent::process_request( $post, $post_data );
2874
2875 // Firefox for desktop doesn't handle the "api.whatsapp.com" URL properly, so use "web.whatsapp.com"
2876 if ( User_Agent_Info::is_firefox_desktop() ) {
2877 $url = 'https://web.whatsapp.com/send?text=';
2878 } else {
2879 $url = 'https://api.whatsapp.com/send?text=';
2880 }
2881
2882 $url .= rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) );
2883
2884 parent::redirect_request( $url );
2885 }
2886 }
2887
2888 /**
2889 * Skype sharing service.
2890 */
2891 class Share_Skype extends Sharing_Source {
2892 /**
2893 * Service short name.
2894 *
2895 * @var string
2896 */
2897 public $shortname = 'skype';
2898
2899 /**
2900 * Service icon font code.
2901 *
2902 * @var string
2903 */
2904 public $icon = '\f220';
2905
2906 /**
2907 * Sharing type.
2908 *
2909 * @var string
2910 */
2911 private $share_type = 'default';
2912
2913 /**
2914 * Constructor.
2915 *
2916 * @param int $id Sharing source ID.
2917 * @param array $settings Sharing settings.
2918 */
2919 public function __construct( $id, array $settings ) {
2920 parent::__construct( $id, $settings );
2921
2922 if ( isset( $settings['share_type'] ) ) {
2923 $this->share_type = $settings['share_type'];
2924 }
2925
2926 if ( 'official' === $this->button_style ) {
2927 $this->smart = true;
2928 } else {
2929 $this->smart = false;
2930 }
2931
2932 }
2933
2934 /**
2935 * Service name.
2936 *
2937 * @return string
2938 */
2939 public function get_name() {
2940 return __( 'Skype', 'jetpack' );
2941 }
2942
2943 /**
2944 * Get the markup of the sharing button.
2945 *
2946 * @param WP_Post $post Post object.
2947 *
2948 * @return string
2949 */
2950 public function get_display( $post ) {
2951 if ( $this->smart ) {
2952 $skype_share_html = sprintf(
2953 '<div class="skype-share" data-href="%1$s" data-lang="%2$s" data-style="small" data-source="jetpack" ></div>',
2954 esc_attr( $this->get_share_url( $post->ID ) ),
2955 'en-US'
2956 );
2957 return $skype_share_html;
2958 }
2959
2960 /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
2961 if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'skype' ) ) {
2962 sharing_register_post_for_share_counts( $post->ID );
2963 }
2964 return $this->get_link(
2965 $this->get_process_request_url( $post->ID ),
2966 _x( 'Skype', 'share to', 'jetpack' ),
2967 __( 'Click to share on Skype', 'jetpack' ),
2968 'share=skype',
2969 'sharing-skype-' . $post->ID
2970 );
2971 }
2972
2973 /**
2974 * AMP display for Skype.
2975 *
2976 * @param \WP_Post $post The current post being viewed.
2977 */
2978 public function get_amp_display( $post ) {
2979 $attrs = array(
2980 'data-share-endpoint' => sprintf(
2981 'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
2982 rawurlencode( $this->get_share_url( $post->ID ) ),
2983 'en-US'
2984 ),
2985 );
2986
2987 return $this->build_amp_markup( $attrs );
2988 }
2989
2990 /**
2991 * Process sharing request. Add actions that need to happen when sharing here.
2992 *
2993 * @param WP_Post $post Post object.
2994 * @param array $post_data Array of information about the post we're sharing.
2995 *
2996 * @return void
2997 */
2998 public function process_request( $post, array $post_data ) {
2999 $skype_url = sprintf(
3000 'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
3001 rawurlencode( $this->get_share_url( $post->ID ) ),
3002 'en-US'
3003 );
3004
3005 // Record stats
3006 parent::process_request( $post, $post_data );
3007
3008 parent::redirect_request( $skype_url );
3009 }
3010
3011 /**
3012 * Add content specific to a service in the footer.
3013 */
3014 public function display_footer() {
3015 if ( $this->smart ) :
3016 ?>
3017 <script>
3018 (function(r, d, s) {
3019 r.loadSkypeWebSdkAsync = r.loadSkypeWebSdkAsync || function(p) {
3020 var js, sjs = d.getElementsByTagName(s)[0];
3021 if (d.getElementById(p.id)) { return; }
3022 js = d.createElement(s);
3023 js.id = p.id;
3024 js.src = p.scriptToLoad;
3025 js.onload = p.callback
3026 sjs.parentNode.insertBefore(js, sjs);
3027 };
3028 var p = {
3029 scriptToLoad: 'https://swx.cdn.skype.com/shared/v/latest/skypewebsdk.js',
3030 id: 'skype_web_sdk'
3031 };
3032 r.loadSkypeWebSdkAsync(p);
3033 })(window, document, 'script');
3034 </script>
3035 <?php
3036 else :
3037 $this->js_dialog(
3038 $this->shortname,
3039 array(
3040 'width' => 305,
3041 'height' => 665,
3042 )
3043 );
3044 endif;
3045 }
3046 }
3047