← All changes
|
jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-base.php
+380
-307
13.5.2
→
16.3
View file →
| @@ -9,10 +9,10 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Publicize; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Connection\Client; |
| 13 | -use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 14 | 13 | use Automattic\Jetpack\Current_Plan; |
| 14 | +use Automattic\Jetpack\Paths; | |
| 15 | 15 | use Automattic\Jetpack\Redirect; |
| 16 | 16 | use Automattic\Jetpack\Status; |
| 17 | 17 | use WP_Error; |
| 18 | 18 | use WP_Post; |
| @@ -65,8 +65,32 @@ | ||
| 65 | 65 | * @var string |
| 66 | 66 | */ |
| 67 | 67 | const POST_JETPACK_SOCIAL_OPTIONS = '_wpas_options'; |
| 68 | 68 | |
| 69 | + /** | |
| 70 | + * Post meta key for per-connection customization overrides. | |
| 71 | + * | |
| 72 | + * @var string | |
| 73 | + */ | |
| 74 | + const POST_CONNECTION_OVERRIDES = '_wpas_connection_overrides'; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Post meta key for enabling per-network customization. | |
| 78 | + * | |
| 79 | + * @var string | |
| 80 | + */ | |
| 81 | + const POST_CUSTOMIZE_PER_NETWORK = '_wpas_customize_per_network'; | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Attachment meta key for the social image focal point. | |
| 85 | + * | |
| 86 | + * Stored on the image (attachment), not the post, so a focal point set once is | |
| 87 | + * shared by every post that uses the image. | |
| 88 | + * | |
| 89 | + * @var string | |
| 90 | + */ | |
| 91 | + const ATTACHMENT_IMAGE_FOCAL_POINT = '_jetpack_social_image_focal_point'; | |
| 92 | + | |
| 69 | 93 | // Skip meta keys. We used to rely on _wpas_skip_ appended with the token_id to skip posts. But to support |
| 70 | 94 | // multiple connections for the same token, we are going to use the _wpas_skip_publicize_ which |
| 71 | 95 | // will be appended with the connection_id. |
| 72 | 96 | /** |
| @@ -237,19 +261,20 @@ | ||
| 237 | 261 | add_action( 'save_post', array( $this, 'save_meta' ), 20, 2 ); |
| 238 | 262 | |
| 239 | 263 | // Default checkbox state for each Connection. |
| 240 | 264 | add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 2 ); |
| 241 | - add_filter( 'jetpack_open_graph_tags', array( $this, 'add_jetpack_social_og_images' ), 12, 1 ); // $priority = 12, to run after Jetpack adds the tags in the Jetpack_Twitter_Cards class. | |
| 242 | 265 | |
| 266 | + // Add images generated by Social Image Generator to the Open Graph tags on singular posts. | |
| 267 | + add_filter( 'jetpack_open_graph_tags', array( $this, 'add_jetpack_social_og_images' ), 12, 1 ); // $priority = 12, to run after the Twitter_Cards class adds its tags. | |
| 268 | + | |
| 243 | 269 | // Alter the "Post Publish" admin notice to mention the Connections we Publicized to. |
| 244 | 270 | add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 ); |
| 245 | 271 | |
| 246 | - // Connection test callback. | |
| 247 | - add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); | |
| 248 | - | |
| 249 | 272 | // Custom priority to ensure post type support is added prior to thumbnail support being added to the theme. |
| 250 | 273 | add_action( 'init', array( $this, 'add_post_type_support' ), 8 ); |
| 251 | 274 | add_action( 'init', array( $this, 'register_post_meta' ), 20 ); |
| 275 | + // Last, so a veto from another filter is respected before any row changes. | |
| 276 | + add_filter( 'update_post_metadata', array( $this, 'collapse_duplicate_post_meta' ), PHP_INT_MAX, 5 ); | |
| 252 | 277 | |
| 253 | 278 | // The custom priority for this action ensures that any existing code that |
| 254 | 279 | // removes post-thumbnails support during 'init' continues to work. |
| 255 | 280 | add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 ); |
| @@ -273,8 +298,23 @@ | ||
| 273 | 298 | */ |
| 274 | 299 | abstract public function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
| 275 | 300 | |
| 276 | 301 | /** |
| 302 | + * Whether the site has the feature flag enabled. | |
| 303 | + * | |
| 304 | + * @deprecated 0.69.1 Use Current_Plan::supports() directly instead. | |
| 305 | + * | |
| 306 | + * @todo Remove this method After March 2026. | |
| 307 | + * | |
| 308 | + * @param string $flag_name The feature flag to check. Will be prefixed with 'jetpack_social_has_' for the option. | |
| 309 | + * @param string $feature_name The feature name to check for for the Current_Plan check, without the social- prefix. | |
| 310 | + * @return bool | |
| 311 | + */ | |
| 312 | + public function has_feature_flag( $flag_name, $feature_name ): bool { | |
| 313 | + return Publicize_Script_Data::has_feature_flag( $feature_name ); | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 277 | 317 | * Does the given user have a connection to the service on the given blog? |
| 278 | 318 | * |
| 279 | 319 | * @param string $service_name 'facebook', 'twitter', etc. |
| 280 | 320 | * @param false|int $_blog_id The blog ID. Use false (default) for the current blog. |
| @@ -290,9 +330,9 @@ | ||
| 290 | 330 | $_user_id = $this->user_id(); |
| 291 | 331 | } |
| 292 | 332 | |
| 293 | 333 | $connections = $this->get_connections( $service_name, $_blog_id, $_user_id ); |
| 294 | - return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); | |
| 334 | + return is_array( $connections ) && count( $connections ) > 0; | |
| 295 | 335 | } |
| 296 | 336 | |
| 297 | 337 | /** |
| 298 | 338 | * Generates a connection URL. |
| @@ -469,8 +509,25 @@ | ||
| 469 | 509 | if ( 'instagram-business' === $service_name && isset( $cmeta['connection_data']['meta']['username'] ) ) { |
| 470 | 510 | return 'https://instagram.com/' . $cmeta['connection_data']['meta']['username']; |
| 471 | 511 | } |
| 472 | 512 | |
| 513 | + if ( 'linkedin' === $service_name ) { | |
| 514 | + | |
| 515 | + $entity_type = $cmeta['connection_data']['meta']['entity_type'] ?? 'person'; | |
| 516 | + | |
| 517 | + if ( 'organization' === $entity_type && ! empty( $cmeta['connection_data']['meta']['external_name'] ) ) { | |
| 518 | + return 'https://www.linkedin.com/company/' . $cmeta['connection_data']['meta']['external_name']; | |
| 519 | + } | |
| 520 | + | |
| 521 | + if ( 'person' === $entity_type && ! empty( $cmeta['external_name'] ) ) { | |
| 522 | + return 'https://www.linkedin.com/in/' . $cmeta['external_name']; | |
| 523 | + } | |
| 524 | + } | |
| 525 | + | |
| 526 | + if ( 'threads' === $service_name && isset( $cmeta['external_name'] ) ) { | |
| 527 | + return 'https://www.threads.net/@' . $cmeta['external_name']; | |
| 528 | + } | |
| 529 | + | |
| 473 | 530 | if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) { |
| 474 | 531 | return 'https://mastodon.social/@' . $cmeta['external_name']; |
| 475 | 532 | } |
| 476 | 533 | |
| @@ -485,28 +542,10 @@ | ||
| 485 | 542 | if ( 'twitter' === $service_name ) { |
| 486 | 543 | return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'. |
| 487 | 544 | } |
| 488 | 545 | |
| 489 | - if ( 'linkedin' === $service_name ) { | |
| 490 | - if ( ! isset( $cmeta['connection_data']['meta']['profile_url'] ) ) { | |
| 491 | - return false; | |
| 492 | - } | |
| 493 | - | |
| 494 | - $profile_url_query = wp_parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY ); | |
| 495 | - $profile_url_query_args = null; | |
| 496 | - wp_parse_str( $profile_url_query, $profile_url_query_args ); | |
| 497 | - | |
| 498 | - $id = null; | |
| 499 | - | |
| 500 | - if ( isset( $profile_url_query_args['key'] ) ) { | |
| 501 | - $id = $profile_url_query_args['key']; | |
| 502 | - } elseif ( isset( $profile_url_query_args['id'] ) ) { | |
| 503 | - $id = $profile_url_query_args['id']; | |
| 504 | - } else { | |
| 505 | - return false; | |
| 506 | - } | |
| 507 | - | |
| 508 | - return esc_url_raw( add_query_arg( 'id', rawurlencode( $id ), 'https://www.linkedin.com/profile/view' ) ); | |
| 546 | + if ( 'bluesky' === $service_name ) { | |
| 547 | + return 'https://bsky.app/profile/' . $cmeta['external_id']; | |
| 509 | 548 | } |
| 510 | 549 | |
| 511 | 550 | return false; // no fallback. we just won't link it. |
| 512 | 551 | } |
| @@ -520,10 +559,10 @@ | ||
| 520 | 559 | */ |
| 521 | 560 | public function get_display_name( $service_name, $connection ) { |
| 522 | 561 | $cmeta = $this->get_connection_meta( $connection ); |
| 523 | 562 | |
| 524 | - if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) { | |
| 525 | - return $cmeta['external_name']; | |
| 563 | + if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) { | |
| 564 | + return $cmeta['external_display']; | |
| 526 | 565 | } |
| 527 | 566 | |
| 528 | 567 | if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) { |
| 529 | 568 | return $cmeta['connection_data']['meta']['display_name']; |
| @@ -553,19 +592,37 @@ | ||
| 553 | 592 | * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack). |
| 554 | 593 | * @return string |
| 555 | 594 | */ |
| 556 | 595 | public function get_username( $service_name, $connection ) { |
| 596 | + $handle = $this->get_external_handle( $service_name, $connection ); | |
| 597 | + | |
| 598 | + return $handle ?? $this->get_display_name( $service_name, $connection ); | |
| 599 | + } | |
| 600 | + | |
| 601 | + /** | |
| 602 | + * Returns the external handle for the Connection. | |
| 603 | + * | |
| 604 | + * @param string $service_name 'facebook', 'linkedin', etc. | |
| 605 | + * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack). | |
| 606 | + * @return string|null | |
| 607 | + */ | |
| 608 | + public function get_external_handle( $service_name, $connection ) { | |
| 557 | 609 | $cmeta = $this->get_connection_meta( $connection ); |
| 558 | 610 | |
| 559 | - if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) { | |
| 560 | - return $cmeta['external_display']; | |
| 561 | - } | |
| 611 | + switch ( $service_name ) { | |
| 612 | + case 'mastodon': | |
| 613 | + return $cmeta['external_display'] ?? null; | |
| 562 | 614 | |
| 563 | - if ( isset( $cmeta['connection_data']['meta']['username'] ) ) { | |
| 564 | - return $cmeta['connection_data']['meta']['username']; | |
| 615 | + case 'bluesky': | |
| 616 | + case 'threads': | |
| 617 | + return $cmeta['external_name'] ?? null; | |
| 618 | + | |
| 619 | + case 'instagram-business': | |
| 620 | + return $cmeta['connection_data']['meta']['username'] ?? null; | |
| 621 | + | |
| 622 | + default: | |
| 623 | + return null; | |
| 565 | 624 | } |
| 566 | - | |
| 567 | - return $this->get_display_name( $service_name, $connection ); | |
| 568 | 625 | } |
| 569 | 626 | |
| 570 | 627 | /** |
| 571 | 628 | * Returns a profile picture for the Connection |
| @@ -572,9 +629,9 @@ | ||
| 572 | 629 | * |
| 573 | 630 | * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack). |
| 574 | 631 | * @return string |
| 575 | 632 | */ |
| 576 | - private function get_profile_picture( $connection ) { | |
| 633 | + public function get_profile_picture( $connection ) { | |
| 577 | 634 | $cmeta = $this->get_connection_meta( $connection ); |
| 578 | 635 | |
| 579 | 636 | if ( isset( $cmeta['profile_picture'] ) ) { |
| 580 | 637 | return $cmeta['profile_picture']; |
| @@ -678,17 +735,8 @@ | ||
| 678 | 735 | return isset( $connection_data['meta']['options_responses'] ); |
| 679 | 736 | } |
| 680 | 737 | |
| 681 | 738 | /** |
| 682 | - * AJAX Handler to run connection tests on all Connections | |
| 683 | - * | |
| 684 | - * @return void | |
| 685 | - */ | |
| 686 | - public function test_publicize_conns() { | |
| 687 | - wp_send_json_success( $this->get_publicize_conns_test_results() ); | |
| 688 | - } | |
| 689 | - | |
| 690 | - /** | |
| 691 | 739 | * Parse the error code returned by the XML-RPC API call. |
| 692 | 740 | * |
| 693 | 741 | * @param string $code Error code in numerical format. |
| 694 | 742 | * |
| @@ -840,20 +888,19 @@ | ||
| 840 | 888 | } else { |
| 841 | 889 | $post_id = null; |
| 842 | 890 | } |
| 843 | 891 | |
| 844 | - $services = $this->get_services( 'connected' ); | |
| 845 | - $all_done = $this->post_is_done_sharing( $post_id ); | |
| 846 | - | |
| 847 | 892 | // We don't allow Publicizing to the same external id twice, to prevent spam. |
| 848 | 893 | $service_id_done = (array) get_post_meta( $post_id, $this->POST_SERVICE_DONE, true ); |
| 849 | 894 | |
| 850 | - foreach ( $services as $service_name => $connections ) { | |
| 895 | + $connections = Connections::get_all_for_user(); | |
| 896 | + | |
| 897 | + if ( ! empty( $connections ) ) { | |
| 898 | + | |
| 851 | 899 | foreach ( $connections as $connection ) { |
| 852 | - $connection_meta = $this->get_connection_meta( $connection ); | |
| 853 | - $connection_data = $connection_meta['connection_data']; | |
| 854 | - $unique_id = $this->get_connection_unique_id( $connection ); | |
| 855 | - $connection_id = $this->get_connection_id( $connection ); | |
| 900 | + $service_name = $connection['service_name']; | |
| 901 | + $unique_id = $connection['id']; | |
| 902 | + $connection_id = $connection['connection_id']; | |
| 856 | 903 | // Was this connection (OR, old-format service) already Publicized to? |
| 857 | 904 | $done = ! empty( $post ) && ( |
| 858 | 905 | // Flags based on token_id. |
| 859 | 906 | 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $unique_id, true ) |
| @@ -870,12 +917,12 @@ | ||
| 870 | 917 | * |
| 871 | 918 | * @param bool true Should the post be publicized to a given service? Default to true. |
| 872 | 919 | * @param int $post_id Post ID. |
| 873 | 920 | * @param string $service_name Service name. |
| 874 | - * @param array $connection_data Array of information about all Publicize details for the site. | |
| 921 | + * @param array $connection The connection data. | |
| 875 | 922 | */ |
| 876 | 923 | /* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */ |
| 877 | - if ( ! apply_filters( 'wpas_submit_post?', true, $post_id, $service_name, $connection_data ) ) { | |
| 924 | + if ( ! apply_filters( 'wpas_submit_post?', true, $post_id, $service_name, $connection ) ) { | |
| 878 | 925 | continue; |
| 879 | 926 | } |
| 880 | 927 | |
| 881 | 928 | // Should we be skipping this one? |
| @@ -892,22 +939,17 @@ | ||
| 892 | 939 | // Flags based on the connection_id. |
| 893 | 940 | get_post_meta( $post->ID, $this->POST_SKIP_PUBLICIZE . $connection_id, true ) |
| 894 | 941 | || |
| 895 | 942 | // Old flags. |
| 896 | - get_post_meta( $post->ID, $this->POST_SKIP . $service_name ) | |
| 943 | + get_post_meta( $post->ID, $this->POST_SKIP . $service_name, false ) | |
| 897 | 944 | ) |
| 898 | 945 | ) |
| 899 | 946 | || |
| 900 | 947 | ( |
| 901 | - is_array( $connection ) | |
| 902 | - && | |
| 903 | - isset( $connection_meta['external_id'] ) && ! empty( $service_id_done[ $service_name ][ $connection_meta['external_id'] ] ) | |
| 948 | + isset( $connection['external_id'] ) && ! empty( $service_id_done[ $service_name ][ $connection['external_id'] ] ) | |
| 904 | 949 | ) |
| 905 | 950 | ); |
| 906 | 951 | |
| 907 | - // If this one has already been publicized to, don't let it happen again. | |
| 908 | - $toggleable = ! $done && ! $all_done; | |
| 909 | - | |
| 910 | 952 | // Determine the state of the checkbox (on/off) and allow filtering. |
| 911 | 953 | $enabled = $done || ! $skip; |
| 912 | 954 | /** |
| 913 | 955 | * Filter the checkbox state of each Publicize connection appearing in the post editor. |
| @@ -922,14 +964,11 @@ | ||
| 922 | 964 | */ |
| 923 | 965 | $enabled = apply_filters( 'publicize_checkbox_default', $enabled, $post_id, $service_name, $connection ); |
| 924 | 966 | |
| 925 | 967 | /** |
| 926 | - * If this is a global connection and this user doesn't have enough permissions to modify | |
| 927 | - * those connections, don't let them change it. | |
| 968 | + * If this is a shared connection and this user doesn't have enough permissions to modify. | |
| 928 | 969 | */ |
| 929 | - if ( ! $done && $this->is_global_connection( $connection_meta ) && ! current_user_can( $this->GLOBAL_CAP ) ) { | |
| 930 | - $toggleable = false; | |
| 931 | - | |
| 970 | + if ( ! $done && Connections::is_shared( $connection ) && ! current_user_can( $this->GLOBAL_CAP ) ) { | |
| 932 | 971 | /** |
| 933 | 972 | * Filters the checkboxes for global connections with non-prilvedged users. |
| 934 | 973 | * |
| 935 | 974 | * @since 0.1.0 |
| @@ -947,22 +986,14 @@ | ||
| 947 | 986 | if ( $done ) { |
| 948 | 987 | $enabled = true; |
| 949 | 988 | } |
| 950 | 989 | |
| 951 | - $connection_list[] = array( | |
| 952 | - 'id' => $connection_id, | |
| 953 | - 'unique_id' => $unique_id, | |
| 954 | - 'service_name' => $service_name, | |
| 955 | - 'service_label' => static::get_service_label( $service_name ), | |
| 956 | - 'display_name' => $this->get_display_name( $service_name, $connection ), | |
| 957 | - 'username' => $this->get_username( $service_name, $connection ), | |
| 958 | - 'profile_picture' => $this->get_profile_picture( $connection ), | |
| 959 | - 'enabled' => $enabled, | |
| 960 | - 'done' => $done, | |
| 961 | - 'toggleable' => $toggleable, | |
| 962 | - 'global' => 0 == $connection_data['user_id'], // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual,WordPress.PHP.StrictComparisons.LooseComparison -- Other types can be used at times. | |
| 963 | - 'external_id' => $connection_meta['external_id'] ?? '', | |
| 964 | - 'user_id' => $connection_data['user_id'], | |
| 990 | + $connection_list[] = array_merge( | |
| 991 | + $connection, | |
| 992 | + array( | |
| 993 | + 'enabled' => $enabled, | |
| 994 | + 'done' => $done, | |
| 995 | + ) | |
| 965 | 996 | ); |
| 966 | 997 | } |
| 967 | 998 | } |
| 968 | 999 | |
| @@ -981,37 +1012,20 @@ | ||
| 981 | 1012 | */ |
| 982 | 1013 | abstract public function post_is_done_sharing( $post_id = null ); |
| 983 | 1014 | |
| 984 | 1015 | /** |
| 985 | - * Retrieves full list of available Publicize connection services. | |
| 1016 | + * Retrieve the available Publicize services and their connect URLs. | |
| 986 | 1017 | * |
| 987 | - * Retrieves current available publicize service connections | |
| 988 | - * with associated labels and URLs. | |
| 989 | - * | |
| 990 | 1018 | * @since 0.1.0 |
| 991 | 1019 | * @since-jetpack 6.7.0 |
| 1020 | + * @deprecated 0.88.0 Services and their connect URLs come from the `publicize/services` REST endpoint. | |
| 992 | 1021 | * |
| 993 | - * @return array { | |
| 994 | - * Array of UI service connection data for all services | |
| 995 | - * | |
| 996 | - * @type string 'name' Name of service. | |
| 997 | - * @type string 'label' Display label for service. | |
| 998 | - * @type string 'url' URL for adding connection to service. | |
| 999 | - * } | |
| 1022 | + * @return array Always empty. | |
| 1000 | 1023 | */ |
| 1001 | 1024 | public function get_available_service_data() { |
| 1002 | - $available_services = $this->get_services( 'all' ); | |
| 1003 | - $available_service_data = array(); | |
| 1025 | + _deprecated_function( __METHOD__, 'publicize-0.88.0' ); | |
| 1004 | 1026 | |
| 1005 | - foreach ( $available_services as $service_name => $service ) { | |
| 1006 | - $available_service_data[] = array( | |
| 1007 | - 'name' => $service_name, | |
| 1008 | - 'label' => static::get_service_label( $service_name ), | |
| 1009 | - 'url' => $this->connect_url( $service_name ), | |
| 1010 | - ); | |
| 1011 | - } | |
| 1012 | - | |
| 1013 | - return $available_service_data; | |
| 1027 | + return array(); | |
| 1014 | 1028 | } |
| 1015 | 1029 | |
| 1016 | 1030 | /** |
| 1017 | 1031 | * Site Data |
| @@ -1100,13 +1114,20 @@ | ||
| 1100 | 1114 | * |
| 1101 | 1115 | * Registers for each post type that with `publicize` feature support. |
| 1102 | 1116 | */ |
| 1103 | 1117 | public function register_post_meta() { |
| 1118 | + /* | |
| 1119 | + * Default the share-message meta to the saved global template | |
| 1120 | + */ | |
| 1121 | + $message_default = $this->has_paid_features() | |
| 1122 | + ? ( new Jetpack_Social_Settings\Settings() )->get_message_template() | |
| 1123 | + : ''; | |
| 1124 | + | |
| 1104 | 1125 | $message_args = array( |
| 1105 | 1126 | 'type' => 'string', |
| 1106 | 1127 | 'description' => __( 'The message to use instead of the title when sharing to Jetpack Social services', 'jetpack-publicize-pkg' ), |
| 1107 | 1128 | 'single' => true, |
| 1108 | - 'default' => '', | |
| 1129 | + 'default' => $message_default, | |
| 1109 | 1130 | 'show_in_rest' => array( |
| 1110 | 1131 | 'name' => 'jetpack_publicize_message', |
| 1111 | 1132 | ), |
| 1112 | 1133 | 'auth_callback' => array( $this, 'message_meta_auth_callback' ), |
| @@ -1139,11 +1160,14 @@ | ||
| 1139 | 1160 | 'description' => __( 'Post options related to Jetpack Social.', 'jetpack-publicize-pkg' ), |
| 1140 | 1161 | 'single' => true, |
| 1141 | 1162 | 'default' => array( |
| 1142 | 1163 | 'image_generator_settings' => array( |
| 1143 | - 'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(), | |
| 1144 | - 'enabled' => false, | |
| 1164 | + 'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(), | |
| 1165 | + 'default_image_id' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_image_id(), | |
| 1166 | + 'font' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_font(), | |
| 1167 | + 'enabled' => false, | |
| 1145 | 1168 | ), |
| 1169 | + 'version' => 2, | |
| 1146 | 1170 | ), |
| 1147 | 1171 | 'show_in_rest' => array( |
| 1148 | 1172 | 'name' => 'jetpack_social_options', |
| 1149 | 1173 | 'schema' => array( |
| @@ -1148,9 +1172,12 @@ | ||
| 1148 | 1172 | 'name' => 'jetpack_social_options', |
| 1149 | 1173 | 'schema' => array( |
| 1150 | 1174 | 'type' => 'object', |
| 1151 | 1175 | 'properties' => array( |
| 1152 | - 'attached_media' => array( | |
| 1176 | + 'version' => array( | |
| 1177 | + 'type' => 'number', | |
| 1178 | + ), | |
| 1179 | + 'attached_media' => array( | |
| 1153 | 1180 | 'type' => 'array', |
| 1154 | 1181 | 'items' => array( |
| 1155 | 1182 | 'type' => 'object', |
| 1156 | 1183 | 'properties' => array( |
| @@ -1165,34 +1192,41 @@ | ||
| 1165 | 1192 | ), |
| 1166 | 1193 | ), |
| 1167 | 1194 | ), |
| 1168 | 1195 | ), |
| 1169 | - 'should_upload_attached_media' => array( | |
| 1170 | - 'type' => 'boolean', | |
| 1171 | - ), | |
| 1172 | - 'image_generator_settings' => array( | |
| 1196 | + 'image_generator_settings' => array( | |
| 1173 | 1197 | 'type' => 'object', |
| 1174 | 1198 | 'properties' => array( |
| 1175 | - 'enabled' => array( | |
| 1199 | + 'enabled' => array( | |
| 1176 | 1200 | 'type' => 'boolean', |
| 1177 | 1201 | ), |
| 1178 | - 'custom_text' => array( | |
| 1202 | + 'custom_text' => array( | |
| 1179 | 1203 | 'type' => 'string', |
| 1180 | 1204 | ), |
| 1181 | - 'image_type' => array( | |
| 1205 | + 'image_type' => array( | |
| 1182 | 1206 | 'type' => 'string', |
| 1183 | 1207 | ), |
| 1184 | - 'image_id' => array( | |
| 1208 | + 'image_id' => array( | |
| 1185 | 1209 | 'type' => 'number', |
| 1186 | 1210 | ), |
| 1187 | - 'template' => array( | |
| 1211 | + 'template' => array( | |
| 1188 | 1212 | 'type' => 'string', |
| 1189 | 1213 | ), |
| 1190 | - 'token' => array( | |
| 1214 | + 'font' => array( | |
| 1191 | 1215 | 'type' => 'string', |
| 1192 | 1216 | ), |
| 1217 | + 'token' => array( | |
| 1218 | + 'type' => 'string', | |
| 1219 | + ), | |
| 1220 | + 'default_image_id' => array( | |
| 1221 | + 'type' => 'number', | |
| 1222 | + ), | |
| 1193 | 1223 | ), |
| 1194 | 1224 | ), |
| 1225 | + 'media_source' => array( | |
| 1226 | + 'type' => 'string', | |
| 1227 | + 'enum' => array( 'featured-image', 'sig', 'media-library', 'upload-video', 'none' ), | |
| 1228 | + ), | |
| 1195 | 1229 | ), |
| 1196 | 1230 | ), |
| 1197 | 1231 | ), |
| 1198 | 1232 | 'auth_callback' => array( $this, 'message_meta_auth_callback' ), |
| @@ -1197,8 +1231,60 @@ | ||
| 1197 | 1231 | ), |
| 1198 | 1232 | 'auth_callback' => array( $this, 'message_meta_auth_callback' ), |
| 1199 | 1233 | ); |
| 1200 | 1234 | |
| 1235 | + $image_focal_point_args = array( | |
| 1236 | + 'type' => 'object', | |
| 1237 | + 'description' => __( 'The focal point of the image, used to crop social share variants.', 'jetpack-publicize-pkg' ), | |
| 1238 | + 'single' => true, | |
| 1239 | + 'default' => array( | |
| 1240 | + 'x' => 0.5, | |
| 1241 | + 'y' => 0.5, | |
| 1242 | + ), | |
| 1243 | + 'show_in_rest' => array( | |
| 1244 | + 'schema' => array( | |
| 1245 | + 'type' => 'object', | |
| 1246 | + 'required' => array( 'x', 'y' ), | |
| 1247 | + 'properties' => array( | |
| 1248 | + 'x' => array( | |
| 1249 | + 'type' => 'number', | |
| 1250 | + 'minimum' => 0, | |
| 1251 | + 'maximum' => 1, | |
| 1252 | + ), | |
| 1253 | + 'y' => array( | |
| 1254 | + 'type' => 'number', | |
| 1255 | + 'minimum' => 0, | |
| 1256 | + 'maximum' => 1, | |
| 1257 | + ), | |
| 1258 | + ), | |
| 1259 | + 'additionalProperties' => false, | |
| 1260 | + ), | |
| 1261 | + ), | |
| 1262 | + 'auth_callback' => array( $this, 'image_focal_point_auth_callback' ), | |
| 1263 | + ); | |
| 1264 | + | |
| 1265 | + $connection_overrides_args = array( | |
| 1266 | + 'type' => 'object', | |
| 1267 | + 'description' => __( 'Per-connection customizations for message and media.', 'jetpack-publicize-pkg' ), | |
| 1268 | + 'single' => true, | |
| 1269 | + 'default' => array(), | |
| 1270 | + 'auth_callback' => array( $this, 'message_meta_auth_callback' ), | |
| 1271 | + ); | |
| 1272 | + | |
| 1273 | + $customize_per_network_default = ( | |
| 1274 | + $this->has_paid_features() | |
| 1275 | + && $this->any_connection_has_custom_template() | |
| 1276 | + ); | |
| 1277 | + | |
| 1278 | + $customize_per_network_args = array( | |
| 1279 | + 'type' => 'boolean', | |
| 1280 | + 'description' => __( 'Whether to enable per-network customization.', 'jetpack-publicize-pkg' ), | |
| 1281 | + 'single' => true, | |
| 1282 | + 'default' => $customize_per_network_default, | |
| 1283 | + 'show_in_rest' => $this->has_paid_features(), | |
| 1284 | + 'auth_callback' => array( $this, 'message_meta_auth_callback' ), | |
| 1285 | + ); | |
| 1286 | + | |
| 1201 | 1287 | foreach ( get_post_types() as $post_type ) { |
| 1202 | 1288 | if ( ! $this->post_type_is_publicizeable( $post_type ) ) { |
| 1203 | 1289 | continue; |
| 1204 | 1290 | } |
| @@ -1206,17 +1292,120 @@ | ||
| 1206 | 1292 | $message_args['object_subtype'] = $post_type; |
| 1207 | 1293 | $publicize_feature_enable_args['object_subtype'] = $post_type; |
| 1208 | 1294 | $already_shared_flag_args['object_subtype'] = $post_type; |
| 1209 | 1295 | $jetpack_social_options_args['object_subtype'] = $post_type; |
| 1296 | + $connection_overrides_args['object_subtype'] = $post_type; | |
| 1297 | + $customize_per_network_args['object_subtype'] = $post_type; | |
| 1210 | 1298 | |
| 1211 | 1299 | register_meta( 'post', $this->POST_MESS, $message_args ); |
| 1212 | 1300 | register_meta( 'post', self::POST_PUBLICIZE_FEATURE_ENABLED, $publicize_feature_enable_args ); |
| 1213 | 1301 | register_meta( 'post', $this->POST_DONE . 'all', $already_shared_flag_args ); |
| 1214 | 1302 | register_meta( 'post', self::POST_JETPACK_SOCIAL_OPTIONS, $jetpack_social_options_args ); |
| 1303 | + register_meta( 'post', self::POST_CONNECTION_OVERRIDES, $connection_overrides_args ); | |
| 1304 | + register_meta( 'post', self::POST_CUSTOMIZE_PER_NETWORK, $customize_per_network_args ); | |
| 1215 | 1305 | } |
| 1306 | + | |
| 1307 | + // The focal point lives on the image (attachment), not the post, so it is shared | |
| 1308 | + // by every post that uses the image. Registered once, not per publicizeable type. | |
| 1309 | + register_post_meta( 'attachment', self::ATTACHMENT_IMAGE_FOCAL_POINT, $image_focal_point_args ); | |
| 1216 | 1310 | } |
| 1217 | 1311 | |
| 1218 | 1312 | /** |
| 1313 | + * Collapse duplicate rows of a single-value Publicize post meta key into one on update. | |
| 1314 | + * | |
| 1315 | + * With several rows core skips its "unchanged" check, so an UPDATE that matches no changed row fails. | |
| 1316 | + * | |
| 1317 | + * @param null|bool $check Whether to short-circuit the update. | |
| 1318 | + * @param int $object_id Post ID. | |
| 1319 | + * @param string $meta_key Meta key. | |
| 1320 | + * @param mixed $meta_value Unslashed meta value. | |
| 1321 | + * @param mixed $prev_value Previous value to match, if any. | |
| 1322 | + * @return null|bool Null to let core update the meta, otherwise whether the update succeeded. | |
| 1323 | + */ | |
| 1324 | + public function collapse_duplicate_post_meta( $check, $object_id, $meta_key, $meta_value, $prev_value ) { | |
| 1325 | + // Cheap bail first: this runs on every post meta update site-wide. | |
| 1326 | + if ( null !== $check || ! empty( $prev_value ) || 0 !== strncmp( $meta_key, '_wpas_', 6 ) ) { | |
| 1327 | + return $check; | |
| 1328 | + } | |
| 1329 | + | |
| 1330 | + $single_keys = array( | |
| 1331 | + $this->POST_MESS, | |
| 1332 | + self::POST_PUBLICIZE_FEATURE_ENABLED, | |
| 1333 | + $this->POST_DONE . 'all', | |
| 1334 | + self::POST_JETPACK_SOCIAL_OPTIONS, | |
| 1335 | + self::POST_CONNECTION_OVERRIDES, | |
| 1336 | + self::POST_CUSTOMIZE_PER_NETWORK, | |
| 1337 | + ); | |
| 1338 | + if ( ! in_array( $meta_key, $single_keys, true ) ) { | |
| 1339 | + return $check; | |
| 1340 | + } | |
| 1341 | + | |
| 1342 | + $stored = get_metadata_raw( 'post', $object_id, $meta_key, false ); | |
| 1343 | + if ( ! is_array( $stored ) || count( $stored ) < 2 ) { | |
| 1344 | + return $check; | |
| 1345 | + } | |
| 1346 | + | |
| 1347 | + global $wpdb; | |
| 1348 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Row IDs aren't exposed by the meta API. | |
| 1349 | + $rows = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s ORDER BY meta_id ASC", $object_id, $meta_key ) ); | |
| 1350 | + if ( ! is_array( $rows ) || count( $rows ) < 2 ) { | |
| 1351 | + return $check; | |
| 1352 | + } | |
| 1353 | + | |
| 1354 | + // Bail before deleting, so a failed write leaves every row intact. | |
| 1355 | + $keep = array_shift( $rows ); | |
| 1356 | + // Cast like `$wpdb` does, e.g. `true` is stored as "1". | |
| 1357 | + $wanted = (string) maybe_serialize( $meta_value ); | |
| 1358 | + if ( $wanted !== $keep->meta_value && ! update_metadata_by_mid( 'post', (int) $keep->meta_id, $meta_value ) ) { | |
| 1359 | + // A 0-row UPDATE (e.g. a concurrent collapse) looks like a failure, so confirm the row. | |
| 1360 | + $current = get_metadata_by_mid( 'post', (int) $keep->meta_id ); | |
| 1361 | + if ( ! $current || $wanted !== (string) maybe_serialize( $current->meta_value ) ) { | |
| 1362 | + return false; | |
| 1363 | + } | |
| 1364 | + } | |
| 1365 | + | |
| 1366 | + // The kept row holds the value, so a redundant row that is already gone is not a failure. | |
| 1367 | + foreach ( $rows as $row ) { | |
| 1368 | + delete_metadata_by_mid( 'post', (int) $row->meta_id ); | |
| 1369 | + } | |
| 1370 | + | |
| 1371 | + // The 0-row and already-deleted paths skip core's cache flush. | |
| 1372 | + wp_cache_delete( $object_id, 'post_meta' ); | |
| 1373 | + | |
| 1374 | + // Unlike core, report an unchanged value as success, which is what lets the REST save complete. | |
| 1375 | + return true; | |
| 1376 | + } | |
| 1377 | + | |
| 1378 | + /** | |
| 1379 | + * Auth callback for the image focal point attachment meta. | |
| 1380 | + * | |
| 1381 | + * Writing the focal point edits the image, so it requires edit rights on the attachment. | |
| 1382 | + * | |
| 1383 | + * @param bool $allowed Whether the user can edit the meta. Unused; recomputed here. | |
| 1384 | + * @param string $meta_key The meta key. Unused. | |
| 1385 | + * @param int $object_id The attachment ID. | |
| 1386 | + * @return bool | |
| 1387 | + */ | |
| 1388 | + public function image_focal_point_auth_callback( $allowed, $meta_key, $object_id ) { | |
| 1389 | + return current_user_can( 'edit_post', $object_id ); | |
| 1390 | + } | |
| 1391 | + | |
| 1392 | + /** | |
| 1393 | + * Whether any connection available to the current user has a custom message template. | |
| 1394 | + * | |
| 1395 | + * @return bool | |
| 1396 | + */ | |
| 1397 | + protected function any_connection_has_custom_template() { | |
| 1398 | + foreach ( Connections::get_all_for_user() as $connection ) { | |
| 1399 | + if ( '' !== trim( (string) ( $connection['template'] ?? '' ) ) ) { | |
| 1400 | + return true; | |
| 1401 | + } | |
| 1402 | + } | |
| 1403 | + | |
| 1404 | + return false; | |
| 1405 | + } | |
| 1406 | + | |
| 1407 | + /** | |
| 1219 | 1408 | * Helper function to allow us to not publicize posts in certain contexts. |
| 1220 | 1409 | * |
| 1221 | 1410 | * @param WP_Post $post Post object. |
| 1222 | 1411 | */ |
| @@ -1248,15 +1437,11 @@ | ||
| 1248 | 1437 | |
| 1249 | 1438 | // - API/XML-RPC Test Posts |
| 1250 | 1439 | if ( |
| 1251 | 1440 | ( |
| 1252 | - defined( 'XMLRPC_REQUEST' ) | |
| 1253 | - && | |
| 1254 | - XMLRPC_REQUEST | |
| 1441 | + ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) | |
| 1255 | 1442 | || |
| 1256 | - defined( 'APP_REQUEST' ) | |
| 1257 | - && | |
| 1258 | - APP_REQUEST | |
| 1443 | + ( defined( 'APP_REQUEST' ) && constant( 'APP_REQUEST' ) ) | |
| 1259 | 1444 | ) |
| 1260 | 1445 | && |
| 1261 | 1446 | str_starts_with( $post->post_title, 'Temporary Post Used For Theme Detection' ) |
| 1262 | 1447 | ) { |
| @@ -1295,9 +1480,9 @@ | ||
| 1295 | 1480 | |
| 1296 | 1481 | $submit_post = $this->should_submit_post_pre_checks( $post ); |
| 1297 | 1482 | |
| 1298 | 1483 | // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're only checking if a value is set |
| 1299 | - $admin_page = isset( $_POST[ $this->ADMIN_PAGE ] ) ? $_POST[ $this->ADMIN_PAGE ] : null; | |
| 1484 | + $admin_page = $_POST[ $this->ADMIN_PAGE ] ?? null; | |
| 1300 | 1485 | |
| 1301 | 1486 | // Did this request happen via wp-admin? |
| 1302 | 1487 | $from_web = isset( $_SERVER['REQUEST_METHOD'] ) |
| 1303 | 1488 | && |
| @@ -1307,14 +1492,10 @@ | ||
| 1307 | 1492 | |
| 1308 | 1493 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 1309 | 1494 | $title = isset( $_POST['wpas_title'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpas_title'] ) ) : null; |
| 1310 | 1495 | |
| 1311 | - if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && $title ) { | |
| 1312 | - if ( empty( $title ) ) { | |
| 1313 | - delete_post_meta( $post_id, $this->POST_MESS ); | |
| 1314 | - } else { | |
| 1315 | - update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) ); | |
| 1316 | - } | |
| 1496 | + if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && ! empty( $title ) ) { | |
| 1497 | + update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) ); | |
| 1317 | 1498 | } |
| 1318 | 1499 | |
| 1319 | 1500 | // Change current user to provide context for get_services() if we're running during cron. |
| 1320 | 1501 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| @@ -1434,13 +1615,8 @@ | ||
| 1434 | 1615 | } |
| 1435 | 1616 | |
| 1436 | 1617 | $labels = array(); |
| 1437 | 1618 | foreach ( $services as $service_name => $display_names ) { |
| 1438 | - // Twitter connections do not trigger Publicize anymore. Skip. | |
| 1439 | - if ( 'Twitter' === $service_name ) { | |
| 1440 | - continue; | |
| 1441 | - } | |
| 1442 | - | |
| 1443 | 1619 | $labels[] = sprintf( |
| 1444 | 1620 | /* translators: Service name is %1$s, and account name is %2$s. */ |
| 1445 | 1621 | esc_html__( '%1$s (%2$s)', 'jetpack-publicize-pkg' ), |
| 1446 | 1622 | esc_html( $service_name ), |
| @@ -1585,9 +1761,9 @@ | ||
| 1585 | 1761 | if ( ! wp_attachment_is_image( $media_id ) ) { |
| 1586 | 1762 | return array(); |
| 1587 | 1763 | } |
| 1588 | 1764 | |
| 1589 | - $image = wp_get_attachment_image_src( $media_id, array( 1200 ) ); | |
| 1765 | + $image = wp_get_attachment_image_src( $media_id, array( 1200, 1200 ) ); | |
| 1590 | 1766 | |
| 1591 | 1767 | if ( ! $image ) { |
| 1592 | 1768 | return array(); |
| 1593 | 1769 | } |
| @@ -1627,8 +1803,18 @@ | ||
| 1627 | 1803 | if ( $attached_media ) { |
| 1628 | 1804 | return $attached_media; |
| 1629 | 1805 | } |
| 1630 | 1806 | |
| 1807 | + $featured_image_id = get_post_thumbnail_id( $post_id ); | |
| 1808 | + | |
| 1809 | + if ( $featured_image_id && Current_Plan::supports( 'social-image-focal-point' ) ) { | |
| 1810 | + $featured_image = Focal_Point::get_cropped_image( $featured_image_id ); | |
| 1811 | + | |
| 1812 | + if ( $featured_image ) { | |
| 1813 | + return $featured_image; | |
| 1814 | + } | |
| 1815 | + } | |
| 1816 | + | |
| 1631 | 1817 | return array(); |
| 1632 | 1818 | } |
| 1633 | 1819 | |
| 1634 | 1820 | /** |
| @@ -1633,75 +1819,49 @@ | ||
| 1633 | 1819 | |
| 1634 | 1820 | /** |
| 1635 | 1821 | * Returns the image size in bytes of a remote image. |
| 1636 | 1822 | * |
| 1823 | + * @deprecated 0.66.3 | |
| 1824 | + * | |
| 1637 | 1825 | * @param string $image_url Image URL. |
| 1638 | 1826 | * @return integer|null $bytes Image size in bytes, or null if request failed. |
| 1639 | 1827 | */ |
| 1640 | - public function get_remote_filesize( $image_url ) { | |
| 1641 | - $response = wp_remote_get( $image_url, array( 'method' => 'HEAD' ) ); | |
| 1828 | + public function get_remote_filesize( $image_url ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1829 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1642 | 1830 | |
| 1643 | - if ( is_wp_error( $response ) ) { | |
| 1644 | - return null; | |
| 1645 | - } | |
| 1646 | - | |
| 1647 | - $size = wp_remote_retrieve_header( $response, 'content-length' ); | |
| 1648 | - | |
| 1649 | - return ! empty( $size ) ? $size : null; | |
| 1831 | + return null; | |
| 1650 | 1832 | } |
| 1651 | 1833 | |
| 1652 | 1834 | /** |
| 1653 | 1835 | * Returns the resized Photon URL for a given image. |
| 1654 | 1836 | * |
| 1837 | + * @deprecated 0.66.3 | |
| 1838 | + * | |
| 1655 | 1839 | * @param string $image_url Image URL. |
| 1656 | 1840 | * @param int $width Image width. |
| 1657 | 1841 | * @param int $height Image height. |
| 1658 | 1842 | * @return string |
| 1659 | 1843 | */ |
| 1660 | - public function get_resized_image_url( $image_url, $width, $height ) { | |
| 1661 | - return apply_filters( | |
| 1662 | - 'jetpack_photon_url', | |
| 1663 | - $image_url, | |
| 1664 | - array( | |
| 1665 | - 'w' => $width, | |
| 1666 | - 'h' => $height, | |
| 1667 | - ) | |
| 1668 | - ); | |
| 1844 | + public function get_resized_image_url( $image_url, $width, $height ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1845 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1846 | + | |
| 1847 | + return $image_url; | |
| 1669 | 1848 | } |
| 1670 | 1849 | |
| 1671 | 1850 | /** |
| 1672 | 1851 | * This function runs the image through Site Accelerator to compress it, and also scales it down if needed. |
| 1673 | 1852 | * |
| 1853 | + * @deprecated 0.66.3 | |
| 1854 | + * | |
| 1674 | 1855 | * @param string $url Image URL. |
| 1675 | 1856 | * @param int $width Image width. |
| 1676 | 1857 | * @param int $height Image height. |
| 1677 | 1858 | * @return array The compressed image data. |
| 1678 | 1859 | */ |
| 1679 | - public function compress_and_scale_og_image( $url, $width, $height ) { | |
| 1680 | - // If the dimensions are fine we just run it through Site Accelerator to compress it. | |
| 1681 | - if ( 1200 >= $width && 1200 >= $height ) { | |
| 1682 | - return array( | |
| 1683 | - 'url' => $this->get_resized_image_url( $url, $width, $height ), | |
| 1684 | - 'width' => $width, | |
| 1685 | - 'height' => $height, | |
| 1686 | - ); | |
| 1687 | - } | |
| 1860 | + public function compress_and_scale_og_image( $url, $width, $height ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1861 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1688 | 1862 | |
| 1689 | - if ( $height > $width ) { | |
| 1690 | - // Portrait. | |
| 1691 | - $width = 1200 * $width / $height; | |
| 1692 | - $height = 1200; | |
| 1693 | - } else { | |
| 1694 | - // Landscape. | |
| 1695 | - $height = 1200 * $height / $width; | |
| 1696 | - $width = 1200; | |
| 1697 | - } | |
| 1698 | - | |
| 1699 | - return array( | |
| 1700 | - 'url' => $this->get_resized_image_url( $url, $width, $height ), | |
| 1701 | - 'width' => $width, | |
| 1702 | - 'height' => $height, | |
| 1703 | - ); | |
| 1863 | + return array(); | |
| 1704 | 1864 | } |
| 1705 | 1865 | |
| 1706 | 1866 | /** |
| 1707 | 1867 | * Reduce the filesize of an image by reducing the dimensions. Uses Photon. |
| @@ -1706,8 +1866,10 @@ | ||
| 1706 | 1866 | /** |
| 1707 | 1867 | * Reduce the filesize of an image by reducing the dimensions. Uses Photon. |
| 1708 | 1868 | * Returns null if the image cannot be reduced enough. |
| 1709 | 1869 | * |
| 1870 | + * @deprecated 0.66.3 | |
| 1871 | + * | |
| 1710 | 1872 | * @param string $url Image URL. |
| 1711 | 1873 | * @param int $width Image width. |
| 1712 | 1874 | * @param int $height Image height. |
| 1713 | 1875 | * @param int $filesize Image filesize. |
| @@ -1713,28 +1875,12 @@ | ||
| 1713 | 1875 | * @param int $filesize Image filesize. |
| 1714 | 1876 | * @param int $tries Number of times to try reducing the image size. Default is 5. |
| 1715 | 1877 | * @return array|null |
| 1716 | 1878 | */ |
| 1717 | - public function reduce_file_size( $url, $width, $height, $filesize, $tries = 5 ) { | |
| 1718 | - while ( $tries > 0 && $filesize > self::OG_IMAGE_MAX_FILESIZE ) { | |
| 1719 | - $width *= 0.75; | |
| 1720 | - $height *= 0.75; | |
| 1721 | - $url = $this->get_resized_image_url( $url, $width, $height ); | |
| 1722 | - $filesize = $this->get_remote_filesize( $url ); | |
| 1723 | - --$tries; | |
| 1724 | - } | |
| 1879 | + public function reduce_file_size( $url, $width, $height, $filesize, $tries = 5 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1880 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1725 | 1881 | |
| 1726 | - // If the image is still too large, we failed. | |
| 1727 | - if ( $filesize > self::OG_IMAGE_MAX_FILESIZE ) { | |
| 1728 | - // TODO: Track this to see if conversion failed. | |
| 1729 | - return null; | |
| 1730 | - } | |
| 1731 | - | |
| 1732 | - return array( | |
| 1733 | - 'url' => $url, | |
| 1734 | - 'width' => $width, | |
| 1735 | - 'height' => $height, | |
| 1736 | - ); | |
| 1882 | + return null; | |
| 1737 | 1883 | } |
| 1738 | 1884 | |
| 1739 | 1885 | /** |
| 1740 | 1886 | * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags, |
| @@ -1739,46 +1885,16 @@ | ||
| 1739 | 1885 | /** |
| 1740 | 1886 | * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags, |
| 1741 | 1887 | * or to make the Jetpack open graph images pass restrictions. |
| 1742 | 1888 | * |
| 1889 | + * @deprecated 0.66.3 use add_jetpack_social_og_images instead. | |
| 1890 | + * | |
| 1743 | 1891 | * @param array $tags Current tags. |
| 1892 | + * | |
| 1893 | + * @return array The modified tags. | |
| 1744 | 1894 | */ |
| 1745 | 1895 | public function jetpack_social_open_graph_filter( $tags ) { |
| 1746 | - $social_opengraph_image = $this->get_social_opengraph_image( get_the_ID() ); | |
| 1747 | - | |
| 1748 | - if ( ! empty( $social_opengraph_image ) ) { | |
| 1749 | - $tags = $this->add_jetpack_social_og_image( $tags, $social_opengraph_image ); | |
| 1750 | - } | |
| 1751 | - | |
| 1752 | - if ( empty( $tags['og:image'] ) || ! is_string( $tags['og:image'] ) || empty( $tags['og:image:width'] ) || empty( $tags['og:image:height'] ) ) { | |
| 1753 | - return $tags; | |
| 1754 | - } | |
| 1755 | - | |
| 1756 | - // If we do not have a SIG or attached image, but we have an image in post body | |
| 1757 | - // we need to check that the image is not too large for the social sites. | |
| 1758 | - $filesize = $this->get_remote_filesize( $tags['og:image'] ); | |
| 1759 | - // If the image is small enough, we do not need to do anything. | |
| 1760 | - if ( empty( $filesize ) || $filesize <= self::OG_IMAGE_MAX_FILESIZE ) { | |
| 1761 | - return $tags; | |
| 1762 | - } | |
| 1763 | - | |
| 1764 | - $compressed_image = $this->compress_and_scale_og_image( $tags['og:image'], $tags['og:image:width'], $tags['og:image:height'] ); | |
| 1765 | - $filesize = $this->get_remote_filesize( $compressed_image['url'] ); | |
| 1766 | - // If the compressed image is small enough, we use it. | |
| 1767 | - if ( $filesize <= self::OG_IMAGE_MAX_FILESIZE ) { | |
| 1768 | - $tags['og:image'] = $compressed_image['url']; | |
| 1769 | - $tags['og:image:width'] = $compressed_image['width']; | |
| 1770 | - $tags['og:image:height'] = $compressed_image['height']; | |
| 1771 | - return $tags; | |
| 1772 | - } | |
| 1773 | - | |
| 1774 | - $reduced_image = $this->reduce_file_size( $compressed_image['url'], $compressed_image['width'], $compressed_image['height'], $filesize ); | |
| 1775 | - if ( ! empty( $reduced_image ) ) { | |
| 1776 | - $tags['og:image'] = $reduced_image['url']; | |
| 1777 | - $tags['og:image:width'] = $reduced_image['width']; | |
| 1778 | - $tags['og:image:height'] = $reduced_image['height']; | |
| 1779 | - } | |
| 1780 | - | |
| 1896 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1781 | 1897 | return $tags; |
| 1782 | 1898 | } |
| 1783 | 1899 | |
| 1784 | 1900 | /** |
| @@ -1783,29 +1899,19 @@ | ||
| 1783 | 1899 | |
| 1784 | 1900 | /** |
| 1785 | 1901 | * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags. |
| 1786 | 1902 | * |
| 1903 | + * @deprecated 0.66.3 use add_jetpack_social_og_images instead. | |
| 1904 | + * | |
| 1787 | 1905 | * @param array $tags Current tags. |
| 1788 | 1906 | * @param array $opengraph_image The Jetpack Social image data. |
| 1907 | + * | |
| 1908 | + * @return array The modified tags. | |
| 1789 | 1909 | */ |
| 1790 | - public function add_jetpack_social_og_image( $tags, $opengraph_image ) { | |
| 1791 | - // If this code is running in Jetpack, we need to add Twitter cards. | |
| 1792 | - // Some active plugins disable Jetpack's Twitter Cards, so we need | |
| 1793 | - // to check if the class was instantiated before adding the cards. | |
| 1794 | - $needs_twitter_cards = class_exists( 'Jetpack_Twitter_Cards' ); | |
| 1910 | + public function add_jetpack_social_og_image( $tags, $opengraph_image ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 1911 | + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' ); | |
| 1795 | 1912 | |
| 1796 | - return array_merge( | |
| 1797 | - $tags, | |
| 1798 | - array( | |
| 1799 | - 'og:image' => $opengraph_image['url'], | |
| 1800 | - 'og:image:width' => $opengraph_image['width'], | |
| 1801 | - 'og:image:height' => $opengraph_image['height'], | |
| 1802 | - ), | |
| 1803 | - $needs_twitter_cards ? array( | |
| 1804 | - 'twitter:image' => $opengraph_image['url'], | |
| 1805 | - 'twitter:card' => 'summary_large_image', | |
| 1806 | - ) : array() | |
| 1807 | - ); | |
| 1913 | + return $tags; | |
| 1808 | 1914 | } |
| 1809 | 1915 | |
| 1810 | 1916 | /** |
| 1811 | 1917 | * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags. |
| @@ -1812,10 +1918,22 @@ | ||
| 1812 | 1918 | * |
| 1813 | 1919 | * @param array $tags Current tags. |
| 1814 | 1920 | */ |
| 1815 | 1921 | public function add_jetpack_social_og_images( $tags ) { |
| 1816 | - $opengraph_image = $this->get_social_opengraph_image( get_the_ID() ); | |
| 1922 | + if ( ! is_singular() ) { | |
| 1923 | + return $tags; | |
| 1924 | + } | |
| 1817 | 1925 | |
| 1926 | + $post_id = get_the_ID(); | |
| 1927 | + if ( ! $post_id ) { | |
| 1928 | + return $tags; | |
| 1929 | + } | |
| 1930 | + | |
| 1931 | + if ( ! post_type_supports( get_post_type( $post_id ), 'publicize' ) ) { | |
| 1932 | + return $tags; | |
| 1933 | + } | |
| 1934 | + | |
| 1935 | + $opengraph_image = $this->get_social_opengraph_image( $post_id ); | |
| 1818 | 1936 | if ( empty( $opengraph_image ) ) { |
| 1819 | 1937 | return $tags; |
| 1820 | 1938 | } |
| 1821 | 1939 | |
| @@ -1821,9 +1939,9 @@ | ||
| 1821 | 1939 | |
| 1822 | 1940 | // If this code is running in Jetpack, we need to add Twitter cards. |
| 1823 | 1941 | // Some active plugins disable Jetpack's Twitter Cards, so we need |
| 1824 | 1942 | // to check if the class was instantiated before adding the cards. |
| 1825 | - $needs_twitter_cards = class_exists( 'Jetpack_Twitter_Cards' ); | |
| 1943 | + $needs_twitter_cards = class_exists( \Automattic\Jetpack\Post_Media\Twitter_Cards::class ); | |
| 1826 | 1944 | |
| 1827 | 1945 | return array_merge( |
| 1828 | 1946 | $tags, |
| 1829 | 1947 | array( |
| @@ -1865,20 +1983,18 @@ | ||
| 1865 | 1983 | return str_replace( $search, $replace, $string ); |
| 1866 | 1984 | } |
| 1867 | 1985 | |
| 1868 | 1986 | /** |
| 1869 | - * Get Calypso URL for Publicize connections. | |
| 1987 | + * Get the URL to the connections management page. | |
| 1870 | 1988 | * |
| 1871 | - * @param string $source The idenfitier of the place the function is called from. | |
| 1872 | 1989 | * @return string |
| 1873 | 1990 | */ |
| 1874 | - public function publicize_connections_url( $source = 'calypso-marketing-connections' ) { | |
| 1875 | - $allowed_sources = array( 'jetpack-social-connections-admin-page', 'jetpack-social-connections-classic-editor', 'calypso-marketing-connections' ); | |
| 1876 | - $source = in_array( $source, $allowed_sources, true ) ? $source : 'calypso-marketing-connections'; | |
| 1877 | - $blog_id = Connection_Manager::get_site_id( true ); | |
| 1878 | - $site = ( new Status() )->get_site_suffix(); | |
| 1991 | + public function publicize_connections_url() { | |
| 1992 | + $has_social_admin_page = defined( 'JETPACK_SOCIAL_PLUGIN_DIR' ); | |
| 1879 | 1993 | |
| 1880 | - return Redirect::get_url( $source, array( 'site' => $blog_id ? $blog_id : $site ) ); | |
| 1994 | + $page = $has_social_admin_page ? 'jetpack-social' : 'jetpack#/sharing'; | |
| 1995 | + | |
| 1996 | + return ( new Paths() )->admin_url( array( 'page' => $page ) ); | |
| 1881 | 1997 | } |
| 1882 | 1998 | |
| 1883 | 1999 | /** |
| 1884 | 2000 | * Get the Jetpack Social info from the API. |
| @@ -1893,9 +2009,8 @@ | ||
| 1893 | 2009 | if ( isset( $api_data_response ) ) { |
| 1894 | 2010 | return ! is_wp_error( $api_data_response ) ? $api_data_response : array(); |
| 1895 | 2011 | } |
| 1896 | 2012 | |
| 1897 | - $rest_controller = new REST_Controller(); | |
| 1898 | 2013 | $response = Client::wpcom_json_api_request_as_blog( |
| 1899 | 2014 | sprintf( 'sites/%d/jetpack-social', absint( $blog_id ) ), |
| 1900 | 2015 | '2', |
| 1901 | 2016 | array( |
| @@ -1904,9 +2019,9 @@ | ||
| 1904 | 2019 | ), |
| 1905 | 2020 | null, |
| 1906 | 2021 | 'wpcom' |
| 1907 | 2022 | ); |
| 1908 | - $api_data_response = $rest_controller->make_proper_response( $response ); | |
| 2023 | + $api_data_response = Publicize_Utils::make_proper_response( $response ); | |
| 1909 | 2024 | |
| 1910 | 2025 | if ( ! is_wp_error( $api_data_response ) ) { |
| 1911 | 2026 | set_transient( $key, $api_data_response, DAY_IN_SECONDS ); |
| 1912 | 2027 | return $api_data_response; |
| @@ -1920,24 +2035,8 @@ | ||
| 1920 | 2035 | return array(); |
| 1921 | 2036 | } |
| 1922 | 2037 | |
| 1923 | 2038 | /** |
| 1924 | - * Get the Publicize shares info. | |
| 1925 | - * | |
| 1926 | - * @param string $blog_id The WPCOM blog_id for the current blog. | |
| 1927 | - * @return ?array | |
| 1928 | - */ | |
| 1929 | - public function get_publicize_shares_info( $blog_id ) { | |
| 1930 | - $data = $this->get_api_data( $blog_id ); | |
| 1931 | - | |
| 1932 | - if ( empty( $data ) ) { | |
| 1933 | - return null; | |
| 1934 | - } | |
| 1935 | - | |
| 1936 | - return $data; | |
| 1937 | - } | |
| 1938 | - | |
| 1939 | - /** | |
| 1940 | 2039 | * Check if enhanced publishing is enabled. |
| 1941 | 2040 | * |
| 1942 | 2041 | * @deprecated $$next-version use Automattic\Jetpack\Publicize\Publicize_Base\has_enhanced_publishing_feature instead. |
| 1943 | 2042 | * @param int $blog_id The blog ID for the current blog. |
| @@ -1978,8 +2077,10 @@ | ||
| 1978 | 2077 | |
| 1979 | 2078 | /** |
| 1980 | 2079 | * Check if the auto-conversion feature is one of the active features. |
| 1981 | 2080 | * |
| 2081 | + * TODO: Remove this after certain releases of Jetpack v15. | |
| 2082 | + * | |
| 1982 | 2083 | * @param string $type Whether image or video. |
| 1983 | 2084 | * |
| 1984 | 2085 | * @return bool |
| 1985 | 2086 | */ |
| @@ -2014,41 +2115,13 @@ | ||
| 2014 | 2115 | */ |
| 2015 | 2116 | public function get_supported_additional_connections() { |
| 2016 | 2117 | $additional_connections = array(); |
| 2017 | 2118 | |
| 2018 | - if ( $this->has_connection_feature( 'instagram' ) ) { | |
| 2019 | - $additional_connections[] = 'instagram-business'; | |
| 2119 | + if ( $this->has_connection_feature( 'threads' ) ) { | |
| 2120 | + $additional_connections[] = 'threads'; | |
| 2020 | 2121 | } |
| 2021 | 2122 | |
| 2022 | - if ( $this->has_connection_feature( 'mastodon' ) ) { | |
| 2023 | - $additional_connections[] = 'mastodon'; | |
| 2024 | - } | |
| 2025 | - | |
| 2026 | - if ( $this->has_connection_feature( 'nextdoor' ) ) { | |
| 2027 | - $additional_connections[] = 'nextdoor'; | |
| 2028 | - } | |
| 2029 | - | |
| 2030 | 2123 | return $additional_connections; |
| 2031 | - } | |
| 2032 | - | |
| 2033 | - /** | |
| 2034 | - * Call the WPCOM REST API to calculate the scheduled shares. | |
| 2035 | - * | |
| 2036 | - * @param string $blog_id The blog_id. | |
| 2037 | - */ | |
| 2038 | - public function calculate_scheduled_shares( $blog_id ) { | |
| 2039 | - $response = Client::wpcom_json_api_request_as_blog( | |
| 2040 | - sprintf( 'sites/%d/jetpack-social/count-scheduled-shares', absint( $blog_id ) ), | |
| 2041 | - '2', | |
| 2042 | - array( | |
| 2043 | - 'headers' => array( 'content-type' => 'application/json' ), | |
| 2044 | - 'method' => 'GET', | |
| 2045 | - ), | |
| 2046 | - null, | |
| 2047 | - 'wpcom' | |
| 2048 | - ); | |
| 2049 | - $rest_controller = new REST_Controller(); | |
| 2050 | - return $rest_controller->make_proper_response( $response ); | |
| 2051 | 2124 | } |
| 2052 | 2125 | |
| 2053 | 2126 | /** |
| 2054 | 2127 | * Check if we have a paid Jetpack Social plan. |