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-service.php

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

1,254 lines 32.6 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 * Utilities to register and interact with a sharing service.
4 *
5 * Sharing_Service gets info about a service.
6 * Sharing_Service_Total and Sharing_Post_Total get stats data.
7 *
8 * @package automattic/jetpack
9 *
10 * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
11 */
12
13 use Automattic\Jetpack\Assets;
14 use Automattic\Jetpack\Redirect;
15 use Automattic\Jetpack\Status;
16 use Automattic\Jetpack\Sync\Settings;
17
18 require_once __DIR__ . '/sharing-sources.php';
19
20 define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION );
21
22 /**
23 * Interact with a sharing service.
24 */
25 class Sharing_Service {
26 /**
27 * Should the service be available globally?
28 *
29 * @var bool
30 */
31 private $global = false;
32
33 /**
34 * Default sharing label.
35 *
36 * @var string
37 */
38 public $default_sharing_label = '';
39
40 /**
41 * Initialize the sharing service.
42 * Only run this method once upon module loading.
43 *
44 * @return void
45 */
46 public static function init() {
47 add_filter( 'the_content', 'sharing_display', 19 );
48 add_filter( 'the_excerpt', 'sharing_display', 19 );
49 }
50
51 /**
52 * Constructor.
53 */
54 public function __construct() {
55 $this->default_sharing_label = __( 'Share this:', 'jetpack' );
56 }
57
58 /**
59 * Gets a generic list of all services, without any config
60 *
61 * @return array
62 */
63 public function get_all_services_blog() {
64 $options = get_option( 'sharing-options' );
65 $all = $this->get_all_services();
66 $services = array();
67
68 foreach ( $all as $id => $name ) {
69 if ( isset( $all[ $id ] ) ) {
70 $config = array();
71
72 // Pre-load custom modules otherwise they won't know who they are
73 if ( substr( $id, 0, 7 ) === 'custom-' && is_array( $options[ $id ] ) ) {
74 $config = $options[ $id ];
75 }
76
77 $services[ $id ] = new $all[ $id ]( $id, $config );
78 }
79 }
80
81 return $services;
82 }
83
84 /**
85 * Gets a list of all available service names and classes
86 *
87 * @param bool $include_custom Include custom sharing services.
88 *
89 * @return array
90 */
91 public function get_all_services( $include_custom = true ) {
92 // Default services
93 // if you update this list, please update the REST API tests
94 // in bin/tests/api/suites/SharingTest.php
95 $services = array(
96 'print' => 'Share_Print',
97 'email' => 'Share_Email',
98 'facebook' => 'Share_Facebook',
99 'linkedin' => 'Share_LinkedIn',
100 'reddit' => 'Share_Reddit',
101 'twitter' => 'Share_Twitter',
102 'tumblr' => 'Share_Tumblr',
103 'pinterest' => 'Share_Pinterest',
104 'pocket' => 'Share_Pocket',
105 'telegram' => 'Share_Telegram',
106 'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp',
107 'skype' => 'Share_Skype',
108 );
109
110 if ( is_multisite() && is_plugin_active( 'press-this/press-this-plugin.php' ) ) {
111 $services['press-this'] = 'Share_PressThis';
112 }
113
114 if ( $include_custom ) {
115 // Add any custom services in
116 $options = $this->get_global_options();
117 foreach ( (array) $options['custom'] as $custom_id ) {
118 $services[ $custom_id ] = 'Share_Custom';
119 }
120 }
121
122 /**
123 * Filters the list of available Sharing Services.
124 *
125 * @module sharedaddy
126 *
127 * @since 1.1.0
128 *
129 * @param array $services Array of all available Sharing Services.
130 */
131 return apply_filters( 'sharing_services', $services );
132 }
133
134 /**
135 * Save a new custom sharing service.
136 *
137 * @param string $label Service name.
138 * @param string $url Service sharing URL.
139 * @param string $icon Service icon.
140 *
141 * @return bool|Share_Custom
142 */
143 public function new_service( $label, $url, $icon ) {
144 // Validate.
145 $label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
146 $url = trim( esc_url_raw( $url ) );
147 $icon = trim( esc_url_raw( $icon ) );
148
149 if ( $label && $url && $icon ) {
150 $options = get_option( 'sharing-options' );
151 if ( ! is_array( $options ) ) {
152 $options = array();
153 }
154
155 $service_id = 'custom-' . time();
156
157 // Add a new custom service
158 $options['global']['custom'][] = $service_id;
159 if ( false !== $this->global ) {
160 $this->global['custom'][] = $service_id;
161 }
162
163 update_option( 'sharing-options', $options );
164
165 // Create a custom service and set the options for it
166 $service = new Share_Custom(
167 $service_id,
168 array(
169 'name' => $label,
170 'url' => $url,
171 'icon' => $icon,
172 )
173 );
174 $this->set_service( $service_id, $service );
175
176 // Return the service
177 return $service;
178 }
179
180 return false;
181 }
182
183 /**
184 * Delete a sharing service.
185 *
186 * @param string $service_id Service ID.
187 *
188 * @return bool
189 */
190 public function delete_service( $service_id ) {
191 $options = get_option( 'sharing-options' );
192 if ( isset( $options[ $service_id ] ) ) {
193 unset( $options[ $service_id ] );
194 }
195
196 $key = array_search( $service_id, $options['global']['custom'], true );
197 if ( $key !== false ) {
198 unset( $options['global']['custom'][ $key ] );
199 }
200
201 update_option( 'sharing-options', $options );
202 return true;
203 }
204
205 /**
206 * Save enabled sharing services.
207 *
208 * @param array $visible Visible sharing services.
209 * @param array $hidden Hidden sharing services (available under a dropdown).
210 *
211 * @return bool
212 */
213 public function set_blog_services( array $visible, array $hidden ) {
214 $services = $this->get_all_services();
215 // Validate the services
216 $available = array_keys( $services );
217
218 // Only allow services that we have defined
219 $hidden = array_intersect( $hidden, $available );
220 $visible = array_intersect( $visible, $available );
221
222 // Ensure we don't have the same ones in hidden and visible
223 $hidden = array_diff( $hidden, $visible );
224
225 /**
226 * Control the state of the list of sharing services.
227 *
228 * @module sharedaddy
229 *
230 * @since 1.1.0
231 *
232 * @param array $args {
233 * Array of options describing the state of the sharing services.
234 *
235 * @type array $services List of all available service names and classes.
236 * @type array $available Validated list of all available service names and classes.
237 * @type array $hidden List of services hidden behind a "More" button.
238 * @type array $visible List of visible services.
239 * @type array $this->get_blog_services() Array of Sharing Services currently enabled.
240 * }
241 */
242 do_action(
243 'sharing_get_services_state',
244 array(
245 'services' => $services,
246 'available' => $available,
247 'hidden' => $hidden,
248 'visible' => $visible,
249 'currently_enabled' => $this->get_blog_services(),
250 )
251 );
252
253 return update_option(
254 'sharing-services',
255 array(
256 'visible' => $visible,
257 'hidden' => $hidden,
258 )
259 );
260 }
261
262 /**
263 * Get information about enabled sharing services on the site.
264 *
265 * @return array
266 */
267 public function get_blog_services() {
268 $options = get_option( 'sharing-options' );
269 $enabled = get_option( 'sharing-services' );
270 $services = $this->get_all_services();
271
272 /**
273 * Check if options exist and are well formatted.
274 * This avoids issues on sites with corrupted options.
275 *
276 * @see https://github.com/Automattic/jetpack/issues/6121
277 */
278 if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) {
279 $global_options = array( 'global' => $this->get_global_options() );
280 $options = is_array( $options )
281 ? array_merge( $options, $global_options )
282 : $global_options;
283 }
284
285 $global = $options['global'];
286
287 // Default services
288 if ( ! is_array( $enabled ) ) {
289 $enabled = array(
290 'visible' => array(
291 'twitter',
292 'facebook',
293 ),
294 'hidden' => array(),
295 );
296
297 /**
298 * Filters the list of default Sharing Services.
299 *
300 * @module sharedaddy
301 *
302 * @since 1.1.0
303 *
304 * @param array $enabled Array of default Sharing Services.
305 */
306 $enabled = apply_filters( 'sharing_default_services', $enabled );
307 }
308
309 // Cleanup after any filters that may have produced duplicate services
310 if ( is_array( $enabled['visible'] ) ) {
311 $enabled['visible'] = array_unique( $enabled['visible'] );
312 } else {
313 $enabled['visible'] = array();
314 }
315
316 if ( is_array( $enabled['hidden'] ) ) {
317 $enabled['hidden'] = array_unique( $enabled['hidden'] );
318 } else {
319 $enabled['hidden'] = array();
320 }
321
322 // Form the enabled services
323 $blog = array(
324 'visible' => array(),
325 'hidden' => array(),
326 );
327
328 foreach ( $blog as $area => $stuff ) {
329 foreach ( (array) $enabled[ $area ] as $service ) {
330 if ( isset( $services[ $service ] ) ) {
331 if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) {
332 $options[ $service ] = array();
333 }
334 $blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) );
335 }
336 }
337 }
338
339 /**
340 * Filters the list of enabled Sharing Services.
341 *
342 * @module sharedaddy
343 *
344 * @since 1.1.0
345 *
346 * @param array $blog Array of enabled Sharing Services.
347 */
348 $blog = apply_filters( 'sharing_services_enabled', $blog );
349
350 // Add CSS for NASCAR
351 if ( count( $blog['visible'] ) || count( $blog['hidden'] ) ) {
352 add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
353 }
354
355 // Convenience for checking if a service is present
356 $blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
357 return $blog;
358 }
359
360 /**
361 * Get information about a specific enabled sharing service.
362 *
363 * @param string $service_name Service name.
364 *
365 * @return bool|Sharing_Source
366 */
367 public function get_service( $service_name ) {
368 $services = $this->get_blog_services();
369
370 if ( isset( $services['visible'][ $service_name ] ) ) {
371 return $services['visible'][ $service_name ];
372 }
373
374 if ( isset( $services['hidden'][ $service_name ] ) ) {
375 return $services['hidden'][ $service_name ];
376 }
377
378 return false;
379 }
380
381 /**
382 * Update global sharing options.
383 *
384 * @param array $data Array of new sharing options to save.
385 */
386 public function set_global_options( $data ) {
387 $options = get_option( 'sharing-options' );
388
389 // No options yet.
390 if ( ! is_array( $options ) ) {
391 $options = array();
392 }
393
394 // Defaults.
395 $options['global'] = array(
396 'button_style' => 'icon-text',
397 'sharing_label' => $this->default_sharing_label,
398 'open_links' => 'same',
399 'show' => ! isset( $options['global'] ) ? array( 'post', 'page' ) : array(),
400 'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array(),
401 );
402
403 /**
404 * Filters global sharing settings.
405 *
406 * @module sharedaddy
407 *
408 * @since 1.1.0
409 *
410 * @param array $options['global'] Array of global sharing settings.
411 */
412 $options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
413
414 // Validate options and set from our data
415 if (
416 isset( $data['button_style'] )
417 && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ), true )
418 ) {
419 $options['global']['button_style'] = $data['button_style'];
420 }
421
422 if ( isset( $data['sharing_label'] ) ) {
423 if ( $this->default_sharing_label === $data['sharing_label'] ) {
424 $options['global']['sharing_label'] = false;
425 } else {
426 $options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
427 }
428 }
429
430 if (
431 isset( $data['open_links'] )
432 && in_array( $data['open_links'], array( 'new', 'same' ), true )
433 ) {
434 $options['global']['open_links'] = $data['open_links'];
435 }
436
437 $shows = array_values( get_post_types( array( 'public' => true ) ) );
438 $shows[] = 'index';
439 if ( isset( $data['show'] ) ) {
440 if ( is_scalar( $data['show'] ) ) {
441 switch ( $data['show'] ) {
442 case 'posts':
443 $data['show'] = array( 'post', 'page' );
444 break;
445 case 'index':
446 $data['show'] = array( 'index' );
447 break;
448 case 'posts-index':
449 $data['show'] = array( 'post', 'page', 'index' );
450 break;
451 }
452 }
453
454 $data['show'] = array_intersect( $data['show'], $shows );
455 if ( $data['show'] ) {
456 $options['global']['show'] = $data['show'];
457 }
458 }
459
460 update_option( 'sharing-options', $options );
461 return $options['global'];
462 }
463
464 /**
465 * Get global sharing options for the site.
466 *
467 * @return array
468 */
469 public function get_global_options() {
470 if ( $this->global === false ) {
471 $options = get_option( 'sharing-options' );
472
473 if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) {
474 $this->global = $options['global'];
475 } else {
476 $this->global = $this->set_global_options( $options );
477 }
478 }
479
480 if ( ! isset( $this->global['show'] ) ) {
481 $this->global['show'] = array( 'post', 'page' );
482 } elseif ( is_scalar( $this->global['show'] ) ) {
483 switch ( $this->global['show'] ) {
484 case 'posts':
485 $this->global['show'] = array( 'post', 'page' );
486 break;
487 case 'index':
488 $this->global['show'] = array( 'index' );
489 break;
490 case 'posts-index':
491 $this->global['show'] = array( 'post', 'page', 'index' );
492 break;
493 }
494 }
495
496 if ( false === $this->global['sharing_label'] ) {
497 $this->global['sharing_label'] = $this->default_sharing_label;
498 }
499
500 return $this->global;
501 }
502
503 /**
504 * Save a sharing service for use.
505 *
506 * @param int $id Sharing unique ID.
507 * @param Sharing_Source $service Sharing service.
508 *
509 * @return void
510 */
511 public function set_service( $id, Sharing_Source $service ) {
512 // Update the options for this service
513 $options = get_option( 'sharing-options' );
514
515 // No options yet
516 if ( ! is_array( $options ) ) {
517 $options = array();
518 }
519
520 /**
521 * Get the state of a sharing button.
522 *
523 * @module sharedaddy
524 *
525 * @since 1.1.0
526 *
527 * @param array $args {
528 * State of a sharing button.
529 *
530 * @type string $id Service ID.
531 * @type array $options Array of all sharing options.
532 * @type array $service Details about a service.
533 * }
534 */
535 do_action(
536 'sharing_get_button_state',
537 array(
538 'id' => $id,
539 'options' => $options,
540 'service' => $service,
541 )
542 );
543
544 $options[ $id ] = $service->get_options();
545
546 update_option( 'sharing-options', array_filter( $options ) );
547 }
548
549 /**
550 * Get stats for a site, a post, or a sharing service.
551 * Soon to come to a .org plugin near you!
552 *
553 * @param string|bool $service_name Service name.
554 * @param int|bool $post_id Post ID.
555 * @param int|bool $_blog_id Blog ID.
556 *
557 * @return int
558 */
559 public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
560 global $wpdb, $blog_id;
561 if ( ! $_blog_id ) {
562 $_blog_id = $blog_id;
563 }
564 if ( $service_name === false ) {
565 if ( $post_id > 0 ) {
566 // total number of shares for this post
567 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d', $_blog_id, $post_id ) );
568 } else {
569 // total number of shares for this blog
570 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d', $_blog_id ) );
571 }
572 }
573
574 if ( $post_id > 0 ) {
575 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s', $_blog_id, $post_id, $service_name ) );
576 } else {
577 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $_blog_id, $service_name ) );
578 }
579 }
580
581 /**
582 * Get total stats for a site, for all sharing services.
583 *
584 * @param int|bool $post_id Post ID.
585 *
586 * @return array
587 */
588 public function get_services_total( $post_id = false ) {
589 $totals = array();
590 $services = $this->get_blog_services();
591
592 if ( ! empty( $services ) && isset( $services['all'] ) ) {
593 foreach ( $services['all'] as $key => $value ) {
594 $totals[ $key ] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) );
595 }
596 }
597 usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
598
599 return $totals;
600 }
601
602 /**
603 * Get sharing stats for all posts on the site.
604 *
605 * @return array
606 */
607 public function get_posts_total() {
608 $totals = array();
609 global $wpdb, $blog_id;
610
611 $my_data = $wpdb->get_results( $wpdb->prepare( 'SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d GROUP BY post_id ORDER BY count DESC ', $blog_id ) );
612
613 if ( ! empty( $my_data ) ) {
614 foreach ( $my_data as $row ) {
615 $totals[] = new Sharing_Post_Total( $row->id, $row->total );
616 }
617 }
618
619 usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
620
621 return $totals;
622 }
623 }
624
625 /**
626 * Get stats for a specific sharing service.
627 */
628 class Sharing_Service_Total {
629 /**
630 * Sharing service ID.
631 *
632 * @var int
633 */
634 public $id = '';
635
636 /**
637 * Service name.
638 *
639 * @var string
640 */
641 public $name = '';
642
643 /**
644 * Sharing service name.
645 *
646 * @var string
647 */
648 public $service = '';
649
650 /**
651 * Total number of shares for this service.
652 *
653 * @var string
654 */
655 public $total = 0;
656
657 /**
658 * Constructor.
659 *
660 * @param int $id Service ID.
661 * @param int $total Total shares.
662 */
663 public function __construct( $id, $total ) {
664 $services = new Sharing_Service();
665 $this->id = esc_html( $id );
666 $this->service = $services->get_service( $id );
667 $this->total = (int) $total;
668
669 $this->name = $this->service->get_name();
670 }
671
672 /**
673 * Compare total shares between 2 posts.
674 *
675 * @param object $a Sharing_Service_Total object.
676 * @param object $b Sharing_Service_Total object.
677 *
678 * @return bool
679 */
680 public static function cmp( $a, $b ) {
681 if ( $a->total === $b->total ) {
682 return $a->name < $b->name;
683 }
684 return $a->total < $b->total;
685 }
686 }
687
688 /**
689 * Get sharing stats for a specific post.
690 */
691 class Sharing_Post_Total {
692 /**
693 * Sharing service ID.
694 *
695 * @var int
696 */
697 public $id = 0;
698
699 /**
700 * Total shares.
701 *
702 * @var int
703 */
704 public $total = 0;
705
706 /**
707 * Post title.
708 *
709 * @var string
710 */
711 public $title = '';
712
713 /**
714 * Post permalink.
715 *
716 * @var string
717 */
718 public $url = '';
719
720 /**
721 * Constructor.
722 *
723 * @param int $id Service ID.
724 * @param int $total Total shares.
725 */
726 public function __construct( $id, $total ) {
727 $this->id = (int) $id;
728 $this->total = (int) $total;
729 $this->title = get_the_title( $this->id );
730 $this->url = get_permalink( $this->id );
731 }
732
733 /**
734 * Compare total shares between 2 posts.
735 *
736 * @param object $a Sharing_Post_Total object.
737 * @param object $b Sharing_Post_Total object.
738 *
739 * @return bool
740 */
741 public static function cmp( $a, $b ) {
742 if ( $a->total === $b->total ) {
743 return $a->id < $b->id;
744 }
745 return $a->total < $b->total;
746 }
747 }
748
749 /**
750 * Populate sharing counts global with a post we want to count shares for.
751 *
752 * @param int $post_id Post ID.
753 *
754 * @return void
755 */
756 function sharing_register_post_for_share_counts( $post_id ) {
757 global $jetpack_sharing_counts;
758
759 if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) ) {
760 $jetpack_sharing_counts = array();
761 }
762
763 $jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
764 }
765
766 /**
767 * Determine whether we should load sharing scripts or not.
768 *
769 * @return bool
770 */
771 function sharing_maybe_enqueue_scripts() {
772 $sharer = new Sharing_Service();
773 $global_options = $sharer->get_global_options();
774
775 $enqueue = false;
776 if ( is_singular() && in_array( get_post_type(), $global_options['show'], true ) ) {
777 $enqueue = true;
778 } elseif (
779 in_array( 'index', $global_options['show'], true )
780 && (
781 is_home()
782 || is_front_page()
783 || is_archive()
784 || is_search()
785 || in_array( get_post_type(), $global_options['show'], true )
786 )
787 ) {
788 $enqueue = true;
789 }
790
791 /**
792 * Filter to decide when sharing scripts should be enqueued.
793 *
794 * @module sharedaddy
795 *
796 * @since 3.2.0
797 *
798 * @param bool $enqueue Decide if the sharing scripts should be enqueued.
799 */
800 return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
801 }
802
803 /**
804 * Add sharing JavaScript to the footer of a page.
805 *
806 * @return void
807 */
808 function sharing_add_footer() {
809 if (
810 class_exists( 'Jetpack_AMP_Support' )
811 && Jetpack_AMP_Support::is_amp_request()
812 ) {
813 return;
814 }
815
816 global $jetpack_sharing_counts;
817
818 if (
819 /**
820 * Filter all JavaScript output by the sharing module.
821 *
822 * @module sharedaddy
823 *
824 * @since 1.1.0
825 *
826 * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
827 */
828 apply_filters( 'sharing_js', true )
829 && sharing_maybe_enqueue_scripts()
830 ) {
831 if (
832 /**
833 * Filter the display of sharing counts next to the sharing buttons.
834 *
835 * @module sharedaddy
836 *
837 * @since 3.2.0
838 *
839 * @param bool true Control the display of counters next to the sharing buttons. Default to true.
840 */
841 apply_filters( 'jetpack_sharing_counts', true )
842 && is_array( $jetpack_sharing_counts )
843 && count( $jetpack_sharing_counts )
844 ) :
845 $sharing_post_urls = array_filter( $jetpack_sharing_counts );
846 if ( $sharing_post_urls ) :
847 ?>
848
849 <script type="text/javascript">
850 window.WPCOM_sharing_counts = <?php echo wp_json_encode( array_flip( $sharing_post_urls ) ); ?>;
851 </script>
852 <?php
853 endif;
854 endif;
855
856 wp_enqueue_script( 'sharing-js' );
857 $sharing_js_options = array(
858 'lang' => get_base_recaptcha_lang_code(),
859 /** This filter is documented in modules/sharedaddy/sharing-service.php */
860 'counts' => apply_filters( 'jetpack_sharing_counts', true ),
861 'is_stats_active' => Jetpack::is_module_active( 'stats' ),
862 );
863 wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options );
864 }
865 $sharer = new Sharing_Service();
866 $enabled = $sharer->get_blog_services();
867 foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) {
868 $service->display_footer();
869 }
870 }
871
872 /**
873 * Enqueue sharing CSS in head.
874 *
875 * @return void
876 */
877 function sharing_add_header() {
878 $sharer = new Sharing_Service();
879 $enabled = $sharer->get_blog_services();
880
881 foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) {
882 $service->display_header();
883 }
884
885 if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
886 wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) . 'sharing.css', array(), JETPACK__VERSION );
887 wp_enqueue_style( 'social-logos' );
888 }
889
890 }
891 add_action( 'wp_head', 'sharing_add_header', 1 );
892
893 /**
894 * Launch sharing requests on page load when a specific query string is used.
895 *
896 * @return void
897 */
898 function sharing_process_requests() {
899 global $post;
900
901 // Only process if: single post and share=X defined
902 if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
903 $sharer = new Sharing_Service();
904
905 $service = $sharer->get_service( sanitize_text_field( wp_unslash( $_GET['share'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
906 if ( $service ) {
907 $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
908 }
909 }
910 }
911 add_action( 'template_redirect', 'sharing_process_requests', 9 );
912
913 /**
914 * Gets the url to customise the sharing buttons in Calypso.
915 *
916 * @return string the customisation URL or null if it couldn't be determinde.
917 */
918 function get_sharing_buttons_customisation_url() {
919 return Redirect::get_url( 'calypso-marketing-sharing-buttons', array( 'site' => ( new Status() )->get_site_suffix() ) );
920 }
921
922 /**
923 * Append sharing links to text.
924 *
925 * @param string $text The original text to append sharing links onto.
926 * @param bool $echo Where to echo the text or return.
927 *
928 * @return string The original $text with, if conditions are met, the sharing links.
929 */
930 function sharing_display( $text = '', $echo = false ) {
931 global $post, $wp_current_filter;
932
933 if ( Settings::is_syncing() ) {
934 return $text;
935 }
936
937 if ( empty( $post ) ) {
938 return $text;
939 }
940
941 if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
942 return $text;
943 }
944
945 // Prevent from rendering sharing buttons in block which is fetched from REST endpoint by editor
946 if ( defined( 'REST_REQUEST' ) && REST_REQUEST &&
947 isset( $_GET['context'] ) && 'edit' === $_GET['context'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
948 return $text;
949 }
950
951 // Don't output flair on excerpts.
952 if ( in_array( 'get_the_excerpt', (array) $wp_current_filter, true ) ) {
953 return $text;
954 }
955
956 // Ensure we don't display sharing buttons on post excerpts that are hooked inside the post content
957 if ( in_array( 'the_excerpt', (array) $wp_current_filter, true ) &&
958 in_array( 'the_content', (array) $wp_current_filter, true ) ) {
959 return $text;
960 }
961
962 // Don't allow flair to be added to the_content more than once (prevent infinite loops).
963 $done = false;
964 foreach ( $wp_current_filter as $filter ) {
965 if ( 'the_content' === $filter ) {
966 if ( $done ) {
967 return $text;
968 } else {
969 $done = true;
970 }
971 }
972 }
973
974 // check whether we are viewing the front page and whether the front page option is checked.
975 $options = get_option( 'sharing-options' );
976 $display_options = null;
977
978 if ( is_array( $options ) ) {
979 $display_options = $options['global']['show'];
980 }
981
982 if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options, true ) ) ) {
983 return $text;
984 }
985
986 if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter, true ) ) {
987 // Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
988 // We only want to output the sharing buttons once. Let's stick with the_content().
989 return $text;
990 }
991
992 $sharer = new Sharing_Service();
993 $global = $sharer->get_global_options();
994
995 $show = false;
996 if ( ! is_feed() ) {
997 if ( is_singular() && in_array( get_post_type(), $global['show'], true ) ) {
998 $show = true;
999 } elseif ( in_array( 'index', $global['show'], true ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'], true ) ) ) {
1000 $show = true;
1001 }
1002 }
1003
1004 /**
1005 * Filter to decide if sharing buttons should be displayed.
1006 *
1007 * @module sharedaddy
1008 *
1009 * @since 1.1.0
1010 *
1011 * @param bool $show Should the sharing buttons be displayed.
1012 * @param WP_Post $post The post to share.
1013 */
1014 $show = apply_filters( 'sharing_show', $show, $post );
1015
1016 // Disabled for this post?
1017 $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
1018
1019 if ( ! empty( $switched_status ) ) {
1020 $show = false;
1021 }
1022
1023 // Is the post private?
1024 $post_status = get_post_status( $post->ID );
1025
1026 if ( 'private' === $post_status ) {
1027 $show = false;
1028 }
1029
1030 // Hide on password protected posts unless password is provided.
1031 if ( post_password_required( $post->ID ) ) {
1032 $show = false;
1033 }
1034
1035 /**
1036 * Filter the Sharing buttons' Ajax action name Jetpack checks for.
1037 * This allows the use of the buttons with your own Ajax implementation.
1038 *
1039 * @module sharedaddy
1040 *
1041 * @since 7.3.0
1042 *
1043 * @param string $sharing_ajax_action_name Name of the Sharing buttons' Ajax action.
1044 */
1045 $ajax_action = apply_filters( 'sharing_ajax_action', 'get_latest_posts' );
1046
1047 // Allow to be used in ajax requests for latest posts.
1048 if (
1049 defined( 'DOING_AJAX' )
1050 && DOING_AJAX
1051 && isset( $_REQUEST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce handling happens within each custom implementation.
1052 && $ajax_action === $_REQUEST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce handling happens within each custom implementation.
1053 ) {
1054 $show = true;
1055 }
1056
1057 $sharing_content = '';
1058 $enabled = false;
1059
1060 if ( $show ) {
1061 /**
1062 * Filters the list of enabled Sharing Services.
1063 *
1064 * @module sharedaddy
1065 *
1066 * @since 2.2.3
1067 *
1068 * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
1069 */
1070 $enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
1071
1072 if ( count( $enabled['all'] ) > 0 ) {
1073 $dir = get_option( 'text_direction' );
1074
1075 // Wrapper.
1076 $sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
1077 if ( '' !== $global['sharing_label'] ) {
1078 $sharing_content .= sprintf(
1079 /**
1080 * Filter the sharing buttons' headline structure.
1081 *
1082 * @module sharedaddy
1083 *
1084 * @since 4.4.0
1085 *
1086 * @param string $sharing_headline Sharing headline structure.
1087 * @param string $global['sharing_label'] Sharing title.
1088 * @param string $sharing Module name.
1089 */
1090 apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
1091 esc_html( $global['sharing_label'] )
1092 );
1093 }
1094 $sharing_content .= '<div class="sd-content"><ul>';
1095
1096 // Visible items.
1097 $visible = '';
1098 foreach ( $enabled['visible'] as $service ) {
1099 $klasses = array( 'share-' . $service->get_class() );
1100 if ( $service->is_deprecated() ) {
1101 if ( ! current_user_can( 'manage_options' ) ) {
1102 continue;
1103 }
1104 $klasses[] = 'share-deprecated';
1105 }
1106 // Individual HTML for sharing service.
1107 $visible .= '<li class="' . implode( ' ', $klasses ) . '">' . $service->get_display( $post ) . '</li>';
1108 }
1109
1110 $parts = array();
1111 $parts[] = $visible;
1112 if ( count( $enabled['hidden'] ) > 0 ) {
1113 if ( count( $enabled['visible'] ) > 0 ) {
1114 $expand = __( 'More', 'jetpack' );
1115 } else {
1116 $expand = __( 'Share', 'jetpack' );
1117 }
1118 $parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>' . $expand . '</span></a></li>';
1119 }
1120
1121 if ( 'rtl' === $dir ) {
1122 $parts = array_reverse( $parts );
1123 }
1124
1125 $sharing_content .= implode( '', $parts );
1126 $sharing_content .= '<li class="share-end"></li></ul>';
1127
1128 // Link to customization options if user can manage them.
1129 if ( current_user_can( 'manage_options' ) ) {
1130 $link_url = get_sharing_buttons_customisation_url();
1131 if ( ! empty( $link_url ) ) {
1132 $link_text = __( 'Customize buttons', 'jetpack' );
1133 $sharing_content .= '<p class="share-customize-link"><a href="' . esc_url( $link_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $link_text ) . '</a></p>';
1134 }
1135 }
1136
1137 if ( count( $enabled['hidden'] ) > 0 ) {
1138 $sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
1139
1140 if ( count( $enabled['hidden'] ) === 1 ) {
1141 $sharing_content .= 'width:150px;';
1142 }
1143
1144 $sharing_content .= '">';
1145
1146 if ( count( $enabled['hidden'] ) === 1 ) {
1147 $sharing_content .= '<ul style="background-image:none;">';
1148 } else {
1149 $sharing_content .= '<ul>';
1150 }
1151
1152 $count = 1;
1153 foreach ( $enabled['hidden'] as $service ) {
1154 // Individual HTML for sharing service.
1155 $klasses = array( 'share-' . $service->get_class() );
1156 if ( $service->is_deprecated() ) {
1157 if ( ! current_user_can( 'manage_options' ) ) {
1158 continue;
1159 }
1160 $klasses[] = 'share-deprecated';
1161 }
1162 $sharing_content .= '<li class="' . implode( ' ', $klasses ) . '">';
1163 $sharing_content .= $service->get_display( $post );
1164 $sharing_content .= '</li>';
1165
1166 if ( ( $count % 2 ) === 0 ) {
1167 $sharing_content .= '<li class="share-end"></li>';
1168 }
1169
1170 $count ++;
1171 }
1172
1173 // End of wrapper.
1174 $sharing_content .= '<li class="share-end"></li></ul></div></div>';
1175 }
1176
1177 $sharing_content .= '</div></div></div>';
1178
1179 // Register our JS.
1180 if ( defined( 'JETPACK__VERSION' ) ) {
1181 $ver = JETPACK__VERSION;
1182 } else {
1183 $ver = '20211226';
1184 }
1185
1186 // @todo: Investigate if we can load this JS in the footer instead.
1187 wp_register_script(
1188 'sharing-js',
1189 Assets::get_file_url_for_environment(
1190 '_inc/build/sharedaddy/sharing.min.js',
1191 'modules/sharedaddy/sharing.js'
1192 ),
1193 array(),
1194 $ver,
1195 false
1196 );
1197
1198 // Enqueue scripts for the footer.
1199 add_action( 'wp_footer', 'sharing_add_footer' );
1200 }
1201 }
1202
1203 /**
1204 * Filters the content markup of the Jetpack sharing links
1205 *
1206 * @module sharedaddy
1207 *
1208 * @since 3.8.0
1209 * @since 6.2.0 Started sending $enabled as a second parameter.
1210 *
1211 * @param string $sharing_content Content markup of the Jetpack sharing links
1212 * @param array $enabled Array of Sharing Services currently enabled.
1213 */
1214 $sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content, $enabled );
1215
1216 if ( $echo ) {
1217 echo $text . $sharing_markup; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1218 } else {
1219 return $text . $sharing_markup;
1220 }
1221 }
1222
1223 /**
1224 * Get reCAPTCHA language code based off the language code of the site.
1225 *
1226 * @return string
1227 */
1228 function get_base_recaptcha_lang_code() {
1229 $base_recaptcha_lang_code_mapping = array(
1230 'en' => 'en',
1231 'nl' => 'nl',
1232 'fr' => 'fr',
1233 'fr-be' => 'fr',
1234 'fr-ca' => 'fr',
1235 'fr-ch' => 'fr',
1236 'de' => 'de',
1237 'pt' => 'pt',
1238 'pt-br' => 'pt',
1239 'ru' => 'ru',
1240 'es' => 'es',
1241 'tr' => 'tr',
1242 );
1243
1244 $blog_lang_code = get_bloginfo( 'language' );
1245 if ( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) ) {
1246 return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
1247 }
1248
1249 // if no base mapping is found return default 'en'
1250 return 'en';
1251 }
1252
1253 Sharing_Service::init();
1254