← All changes
|
extensions/blocks/sharing-button/class-sharing-source-block.php
+133
-69
13.1.5
→
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 | /** |
| @@ -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 | /** |
| @@ -402,9 +430,9 @@ | ||
| 402 | 430 | * @param int $post_id The current post id if it is defined. |
| 403 | 431 | * @return string The nonce action name. |
| 404 | 432 | */ |
| 405 | 433 | protected function get_email_share_nonce_action( $post_id ) { |
| 406 | - if ( ! empty( $post_id ) && 0 !== $post_id ) { | |
| 434 | + if ( ! empty( $post_id ) ) { | |
| 407 | 435 | return 'jetpack-email-share-' . $post_id; |
| 408 | 436 | } |
| 409 | 437 | |
| 410 | 438 | return 'jetpack-email-share'; |
| @@ -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 | } |