PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.4.3
Jetpack – WP Security, Backup, Speed, & Growth v7.4.3
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 14.3.1 All 501 releases
jetpack / modules / sharedaddy / sharing-service.php

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

959 lines 26.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include_once dirname( __FILE__ ) . '/sharing-sources.php';
4
5 define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION );
6
7 class Sharing_Service {
8 private $global = false;
9 public $default_sharing_label = '';
10
11 public function __construct() {
12 $this->default_sharing_label = __( 'Share this:', 'jetpack' );
13 }
14
15 /**
16 * Gets a generic list of all services, without any config
17 */
18 public function get_all_services_blog() {
19 $options = get_option( 'sharing-options' );
20
21 $all = $this->get_all_services();
22 $services = array();
23
24 foreach ( $all as $id => $name ) {
25 if ( isset( $all[ $id ] ) ) {
26 $config = array();
27
28 // Pre-load custom modules otherwise they won't know who they are
29 if ( substr( $id, 0, 7 ) == 'custom-' && is_array( $options[ $id ] ) ) {
30 $config = $options[ $id ];
31 }
32
33 $services[ $id ] = new $all[ $id ]( $id, $config );
34 }
35 }
36
37 return $services;
38 }
39
40 /**
41 * Gets a list of all available service names and classes
42 */
43 public function get_all_services( $include_custom = true ) {
44 // Default services
45 // if you update this list, please update the REST API tests
46 // in bin/tests/api/suites/SharingTest.php
47 $services = array(
48 'print' => 'Share_Print',
49 'facebook' => 'Share_Facebook',
50 'linkedin' => 'Share_LinkedIn',
51 'reddit' => 'Share_Reddit',
52 'twitter' => 'Share_Twitter',
53 'tumblr' => 'Share_Tumblr',
54 'pinterest' => 'Share_Pinterest',
55 'pocket' => 'Share_Pocket',
56 'telegram' => 'Share_Telegram',
57 'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp',
58 'skype' => 'Share_Skype',
59 );
60
61 /**
62 * Filters if Email Sharing is enabled.
63 *
64 * E-Mail sharing is often problematic due to spam concerns, so this filter enables it to be quickly and simply toggled.
65 * @module sharedaddy
66 *
67 * @since 5.1.0
68 *
69 * @param bool $email Is e-mail sharing enabled? Default false if Akismet is not active or true if Akismet is active.
70 */
71 if ( apply_filters( 'sharing_services_email', Jetpack::is_akismet_active() ) ) {
72 $services['email'] = 'Share_Email';
73 }
74
75 if ( is_multisite() && is_plugin_active( 'press-this/press-this-plugin.php' ) ) {
76 $services['press-this'] = 'Share_PressThis';
77 }
78
79 if ( $include_custom ) {
80 // Add any custom services in
81 $options = $this->get_global_options();
82 foreach ( (array) $options['custom'] as $custom_id ) {
83 $services[ $custom_id ] = 'Share_Custom';
84 }
85 }
86
87 /**
88 * Filters the list of available Sharing Services.
89 *
90 * @module sharedaddy
91 *
92 * @since 1.1.0
93 *
94 * @param array $services Array of all available Sharing Services.
95 */
96 return apply_filters( 'sharing_services', $services );
97 }
98
99 public function new_service( $label, $url, $icon ) {
100 // Validate
101 $label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
102 $url = trim( esc_url_raw( $url ) );
103 $icon = trim( esc_url_raw( $icon ) );
104
105 if ( $label && $url && $icon ) {
106 $options = get_option( 'sharing-options' );
107 if ( ! is_array( $options ) ) {
108 $options = array();
109 }
110
111 $service_id = 'custom-' . time();
112
113 // Add a new custom service
114 $options['global']['custom'][] = $service_id;
115 if ( false !== $this->global ) {
116 $this->global['custom'][] = $service_id;
117 }
118
119 update_option( 'sharing-options', $options );
120
121 // Create a custom service and set the options for it
122 $service = new Share_Custom(
123 $service_id, array(
124 'name' => $label,
125 'url' => $url,
126 'icon' => $icon,
127 )
128 );
129 $this->set_service( $service_id, $service );
130
131 // Return the service
132 return $service;
133 }
134
135 return false;
136 }
137
138 public function delete_service( $service_id ) {
139 $options = get_option( 'sharing-options' );
140 if ( isset( $options[ $service_id ] ) ) {
141 unset( $options[ $service_id ] );
142 }
143
144 $key = array_search( $service_id, $options['global']['custom'] );
145 if ( $key !== false ) {
146 unset( $options['global']['custom'][ $key ] );
147 }
148
149 update_option( 'sharing-options', $options );
150 return true;
151 }
152
153 public function set_blog_services( array $visible, array $hidden ) {
154 $services = $this->get_all_services();
155 // Validate the services
156 $available = array_keys( $services );
157
158 // Only allow services that we have defined
159 $hidden = array_intersect( $hidden, $available );
160 $visible = array_intersect( $visible, $available );
161
162 // Ensure we don't have the same ones in hidden and visible
163 $hidden = array_diff( $hidden, $visible );
164
165 /**
166 * Control the state of the list of sharing services.
167 *
168 * @module sharedaddy
169 *
170 * @since 1.1.0
171 *
172 * @param array $args {
173 * Array of options describing the state of the sharing services.
174 *
175 * @type array $services List of all available service names and classes.
176 * @type array $available Validated list of all available service names and classes.
177 * @type array $hidden List of services hidden behind a "More" button.
178 * @type array $visible List of visible services.
179 * @type array $this->get_blog_services() Array of Sharing Services currently enabled.
180 * }
181 */
182 do_action(
183 'sharing_get_services_state', array(
184 'services' => $services,
185 'available' => $available,
186 'hidden' => $hidden,
187 'visible' => $visible,
188 'currently_enabled' => $this->get_blog_services(),
189 )
190 );
191
192 return update_option(
193 'sharing-services', array(
194 'visible' => $visible,
195 'hidden' => $hidden,
196 )
197 );
198 }
199
200 public function get_blog_services() {
201 $options = get_option( 'sharing-options' );
202 $enabled = get_option( 'sharing-services' );
203 $services = $this->get_all_services();
204
205 /**
206 * Check if options exist and are well formatted.
207 * This avoids issues on sites with corrupted options.
208 * @see https://github.com/Automattic/jetpack/issues/6121
209 */
210 if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) {
211 $global_options = array( 'global' => $this->get_global_options() );
212 $options = is_array( $options )
213 ? array_merge( $options, $global_options )
214 : $global_options;
215 }
216
217 $global = $options['global'];
218
219 // Default services
220 if ( ! is_array( $enabled ) ) {
221 $enabled = array(
222 'visible' => array(
223 'twitter',
224 'facebook',
225 ),
226 'hidden' => array(),
227 );
228
229 /**
230 * Filters the list of default Sharing Services.
231 *
232 * @module sharedaddy
233 *
234 * @since 1.1.0
235 *
236 * @param array $enabled Array of default Sharing Services.
237 */
238 $enabled = apply_filters( 'sharing_default_services', $enabled );
239 }
240
241 // Cleanup after any filters that may have produced duplicate services
242 if ( is_array( $enabled['visible'] ) ) {
243 $enabled['visible'] = array_unique( $enabled['visible'] );
244 } else {
245 $enabled['visible'] = array();
246 }
247
248 if ( is_array( $enabled['hidden'] ) ) {
249 $enabled['hidden'] = array_unique( $enabled['hidden'] );
250 } else {
251 $enabled['hidden'] = array();
252 }
253
254 // Form the enabled services
255 $blog = array(
256 'visible' => array(),
257 'hidden' => array(),
258 );
259
260 foreach ( $blog as $area => $stuff ) {
261 foreach ( (array) $enabled[ $area ] as $service ) {
262 if ( isset( $services[ $service ] ) ) {
263 if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) {
264 $options[ $service ] = array();
265 }
266 $blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) );
267 }
268 }
269 }
270
271 /**
272 * Filters the list of enabled Sharing Services.
273 *
274 * @module sharedaddy
275 *
276 * @since 1.1.0
277 *
278 * @param array $blog Array of enabled Sharing Services.
279 */
280 $blog = apply_filters( 'sharing_services_enabled', $blog );
281
282 // Add CSS for NASCAR
283 if ( count( $blog['visible'] ) || count( $blog['hidden'] ) ) {
284 add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
285 }
286
287 // Convenience for checking if a service is present
288 $blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
289 return $blog;
290 }
291
292 public function get_service( $service_name ) {
293 $services = $this->get_blog_services();
294
295 if ( isset( $services['visible'][ $service_name ] ) ) {
296 return $services['visible'][ $service_name ];
297 }
298
299 if ( isset( $services['hidden'][ $service_name ] ) ) {
300 return $services['hidden'][ $service_name ];
301 }
302
303 return false;
304 }
305
306 public function set_global_options( $data ) {
307 $options = get_option( 'sharing-options' );
308
309 // No options yet.
310 if ( ! is_array( $options ) ) {
311 $options = array();
312 }
313
314 // Defaults.
315 $options['global'] = array(
316 'button_style' => 'icon-text',
317 'sharing_label' => $this->default_sharing_label,
318 'open_links' => 'same',
319 'show' => ! isset( $options['global'] ) ? array( 'post', 'page' ) : array(),
320 'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array(),
321 );
322
323 /**
324 * Filters global sharing settings.
325 *
326 * @module sharedaddy
327 *
328 * @since 1.1.0
329 *
330 * @param array $options['global'] Array of global sharing settings.
331 */
332 $options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
333
334 // Validate options and set from our data
335 if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) ) {
336 $options['global']['button_style'] = $data['button_style'];
337 }
338
339 if ( isset( $data['sharing_label'] ) ) {
340 if ( $this->default_sharing_label === $data['sharing_label'] ) {
341 $options['global']['sharing_label'] = false;
342 } else {
343 $options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
344 }
345 }
346
347 if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) ) {
348 $options['global']['open_links'] = $data['open_links'];
349 }
350
351 $shows = array_values( get_post_types( array( 'public' => true ) ) );
352 $shows[] = 'index';
353 if ( isset( $data['show'] ) ) {
354 if ( is_scalar( $data['show'] ) ) {
355 switch ( $data['show'] ) {
356 case 'posts':
357 $data['show'] = array( 'post', 'page' );
358 break;
359 case 'index':
360 $data['show'] = array( 'index' );
361 break;
362 case 'posts-index':
363 $data['show'] = array( 'post', 'page', 'index' );
364 break;
365 }
366 }
367
368 if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
369 $options['global']['show'] = $data['show'];
370 }
371 }
372
373 update_option( 'sharing-options', $options );
374 return $options['global'];
375 }
376
377 public function get_global_options() {
378 if ( $this->global === false ) {
379 $options = get_option( 'sharing-options' );
380
381 if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) {
382 $this->global = $options['global'];
383 } else {
384 $this->global = $this->set_global_options( $options['global'] );
385 }
386 }
387
388 if ( ! isset( $this->global['show'] ) ) {
389 $this->global['show'] = array( 'post', 'page' );
390 } elseif ( is_scalar( $this->global['show'] ) ) {
391 switch ( $this->global['show'] ) {
392 case 'posts':
393 $this->global['show'] = array( 'post', 'page' );
394 break;
395 case 'index':
396 $this->global['show'] = array( 'index' );
397 break;
398 case 'posts-index':
399 $this->global['show'] = array( 'post', 'page', 'index' );
400 break;
401 }
402 }
403
404 if ( false === $this->global['sharing_label'] ) {
405 $this->global['sharing_label'] = $this->default_sharing_label;
406 }
407
408 return $this->global;
409 }
410
411 public function set_service( $id, Sharing_Source $service ) {
412 // Update the options for this service
413 $options = get_option( 'sharing-options' );
414
415 // No options yet
416 if ( ! is_array( $options ) ) {
417 $options = array();
418 }
419
420 /**
421 * Get the state of a sharing button.
422 *
423 * @module sharedaddy
424 *
425 * @since 1.1.0
426 *
427 * @param array $args {
428 * State of a sharing button.
429 *
430 * @type string $id Service ID.
431 * @type array $options Array of all sharing options.
432 * @type array $service Details about a service.
433 * }
434 */
435 do_action(
436 'sharing_get_button_state', array(
437 'id' => $id,
438 'options' => $options,
439 'service' => $service,
440 )
441 );
442
443 $options[ $id ] = $service->get_options();
444
445 update_option( 'sharing-options', array_filter( $options ) );
446 }
447
448 // Soon to come to a .org plugin near you!
449 public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
450 global $wpdb, $blog_id;
451 if ( ! $_blog_id ) {
452 $_blog_id = $blog_id;
453 }
454 if ( $service_name == false ) {
455 if ( $post_id > 0 ) {
456 // total number of shares for this post
457 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 ) );
458 } else {
459 // total number of shares for this blog
460 return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d', $_blog_id ) );
461 }
462 }
463
464 if ( $post_id > 0 ) {
465 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 ) );
466 } else {
467 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 ) );
468 }
469 }
470
471 public function get_services_total( $post_id = false ) {
472 $totals = array();
473 $services = $this->get_blog_services();
474
475 if ( ! empty( $services ) && isset( $services['all'] ) ) {
476 foreach ( $services['all'] as $key => $value ) {
477 $totals[ $key ] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) );
478 }
479 }
480 usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
481
482 return $totals;
483 }
484
485 public function get_posts_total() {
486 $totals = array();
487 global $wpdb, $blog_id;
488
489 $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 ) );
490
491 if ( ! empty( $my_data ) ) {
492 foreach ( $my_data as $row ) {
493 $totals[] = new Sharing_Post_Total( $row->id, $row->total );
494 }
495 }
496
497 usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
498
499 return $totals;
500 }
501 }
502
503 class Sharing_Service_Total {
504 public $id = '';
505 public $name = '';
506 public $service = '';
507 public $total = 0;
508
509 public function __construct( $id, $total ) {
510 $services = new Sharing_Service();
511 $this->id = esc_html( $id );
512 $this->service = $services->get_service( $id );
513 $this->total = (int) $total;
514
515 $this->name = $this->service->get_name();
516 }
517
518 static function cmp( $a, $b ) {
519 if ( $a->total == $b->total ) {
520 return $a->name < $b->name;
521 }
522 return $a->total < $b->total;
523 }
524 }
525
526 class Sharing_Post_Total {
527 public $id = 0;
528 public $total = 0;
529 public $title = '';
530 public $url = '';
531
532 public function __construct( $id, $total ) {
533 $this->id = (int) $id;
534 $this->total = (int) $total;
535 $this->title = get_the_title( $this->id );
536 $this->url = get_permalink( $this->id );
537 }
538
539 static function cmp( $a, $b ) {
540 if ( $a->total == $b->total ) {
541 return $a->id < $b->id;
542 }
543 return $a->total < $b->total;
544 }
545 }
546
547 function sharing_register_post_for_share_counts( $post_id ) {
548 global $jetpack_sharing_counts;
549
550 if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) ) {
551 $jetpack_sharing_counts = array();
552 }
553
554 $jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
555 }
556
557 function sharing_maybe_enqueue_scripts() {
558 $sharer = new Sharing_Service();
559 $global_options = $sharer->get_global_options();
560
561 $enqueue = false;
562 if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) {
563 $enqueue = true;
564 } elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) {
565 $enqueue = true;
566 }
567
568 /**
569 * Filter to decide when sharing scripts should be enqueued.
570 *
571 * @module sharedaddy
572 *
573 * @since 3.2.0
574 *
575 * @param bool $enqueue Decide if the sharing scripts should be enqueued.
576 */
577 return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
578 }
579
580 function sharing_add_footer() {
581 if (
582 class_exists( 'Jetpack_AMP_Support' )
583 && Jetpack_AMP_Support::is_amp_request()
584 ) {
585 return;
586 }
587
588 global $jetpack_sharing_counts;
589
590 /**
591 * Filter all JavaScript output by the sharing module.
592 *
593 * @module sharedaddy
594 *
595 * @since 1.1.0
596 *
597 * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
598 */
599 if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) {
600
601 /**
602 * Filter the display of sharing counts next to the sharing buttons.
603 *
604 * @module sharedaddy
605 *
606 * @since 3.2.0
607 *
608 * @param bool true Control the display of counters next to the sharing buttons. Default to true.
609 */
610 if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) :
611 $sharing_post_urls = array_filter( $jetpack_sharing_counts );
612 if ( $sharing_post_urls ) :
613 ?>
614
615 <script type="text/javascript">
616 window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>;
617 </script>
618 <?php
619 endif;
620 endif;
621
622 wp_enqueue_script( 'sharing-js' );
623 $sharing_js_options = array(
624 'lang' => get_base_recaptcha_lang_code(),
625 /** This filter is documented in modules/sharedaddy/sharing-service.php */
626 'counts' => apply_filters( 'jetpack_sharing_counts', true ),
627 'is_stats_active' => Jetpack::is_module_active( 'stats' ),
628 );
629 wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options );
630 }
631 $sharer = new Sharing_Service();
632 $enabled = $sharer->get_blog_services();
633 foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) {
634 $service->display_footer();
635 }
636 }
637
638 function sharing_add_header() {
639 $sharer = new Sharing_Service();
640 $enabled = $sharer->get_blog_services();
641
642 foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) {
643 $service->display_header();
644 }
645
646 if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
647 wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) . 'sharing.css', array(), JETPACK__VERSION );
648 wp_enqueue_style( 'social-logos' );
649 }
650
651 }
652 add_action( 'wp_head', 'sharing_add_header', 1 );
653
654 function sharing_process_requests() {
655 global $post;
656
657 // Only process if: single post and share=X defined
658 if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) {
659 $sharer = new Sharing_Service();
660
661 $service = $sharer->get_service( $_GET['share'] );
662 if ( $service ) {
663 $service->process_request( $post, $_POST );
664 }
665 }
666 }
667 add_action( 'template_redirect', 'sharing_process_requests', 9 );
668
669 function sharing_display( $text = '', $echo = false ) {
670 global $post, $wp_current_filter;
671
672 require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
673 if ( Jetpack_Sync_Settings::is_syncing() ) {
674 return $text;
675 }
676
677 if ( empty( $post ) ) {
678 return $text;
679 }
680
681 if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
682 return $text;
683 }
684
685 // Don't output flair on excerpts
686 if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
687 return $text;
688 }
689
690 // Don't allow flair to be added to the_content more than once (prevent infinite loops)
691 $done = false;
692 foreach ( $wp_current_filter as $filter ) {
693 if ( 'the_content' == $filter ) {
694 if ( $done ) {
695 return $text;
696 } else {
697 $done = true;
698 }
699 }
700 }
701
702 // check whether we are viewing the front page and whether the front page option is checked
703 $options = get_option( 'sharing-options' );
704 $display_options = $options['global']['show'];
705
706 if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) ) {
707 return $text;
708 }
709
710 if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) {
711 // Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
712 // We only want to output the sharing buttons once. Let's stick with the_content().
713 return $text;
714 }
715
716 $sharer = new Sharing_Service();
717 $global = $sharer->get_global_options();
718
719 $show = false;
720 if ( ! is_feed() ) {
721 if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) {
722 $show = true;
723 } elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) {
724 $show = true;
725 }
726 }
727
728 /**
729 * Filter to decide if sharing buttons should be displayed.
730 *
731 * @module sharedaddy
732 *
733 * @since 1.1.0
734 *
735 * @param bool $show Should the sharing buttons be displayed.
736 * @param WP_Post $post The post to share.
737 */
738 $show = apply_filters( 'sharing_show', $show, $post );
739
740 // Disabled for this post?
741 $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
742
743 if ( ! empty( $switched_status ) ) {
744 $show = false;
745 }
746
747 // Private post?
748 $post_status = get_post_status( $post->ID );
749
750 if ( 'private' === $post_status ) {
751 $show = false;
752 }
753
754 /**
755 * Filter the Sharing buttons' Ajax action name Jetpack checks for.
756 * This allows the use of the buttons with your own Ajax implementation.
757 *
758 * @module sharedaddy
759 *
760 * @since 7.3.0
761 *
762 * @param string $sharing_ajax_action_name Name of the Sharing buttons' Ajax action.
763 */
764 $ajax_action = apply_filters( 'sharing_ajax_action', 'get_latest_posts' );
765
766 // Allow to be used in ajax requests for latest posts.
767 if (
768 defined( 'DOING_AJAX' )
769 && DOING_AJAX
770 && isset( $_REQUEST['action'] )
771 && $ajax_action === $_REQUEST['action']
772 ) {
773 $show = true;
774 }
775
776 $sharing_content = '';
777 $enabled = false;
778
779 if ( $show ) {
780 /**
781 * Filters the list of enabled Sharing Services.
782 *
783 * @module sharedaddy
784 *
785 * @since 2.2.3
786 *
787 * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
788 */
789 $enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
790
791 if ( count( $enabled['all'] ) > 0 ) {
792 global $post;
793
794 $dir = get_option( 'text_direction' );
795
796 // Wrapper
797 $sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
798 if ( $global['sharing_label'] != '' ) {
799 $sharing_content .= sprintf(
800 /**
801 * Filter the sharing buttons' headline structure.
802 *
803 * @module sharedaddy
804 *
805 * @since 4.4.0
806 *
807 * @param string $sharing_headline Sharing headline structure.
808 * @param string $global['sharing_label'] Sharing title.
809 * @param string $sharing Module name.
810 */
811 apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
812 esc_html( $global['sharing_label'] )
813 );
814 }
815 $sharing_content .= '<div class="sd-content"><ul>';
816
817 // Visible items
818 $visible = '';
819 foreach ( $enabled['visible'] as $id => $service ) {
820 $klasses = array( 'share-' . $service->get_class() );
821 if ( $service->is_deprecated() ) {
822 if ( ! current_user_can( 'manage_options' ) ) {
823 continue;
824 }
825 $klasses[] = 'share-deprecated';
826 }
827 // Individual HTML for sharing service
828 $visible .= '<li class="' . implode( ' ', $klasses ) . '">' . $service->get_display( $post ) . '</li>';
829 }
830
831 $parts = array();
832 $parts[] = $visible;
833 if ( count( $enabled['hidden'] ) > 0 ) {
834 if ( count( $enabled['visible'] ) > 0 ) {
835 $expand = __( 'More', 'jetpack' );
836 } else {
837 $expand = __( 'Share', 'jetpack' );
838 }
839 $parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>' . $expand . '</span></a></li>';
840 }
841
842 if ( $dir == 'rtl' ) {
843 $parts = array_reverse( $parts );
844 }
845
846 $sharing_content .= implode( '', $parts );
847 $sharing_content .= '<li class="share-end"></li></ul>';
848
849 if ( count( $enabled['hidden'] ) > 0 ) {
850 $sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
851
852 if ( count( $enabled['hidden'] ) == 1 ) {
853 $sharing_content .= 'width:150px;';
854 }
855
856 $sharing_content .= '">';
857
858 if ( count( $enabled['hidden'] ) == 1 ) {
859 $sharing_content .= '<ul style="background-image:none;">';
860 } else {
861 $sharing_content .= '<ul>';
862 }
863
864 $count = 1;
865 foreach ( $enabled['hidden'] as $id => $service ) {
866 // Individual HTML for sharing service
867 $klasses = array( 'share-' . $service->get_class() );
868 if ( $service->is_deprecated() ) {
869 if ( ! current_user_can( 'manage_options' ) ) {
870 continue;
871 }
872 $klasses[] = 'share-deprecated';
873 }
874 $sharing_content .= '<li class="' . implode( ' ', $klasses ) . '">';
875 $sharing_content .= $service->get_display( $post );
876 $sharing_content .= '</li>';
877
878 if ( ( $count % 2 ) == 0 ) {
879 $sharing_content .= '<li class="share-end"></li>';
880 }
881
882 $count ++;
883 }
884
885 // End of wrapper
886 $sharing_content .= '<li class="share-end"></li></ul></div></div>';
887 }
888
889 $sharing_content .= '</div></div></div>';
890
891 // Register our JS
892 if ( defined( 'JETPACK__VERSION' ) ) {
893 $ver = JETPACK__VERSION;
894 } else {
895 $ver = '20141212';
896 }
897 wp_register_script(
898 'sharing-js',
899 Jetpack::get_file_url_for_environment(
900 '_inc/build/sharedaddy/sharing.min.js',
901 'modules/sharedaddy/sharing.js'
902 ),
903 array( 'jquery' ),
904 $ver
905 );
906
907 // Enqueue scripts for the footer
908 add_action( 'wp_footer', 'sharing_add_footer' );
909 }
910 }
911
912 /**
913 * Filters the content markup of the Jetpack sharing links
914 *
915 * @module sharedaddy
916 *
917 * @since 3.8.0
918 * @since 6.2.0 Started sending $enabled as a second parameter.
919 *
920 * @param string $sharing_content Content markup of the Jetpack sharing links
921 * @param array $enabled Array of Sharing Services currently enabled.
922 */
923 $sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content, $enabled );
924
925 if ( $echo ) {
926 echo $text . $sharing_markup;
927 } else {
928 return $text . $sharing_markup;
929 }
930 }
931
932 add_filter( 'the_content', 'sharing_display', 19 );
933 add_filter( 'the_excerpt', 'sharing_display', 19 );
934 function get_base_recaptcha_lang_code() {
935
936 $base_recaptcha_lang_code_mapping = array(
937 'en' => 'en',
938 'nl' => 'nl',
939 'fr' => 'fr',
940 'fr-be' => 'fr',
941 'fr-ca' => 'fr',
942 'fr-ch' => 'fr',
943 'de' => 'de',
944 'pt' => 'pt',
945 'pt-br' => 'pt',
946 'ru' => 'ru',
947 'es' => 'es',
948 'tr' => 'tr',
949 );
950
951 $blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' );
952 if ( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) ) {
953 return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
954 }
955
956 // if no base mapping is found return default 'en'
957 return 'en';
958 }
959