PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 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 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-base.php +504 -211 12.8.3 → 16.3 View file →
@@ -10,10 +10,13 @@
10 10 namespace Automattic\Jetpack\Publicize;
11 11
12 12 use Automattic\Jetpack\Connection\Client;
13 13 use Automattic\Jetpack\Current_Plan;
14 +use Automattic\Jetpack\Paths;
14 15 use Automattic\Jetpack\Redirect;
15 16 use Automattic\Jetpack\Status;
17 +use WP_Error;
18 +use WP_Post;
16 19
17 20 /**
18 21 * Base class for Publicize.
19 22 */
@@ -49,28 +52,45 @@
49 52 */
50 53 public $POST_MESS = '_wpas_mess';
51 54
52 55 /**
53 - * Post meta key for flagging when the post is a tweetstorm.
56 + * Post meta key for the flagging when the post share feature is disabled.
54 57 *
55 58 * @var string
56 59 */
57 - public $POST_TWEETSTORM = '_wpas_is_tweetstorm';
60 + const POST_PUBLICIZE_FEATURE_ENABLED = '_wpas_feature_enabled';
58 61
59 62 /**
60 - * Post meta key for the flagging when the post share feature is disabled.
63 + * Post meta key for Jetpack Social options.
61 64 *
62 65 * @var string
63 66 */
64 - const POST_PUBLICIZE_FEATURE_ENABLED = '_wpas_feature_enabled';
67 + const POST_JETPACK_SOCIAL_OPTIONS = '_wpas_options';
65 68
66 69 /**
67 - * Post meta key for Jetpack Social options.
70 + * Post meta key for per-connection customization overrides.
68 71 *
69 72 * @var string
70 73 */
71 - const POST_JETPACK_SOCIAL_OPTIONS = '_wpas_options';
74 + const POST_CONNECTION_OVERRIDES = '_wpas_connection_overrides';
72 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 +
73 93 // Skip meta keys. We used to rely on _wpas_skip_ appended with the token_id to skip posts. But to support
74 94 // multiple connections for the same token, we are going to use the _wpas_skip_publicize_ which
75 95 // will be appended with the connection_id.
76 96 /**
@@ -130,8 +150,15 @@
130 150 */
131 151 const OPTION_JETPACK_SOCIAL_DISMISSED_NOTICES = 'jetpack_social_dismissed_notices';
132 152
133 153 /**
154 + * The maximum size of an image that can be used as an Open Graph image.
155 + *
156 + * @var string
157 + */
158 + const OG_IMAGE_MAX_FILESIZE = 2000000; // 2MB.
159 +
160 + /**
134 161 * Default pieces of the message used in constructing the
135 162 * content pushed out to other social networks.
136 163 */
137 164
@@ -234,19 +261,20 @@
234 261 add_action( 'save_post', array( $this, 'save_meta' ), 20, 2 );
235 262
236 263 // Default checkbox state for each Connection.
237 264 add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 2 );
238 - 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.
239 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 +
240 269 // Alter the "Post Publish" admin notice to mention the Connections we Publicized to.
241 270 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 );
242 271
243 - // Connection test callback.
244 - add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) );
245 -
246 272 // Custom priority to ensure post type support is added prior to thumbnail support being added to the theme.
247 273 add_action( 'init', array( $this, 'add_post_type_support' ), 8 );
248 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 );
249 277
250 278 // The custom priority for this action ensures that any existing code that
251 279 // removes post-thumbnails support during 'init' continues to work.
252 280 add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 );
@@ -270,8 +298,23 @@
270 298 */
271 299 abstract public function get_services( $filter = 'all', $_blog_id = false, $_user_id = false );
272 300
273 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 + /**
274 317 * Does the given user have a connection to the service on the given blog?
275 318 *
276 319 * @param string $service_name 'facebook', 'twitter', etc.
277 320 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
@@ -287,9 +330,9 @@
287 330 $_user_id = $this->user_id();
288 331 }
289 332
290 333 $connections = $this->get_connections( $service_name, $_blog_id, $_user_id );
291 - return ( is_array( $connections ) && count( $connections ) > 0 ? true : false );
334 + return is_array( $connections ) && count( $connections ) > 0;
292 335 }
293 336
294 337 /**
295 338 * Generates a connection URL.
@@ -450,9 +493,9 @@
450 493 public function get_profile_link( $service_name, $connection ) {
451 494 $cmeta = $this->get_connection_meta( $connection );
452 495
453 496 if ( isset( $cmeta['connection_data']['meta']['link'] ) ) {
454 - if ( 'facebook' === $service_name && 0 === strpos( wp_parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) {
497 + if ( 'facebook' === $service_name && str_starts_with( wp_parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) {
455 498 // App-scoped Facebook user IDs are not usable profile links.
456 499 return false;
457 500 }
458 501
@@ -462,8 +505,37 @@
462 505 if ( 'facebook' === $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
463 506 return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page'];
464 507 }
465 508
509 + if ( 'instagram-business' === $service_name && isset( $cmeta['connection_data']['meta']['username'] ) ) {
510 + return 'https://instagram.com/' . $cmeta['connection_data']['meta']['username'];
511 + }
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 +
530 + if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) {
531 + return 'https://mastodon.social/@' . $cmeta['external_name'];
532 + }
533 +
534 + if ( 'nextdoor' === $service_name && isset( $cmeta['external_id'] ) ) {
535 + return 'https://nextdoor.com/profile/' . $cmeta['external_id'];
536 + }
537 +
466 538 if ( 'tumblr' === $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
467 539 return 'https://' . $cmeta['connection_data']['meta']['tumblr_base_hostname'];
468 540 }
469 541
@@ -470,28 +542,10 @@
470 542 if ( 'twitter' === $service_name ) {
471 543 return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'.
472 544 }
473 545
474 - if ( 'linkedin' === $service_name ) {
475 - if ( ! isset( $cmeta['connection_data']['meta']['profile_url'] ) ) {
476 - return false;
477 - }
478 -
479 - $profile_url_query = wp_parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY );
480 - $profile_url_query_args = null;
481 - wp_parse_str( $profile_url_query, $profile_url_query_args );
482 -
483 - $id = null;
484 -
485 - if ( isset( $profile_url_query_args['key'] ) ) {
486 - $id = $profile_url_query_args['key'];
487 - } elseif ( isset( $profile_url_query_args['id'] ) ) {
488 - $id = $profile_url_query_args['id'];
489 - } else {
490 - return false;
491 - }
492 -
493 - 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'];
494 548 }
495 549
496 550 return false; // no fallback. we just won't link it.
497 551 }
@@ -505,10 +559,10 @@
505 559 */
506 560 public function get_display_name( $service_name, $connection ) {
507 561 $cmeta = $this->get_connection_meta( $connection );
508 562
509 - if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) {
510 - return $cmeta['external_name'];
563 + if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) {
564 + return $cmeta['external_display'];
511 565 }
512 566
513 567 if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
514 568 return $cmeta['connection_data']['meta']['display_name'];
@@ -538,19 +592,37 @@
538 592 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
539 593 * @return string
540 594 */
541 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 ) {
542 609 $cmeta = $this->get_connection_meta( $connection );
543 610
544 - if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) {
545 - return $cmeta['external_display'];
546 - }
611 + switch ( $service_name ) {
612 + case 'mastodon':
613 + return $cmeta['external_display'] ?? null;
547 614
548 - if ( isset( $cmeta['connection_data']['meta']['username'] ) ) {
549 - 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;
550 624 }
551 -
552 - return $this->get_display_name( $service_name, $connection );
553 625 }
554 626
555 627 /**
556 628 * Returns a profile picture for the Connection
@@ -557,9 +629,9 @@
557 629 *
558 630 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
559 631 * @return string
560 632 */
561 - private function get_profile_picture( $connection ) {
633 + public function get_profile_picture( $connection ) {
562 634 $cmeta = $this->get_connection_meta( $connection );
563 635
564 636 if ( isset( $cmeta['profile_picture'] ) ) {
565 637 return $cmeta['profile_picture'];
@@ -663,17 +735,8 @@
663 735 return isset( $connection_data['meta']['options_responses'] );
664 736 }
665 737
666 738 /**
667 - * AJAX Handler to run connection tests on all Connections
668 - *
669 - * @return void
670 - */
671 - public function test_publicize_conns() {
672 - wp_send_json_success( $this->get_publicize_conns_test_results() );
673 - }
674 -
675 - /**
676 739 * Parse the error code returned by the XML-RPC API call.
677 740 *
678 741 * @param string $code Error code in numerical format.
679 742 *
@@ -811,8 +874,9 @@
811 874 * @type bool 'enabled' Default value for the connection (e.g., for a checkbox).
812 875 * @type bool 'done' Has this connection already been publicized to?
813 876 * @type bool 'toggleable' Is the user allowed to change the value for the connection?
814 877 * @type bool 'global' Is this connection a global one?
878 + * @type string 'external_id' External ID for the connection.
815 879 * }
816 880 */
817 881 public function get_filtered_connection_data( $selected_post_id = null ) {
818 882 $connection_list = array();
@@ -824,23 +888,22 @@
824 888 } else {
825 889 $post_id = null;
826 890 }
827 891
828 - $services = $this->get_services( 'connected' );
829 - $all_done = $this->post_is_done_sharing( $post_id );
830 -
831 892 // We don't allow Publicizing to the same external id twice, to prevent spam.
832 893 $service_id_done = (array) get_post_meta( $post_id, $this->POST_SERVICE_DONE, true );
833 894
834 - foreach ( $services as $service_name => $connections ) {
895 + $connections = Connections::get_all_for_user();
896 +
897 + if ( ! empty( $connections ) ) {
898 +
835 899 foreach ( $connections as $connection ) {
836 - $connection_meta = $this->get_connection_meta( $connection );
837 - $connection_data = $connection_meta['connection_data'];
838 - $unique_id = $this->get_connection_unique_id( $connection );
839 - $connection_id = $this->get_connection_id( $connection );
900 + $service_name = $connection['service_name'];
901 + $unique_id = $connection['id'];
902 + $connection_id = $connection['connection_id'];
840 903 // Was this connection (OR, old-format service) already Publicized to?
841 904 $done = ! empty( $post ) && (
842 - // Flags based on token_id
905 + // Flags based on token_id.
843 906 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $unique_id, true )
844 907 ||
845 908 // Old flags.
846 909 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $service_name, true )
@@ -854,12 +917,12 @@
854 917 *
855 918 * @param bool true Should the post be publicized to a given service? Default to true.
856 919 * @param int $post_id Post ID.
857 920 * @param string $service_name Service name.
858 - * @param array $connection_data Array of information about all Publicize details for the site.
921 + * @param array $connection The connection data.
859 922 */
860 923 /* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
861 - 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 ) ) {
862 925 continue;
863 926 }
864 927
865 928 // Should we be skipping this one?
@@ -876,22 +939,17 @@
876 939 // Flags based on the connection_id.
877 940 get_post_meta( $post->ID, $this->POST_SKIP_PUBLICIZE . $connection_id, true )
878 941 ||
879 942 // Old flags.
880 - get_post_meta( $post->ID, $this->POST_SKIP . $service_name )
943 + get_post_meta( $post->ID, $this->POST_SKIP . $service_name, false )
881 944 )
882 945 )
883 946 ||
884 947 (
885 - is_array( $connection )
886 - &&
887 - 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'] ] )
888 949 )
889 950 );
890 951
891 - // If this one has already been publicized to, don't let it happen again.
892 - $toggleable = ! $done && ! $all_done;
893 -
894 952 // Determine the state of the checkbox (on/off) and allow filtering.
895 953 $enabled = $done || ! $skip;
896 954 /**
897 955 * Filter the checkbox state of each Publicize connection appearing in the post editor.
@@ -906,14 +964,11 @@
906 964 */
907 965 $enabled = apply_filters( 'publicize_checkbox_default', $enabled, $post_id, $service_name, $connection );
908 966
909 967 /**
910 - * If this is a global connection and this user doesn't have enough permissions to modify
911 - * 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.
912 969 */
913 - if ( ! $done && $this->is_global_connection( $connection_meta ) && ! current_user_can( $this->GLOBAL_CAP ) ) {
914 - $toggleable = false;
915 -
970 + if ( ! $done && Connections::is_shared( $connection ) && ! current_user_can( $this->GLOBAL_CAP ) ) {
916 971 /**
917 972 * Filters the checkboxes for global connections with non-prilvedged users.
918 973 *
919 974 * @since 0.1.0
@@ -931,20 +986,14 @@
931 986 if ( $done ) {
932 987 $enabled = true;
933 988 }
934 989
935 - $connection_list[] = array(
936 - 'id' => $connection_id,
937 - 'unique_id' => $unique_id,
938 - 'service_name' => $service_name,
939 - 'service_label' => static::get_service_label( $service_name ),
940 - 'display_name' => $this->get_display_name( $service_name, $connection ),
941 - 'username' => $this->get_username( $service_name, $connection ),
942 - 'profile_picture' => $this->get_profile_picture( $connection ),
943 - 'enabled' => $enabled,
944 - 'done' => $done,
945 - 'toggleable' => $toggleable,
946 - 'global' => 0 == $connection_data['user_id'], // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual,WordPress.PHP.StrictComparisons.LooseComparison -- Other types can be used at times.
990 + $connection_list[] = array_merge(
991 + $connection,
992 + array(
993 + 'enabled' => $enabled,
994 + 'done' => $done,
995 + )
947 996 );
948 997 }
949 998 }
950 999
@@ -963,37 +1012,20 @@
963 1012 */
964 1013 abstract public function post_is_done_sharing( $post_id = null );
965 1014
966 1015 /**
967 - * Retrieves full list of available Publicize connection services.
1016 + * Retrieve the available Publicize services and their connect URLs.
968 1017 *
969 - * Retrieves current available publicize service connections
970 - * with associated labels and URLs.
971 - *
972 1018 * @since 0.1.0
973 1019 * @since-jetpack 6.7.0
1020 + * @deprecated 0.88.0 Services and their connect URLs come from the `publicize/services` REST endpoint.
974 1021 *
975 - * @return array {
976 - * Array of UI service connection data for all services
977 - *
978 - * @type string 'name' Name of service.
979 - * @type string 'label' Display label for service.
980 - * @type string 'url' URL for adding connection to service.
981 - * }
1022 + * @return array Always empty.
982 1023 */
983 1024 public function get_available_service_data() {
984 - $available_services = $this->get_services( 'all' );
985 - $available_service_data = array();
1025 + _deprecated_function( __METHOD__, 'publicize-0.88.0' );
986 1026
987 - foreach ( $available_services as $service_name => $service ) {
988 - $available_service_data[] = array(
989 - 'name' => $service_name,
990 - 'label' => static::get_service_label( $service_name ),
991 - 'url' => $this->connect_url( $service_name ),
992 - );
993 - }
994 -
995 - return $available_service_data;
1027 + return array();
996 1028 }
997 1029
998 1030 /**
999 1031 * Site Data
@@ -1082,13 +1114,20 @@
1082 1114 *
1083 1115 * Registers for each post type that with `publicize` feature support.
1084 1116 */
1085 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 +
1086 1125 $message_args = array(
1087 1126 'type' => 'string',
1088 1127 'description' => __( 'The message to use instead of the title when sharing to Jetpack Social services', 'jetpack-publicize-pkg' ),
1089 1128 'single' => true,
1090 - 'default' => '',
1129 + 'default' => $message_default,
1091 1130 'show_in_rest' => array(
1092 1131 'name' => 'jetpack_publicize_message',
1093 1132 ),
1094 1133 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
@@ -1093,19 +1132,8 @@
1093 1132 ),
1094 1133 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1095 1134 );
1096 1135
1097 - $tweetstorm_args = array(
1098 - 'type' => 'boolean',
1099 - 'description' => __( 'Whether or not the post should be treated as a Twitter thread.', 'jetpack-publicize-pkg' ),
1100 - 'single' => true,
1101 - 'default' => false,
1102 - 'show_in_rest' => array(
1103 - 'name' => 'jetpack_is_tweetstorm',
1104 - ),
1105 - 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1106 - );
1107 -
1108 1136 $publicize_feature_enable_args = array(
1109 1137 'type' => 'boolean',
1110 1138 'description' => __( 'Whether or not the Share Post feature is enabled.', 'jetpack-publicize-pkg' ),
1111 1139 'single' => true,
@@ -1132,11 +1160,14 @@
1132 1160 'description' => __( 'Post options related to Jetpack Social.', 'jetpack-publicize-pkg' ),
1133 1161 'single' => true,
1134 1162 'default' => array(
1135 1163 'image_generator_settings' => array(
1136 - 'template' => ( new Social_Image_Generator\Settings() )->get_default_template(),
1137 - '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,
1138 1168 ),
1169 + 'version' => 2,
1139 1170 ),
1140 1171 'show_in_rest' => array(
1141 1172 'name' => 'jetpack_social_options',
1142 1173 'schema' => array(
@@ -1141,9 +1172,12 @@
1141 1172 'name' => 'jetpack_social_options',
1142 1173 'schema' => array(
1143 1174 'type' => 'object',
1144 1175 'properties' => array(
1145 - 'attached_media' => array(
1176 + 'version' => array(
1177 + 'type' => 'number',
1178 + ),
1179 + 'attached_media' => array(
1146 1180 'type' => 'array',
1147 1181 'items' => array(
1148 1182 'type' => 'object',
1149 1183 'properties' => array(
@@ -1158,34 +1192,41 @@
1158 1192 ),
1159 1193 ),
1160 1194 ),
1161 1195 ),
1162 - 'should_upload_attached_media' => array(
1163 - 'type' => 'boolean',
1164 - ),
1165 - 'image_generator_settings' => array(
1196 + 'image_generator_settings' => array(
1166 1197 'type' => 'object',
1167 1198 'properties' => array(
1168 - 'enabled' => array(
1199 + 'enabled' => array(
1169 1200 'type' => 'boolean',
1170 1201 ),
1171 - 'custom_text' => array(
1202 + 'custom_text' => array(
1172 1203 'type' => 'string',
1173 1204 ),
1174 - 'image_type' => array(
1205 + 'image_type' => array(
1175 1206 'type' => 'string',
1176 1207 ),
1177 - 'image_id' => array(
1208 + 'image_id' => array(
1178 1209 'type' => 'number',
1179 1210 ),
1180 - 'template' => array(
1211 + 'template' => array(
1181 1212 'type' => 'string',
1182 1213 ),
1183 - 'token' => array(
1214 + 'font' => array(
1184 1215 'type' => 'string',
1185 1216 ),
1217 + 'token' => array(
1218 + 'type' => 'string',
1219 + ),
1220 + 'default_image_id' => array(
1221 + 'type' => 'number',
1222 + ),
1186 1223 ),
1187 1224 ),
1225 + 'media_source' => array(
1226 + 'type' => 'string',
1227 + 'enum' => array( 'featured-image', 'sig', 'media-library', 'upload-video', 'none' ),
1228 + ),
1188 1229 ),
1189 1230 ),
1190 1231 ),
1191 1232 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
@@ -1190,8 +1231,60 @@
1190 1231 ),
1191 1232 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1192 1233 );
1193 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 +
1194 1287 foreach ( get_post_types() as $post_type ) {
1195 1288 if ( ! $this->post_type_is_publicizeable( $post_type ) ) {
1196 1289 continue;
1197 1290 }
@@ -1196,22 +1289,123 @@
1196 1289 continue;
1197 1290 }
1198 1291
1199 1292 $message_args['object_subtype'] = $post_type;
1200 - $tweetstorm_args['object_subtype'] = $post_type;
1201 1293 $publicize_feature_enable_args['object_subtype'] = $post_type;
1202 1294 $already_shared_flag_args['object_subtype'] = $post_type;
1203 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;
1204 1298
1205 1299 register_meta( 'post', $this->POST_MESS, $message_args );
1206 - register_meta( 'post', $this->POST_TWEETSTORM, $tweetstorm_args );
1207 1300 register_meta( 'post', self::POST_PUBLICIZE_FEATURE_ENABLED, $publicize_feature_enable_args );
1208 1301 register_meta( 'post', $this->POST_DONE . 'all', $already_shared_flag_args );
1209 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 );
1210 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 );
1211 1310 }
1212 1311
1213 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 + /**
1214 1408 * Helper function to allow us to not publicize posts in certain contexts.
1215 1409 *
1216 1410 * @param WP_Post $post Post object.
1217 1411 */
@@ -1243,18 +1437,14 @@
1243 1437
1244 1438 // - API/XML-RPC Test Posts
1245 1439 if (
1246 1440 (
1247 - defined( 'XMLRPC_REQUEST' )
1248 - &&
1249 - XMLRPC_REQUEST
1441 + ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
1250 1442 ||
1251 - defined( 'APP_REQUEST' )
1252 - &&
1253 - APP_REQUEST
1443 + ( defined( 'APP_REQUEST' ) && constant( 'APP_REQUEST' ) )
1254 1444 )
1255 1445 &&
1256 - 0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' )
1446 + str_starts_with( $post->post_title, 'Temporary Post Used For Theme Detection' )
1257 1447 ) {
1258 1448 $submit_post = false;
1259 1449 }
1260 1450
@@ -1290,9 +1480,9 @@
1290 1480
1291 1481 $submit_post = $this->should_submit_post_pre_checks( $post );
1292 1482
1293 1483 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're only checking if a value is set
1294 - $admin_page = isset( $_POST[ $this->ADMIN_PAGE ] ) ? $_POST[ $this->ADMIN_PAGE ] : null;
1484 + $admin_page = $_POST[ $this->ADMIN_PAGE ] ?? null;
1295 1485
1296 1486 // Did this request happen via wp-admin?
1297 1487 $from_web = isset( $_SERVER['REQUEST_METHOD'] )
1298 1488 &&
@@ -1302,14 +1492,10 @@
1302 1492
1303 1493 // phpcs:ignore WordPress.Security.NonceVerification.Missing
1304 1494 $title = isset( $_POST['wpas_title'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpas_title'] ) ) : null;
1305 1495
1306 - if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && $title ) {
1307 - if ( empty( $title ) ) {
1308 - delete_post_meta( $post_id, $this->POST_MESS );
1309 - } else {
1310 - update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) );
1311 - }
1496 + if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && ! empty( $title ) ) {
1497 + update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) );
1312 1498 }
1313 1499
1314 1500 // Change current user to provide context for get_services() if we're running during cron.
1315 1501 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
@@ -1429,13 +1615,8 @@
1429 1615 }
1430 1616
1431 1617 $labels = array();
1432 1618 foreach ( $services as $service_name => $display_names ) {
1433 - // Twitter connections do not trigger Publicize anymore. Skip.
1434 - if ( 'Twitter' === $service_name ) {
1435 - continue;
1436 - }
1437 -
1438 1619 $labels[] = sprintf(
1439 1620 /* translators: Service name is %1$s, and account name is %2$s. */
1440 1621 esc_html__( '%1$s (%2$s)', 'jetpack-publicize-pkg' ),
1441 1622 esc_html( $service_name ),
@@ -1580,9 +1761,9 @@
1580 1761 if ( ! wp_attachment_is_image( $media_id ) ) {
1581 1762 return array();
1582 1763 }
1583 1764
1584 - $image = wp_get_attachment_image_src( $media_id, array( 1200 ) );
1765 + $image = wp_get_attachment_image_src( $media_id, array( 1200, 1200 ) );
1585 1766
1586 1767 if ( ! $image ) {
1587 1768 return array();
1588 1769 }
@@ -1622,19 +1803,137 @@
1622 1803 if ( $attached_media ) {
1623 1804 return $attached_media;
1624 1805 }
1625 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 +
1626 1817 return array();
1627 1818 }
1628 1819
1629 1820 /**
1821 + * Returns the image size in bytes of a remote image.
1822 + *
1823 + * @deprecated 0.66.3
1824 + *
1825 + * @param string $image_url Image URL.
1826 + * @return integer|null $bytes Image size in bytes, or null if request failed.
1827 + */
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' );
1830 +
1831 + return null;
1832 + }
1833 +
1834 + /**
1835 + * Returns the resized Photon URL for a given image.
1836 + *
1837 + * @deprecated 0.66.3
1838 + *
1839 + * @param string $image_url Image URL.
1840 + * @param int $width Image width.
1841 + * @param int $height Image height.
1842 + * @return string
1843 + */
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;
1848 + }
1849 +
1850 + /**
1851 + * This function runs the image through Site Accelerator to compress it, and also scales it down if needed.
1852 + *
1853 + * @deprecated 0.66.3
1854 + *
1855 + * @param string $url Image URL.
1856 + * @param int $width Image width.
1857 + * @param int $height Image height.
1858 + * @return array The compressed image data.
1859 + */
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' );
1862 +
1863 + return array();
1864 + }
1865 +
1866 + /**
1867 + * Reduce the filesize of an image by reducing the dimensions. Uses Photon.
1868 + * Returns null if the image cannot be reduced enough.
1869 + *
1870 + * @deprecated 0.66.3
1871 + *
1872 + * @param string $url Image URL.
1873 + * @param int $width Image width.
1874 + * @param int $height Image height.
1875 + * @param int $filesize Image filesize.
1876 + * @param int $tries Number of times to try reducing the image size. Default is 5.
1877 + * @return array|null
1878 + */
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' );
1881 +
1882 + return null;
1883 + }
1884 +
1885 + /**
1886 + * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags,
1887 + * or to make the Jetpack open graph images pass restrictions.
1888 + *
1889 + * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1890 + *
1891 + * @param array $tags Current tags.
1892 + *
1893 + * @return array The modified tags.
1894 + */
1895 + public function jetpack_social_open_graph_filter( $tags ) {
1896 + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1897 + return $tags;
1898 + }
1899 +
1900 + /**
1630 1901 * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1631 1902 *
1903 + * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1904 + *
1632 1905 * @param array $tags Current tags.
1906 + * @param array $opengraph_image The Jetpack Social image data.
1907 + *
1908 + * @return array The modified tags.
1633 1909 */
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' );
1912 +
1913 + return $tags;
1914 + }
1915 +
1916 + /**
1917 + * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1918 + *
1919 + * @param array $tags Current tags.
1920 + */
1634 1921 public function add_jetpack_social_og_images( $tags ) {
1635 - $opengraph_image = $this->get_social_opengraph_image( get_the_ID() );
1922 + if ( ! is_singular() ) {
1923 + return $tags;
1924 + }
1636 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 );
1637 1936 if ( empty( $opengraph_image ) ) {
1638 1937 return $tags;
1639 1938 }
1640 1939
@@ -1640,9 +1939,9 @@
1640 1939
1641 1940 // If this code is running in Jetpack, we need to add Twitter cards.
1642 1941 // Some active plugins disable Jetpack's Twitter Cards, so we need
1643 1942 // to check if the class was instantiated before adding the cards.
1644 - $needs_twitter_cards = class_exists( 'Jetpack_Twitter_Cards' );
1943 + $needs_twitter_cards = class_exists( \Automattic\Jetpack\Post_Media\Twitter_Cards::class );
1645 1944
1646 1945 return array_merge(
1647 1946 $tags,
1648 1947 array(
@@ -1684,17 +1983,18 @@
1684 1983 return str_replace( $search, $replace, $string );
1685 1984 }
1686 1985
1687 1986 /**
1688 - * Get Calypso URL for Publicize connections.
1987 + * Get the URL to the connections management page.
1689 1988 *
1690 - * @param string $source The idenfitier of the place the function is called from.
1691 1989 * @return string
1692 1990 */
1693 - public function publicize_connections_url( $source = 'calypso-marketing-connections' ) {
1694 - $allowed_sources = array( 'jetpack-social-connections-admin-page', 'jetpack-social-connections-classic-editor', 'calypso-marketing-connections' );
1695 - $source = in_array( $source, $allowed_sources, true ) ? $source : 'calypso-marketing-connections';
1696 - return Redirect::get_url( $source, array( 'site' => ( new Status() )->get_site_suffix() ) );
1991 + public function publicize_connections_url() {
1992 + $has_social_admin_page = defined( 'JETPACK_SOCIAL_PLUGIN_DIR' );
1993 +
1994 + $page = $has_social_admin_page ? 'jetpack-social' : 'jetpack#/sharing';
1995 +
1996 + return ( new Paths() )->admin_url( array( 'page' => $page ) );
1697 1997 }
1698 1998
1699 1999 /**
1700 2000 * Get the Jetpack Social info from the API.
@@ -1709,9 +2009,8 @@
1709 2009 if ( isset( $api_data_response ) ) {
1710 2010 return ! is_wp_error( $api_data_response ) ? $api_data_response : array();
1711 2011 }
1712 2012
1713 - $rest_controller = new REST_Controller();
1714 2013 $response = Client::wpcom_json_api_request_as_blog(
1715 2014 sprintf( 'sites/%d/jetpack-social', absint( $blog_id ) ),
1716 2015 '2',
1717 2016 array(
@@ -1720,9 +2019,9 @@
1720 2019 ),
1721 2020 null,
1722 2021 'wpcom'
1723 2022 );
1724 - $api_data_response = $rest_controller->make_proper_response( $response );
2023 + $api_data_response = Publicize_Utils::make_proper_response( $response );
1725 2024
1726 2025 if ( ! is_wp_error( $api_data_response ) ) {
1727 2026 set_transient( $key, $api_data_response, DAY_IN_SECONDS );
1728 2027 return $api_data_response;
@@ -1736,24 +2035,8 @@
1736 2035 return array();
1737 2036 }
1738 2037
1739 2038 /**
1740 - * Get the Publicize shares info.
1741 - *
1742 - * @param string $blog_id The WPCOM blog_id for the current blog.
1743 - * @return ?array
1744 - */
1745 - public function get_publicize_shares_info( $blog_id ) {
1746 - $data = $this->get_api_data( $blog_id );
1747 -
1748 - if ( empty( $data ) ) {
1749 - return null;
1750 - }
1751 -
1752 - return $data;
1753 - }
1754 -
1755 - /**
1756 2039 * Check if enhanced publishing is enabled.
1757 2040 *
1758 2041 * @deprecated $$next-version use Automattic\Jetpack\Publicize\Publicize_Base\has_enhanced_publishing_feature instead.
1759 2042 * @param int $blog_id The blog ID for the current blog.
@@ -1794,8 +2077,10 @@
1794 2077
1795 2078 /**
1796 2079 * Check if the auto-conversion feature is one of the active features.
1797 2080 *
2081 + * TODO: Remove this after certain releases of Jetpack v15.
2082 + *
1798 2083 * @param string $type Whether image or video.
1799 2084 *
1800 2085 * @return bool
1801 2086 */
@@ -1803,52 +2088,40 @@
1803 2088 return Current_Plan::supports( "social-$type-auto-convert" );
1804 2089 }
1805 2090
1806 2091 /**
1807 - * Check if Instagram connection is enabled.
2092 + * Check if a connection is enabled.
1808 2093 *
2094 + * @param string $connection The connection name like 'instagram', 'mastodon', 'nextdoor' etc.
2095 + *
1809 2096 * @return bool
1810 2097 */
1811 - public function has_instagram_connection_feature() {
1812 - return Current_Plan::supports( 'social-instagram-connection' );
2098 + public function has_connection_feature( $connection ) {
2099 + return Current_Plan::supports( "social-$connection-connection" );
1813 2100 }
1814 2101
1815 2102 /**
1816 - * Check if Mastodon connection is enabled.
2103 + * Check if the new connections management is enabled is enabled.
1817 2104 *
1818 2105 * @return bool
1819 2106 */
1820 - public function has_mastodon_connection_feature() {
1821 - return Current_Plan::supports( 'social-mastodon-connection' );
2107 + public function has_connections_management_feature() {
2108 + return Current_Plan::supports( 'social-connections-management' );
1822 2109 }
1823 2110
1824 2111 /**
1825 - * Check if Nextdoor connection is enabled.
2112 + * Get a list of additional connections that are supported by the current plan.
1826 2113 *
1827 - * @return bool
2114 + * @return array
1828 2115 */
1829 - public function has_nextdoor_connection_feature() {
1830 - return Current_Plan::supports( 'social-nextdoor-connection' );
1831 - }
2116 + public function get_supported_additional_connections() {
2117 + $additional_connections = array();
1832 2118
1833 - /**
1834 - * Call the WPCOM REST API to calculate the scheduled shares.
1835 - *
1836 - * @param string $blog_id The blog_id.
1837 - */
1838 - public function calculate_scheduled_shares( $blog_id ) {
1839 - $response = Client::wpcom_json_api_request_as_blog(
1840 - sprintf( 'sites/%d/jetpack-social/count-scheduled-shares', absint( $blog_id ) ),
1841 - '2',
1842 - array(
1843 - 'headers' => array( 'content-type' => 'application/json' ),
1844 - 'method' => 'GET',
1845 - ),
1846 - null,
1847 - 'wpcom'
1848 - );
1849 - $rest_controller = new REST_Controller();
1850 - return $rest_controller->make_proper_response( $response );
2119 + if ( $this->has_connection_feature( 'threads' ) ) {
2120 + $additional_connections[] = 'threads';
2121 + }
2122 +
2123 + return $additional_connections;
1851 2124 }
1852 2125
1853 2126 /**
1854 2127 * Check if we have a paid Jetpack Social plan.
@@ -1858,9 +2131,9 @@
1858 2131 * @return bool True if we have a paid plan, false otherwise.
1859 2132 */
1860 2133 public function has_paid_plan( $refresh_from_wpcom = false ) {
1861 2134 static $has_paid_plan = null;
1862 - if ( $has_paid_plan === null ) {
2135 + if ( null === $has_paid_plan ) {
1863 2136 $has_paid_plan = Current_Plan::supports( 'social-shares-1000', $refresh_from_wpcom );
1864 2137 }
1865 2138 return $has_paid_plan;
1866 2139 }
@@ -1865,8 +2138,17 @@
1865 2138 return $has_paid_plan;
1866 2139 }
1867 2140
1868 2141 /**
2142 + * Check if we have paid features enabled.
2143 + *
2144 + * @return bool True if we have paid features, false otherwise.
2145 + */
2146 + public function has_paid_features() {
2147 + return $this->has_enhanced_publishing_feature();
2148 + }
2149 +
2150 + /**
1869 2151 * Get an array with all dismissed notices.
1870 2152 *
1871 2153 * @return array
1872 2154 */
@@ -1877,8 +2159,19 @@
1877 2159 return array();
1878 2160 }
1879 2161
1880 2162 return $dismissed_notices;
2163 + }
2164 +
2165 + /**
2166 + * Whether the current user can manage a connection.
2167 + *
2168 + * @param array $connection_data The connection data.
2169 + *
2170 + * @return bool
2171 + */
2172 + public static function can_manage_connection( $connection_data ) {
2173 + return current_user_can( 'edit_others_posts' ) || get_current_user_id() === (int) $connection_data['user_id'];
1881 2174 }
1882 2175 }
1883 2176
1884 2177 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.