← All changes
|
extensions/blocks/sharing-button/class-sharing-source-block.php
+48
-86
13.6.2
→
16.3-a.1
View file →
| @@ -11,9 +11,9 @@ | ||
| 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 | 16 | use WP_Post; |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Base class for sharing sources. |
| @@ -63,14 +63,40 @@ | ||
| 63 | 63 | return $this->id; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * 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! | |
| 68 | 69 | * |
| 69 | - * @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 | |
| 70 | 73 | */ |
| 71 | - public function get_total() { | |
| 72 | - 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 | + ); | |
| 73 | 99 | } |
| 74 | 100 | |
| 75 | 101 | /** |
| 76 | 102 | * Get a post's permalink to use for sharing. |
| @@ -113,10 +139,9 @@ | ||
| 113 | 139 | * @param string $post->post_title Post Title. |
| 114 | 140 | * @param int $post_id Post ID. |
| 115 | 141 | * @param int $this->id Sharing ID. |
| 116 | 142 | */ |
| 117 | - $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id ); | |
| 118 | - | |
| 143 | + $title = $post instanceof WP_Post ? apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id ) : ''; | |
| 119 | 144 | return html_entity_decode( wp_kses( $title, '' ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 120 | 145 | } |
| 121 | 146 | |
| 122 | 147 | /** |
| @@ -374,9 +399,9 @@ | ||
| 374 | 399 | wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We allow external redirects here; we define them ourselves. |
| 375 | 400 | |
| 376 | 401 | // We set up this custom header to indicate to search engines not to index this page. |
| 377 | 402 | header( 'X-Robots-Tag: noindex, nofollow' ); |
| 378 | - die(); | |
| 403 | + die( 0 ); | |
| 379 | 404 | } |
| 380 | 405 | } |
| 381 | 406 | |
| 382 | 407 | /** |
| @@ -405,9 +430,9 @@ | ||
| 405 | 430 | * @param int $post_id The current post id if it is defined. |
| 406 | 431 | * @return string The nonce action name. |
| 407 | 432 | */ |
| 408 | 433 | protected function get_email_share_nonce_action( $post_id ) { |
| 409 | - if ( ! empty( $post_id ) && 0 !== $post_id ) { | |
| 434 | + if ( ! empty( $post_id ) ) { | |
| 410 | 435 | return 'jetpack-email-share-' . $post_id; |
| 411 | 436 | } |
| 412 | 437 | |
| 413 | 438 | return 'jetpack-email-share'; |
| @@ -473,9 +498,9 @@ | ||
| 473 | 498 | * |
| 474 | 499 | * @param WP_Post $post Post object. |
| 475 | 500 | * @param array $post_data Array of information about the post we're sharing. |
| 476 | 501 | * |
| 477 | - * @return void | |
| 502 | + * @return never | |
| 478 | 503 | */ |
| 479 | 504 | public function process_request( $post, array $post_data ) { |
| 480 | 505 | $is_ajax = false; |
| 481 | 506 | if ( |
| @@ -495,15 +520,14 @@ | ||
| 495 | 520 | parent::process_request( $post, $post_data ); |
| 496 | 521 | } |
| 497 | 522 | |
| 498 | 523 | if ( $is_ajax ) { |
| 499 | - 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 ); | |
| 500 | 526 | } else { |
| 501 | 527 | wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' ); |
| 502 | - exit; | |
| 528 | + exit( 0 ); | |
| 503 | 529 | } |
| 504 | - | |
| 505 | - wp_die(); | |
| 506 | 530 | } |
| 507 | 531 | } |
| 508 | 532 | |
| 509 | 533 | /** |
| @@ -541,9 +565,10 @@ | ||
| 541 | 565 | * |
| 542 | 566 | * @return void |
| 543 | 567 | */ |
| 544 | 568 | public function process_request( $post, array $post_data ) { |
| 545 | - $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 ) ); | |
| 546 | 571 | |
| 547 | 572 | // Record stats |
| 548 | 573 | parent::process_request( $post, $post_data ); |
| 549 | 574 | |
| @@ -659,13 +684,11 @@ | ||
| 659 | 684 | * |
| 660 | 685 | * @return string |
| 661 | 686 | */ |
| 662 | 687 | public function get_image( $post ) { |
| 663 | - if ( class_exists( 'Jetpack_PostImages' ) ) { | |
| 664 | - $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) ); | |
| 665 | - if ( ! empty( $image ) ) { | |
| 666 | - return $image['src']; | |
| 667 | - } | |
| 688 | + $image = Images::get_image( $post->ID, array( 'fallback_to_avatars' => true ) ); | |
| 689 | + if ( ! empty( $image ) ) { | |
| 690 | + return $image['src']; | |
| 668 | 691 | } |
| 669 | 692 | |
| 670 | 693 | /** |
| 671 | 694 | * Filters the default image used by the Pinterest Pin It share button. |
| @@ -686,9 +709,9 @@ | ||
| 686 | 709 | * |
| 687 | 710 | * @return string |
| 688 | 711 | */ |
| 689 | 712 | public function get_external_url( $post ) { |
| 690 | - $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 ); | |
| 691 | 714 | |
| 692 | 715 | /** |
| 693 | 716 | * Filters the Pinterest share URL used in sharing button output. |
| 694 | 717 | * |
| @@ -737,52 +760,14 @@ | ||
| 737 | 760 | $pinterest_url = esc_url_raw( $this->get_external_url( $post ) ); |
| 738 | 761 | parent::redirect_request( $pinterest_url ); |
| 739 | 762 | } else { |
| 740 | 763 | echo '// share count bumped'; |
| 741 | - die(); | |
| 764 | + die( 0 ); | |
| 742 | 765 | } |
| 743 | 766 | } |
| 744 | 767 | } |
| 745 | 768 | |
| 746 | 769 | /** |
| 747 | - * Pocket sharing service. | |
| 748 | - */ | |
| 749 | -class Share_Pocket_Block extends Sharing_Source_Block { | |
| 750 | - /** | |
| 751 | - * Service short name. | |
| 752 | - * | |
| 753 | - * @var string | |
| 754 | - */ | |
| 755 | - public $shortname = 'pocket'; | |
| 756 | - | |
| 757 | - /** | |
| 758 | - * Service name. | |
| 759 | - * | |
| 760 | - * @return string | |
| 761 | - */ | |
| 762 | - public function get_name() { | |
| 763 | - return __( 'Pocket', 'jetpack' ); | |
| 764 | - } | |
| 765 | - | |
| 766 | - /** | |
| 767 | - * Process sharing request. Add actions that need to happen when sharing here. | |
| 768 | - * | |
| 769 | - * @param WP_Post $post Post object. | |
| 770 | - * @param array $post_data Array of information about the post we're sharing. | |
| 771 | - * | |
| 772 | - * @return void | |
| 773 | - */ | |
| 774 | - public function process_request( $post, array $post_data ) { | |
| 775 | - // Record stats | |
| 776 | - parent::process_request( $post, $post_data ); | |
| 777 | - | |
| 778 | - $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 ) ) ); | |
| 779 | - | |
| 780 | - parent::redirect_request( $pocket_url ); | |
| 781 | - } | |
| 782 | -} | |
| 783 | - | |
| 784 | -/** | |
| 785 | 770 | * Telegram sharing service. |
| 786 | 771 | */ |
| 787 | 772 | class Share_Telegram_Block extends Sharing_Source_Block { |
| 788 | 773 | /** |
| @@ -1187,9 +1172,9 @@ | ||
| 1187 | 1172 | * |
| 1188 | 1173 | * @return string |
| 1189 | 1174 | */ |
| 1190 | 1175 | public function get_name() { |
| 1191 | - return __( 'Twitter', 'jetpack' ); | |
| 1176 | + return __( 'X', 'jetpack' ); | |
| 1192 | 1177 | } |
| 1193 | 1178 | |
| 1194 | 1179 | /** |
| 1195 | 1180 | * Determine the Twitter 'via' value for a post. |
| @@ -1324,9 +1309,9 @@ | ||
| 1324 | 1309 | |
| 1325 | 1310 | $url = $post_link; |
| 1326 | 1311 | $twitter_url = add_query_arg( |
| 1327 | 1312 | rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ), |
| 1328 | - 'https://twitter.com/intent/tweet' | |
| 1313 | + 'https://x.com/intent/tweet' | |
| 1329 | 1314 | ); |
| 1330 | 1315 | |
| 1331 | 1316 | parent::redirect_request( $twitter_url ); |
| 1332 | 1317 | } |
| @@ -1401,15 +1386,13 @@ | ||
| 1401 | 1386 | public function process_request( $post, array $post_data ) { |
| 1402 | 1387 | |
| 1403 | 1388 | $post_link = $this->get_share_url( $post->ID ); |
| 1404 | 1389 | |
| 1405 | - // Using the same URL as the official button, which is *not* LinkedIn's documented sharing link | |
| 1406 | - // https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false | |
| 1407 | 1390 | $linkedin_url = add_query_arg( |
| 1408 | 1391 | array( |
| 1409 | 1392 | 'url' => rawurlencode( $post_link ), |
| 1410 | 1393 | ), |
| 1411 | - 'https://www.linkedin.com/cws/share?token=&isFramed=false' | |
| 1394 | + 'https://www.linkedin.com/sharing/share-offsite/' | |
| 1412 | 1395 | ); |
| 1413 | 1396 | |
| 1414 | 1397 | // Record stats |
| 1415 | 1398 | parent::process_request( $post, $post_data ); |
| @@ -1414,29 +1397,8 @@ | ||
| 1414 | 1397 | // Record stats |
| 1415 | 1398 | parent::process_request( $post, $post_data ); |
| 1416 | 1399 | |
| 1417 | 1400 | parent::redirect_request( $linkedin_url ); |
| 1418 | - } | |
| 1419 | -} | |
| 1420 | - | |
| 1421 | -/** | |
| 1422 | - * Skype sharing service. | |
| 1423 | - */ | |
| 1424 | -class Share_Skype_Block extends Sharing_Source_Block { | |
| 1425 | - /** | |
| 1426 | - * Service short name. | |
| 1427 | - * | |
| 1428 | - * @var string | |
| 1429 | - */ | |
| 1430 | - public $shortname = 'skype'; | |
| 1431 | - | |
| 1432 | - /** | |
| 1433 | - * Service name. | |
| 1434 | - * | |
| 1435 | - * @return string | |
| 1436 | - */ | |
| 1437 | - public function get_name() { | |
| 1438 | - return __( 'Skype', 'jetpack' ); | |
| 1439 | 1401 | } |
| 1440 | 1402 | } |
| 1441 | 1403 | |
| 1442 | 1404 | /** |