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 +436 -285 13.3.3 → 16.3 View file →
@@ -9,12 +9,14 @@
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 +use WP_Error;
18 +use WP_Post;
17 19
18 20 /**
19 21 * Base class for Publicize.
20 22 */
@@ -63,8 +65,32 @@
63 65 * @var string
64 66 */
65 67 const POST_JETPACK_SOCIAL_OPTIONS = '_wpas_options';
66 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 +
67 93 // Skip meta keys. We used to rely on _wpas_skip_ appended with the token_id to skip posts. But to support
68 94 // multiple connections for the same token, we are going to use the _wpas_skip_publicize_ which
69 95 // will be appended with the connection_id.
70 96 /**
@@ -235,19 +261,20 @@
235 261 add_action( 'save_post', array( $this, 'save_meta' ), 20, 2 );
236 262
237 263 // Default checkbox state for each Connection.
238 264 add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 2 );
239 - add_filter( 'jetpack_open_graph_tags', array( $this, 'jetpack_social_open_graph_filter' ), 12, 1 ); // $priority = 12, to run after Jetpack adds the tags in the Jetpack_Twitter_Cards class.
240 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 +
241 269 // Alter the "Post Publish" admin notice to mention the Connections we Publicized to.
242 270 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 );
243 271
244 - // Connection test callback.
245 - add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) );
246 -
247 272 // Custom priority to ensure post type support is added prior to thumbnail support being added to the theme.
248 273 add_action( 'init', array( $this, 'add_post_type_support' ), 8 );
249 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 );
250 277
251 278 // The custom priority for this action ensures that any existing code that
252 279 // removes post-thumbnails support during 'init' continues to work.
253 280 add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 );
@@ -271,8 +298,23 @@
271 298 */
272 299 abstract public function get_services( $filter = 'all', $_blog_id = false, $_user_id = false );
273 300
274 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 + /**
275 317 * Does the given user have a connection to the service on the given blog?
276 318 *
277 319 * @param string $service_name 'facebook', 'twitter', etc.
278 320 * @param false|int $_blog_id The blog ID. Use false (default) for the current blog.
@@ -288,9 +330,9 @@
288 330 $_user_id = $this->user_id();
289 331 }
290 332
291 333 $connections = $this->get_connections( $service_name, $_blog_id, $_user_id );
292 - return ( is_array( $connections ) && count( $connections ) > 0 ? true : false );
334 + return is_array( $connections ) && count( $connections ) > 0;
293 335 }
294 336
295 337 /**
296 338 * Generates a connection URL.
@@ -463,8 +505,37 @@
463 505 if ( 'facebook' === $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
464 506 return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page'];
465 507 }
466 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 +
467 538 if ( 'tumblr' === $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
468 539 return 'https://' . $cmeta['connection_data']['meta']['tumblr_base_hostname'];
469 540 }
470 541
@@ -471,28 +542,10 @@
471 542 if ( 'twitter' === $service_name ) {
472 543 return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'.
473 544 }
474 545
475 - if ( 'linkedin' === $service_name ) {
476 - if ( ! isset( $cmeta['connection_data']['meta']['profile_url'] ) ) {
477 - return false;
478 - }
479 -
480 - $profile_url_query = wp_parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY );
481 - $profile_url_query_args = null;
482 - wp_parse_str( $profile_url_query, $profile_url_query_args );
483 -
484 - $id = null;
485 -
486 - if ( isset( $profile_url_query_args['key'] ) ) {
487 - $id = $profile_url_query_args['key'];
488 - } elseif ( isset( $profile_url_query_args['id'] ) ) {
489 - $id = $profile_url_query_args['id'];
490 - } else {
491 - return false;
492 - }
493 -
494 - 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'];
495 548 }
496 549
497 550 return false; // no fallback. we just won't link it.
498 551 }
@@ -506,10 +559,10 @@
506 559 */
507 560 public function get_display_name( $service_name, $connection ) {
508 561 $cmeta = $this->get_connection_meta( $connection );
509 562
510 - if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) {
511 - return $cmeta['external_name'];
563 + if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) {
564 + return $cmeta['external_display'];
512 565 }
513 566
514 567 if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
515 568 return $cmeta['connection_data']['meta']['display_name'];
@@ -539,19 +592,37 @@
539 592 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
540 593 * @return string
541 594 */
542 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 ) {
543 609 $cmeta = $this->get_connection_meta( $connection );
544 610
545 - if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) {
546 - return $cmeta['external_display'];
547 - }
611 + switch ( $service_name ) {
612 + case 'mastodon':
613 + return $cmeta['external_display'] ?? null;
548 614
549 - if ( isset( $cmeta['connection_data']['meta']['username'] ) ) {
550 - 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;
551 624 }
552 -
553 - return $this->get_display_name( $service_name, $connection );
554 625 }
555 626
556 627 /**
557 628 * Returns a profile picture for the Connection
@@ -558,9 +629,9 @@
558 629 *
559 630 * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack).
560 631 * @return string
561 632 */
562 - private function get_profile_picture( $connection ) {
633 + public function get_profile_picture( $connection ) {
563 634 $cmeta = $this->get_connection_meta( $connection );
564 635
565 636 if ( isset( $cmeta['profile_picture'] ) ) {
566 637 return $cmeta['profile_picture'];
@@ -664,17 +735,8 @@
664 735 return isset( $connection_data['meta']['options_responses'] );
665 736 }
666 737
667 738 /**
668 - * AJAX Handler to run connection tests on all Connections
669 - *
670 - * @return void
671 - */
672 - public function test_publicize_conns() {
673 - wp_send_json_success( $this->get_publicize_conns_test_results() );
674 - }
675 -
676 - /**
677 739 * Parse the error code returned by the XML-RPC API call.
678 740 *
679 741 * @param string $code Error code in numerical format.
680 742 *
@@ -812,8 +874,9 @@
812 874 * @type bool 'enabled' Default value for the connection (e.g., for a checkbox).
813 875 * @type bool 'done' Has this connection already been publicized to?
814 876 * @type bool 'toggleable' Is the user allowed to change the value for the connection?
815 877 * @type bool 'global' Is this connection a global one?
878 + * @type string 'external_id' External ID for the connection.
816 879 * }
817 880 */
818 881 public function get_filtered_connection_data( $selected_post_id = null ) {
819 882 $connection_list = array();
@@ -825,23 +888,22 @@
825 888 } else {
826 889 $post_id = null;
827 890 }
828 891
829 - $services = $this->get_services( 'connected' );
830 - $all_done = $this->post_is_done_sharing( $post_id );
831 -
832 892 // We don't allow Publicizing to the same external id twice, to prevent spam.
833 893 $service_id_done = (array) get_post_meta( $post_id, $this->POST_SERVICE_DONE, true );
834 894
835 - foreach ( $services as $service_name => $connections ) {
895 + $connections = Connections::get_all_for_user();
896 +
897 + if ( ! empty( $connections ) ) {
898 +
836 899 foreach ( $connections as $connection ) {
837 - $connection_meta = $this->get_connection_meta( $connection );
838 - $connection_data = $connection_meta['connection_data'];
839 - $unique_id = $this->get_connection_unique_id( $connection );
840 - $connection_id = $this->get_connection_id( $connection );
900 + $service_name = $connection['service_name'];
901 + $unique_id = $connection['id'];
902 + $connection_id = $connection['connection_id'];
841 903 // Was this connection (OR, old-format service) already Publicized to?
842 904 $done = ! empty( $post ) && (
843 - // Flags based on token_id
905 + // Flags based on token_id.
844 906 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $unique_id, true )
845 907 ||
846 908 // Old flags.
847 909 1 === (int) get_post_meta( $post->ID, $this->POST_DONE . $service_name, true )
@@ -855,12 +917,12 @@
855 917 *
856 918 * @param bool true Should the post be publicized to a given service? Default to true.
857 919 * @param int $post_id Post ID.
858 920 * @param string $service_name Service name.
859 - * @param array $connection_data Array of information about all Publicize details for the site.
921 + * @param array $connection The connection data.
860 922 */
861 923 /* phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores */
862 - 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 ) ) {
863 925 continue;
864 926 }
865 927
866 928 // Should we be skipping this one?
@@ -877,22 +939,17 @@
877 939 // Flags based on the connection_id.
878 940 get_post_meta( $post->ID, $this->POST_SKIP_PUBLICIZE . $connection_id, true )
879 941 ||
880 942 // Old flags.
881 - get_post_meta( $post->ID, $this->POST_SKIP . $service_name )
943 + get_post_meta( $post->ID, $this->POST_SKIP . $service_name, false )
882 944 )
883 945 )
884 946 ||
885 947 (
886 - is_array( $connection )
887 - &&
888 - 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'] ] )
889 949 )
890 950 );
891 951
892 - // If this one has already been publicized to, don't let it happen again.
893 - $toggleable = ! $done && ! $all_done;
894 -
895 952 // Determine the state of the checkbox (on/off) and allow filtering.
896 953 $enabled = $done || ! $skip;
897 954 /**
898 955 * Filter the checkbox state of each Publicize connection appearing in the post editor.
@@ -907,14 +964,11 @@
907 964 */
908 965 $enabled = apply_filters( 'publicize_checkbox_default', $enabled, $post_id, $service_name, $connection );
909 966
910 967 /**
911 - * If this is a global connection and this user doesn't have enough permissions to modify
912 - * 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.
913 969 */
914 - if ( ! $done && $this->is_global_connection( $connection_meta ) && ! current_user_can( $this->GLOBAL_CAP ) ) {
915 - $toggleable = false;
916 -
970 + if ( ! $done && Connections::is_shared( $connection ) && ! current_user_can( $this->GLOBAL_CAP ) ) {
917 971 /**
918 972 * Filters the checkboxes for global connections with non-prilvedged users.
919 973 *
920 974 * @since 0.1.0
@@ -932,20 +986,14 @@
932 986 if ( $done ) {
933 987 $enabled = true;
934 988 }
935 989
936 - $connection_list[] = array(
937 - 'id' => $connection_id,
938 - 'unique_id' => $unique_id,
939 - 'service_name' => $service_name,
940 - 'service_label' => static::get_service_label( $service_name ),
941 - 'display_name' => $this->get_display_name( $service_name, $connection ),
942 - 'username' => $this->get_username( $service_name, $connection ),
943 - 'profile_picture' => $this->get_profile_picture( $connection ),
944 - 'enabled' => $enabled,
945 - 'done' => $done,
946 - 'toggleable' => $toggleable,
947 - '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 + )
948 996 );
949 997 }
950 998 }
951 999
@@ -964,37 +1012,20 @@
964 1012 */
965 1013 abstract public function post_is_done_sharing( $post_id = null );
966 1014
967 1015 /**
968 - * Retrieves full list of available Publicize connection services.
1016 + * Retrieve the available Publicize services and their connect URLs.
969 1017 *
970 - * Retrieves current available publicize service connections
971 - * with associated labels and URLs.
972 - *
973 1018 * @since 0.1.0
974 1019 * @since-jetpack 6.7.0
1020 + * @deprecated 0.88.0 Services and their connect URLs come from the `publicize/services` REST endpoint.
975 1021 *
976 - * @return array {
977 - * Array of UI service connection data for all services
978 - *
979 - * @type string 'name' Name of service.
980 - * @type string 'label' Display label for service.
981 - * @type string 'url' URL for adding connection to service.
982 - * }
1022 + * @return array Always empty.
983 1023 */
984 1024 public function get_available_service_data() {
985 - $available_services = $this->get_services( 'all' );
986 - $available_service_data = array();
1025 + _deprecated_function( __METHOD__, 'publicize-0.88.0' );
987 1026
988 - foreach ( $available_services as $service_name => $service ) {
989 - $available_service_data[] = array(
990 - 'name' => $service_name,
991 - 'label' => static::get_service_label( $service_name ),
992 - 'url' => $this->connect_url( $service_name ),
993 - );
994 - }
995 -
996 - return $available_service_data;
1027 + return array();
997 1028 }
998 1029
999 1030 /**
1000 1031 * Site Data
@@ -1083,13 +1114,20 @@
1083 1114 *
1084 1115 * Registers for each post type that with `publicize` feature support.
1085 1116 */
1086 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 +
1087 1125 $message_args = array(
1088 1126 'type' => 'string',
1089 1127 'description' => __( 'The message to use instead of the title when sharing to Jetpack Social services', 'jetpack-publicize-pkg' ),
1090 1128 'single' => true,
1091 - 'default' => '',
1129 + 'default' => $message_default,
1092 1130 'show_in_rest' => array(
1093 1131 'name' => 'jetpack_publicize_message',
1094 1132 ),
1095 1133 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
@@ -1122,11 +1160,14 @@
1122 1160 'description' => __( 'Post options related to Jetpack Social.', 'jetpack-publicize-pkg' ),
1123 1161 'single' => true,
1124 1162 'default' => array(
1125 1163 'image_generator_settings' => array(
1126 - 'template' => ( new Jetpack_Social_Settings\Settings() )->sig_get_default_template(),
1127 - '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,
1128 1168 ),
1169 + 'version' => 2,
1129 1170 ),
1130 1171 'show_in_rest' => array(
1131 1172 'name' => 'jetpack_social_options',
1132 1173 'schema' => array(
@@ -1131,9 +1172,12 @@
1131 1172 'name' => 'jetpack_social_options',
1132 1173 'schema' => array(
1133 1174 'type' => 'object',
1134 1175 'properties' => array(
1135 - 'attached_media' => array(
1176 + 'version' => array(
1177 + 'type' => 'number',
1178 + ),
1179 + 'attached_media' => array(
1136 1180 'type' => 'array',
1137 1181 'items' => array(
1138 1182 'type' => 'object',
1139 1183 'properties' => array(
@@ -1148,34 +1192,41 @@
1148 1192 ),
1149 1193 ),
1150 1194 ),
1151 1195 ),
1152 - 'should_upload_attached_media' => array(
1153 - 'type' => 'boolean',
1154 - ),
1155 - 'image_generator_settings' => array(
1196 + 'image_generator_settings' => array(
1156 1197 'type' => 'object',
1157 1198 'properties' => array(
1158 - 'enabled' => array(
1199 + 'enabled' => array(
1159 1200 'type' => 'boolean',
1160 1201 ),
1161 - 'custom_text' => array(
1202 + 'custom_text' => array(
1162 1203 'type' => 'string',
1163 1204 ),
1164 - 'image_type' => array(
1205 + 'image_type' => array(
1165 1206 'type' => 'string',
1166 1207 ),
1167 - 'image_id' => array(
1208 + 'image_id' => array(
1168 1209 'type' => 'number',
1169 1210 ),
1170 - 'template' => array(
1211 + 'template' => array(
1171 1212 'type' => 'string',
1172 1213 ),
1173 - 'token' => array(
1214 + 'font' => array(
1174 1215 'type' => 'string',
1175 1216 ),
1217 + 'token' => array(
1218 + 'type' => 'string',
1219 + ),
1220 + 'default_image_id' => array(
1221 + 'type' => 'number',
1222 + ),
1176 1223 ),
1177 1224 ),
1225 + 'media_source' => array(
1226 + 'type' => 'string',
1227 + 'enum' => array( 'featured-image', 'sig', 'media-library', 'upload-video', 'none' ),
1228 + ),
1178 1229 ),
1179 1230 ),
1180 1231 ),
1181 1232 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
@@ -1180,8 +1231,60 @@
1180 1231 ),
1181 1232 'auth_callback' => array( $this, 'message_meta_auth_callback' ),
1182 1233 );
1183 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 +
1184 1287 foreach ( get_post_types() as $post_type ) {
1185 1288 if ( ! $this->post_type_is_publicizeable( $post_type ) ) {
1186 1289 continue;
1187 1290 }
@@ -1189,17 +1292,120 @@
1189 1292 $message_args['object_subtype'] = $post_type;
1190 1293 $publicize_feature_enable_args['object_subtype'] = $post_type;
1191 1294 $already_shared_flag_args['object_subtype'] = $post_type;
1192 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;
1193 1298
1194 1299 register_meta( 'post', $this->POST_MESS, $message_args );
1195 1300 register_meta( 'post', self::POST_PUBLICIZE_FEATURE_ENABLED, $publicize_feature_enable_args );
1196 1301 register_meta( 'post', $this->POST_DONE . 'all', $already_shared_flag_args );
1197 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 );
1198 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 );
1199 1310 }
1200 1311
1201 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 + /**
1202 1408 * Helper function to allow us to not publicize posts in certain contexts.
1203 1409 *
1204 1410 * @param WP_Post $post Post object.
1205 1411 */
@@ -1231,15 +1437,11 @@
1231 1437
1232 1438 // - API/XML-RPC Test Posts
1233 1439 if (
1234 1440 (
1235 - defined( 'XMLRPC_REQUEST' )
1236 - &&
1237 - XMLRPC_REQUEST
1441 + ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
1238 1442 ||
1239 - defined( 'APP_REQUEST' )
1240 - &&
1241 - APP_REQUEST
1443 + ( defined( 'APP_REQUEST' ) && constant( 'APP_REQUEST' ) )
1242 1444 )
1243 1445 &&
1244 1446 str_starts_with( $post->post_title, 'Temporary Post Used For Theme Detection' )
1245 1447 ) {
@@ -1278,9 +1480,9 @@
1278 1480
1279 1481 $submit_post = $this->should_submit_post_pre_checks( $post );
1280 1482
1281 1483 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're only checking if a value is set
1282 - $admin_page = isset( $_POST[ $this->ADMIN_PAGE ] ) ? $_POST[ $this->ADMIN_PAGE ] : null;
1484 + $admin_page = $_POST[ $this->ADMIN_PAGE ] ?? null;
1283 1485
1284 1486 // Did this request happen via wp-admin?
1285 1487 $from_web = isset( $_SERVER['REQUEST_METHOD'] )
1286 1488 &&
@@ -1290,14 +1492,10 @@
1290 1492
1291 1493 // phpcs:ignore WordPress.Security.NonceVerification.Missing
1292 1494 $title = isset( $_POST['wpas_title'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpas_title'] ) ) : null;
1293 1495
1294 - if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && $title ) {
1295 - if ( empty( $title ) ) {
1296 - delete_post_meta( $post_id, $this->POST_MESS );
1297 - } else {
1298 - update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) );
1299 - }
1496 + if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && ! empty( $title ) ) {
1497 + update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $title ) ) );
1300 1498 }
1301 1499
1302 1500 // Change current user to provide context for get_services() if we're running during cron.
1303 1501 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
@@ -1417,13 +1615,8 @@
1417 1615 }
1418 1616
1419 1617 $labels = array();
1420 1618 foreach ( $services as $service_name => $display_names ) {
1421 - // Twitter connections do not trigger Publicize anymore. Skip.
1422 - if ( 'Twitter' === $service_name ) {
1423 - continue;
1424 - }
1425 -
1426 1619 $labels[] = sprintf(
1427 1620 /* translators: Service name is %1$s, and account name is %2$s. */
1428 1621 esc_html__( '%1$s (%2$s)', 'jetpack-publicize-pkg' ),
1429 1622 esc_html( $service_name ),
@@ -1568,9 +1761,9 @@
1568 1761 if ( ! wp_attachment_is_image( $media_id ) ) {
1569 1762 return array();
1570 1763 }
1571 1764
1572 - $image = wp_get_attachment_image_src( $media_id, array( 1200 ) );
1765 + $image = wp_get_attachment_image_src( $media_id, array( 1200, 1200 ) );
1573 1766
1574 1767 if ( ! $image ) {
1575 1768 return array();
1576 1769 }
@@ -1610,8 +1803,18 @@
1610 1803 if ( $attached_media ) {
1611 1804 return $attached_media;
1612 1805 }
1613 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 +
1614 1817 return array();
1615 1818 }
1616 1819
1617 1820 /**
@@ -1616,74 +1819,49 @@
1616 1819
1617 1820 /**
1618 1821 * Returns the image size in bytes of a remote image.
1619 1822 *
1823 + * @deprecated 0.66.3
1824 + *
1620 1825 * @param string $image_url Image URL.
1621 1826 * @return integer|null $bytes Image size in bytes, or null if request failed.
1622 1827 */
1623 - public function get_remote_filesize( $image_url ) {
1624 - $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' );
1625 1830
1626 - if ( is_wp_error( $response ) ) {
1627 - return null;
1628 - }
1629 -
1630 - $size = wp_remote_retrieve_header( $response, 'content-length' );
1631 -
1632 - return ! empty( $size ) ? $size : null;
1831 + return null;
1633 1832 }
1634 1833
1635 1834 /**
1636 1835 * Returns the resized Photon URL for a given image.
1637 1836 *
1837 + * @deprecated 0.66.3
1838 + *
1638 1839 * @param string $image_url Image URL.
1639 1840 * @param int $width Image width.
1640 1841 * @param int $height Image height.
1641 1842 * @return string
1642 1843 */
1643 - public function get_resized_image_url( $image_url, $width, $height ) {
1644 - return jetpack_photon_url(
1645 - $image_url,
1646 - array(
1647 - 'w' => $width,
1648 - 'h' => $height,
1649 - )
1650 - );
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;
1651 1848 }
1652 1849
1653 1850 /**
1654 1851 * This function runs the image through Site Accelerator to compress it, and also scales it down if needed.
1655 1852 *
1853 + * @deprecated 0.66.3
1854 + *
1656 1855 * @param string $url Image URL.
1657 1856 * @param int $width Image width.
1658 1857 * @param int $height Image height.
1659 1858 * @return array The compressed image data.
1660 1859 */
1661 - public function compress_and_scale_og_image( $url, $width, $height ) {
1662 - // If the dimensions are fine we just run it through Site Accelerator to compress it.
1663 - if ( 1200 >= $width && 1200 >= $height ) {
1664 - return array(
1665 - 'url' => $this->get_resized_image_url( $url, $width, $height ),
1666 - 'width' => $width,
1667 - 'height' => $height,
1668 - );
1669 - }
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' );
1670 1862
1671 - if ( $height > $width ) {
1672 - // Portrait.
1673 - $width = 1200 * $width / $height;
1674 - $height = 1200;
1675 - } else {
1676 - // Landscape.
1677 - $height = 1200 * $height / $width;
1678 - $width = 1200;
1679 - }
1680 -
1681 - return array(
1682 - 'url' => $this->get_resized_image_url( $url, $width, $height ),
1683 - 'width' => $width,
1684 - 'height' => $height,
1685 - );
1863 + return array();
1686 1864 }
1687 1865
1688 1866 /**
1689 1867 * Reduce the filesize of an image by reducing the dimensions. Uses Photon.
@@ -1688,8 +1866,10 @@
1688 1866 /**
1689 1867 * Reduce the filesize of an image by reducing the dimensions. Uses Photon.
1690 1868 * Returns null if the image cannot be reduced enough.
1691 1869 *
1870 + * @deprecated 0.66.3
1871 + *
1692 1872 * @param string $url Image URL.
1693 1873 * @param int $width Image width.
1694 1874 * @param int $height Image height.
1695 1875 * @param int $filesize Image filesize.
@@ -1695,28 +1875,12 @@
1695 1875 * @param int $filesize Image filesize.
1696 1876 * @param int $tries Number of times to try reducing the image size. Default is 5.
1697 1877 * @return array|null
1698 1878 */
1699 - public function reduce_file_size( $url, $width, $height, $filesize, $tries = 5 ) {
1700 - while ( $tries > 0 && $filesize > self::OG_IMAGE_MAX_FILESIZE ) {
1701 - $width *= 0.75;
1702 - $height *= 0.75;
1703 - $url = $this->get_resized_image_url( $url, $width, $height );
1704 - $filesize = $this->get_remote_filesize( $url );
1705 - --$tries;
1706 - }
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' );
1707 1881
1708 - // If the image is still too large, we failed.
1709 - if ( $filesize > self::OG_IMAGE_MAX_FILESIZE ) {
1710 - // TODO: Track this to see if conversion failed.
1711 - return null;
1712 - }
1713 -
1714 - return array(
1715 - 'url' => $url,
1716 - 'width' => $width,
1717 - 'height' => $height,
1718 - );
1882 + return null;
1719 1883 }
1720 1884
1721 1885 /**
1722 1886 * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags,
@@ -1721,60 +1885,63 @@
1721 1885 /**
1722 1886 * Hooks into jetpack_open_graph_tags to add the Jetpack Social images to the OpenGraph tags,
1723 1887 * or to make the Jetpack open graph images pass restrictions.
1724 1888 *
1889 + * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1890 + *
1725 1891 * @param array $tags Current tags.
1892 + *
1893 + * @return array The modified tags.
1726 1894 */
1727 1895 public function jetpack_social_open_graph_filter( $tags ) {
1728 - $social_opengraph_image = $this->get_social_opengraph_image( get_the_ID() );
1896 + _deprecated_function( __METHOD__, '0.66.3', 'add_jetpack_social_og_images' );
1897 + return $tags;
1898 + }
1729 1899
1730 - if ( ! empty( $social_opengraph_image ) ) {
1731 - $tags = $this->add_jetpack_social_og_image( $tags, $social_opengraph_image );
1732 - }
1900 + /**
1901 + * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1902 + *
1903 + * @deprecated 0.66.3 use add_jetpack_social_og_images instead.
1904 + *
1905 + * @param array $tags Current tags.
1906 + * @param array $opengraph_image The Jetpack Social image data.
1907 + *
1908 + * @return array The modified tags.
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' );
1733 1912
1734 - if ( empty( $tags['og:image'] ) || ! is_string( $tags['og:image'] ) || empty( $tags['og:image:width'] ) || empty( $tags['og:image:height'] ) ) {
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 + */
1921 + public function add_jetpack_social_og_images( $tags ) {
1922 + if ( ! is_singular() ) {
1735 1923 return $tags;
1736 1924 }
1737 1925
1738 - // If we do not have a SIG or attached image, but we have an image in post body
1739 - // we need to check that the image is not too large for the social sites.
1740 - $filesize = $this->get_remote_filesize( $tags['og:image'] );
1741 - // If the image is small enough, we do not need to do anything.
1742 - if ( empty( $filesize ) || $filesize <= self::OG_IMAGE_MAX_FILESIZE ) {
1926 + $post_id = get_the_ID();
1927 + if ( ! $post_id ) {
1743 1928 return $tags;
1744 1929 }
1745 1930
1746 - $compressed_image = $this->compress_and_scale_og_image( $tags['og:image'], $tags['og:image:width'], $tags['og:image:height'] );
1747 - $filesize = $this->get_remote_filesize( $compressed_image['url'] );
1748 - // If the compressed image is small enough, we use it.
1749 - if ( $filesize <= self::OG_IMAGE_MAX_FILESIZE ) {
1750 - $tags['og:image'] = $compressed_image['url'];
1751 - $tags['og:image:width'] = $compressed_image['width'];
1752 - $tags['og:image:height'] = $compressed_image['height'];
1931 + if ( ! post_type_supports( get_post_type( $post_id ), 'publicize' ) ) {
1753 1932 return $tags;
1754 1933 }
1755 1934
1756 - $reduced_image = $this->reduce_file_size( $compressed_image['url'], $compressed_image['width'], $compressed_image['height'], $filesize );
1757 - if ( ! empty( $reduced_image ) ) {
1758 - $tags['og:image'] = $reduced_image['url'];
1759 - $tags['og:image:width'] = $reduced_image['width'];
1760 - $tags['og:image:height'] = $reduced_image['height'];
1935 + $opengraph_image = $this->get_social_opengraph_image( $post_id );
1936 + if ( empty( $opengraph_image ) ) {
1937 + return $tags;
1761 1938 }
1762 1939
1763 - return $tags;
1764 - }
1765 -
1766 - /**
1767 - * Add the Jetpack Social images (attached media, SIG image) to the OpenGraph tags.
1768 - *
1769 - * @param array $tags Current tags.
1770 - * @param array $opengraph_image The Jetpack Social image data.
1771 - */
1772 - public function add_jetpack_social_og_image( $tags, $opengraph_image ) {
1773 1940 // If this code is running in Jetpack, we need to add Twitter cards.
1774 1941 // Some active plugins disable Jetpack's Twitter Cards, so we need
1775 1942 // to check if the class was instantiated before adding the cards.
1776 - $needs_twitter_cards = class_exists( 'Jetpack_Twitter_Cards' );
1943 + $needs_twitter_cards = class_exists( \Automattic\Jetpack\Post_Media\Twitter_Cards::class );
1777 1944
1778 1945 return array_merge(
1779 1946 $tags,
1780 1947 array(
@@ -1816,20 +1983,18 @@
1816 1983 return str_replace( $search, $replace, $string );
1817 1984 }
1818 1985
1819 1986 /**
1820 - * Get Calypso URL for Publicize connections.
1987 + * Get the URL to the connections management page.
1821 1988 *
1822 - * @param string $source The idenfitier of the place the function is called from.
1823 1989 * @return string
1824 1990 */
1825 - public function publicize_connections_url( $source = 'calypso-marketing-connections' ) {
1826 - $allowed_sources = array( 'jetpack-social-connections-admin-page', 'jetpack-social-connections-classic-editor', 'calypso-marketing-connections' );
1827 - $source = in_array( $source, $allowed_sources, true ) ? $source : 'calypso-marketing-connections';
1828 - $blog_id = Connection_Manager::get_site_id( true );
1829 - $site = ( new Status() )->get_site_suffix();
1991 + public function publicize_connections_url() {
1992 + $has_social_admin_page = defined( 'JETPACK_SOCIAL_PLUGIN_DIR' );
1830 1993
1831 - 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 ) );
1832 1997 }
1833 1998
1834 1999 /**
1835 2000 * Get the Jetpack Social info from the API.
@@ -1844,9 +2009,8 @@
1844 2009 if ( isset( $api_data_response ) ) {
1845 2010 return ! is_wp_error( $api_data_response ) ? $api_data_response : array();
1846 2011 }
1847 2012
1848 - $rest_controller = new REST_Controller();
1849 2013 $response = Client::wpcom_json_api_request_as_blog(
1850 2014 sprintf( 'sites/%d/jetpack-social', absint( $blog_id ) ),
1851 2015 '2',
1852 2016 array(
@@ -1855,9 +2019,9 @@
1855 2019 ),
1856 2020 null,
1857 2021 'wpcom'
1858 2022 );
1859 - $api_data_response = $rest_controller->make_proper_response( $response );
2023 + $api_data_response = Publicize_Utils::make_proper_response( $response );
1860 2024
1861 2025 if ( ! is_wp_error( $api_data_response ) ) {
1862 2026 set_transient( $key, $api_data_response, DAY_IN_SECONDS );
1863 2027 return $api_data_response;
@@ -1871,24 +2035,8 @@
1871 2035 return array();
1872 2036 }
1873 2037
1874 2038 /**
1875 - * Get the Publicize shares info.
1876 - *
1877 - * @param string $blog_id The WPCOM blog_id for the current blog.
1878 - * @return ?array
1879 - */
1880 - public function get_publicize_shares_info( $blog_id ) {
1881 - $data = $this->get_api_data( $blog_id );
1882 -
1883 - if ( empty( $data ) ) {
1884 - return null;
1885 - }
1886 -
1887 - return $data;
1888 - }
1889 -
1890 - /**
1891 2039 * Check if enhanced publishing is enabled.
1892 2040 *
1893 2041 * @deprecated $$next-version use Automattic\Jetpack\Publicize\Publicize_Base\has_enhanced_publishing_feature instead.
1894 2042 * @param int $blog_id The blog ID for the current blog.
@@ -1929,8 +2077,10 @@
1929 2077
1930 2078 /**
1931 2079 * Check if the auto-conversion feature is one of the active features.
1932 2080 *
2081 + * TODO: Remove this after certain releases of Jetpack v15.
2082 + *
1933 2083 * @param string $type Whether image or video.
1934 2084 *
1935 2085 * @return bool
1936 2086 */
@@ -1949,8 +2099,17 @@
1949 2099 return Current_Plan::supports( "social-$connection-connection" );
1950 2100 }
1951 2101
1952 2102 /**
2103 + * Check if the new connections management is enabled is enabled.
2104 + *
2105 + * @return bool
2106 + */
2107 + public function has_connections_management_feature() {
2108 + return Current_Plan::supports( 'social-connections-management' );
2109 + }
2110 +
2111 + /**
1953 2112 * Get a list of additional connections that are supported by the current plan.
1954 2113 *
1955 2114 * @return array
1956 2115 */
@@ -1956,44 +2115,16 @@
1956 2115 */
1957 2116 public function get_supported_additional_connections() {
1958 2117 $additional_connections = array();
1959 2118
1960 - if ( $this->has_connection_feature( 'instagram' ) ) {
1961 - $additional_connections[] = 'instagram-business';
2119 + if ( $this->has_connection_feature( 'threads' ) ) {
2120 + $additional_connections[] = 'threads';
1962 2121 }
1963 2122
1964 - if ( $this->has_connection_feature( 'mastodon' ) ) {
1965 - $additional_connections[] = 'mastodon';
1966 - }
1967 -
1968 - if ( $this->has_connection_feature( 'nextdoor' ) ) {
1969 - $additional_connections[] = 'nextdoor';
1970 - }
1971 -
1972 2123 return $additional_connections;
1973 2124 }
1974 2125
1975 2126 /**
1976 - * Call the WPCOM REST API to calculate the scheduled shares.
1977 - *
1978 - * @param string $blog_id The blog_id.
1979 - */
1980 - public function calculate_scheduled_shares( $blog_id ) {
1981 - $response = Client::wpcom_json_api_request_as_blog(
1982 - sprintf( 'sites/%d/jetpack-social/count-scheduled-shares', absint( $blog_id ) ),
1983 - '2',
1984 - array(
1985 - 'headers' => array( 'content-type' => 'application/json' ),
1986 - 'method' => 'GET',
1987 - ),
1988 - null,
1989 - 'wpcom'
1990 - );
1991 - $rest_controller = new REST_Controller();
1992 - return $rest_controller->make_proper_response( $response );
1993 - }
1994 -
1995 - /**
1996 2127 * Check if we have a paid Jetpack Social plan.
1997 2128 *
1998 2129 * @param bool $refresh_from_wpcom Whether to force refresh the plan check.
1999 2130 *
@@ -2000,9 +2131,9 @@
2000 2131 * @return bool True if we have a paid plan, false otherwise.
2001 2132 */
2002 2133 public function has_paid_plan( $refresh_from_wpcom = false ) {
2003 2134 static $has_paid_plan = null;
2004 - if ( $has_paid_plan === null ) {
2135 + if ( null === $has_paid_plan ) {
2005 2136 $has_paid_plan = Current_Plan::supports( 'social-shares-1000', $refresh_from_wpcom );
2006 2137 }
2007 2138 return $has_paid_plan;
2008 2139 }
@@ -2007,8 +2138,17 @@
2007 2138 return $has_paid_plan;
2008 2139 }
2009 2140
2010 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 + /**
2011 2151 * Get an array with all dismissed notices.
2012 2152 *
2013 2153 * @return array
2014 2154 */
@@ -2019,8 +2159,19 @@
2019 2159 return array();
2020 2160 }
2021 2161
2022 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'];
2023 2174 }
2024 2175 }
2025 2176
2026 2177 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.