From 33936fafe1e9a03817f4594e7ac379df5f50b7a4 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 10:49:59 +0530 Subject: [PATCH 1/7] Create connections class for caching --- .../publicize/src/class-connections.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 projects/packages/publicize/src/class-connections.php diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php new file mode 100644 index 0000000000000..36de3b630cdf8 --- /dev/null +++ b/projects/packages/publicize/src/class-connections.php @@ -0,0 +1,78 @@ +is_wpcom_simple(); + + if ( $is_wpcom ) { + // We don't need to cache connections for simple sites. + return Connections_Controller::get_connections( $run_tests ); + } + + $clear_cache = $args['clear_cache'] ?? false; + + if ( $clear_cache || $run_tests ) { + self::clear_transient(); + } + + $connections = get_transient( self::CONNECTIONS_TRANSIENT ); + + // This can be an empty array, so we need to check for false. + if ( false === $connections ) { + $connections = self::fetch_and_cache_connections( $run_tests ); + } + + return $connections; + } + + /** + * Fetch connections from the REST API and cache them. + * + * @param bool $run_tests Whether to run connection tests. + * + * @return array + */ + public static function fetch_and_cache_connections( $run_tests = false ) { + $connections = Connections_Controller::get_connections( $run_tests ); + + if ( is_array( $connections ) ) { + set_transient( self::CONNECTIONS_TRANSIENT, $connections, HOUR_IN_SECONDS * 4 ); + } + + return $connections; + } + + /** + * Delete the transient. + */ + public static function clear_transient() { + delete_transient( self::CONNECTIONS_TRANSIENT ); + } +} From 6a63224664cb481792a3283a654d2276ec9cbb63 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 10:50:22 +0530 Subject: [PATCH 2/7] Update script data to use connections from the REST endpoint --- .../src/class-publicize-script-data.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index b4e7dded6b62d..63ca3c726944a 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -21,8 +21,6 @@ */ class Publicize_Script_Data { - const SERVICES_TRANSIENT = 'jetpack_social_services_list'; - /** * Get the publicize instance - properly typed * @@ -160,8 +158,7 @@ public static function get_store_initial_state() { return array( 'connectionData' => array( - // We do not have this method on WPCOM Publicize class yet. - 'connections' => ! $is_wpcom ? self::publicize()->get_all_connections_for_user() : array(), + 'connections' => Connections::get_all(), ), 'shareStatus' => $share_status, ); @@ -238,17 +235,24 @@ public static function get_api_paths() { $is_simple_site = ( new Host() )->is_wpcom_simple(); + $commom_paths = array( + 'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1', + ); + + $specific_paths = array(); + if ( $is_simple_site ) { - return array( - 'refreshConnections' => '/wpcom/v2/publicize/connection-test-results', - 'resharePost' => '/wpcom/v2/posts/{postId}/publicize', + + $specific_paths = array( + 'resharePost' => '/wpcom/v2/posts/{postId}/publicize', + ); + } else { + $specific_paths = array( + 'resharePost' => '/jetpack/v4/publicize/{postId}', ); } - return array( - 'refreshConnections' => '/jetpack/v4/publicize/connections?test_connections=1', - 'resharePost' => '/jetpack/v4/publicize/{postId}', - ); + return array_merge( $commom_paths, $specific_paths ); } /** From d1762a992b796e1500f0bbe16b711e0f18871edc Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 10:51:16 +0530 Subject: [PATCH 3/7] changelog --- .../changelog/update-unify-social-connections-schema | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/publicize/changelog/update-unify-social-connections-schema diff --git a/projects/packages/publicize/changelog/update-unify-social-connections-schema b/projects/packages/publicize/changelog/update-unify-social-connections-schema new file mode 100644 index 0000000000000..feff6a2e9064f --- /dev/null +++ b/projects/packages/publicize/changelog/update-unify-social-connections-schema @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Social: Use connections REST endpoint for initial state From e3efa9ba114927b2928a172b0748f4b73c20d908 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 17:48:32 +0530 Subject: [PATCH 4/7] Restore deprecated connection fields for time being --- .../publicize/src/class-connections.php | 58 +++++++++++++++---- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php index 36de3b630cdf8..9431bf74b1551 100644 --- a/projects/packages/publicize/src/class-connections.php +++ b/projects/packages/publicize/src/class-connections.php @@ -7,6 +7,7 @@ namespace Automattic\Jetpack\Publicize; +use Automattic\Jetpack\Connection; use Automattic\Jetpack\Publicize\REST_API\Connections_Controller; use Automattic\Jetpack\Status\Host; @@ -33,25 +34,62 @@ public static function get_all( $args = array() ) { if ( $is_wpcom ) { // We don't need to cache connections for simple sites. - return Connections_Controller::get_connections( $run_tests ); - } + $connections = Connections_Controller::get_connections( $run_tests ); + } else { - $clear_cache = $args['clear_cache'] ?? false; + $clear_cache = $args['clear_cache'] ?? false; - if ( $clear_cache || $run_tests ) { - self::clear_transient(); - } + if ( $clear_cache || $run_tests ) { + self::clear_transient(); + } - $connections = get_transient( self::CONNECTIONS_TRANSIENT ); + $connections = get_transient( self::CONNECTIONS_TRANSIENT ); - // This can be an empty array, so we need to check for false. - if ( false === $connections ) { - $connections = self::fetch_and_cache_connections( $run_tests ); + // This can be an empty array, so we need to check for false. + if ( false === $connections ) { + $connections = self::fetch_and_cache_connections( $run_tests ); + } } + // Let us add the deprecated fields for now. + // TODO: Remove this after https://github.com/Automattic/jetpack/pull/40539 is merged. + $connections = self::retain_deprecated_fields( $connections ); + return $connections; } + /** + * Retain deprecated fields. + * + * @param array $connections Connections. + * @return array + */ + private static function retain_deprecated_fields( $connections ) { + return array_map( + function ( $connection ) { + $wpcom_user_data = ( new Connection\Manager() )->get_connected_user_data(); + + $owns_connection = ! empty( $wpcom_user_data['ID'] ) && $wpcom_user_data['ID'] === $connection['user_id']; + + $connection = array_merge( + $connection, + array( + 'external_display' => $connection['display_name'], + 'can_disconnect' => current_user_can( 'edit_others_posts' ) || $owns_connection, + 'label' => $connection['service_label'], + ) + ); + + if ( 'bluesky' === $connection['service_name'] ) { + $connection['external_name'] = $connection['external_handle']; + } + + return $connection; + }, + $connections + ); + } + /** * Fetch connections from the REST API and cache them. * From fb0f012d44c86d9cee8d5842a94f335a7331890f Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 17:48:48 +0530 Subject: [PATCH 5/7] Disable caching for now --- projects/packages/publicize/src/class-connections.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php index 9431bf74b1551..8a4abe4a9e578 100644 --- a/projects/packages/publicize/src/class-connections.php +++ b/projects/packages/publicize/src/class-connections.php @@ -100,9 +100,7 @@ function ( $connection ) { public static function fetch_and_cache_connections( $run_tests = false ) { $connections = Connections_Controller::get_connections( $run_tests ); - if ( is_array( $connections ) ) { - set_transient( self::CONNECTIONS_TRANSIENT, $connections, HOUR_IN_SECONDS * 4 ); - } + // TODO Implement caching here. return $connections; } From f6b41d7c6c247e0640407a216124d0e878cfdc0a Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 19 Dec 2024 17:49:11 +0530 Subject: [PATCH 6/7] Fix display_name for Mastodon --- projects/packages/publicize/src/class-publicize-base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index c2e6c044cc5bf..324c233089b25 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -556,8 +556,8 @@ public function get_profile_link( $service_name, $connection ) { public function get_display_name( $service_name, $connection ) { $cmeta = $this->get_connection_meta( $connection ); - if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) { - return $cmeta['external_name']; + if ( 'mastodon' === $service_name && isset( $cmeta['external_display'] ) ) { + return $cmeta['external_display']; } if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) { From a71f39c56fbbe54b61bb74b16fd13922abde7394 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 20 Dec 2024 09:45:44 +0530 Subject: [PATCH 7/7] Remove the unused caching logic --- .../publicize/src/class-connections.php | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php index 8a4abe4a9e578..de30d41e264ae 100644 --- a/projects/packages/publicize/src/class-connections.php +++ b/projects/packages/publicize/src/class-connections.php @@ -33,22 +33,9 @@ public static function get_all( $args = array() ) { $is_wpcom = ( new Host() )->is_wpcom_simple(); if ( $is_wpcom ) { - // We don't need to cache connections for simple sites. $connections = Connections_Controller::get_connections( $run_tests ); } else { - - $clear_cache = $args['clear_cache'] ?? false; - - if ( $clear_cache || $run_tests ) { - self::clear_transient(); - } - - $connections = get_transient( self::CONNECTIONS_TRANSIENT ); - - // This can be an empty array, so we need to check for false. - if ( false === $connections ) { - $connections = self::fetch_and_cache_connections( $run_tests ); - } + $connections = self::fetch_and_cache_connections( $run_tests ); } // Let us add the deprecated fields for now. @@ -104,11 +91,4 @@ public static function fetch_and_cache_connections( $run_tests = false ) { return $connections; } - - /** - * Delete the transient. - */ - public static function clear_transient() { - delete_transient( self::CONNECTIONS_TRANSIENT ); - } }