PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 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 All 506 releases
← All changes | extensions/blocks/sharing-button/class-sharing-source-block.php +145 -81 13.0.2 → 16.3-a.7 View file →
@@ -11,9 +11,10 @@
11 11
12 12 namespace Automattic\Jetpack\Extensions\Sharing_Button_Block;
13 13
14 14 use Automattic\Jetpack\Device_Detection\User_Agent_Info;
15 -use Jetpack_PostImages;
15 +use Automattic\Jetpack\Post_Media\Images;
16 +use WP_Post;
16 17
17 18 /**
18 19 * Base class for sharing sources.
19 20 * See individual sharing classes below for the implementation of this class.
@@ -62,14 +63,40 @@
62 63 return $this->id;
63 64 }
64 65
65 66 /**
66 - * Get sharing stats for a specific post or sharing service.
67 + * Get stats for a site, a post, or a sharing service.
68 + * Soon to come to a .org plugin near you!
67 69 *
68 - * @return int This is a placeholder that returns 0 at the moment. We might want to implement this in the future.
70 + * @param WP_Post|bool $post Post object.
71 + *
72 + * @return int
69 73 */
70 - public function get_total() {
71 - return 0;
74 + public function get_total( $post = false ) {
75 + global $wpdb, $blog_id;
76 +
77 + $name = strtolower( (string) $this->get_id() );
78 +
79 + if ( $post === false ) {
80 + // get total number of shares for service
81 + return (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
82 + $wpdb->prepare(
83 + 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s',
84 + $blog_id,
85 + $name
86 + )
87 + );
88 + }
89 +
90 + // get total shares for a post
91 + return (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
92 + $wpdb->prepare(
93 + 'SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s',
94 + $blog_id,
95 + $post->ID,
96 + $name
97 + )
98 + );
72 99 }
73 100
74 101 /**
75 102 * Get a post's permalink to use for sharing.
@@ -112,10 +139,9 @@
112 139 * @param string $post->post_title Post Title.
113 140 * @param int $post_id Post ID.
114 141 * @param int $this->id Sharing ID.
115 142 */
116 - $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id );
117 -
143 + $title = $post instanceof WP_Post ? apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id ) : '';
118 144 return html_entity_decode( wp_kses( $title, '' ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
119 145 }
120 146
121 147 /**
@@ -160,9 +186,9 @@
160 186
161 187 /**
162 188 * Get the URL for the link.
163 189 *
164 - * @param WP_Post $post Post object.
190 + * @param int $post_id Post ID.
165 191 * @param string $query Additional query arguments to add to the link. They should be in 'foo=bar&baz=1' format.
166 192 * @param bool|string $id Sharing ID to include in the data-shared attribute.
167 193 * @param array $data_attributes The keys are used as additional attribute names with 'data-' prefix.
168 194 * The values are used as the attribute values.
@@ -167,10 +193,10 @@
167 193 * @param array $data_attributes The keys are used as additional attribute names with 'data-' prefix.
168 194 * The values are used as the attribute values.
169 195 * @return object Link related data (url and data_attributes);
170 196 */
171 - public function get_link( $post, $query = '', $id = false, $data_attributes = array() ) {
172 - $url = $this->get_url( $this->get_process_request_url( $post->ID ), $query, $id );
197 + public function get_link( $post_id, $query = '', $id = false, $data_attributes = array() ) {
198 + $url = $this->get_url( $this->get_process_request_url( $post_id ), $query, $id );
173 199 $data_attributes = $this->get_data_attributes( $id, $data_attributes );
174 200
175 201 return array(
176 202 'url' => $url,
@@ -294,8 +320,9 @@
294 320 if ( ! empty( $data_attributes ) ) {
295 321 $encoded_data_attributes = implode(
296 322 ' ',
297 323 array_map(
324 + /** Format attributes */
298 325 function ( $data_key, $data_value ) {
299 326 return sprintf(
300 327 'data-%s="%s"',
301 328 esc_attr( str_replace( array( ' ', '"' ), '', $data_key ) ),
@@ -365,8 +392,9 @@
365 392 /**
366 393 * Redirect to an external social network site to finish sharing.
367 394 *
368 395 * @param string $url Sharing URL for a given service.
396 + * @return never
369 397 */
370 398 public function redirect_request( $url ) {
371 399 wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We allow external redirects here; we define them ourselves.
372 400
@@ -371,9 +399,9 @@
371 399 wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We allow external redirects here; we define them ourselves.
372 400
373 401 // We set up this custom header to indicate to search engines not to index this page.
374 402 header( 'X-Robots-Tag: noindex, nofollow' );
375 - die();
403 + die( 0 );
376 404 }
377 405 }
378 406
379 407 /**
@@ -398,14 +426,14 @@
398 426
399 427 /**
400 428 * Helper function to return a nonce action based on the current post.
401 429 *
402 - * @param WP_Post|null $post The current post if it is defined.
430 + * @param int $post_id The current post id if it is defined.
403 431 * @return string The nonce action name.
404 432 */
405 - protected function get_email_share_nonce_action( $post ) {
406 - if ( ! empty( $post ) && $post instanceof WP_Post ) {
407 - return 'jetpack-email-share-' . $post->ID;
433 + protected function get_email_share_nonce_action( $post_id ) {
434 + if ( ! empty( $post_id ) ) {
435 + return 'jetpack-email-share-' . $post_id;
408 436 }
409 437
410 438 return 'jetpack-email-share';
411 439 }
@@ -412,9 +440,9 @@
412 440
413 441 /**
414 442 * Get the URL for the link.
415 443 *
416 - * @param WP_Post $post Post object.
444 + * @param int $post_id Post ID.
417 445 * @param string $query Additional query arguments to add to the link. They should be in 'foo=bar&baz=1' format.
418 446 * @param bool|string $id Sharing ID to include in the data-shared attribute.
419 447 * @param array $data_attributes The keys are used as additional attribute names with 'data-' prefix.
420 448 * The values are used as the attribute values.
@@ -419,12 +447,12 @@
419 447 * @param array $data_attributes The keys are used as additional attribute names with 'data-' prefix.
420 448 * The values are used as the attribute values.
421 449 * @return object Link related data (url and data_attributes);
422 450 */
423 - public function get_link( $post, $query = '', $id = false, $data_attributes = array() ) {
451 + public function get_link( $post_id, $query = '', $id = false, $data_attributes = array() ) {
424 452 // We don't need to open new window, so we set it to false.
425 453 $id = false;
426 - $tracking_url = $this->get_process_request_url( $post->ID );
454 + $tracking_url = $this->get_process_request_url( $post_id );
427 455 if ( false === stripos( $tracking_url, '?' ) ) {
428 456 $tracking_url .= '?';
429 457 } else {
430 458 $tracking_url .= '&';
@@ -436,14 +464,14 @@
436 464 'email-share-error-text' => __(
437 465 "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.",
438 466 'jetpack'
439 467 ),
440 - 'email-share-nonce' => wp_create_nonce( $this->get_email_share_nonce_action( $post ) ),
468 + 'email-share-nonce' => wp_create_nonce( $this->get_email_share_nonce_action( $post_id ) ),
441 469 'email-share-track-url' => $tracking_url,
442 470 );
443 471
444 - $post_title = $this->get_share_title( $post->ID );
445 - $post_url = $this->get_share_url( $post->ID );
472 + $post_title = $this->get_share_title( $post_id );
473 + $post_url = $this->get_share_url( $post_id );
446 474
447 475 /** This filter is documented in plugins/jetpack/modules/sharedaddy/sharedaddy.php */
448 476 $email_subject = apply_filters(
449 477 'wp_sharing_email_send_post_subject',
@@ -470,9 +498,9 @@
470 498 *
471 499 * @param WP_Post $post Post object.
472 500 * @param array $post_data Array of information about the post we're sharing.
473 501 *
474 - * @return void
502 + * @return never
475 503 */
476 504 public function process_request( $post, array $post_data ) {
477 505 $is_ajax = false;
478 506 if (
@@ -492,15 +520,14 @@
492 520 parent::process_request( $post, $post_data );
493 521 }
494 522
495 523 if ( $is_ajax ) {
496 - wp_send_json_success();
524 + // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
525 + wp_send_json_success( null, null, JSON_UNESCAPED_SLASHES );
497 526 } else {
498 527 wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' );
499 - exit;
528 + exit( 0 );
500 529 }
501 -
502 - wp_die();
503 530 }
504 531 }
505 532
506 533 /**
@@ -538,9 +565,10 @@
538 565 *
539 566 * @return void
540 567 */
541 568 public function process_request( $post, array $post_data ) {
542 - $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 ) );
569 + $post_id = $post instanceof WP_Post ? $post->ID : 0;
570 + $fb_url = 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode( $this->get_share_url( $post_id ) ) . '&t=' . rawurlencode( $this->get_share_title( $post_id ) );
543 571
544 572 // Record stats
545 573 parent::process_request( $post, $post_data );
546 574
@@ -569,8 +597,29 @@
569 597 }
570 598 }
571 599
572 600 /**
601 + * Native Share button (relying on the Web Share API).
602 + */
603 +class Share_Native_Block extends Sharing_Source_Block {
604 + /**
605 + * Service short name.
606 + *
607 + * @var string
608 + */
609 + public $shortname = 'native';
610 +
611 + /**
612 + * Service name.
613 + *
614 + * @return string
615 + */
616 + public function get_name() {
617 + return __( 'Web Share', 'jetpack' );
618 + }
619 +}
620 +
621 +/**
573 622 * Tumblr sharing service.
574 623 */
575 624 class Share_Tumblr_Block extends Sharing_Source_Block {
576 625 /**
@@ -635,13 +684,11 @@
635 684 *
636 685 * @return string
637 686 */
638 687 public function get_image( $post ) {
639 - if ( class_exists( 'Jetpack_PostImages' ) ) {
640 - $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
641 - if ( ! empty( $image ) ) {
642 - return $image['src'];
643 - }
688 + $image = Images::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
689 + if ( ! empty( $image ) ) {
690 + return $image['src'];
644 691 }
645 692
646 693 /**
647 694 * Filters the default image used by the Pinterest Pin It share button.
@@ -662,9 +709,9 @@
662 709 *
663 710 * @return string
664 711 */
665 712 public function get_external_url( $post ) {
666 - $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 );
713 + $url = 'https://www.pinterest.com/pin/create/link/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&media=' . rawurlencode( $this->get_image( $post ) ) . '&description=' . rawurlencode( $post->post_title );
667 714
668 715 /**
669 716 * Filters the Pinterest share URL used in sharing button output.
670 717 *
@@ -713,52 +760,14 @@
713 760 $pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
714 761 parent::redirect_request( $pinterest_url );
715 762 } else {
716 763 echo '// share count bumped';
717 - die();
764 + die( 0 );
718 765 }
719 766 }
720 767 }
721 768
722 769 /**
723 - * Pocket sharing service.
724 - */
725 -class Share_Pocket_Block extends Sharing_Source_Block {
726 - /**
727 - * Service short name.
728 - *
729 - * @var string
730 - */
731 - public $shortname = 'pocket';
732 -
733 - /**
734 - * Service name.
735 - *
736 - * @return string
737 - */
738 - public function get_name() {
739 - return __( 'Pocket', 'jetpack' );
740 - }
741 -
742 - /**
743 - * Process sharing request. Add actions that need to happen when sharing here.
744 - *
745 - * @param WP_Post $post Post object.
746 - * @param array $post_data Array of information about the post we're sharing.
747 - *
748 - * @return void
749 - */
750 - public function process_request( $post, array $post_data ) {
751 - // Record stats
752 - parent::process_request( $post, $post_data );
753 -
754 - $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 ) ) );
755 -
756 - parent::redirect_request( $pocket_url );
757 - }
758 -}
759 -
760 -/**
761 770 * Telegram sharing service.
762 771 */
763 772 class Share_Telegram_Block extends Sharing_Source_Block {
764 773 /**
@@ -954,8 +963,47 @@
954 963 }
955 964 }
956 965
957 966 /**
967 + * Bluesky sharing button.
968 + */
969 +class Share_Bluesky_Block extends Sharing_Source_Block {
970 + /**
971 + * Service short name.
972 + *
973 + * @var string
974 + */
975 + public $shortname = 'bluesky';
976 +
977 + /**
978 + * Service name.
979 + *
980 + * @return string
981 + */
982 + public function get_name() {
983 + return __( 'Bluesky', 'jetpack' );
984 + }
985 +
986 + /**
987 + * Process sharing request. Add actions that need to happen when sharing here.
988 + *
989 + * @param WP_Post $post Post object.
990 + * @param array $post_data Array of information about the post we're sharing.
991 + *
992 + * @return void
993 + */
994 + public function process_request( $post, array $post_data ) {
995 + // Record stats
996 + parent::process_request( $post, $post_data );
997 +
998 + $url = 'https://bsky.app/intent/compose?text=';
999 + $url .= rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) );
1000 +
1001 + parent::redirect_request( $url );
1002 + }
1003 +}
1004 +
1005 +/**
958 1006 * X sharing button.
959 1007 *
960 1008 * While the old Twitter button had an official button,
961 1009 * this new X button does not, since there is no official X button yet.
@@ -1124,9 +1172,9 @@
1124 1172 *
1125 1173 * @return string
1126 1174 */
1127 1175 public function get_name() {
1128 - return __( 'Twitter', 'jetpack' );
1176 + return __( 'X', 'jetpack' );
1129 1177 }
1130 1178
1131 1179 /**
1132 1180 * Determine the Twitter 'via' value for a post.
@@ -1261,9 +1309,9 @@
1261 1309
1262 1310 $url = $post_link;
1263 1311 $twitter_url = add_query_arg(
1264 1312 rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
1265 - 'https://twitter.com/intent/tweet'
1313 + 'https://x.com/intent/tweet'
1266 1314 );
1267 1315
1268 1316 parent::redirect_request( $twitter_url );
1269 1317 }
@@ -1338,15 +1386,13 @@
1338 1386 public function process_request( $post, array $post_data ) {
1339 1387
1340 1388 $post_link = $this->get_share_url( $post->ID );
1341 1389
1342 - // Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
1343 - // https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
1344 1390 $linkedin_url = add_query_arg(
1345 1391 array(
1346 1392 'url' => rawurlencode( $post_link ),
1347 1393 ),
1348 - 'https://www.linkedin.com/cws/share?token=&isFramed=false'
1394 + 'https://www.linkedin.com/sharing/share-offsite/'
1349 1395 );
1350 1396
1351 1397 // Record stats
1352 1398 parent::process_request( $post, $post_data );
@@ -1355,17 +1401,17 @@
1355 1401 }
1356 1402 }
1357 1403
1358 1404 /**
1359 - * Skype sharing service.
1405 + * Threads sharing button.
1360 1406 */
1361 -class Share_Skype_Block extends Sharing_Source_Block {
1407 +class Share_Threads_Block extends Sharing_Source_Block {
1362 1408 /**
1363 1409 * Service short name.
1364 1410 *
1365 1411 * @var string
1366 1412 */
1367 - public $shortname = 'skype';
1413 + public $shortname = 'threads';
1368 1414
1369 1415 /**
1370 1416 * Service name.
1371 1417 *
@@ -1371,7 +1417,25 @@
1371 1417 *
1372 1418 * @return string
1373 1419 */
1374 1420 public function get_name() {
1375 - return __( 'Skype', 'jetpack' );
1421 + return __( 'Threads', 'jetpack' );
1422 + }
1423 +
1424 + /**
1425 + * Process sharing request. Add actions that need to happen when sharing here.
1426 + *
1427 + * @param WP_Post $post Post object.
1428 + * @param array $post_data Array of information about the post we're sharing.
1429 + *
1430 + * @return void
1431 + */
1432 + public function process_request( $post, array $post_data ) {
1433 + // Record stats
1434 + parent::process_request( $post, $post_data );
1435 +
1436 + $url = 'https://www.threads.net/intent/post/?text=';
1437 + $url .= rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) );
1438 +
1439 + parent::redirect_request( $url );
1376 1440 }
1377 1441 }