From a21bf5035d70914b2a6f5ccfe31adff8e9cf509d Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:46:05 +0530 Subject: [PATCH 01/14] Declare get_all_connections_for_user abstract class --- projects/packages/publicize/src/class-publicize-base.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index b88bd1a6b12ca..111d042038e20 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -397,6 +397,13 @@ public static function get_service_label( $service_name ) { */ abstract public function get_connections( $service_name, $_blog_id = false, $_user_id = false ); + /** + * Get all Connections from all services for a user + * + * @param array $args Arguments to run operations such as force refresh and connection test results. + */ + abstract public function get_all_connections_for_user( $args = array() ); + /** * Get a single Connection of a Service * From 783ada0ba886fa7ce16d0abf1b8405325a29e7a9 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:46:33 +0530 Subject: [PATCH 02/14] Add and update the required methods in base class --- .../publicize/src/class-publicize-base.php | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index 111d042038e20..e4aa5e6639054 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -477,6 +477,32 @@ abstract public function globalize_connection( $connection_id ); */ abstract public function unglobalize_connection( $connection_id ); + /** + * Returns the external handle for the Connection. + * + * @param string $service_name 'facebook', 'linkedin', etc. + * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack). + * @return string + */ + public function get_external_handle( $service_name, $connection ) { + $cmeta = $this->get_connection_meta( $connection ); + + switch ( $service_name ) { + case 'mastodon': + return $cmeta['external_display'] ?? ''; + + case 'bluesky': + case 'threads': + return $cmeta['external_name'] ?? ''; + + case 'instagram-business': + return $cmeta['connection_data']['meta']['username'] ?? ''; + + default: + return ''; + } + } + /** * Returns an external URL to the Connection's profile * @@ -615,7 +641,7 @@ public function get_username( $service_name, $connection ) { * @param object|array $connection The Connection object (WordPress.com) or array (Jetpack). * @return string */ - private function get_profile_picture( $connection ) { + public function get_profile_picture( $connection ) { $cmeta = $this->get_connection_meta( $connection ); if ( isset( $cmeta['profile_picture'] ) ) { From 666866dbba49087ee99876084ee5dedad5a1d640 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:47:04 +0530 Subject: [PATCH 03/14] Update get_all_connections_for_user method in jetpack --- .../publicize/src/class-publicize.php | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize.php b/projects/packages/publicize/src/class-publicize.php index beb043db17f16..987c4d25ee22d 100644 --- a/projects/packages/publicize/src/class-publicize.php +++ b/projects/packages/publicize/src/class-publicize.php @@ -242,25 +242,25 @@ public function get_all_connections_for_user( $args = array() ) { $connections_to_return = array(); if ( ! empty( $connections ) ) { foreach ( (array) $connections as $service_name => $connections_for_service ) { - foreach ( $connections_for_service as $id => $connection ) { + foreach ( $connections_for_service as $connection ) { $user_id = (int) $connection['connection_data']['user_id']; + + $connection_meta = $this->get_connection_meta( $connection ); // phpcs:ignore WordPress.PHP.YodaConditions.NotYoda if ( $user_id === 0 || $this->user_id() === $user_id ) { - if ( $this->use_admin_ui_v1() ) { - $connections_to_return[] = array_merge( - $connection, - array( - 'service_name' => $service_name, - 'connection_id' => $connection['connection_data']['id'], - 'can_disconnect' => self::can_manage_connection( $connection['connection_data'] ), - 'profile_link' => $this->get_profile_link( $service_name, $connection ), - 'shared' => '0' === $connection['connection_data']['user_id'], - 'status' => 'ok', - ) - ); - } else { - $connections_to_return[ $service_name ][ $id ] = $connection; - } + $connections_to_return[] = array( + 'connection_id' => (string) $this->get_connection_id( $connection ), + 'display_name' => $this->get_display_name( $service_name, $connection ), + 'external_handle' => $this->get_external_handle( $service_name, $connection ), + 'external_id' => $connection_meta['external_id'] ?? '', + 'profile_link' => $this->get_profile_link( $service_name, $connection ), + 'profile_picture' => $this->get_profile_picture( $connection ), + 'service_label' => $this->get_service_label( $service_name ), + 'service_name' => $service_name, + 'shared' => ! $user_id, + 'status' => 'ok', + 'user_id' => $user_id, + ); } } } From 2f982e936535022adc6a8b8afd5ae2781010a5c4 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:47:33 +0530 Subject: [PATCH 04/14] Use get_all_connections_for_user method for script data on WPCOM --- .../packages/publicize/src/class-publicize-script-data.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index 1b285f1d09801..ef9ee9f3a3639 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -158,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' => self::publicize()->get_all_connections_for_user(), ), 'shareStatus' => $share_status, ); From 0cd6366ab2ec14e6a9b88aa08e46969995f9ece2 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:47:53 +0530 Subject: [PATCH 05/14] Add status field to connection test results --- .../publicize-connection-test-results.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php index 38b8529d53050..5468da5a6e328 100644 --- a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php +++ b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php @@ -79,6 +79,14 @@ public function get_item_schema() { 'type' => 'string', 'format' => 'uri', ), + 'status' => array( + 'type' => 'string', + 'description' => __( 'The connection status.', 'jetpack' ), + 'enum' => array( + 'ok', + 'broken', + ), + ), ), ); @@ -120,6 +128,8 @@ public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAna foreach ( $mapping as $field => $test_result_field ) { $item[ $field ] = $test_result[ $test_result_field ]; } + // Compare to `true` because the API returns a 'must_reauth' for LinkedIn. + $item['status'] = true === $test_result['connectionTestPassed'] ? 'ok' : 'broken'; } if ( From ced1f63040ec0972f06098ba13959e4ee1307bf6 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:48:13 +0530 Subject: [PATCH 06/14] Fix PHP fatal on WPCOM for profile link --- 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 e4aa5e6639054..e3fc4b7421592 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -530,8 +530,8 @@ public function get_profile_link( $service_name, $connection ) { return 'https://instagram.com/' . $cmeta['connection_data']['meta']['username']; } - if ( 'threads' === $service_name && isset( $connection['external_name'] ) ) { - return 'https://www.threads.net/@' . $connection['external_name']; + if ( 'threads' === $service_name && isset( $cmeta['external_name'] ) ) { + return 'https://www.threads.net/@' . $cmeta['external_name']; } if ( 'mastodon' === $service_name && isset( $cmeta['external_name'] ) ) { From b449a434d3e21da266b51326dfca2fae16d7915c Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:48:29 +0530 Subject: [PATCH 07/14] Ensure to clear old transients to avoid data mismatch --- projects/packages/publicize/src/class-publicize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/publicize/src/class-publicize.php b/projects/packages/publicize/src/class-publicize.php index 987c4d25ee22d..fb75b8cc71277 100644 --- a/projects/packages/publicize/src/class-publicize.php +++ b/projects/packages/publicize/src/class-publicize.php @@ -19,7 +19,7 @@ */ class Publicize extends Publicize_Base { - const JETPACK_SOCIAL_CONNECTIONS_TRANSIENT = 'jetpack_social_connections'; + const JETPACK_SOCIAL_CONNECTIONS_TRANSIENT = 'jetpack_social_connection_list'; /** * Transitory storage of connection testing results. From 1cccf43e2c6633ff8b83c0326918f2623d26fd2b Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:48:42 +0530 Subject: [PATCH 08/14] Sync WPCOM_Features --- .../wpcomsh/wpcom-features/class-wpcom-features.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php b/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php index 94cb012d150b3..e50c986ec5e45 100644 --- a/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php +++ b/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php @@ -1089,12 +1089,8 @@ class WPCOM_Features { ), ), self::SOCIAL_CONNECTIONS_MANAGEMENT => array( - array( - // This feature isn't launched yet, so we're ensuring that it's not available on any plans. - 'before' => '1900-01-01', - self::WPCOM_ALL_SITES, - self::JETPACK_ALL_SITES, - ), + self::WPCOM_ALL_SITES, + self::JETPACK_ALL_SITES, ), self::SOCIAL_EDITOR_PREVIEW => array( array( From e9786535cafcccf038fc69f30d5178c3d303ca66 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 17:50:04 +0530 Subject: [PATCH 09/14] Add changelogs --- .../changelog/update-unify-schema-for-connections-management | 4 ++++ .../changelog/update-unify-schema-for-connections-management | 4 ++++ .../changelog/update-unify-schema-for-connections-management | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 projects/packages/publicize/changelog/update-unify-schema-for-connections-management create mode 100644 projects/plugins/jetpack/changelog/update-unify-schema-for-connections-management create mode 100644 projects/plugins/wpcomsh/changelog/update-unify-schema-for-connections-management diff --git a/projects/packages/publicize/changelog/update-unify-schema-for-connections-management b/projects/packages/publicize/changelog/update-unify-schema-for-connections-management new file mode 100644 index 0000000000000..844c71c7c7f71 --- /dev/null +++ b/projects/packages/publicize/changelog/update-unify-schema-for-connections-management @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Social | Enabled connections management on WPCOM diff --git a/projects/plugins/jetpack/changelog/update-unify-schema-for-connections-management b/projects/plugins/jetpack/changelog/update-unify-schema-for-connections-management new file mode 100644 index 0000000000000..dcb94d4330f9d --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-unify-schema-for-connections-management @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Updated publicize connection test restults endpoint to add status field diff --git a/projects/plugins/wpcomsh/changelog/update-unify-schema-for-connections-management b/projects/plugins/wpcomsh/changelog/update-unify-schema-for-connections-management new file mode 100644 index 0000000000000..844c71c7c7f71 --- /dev/null +++ b/projects/plugins/wpcomsh/changelog/update-unify-schema-for-connections-management @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Social | Enabled connections management on WPCOM From 22b9c81d0a77bb8240ee0606fa9f0012fc763bed Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 19:06:20 +0530 Subject: [PATCH 10/14] Attempt to fix tests --- .../wpcom-fields/test-post-fields-publicize-connections.php | 2 +- .../test_post-fields-publicize-connections-inactive.php | 2 +- .../tests/php/modules/publicize/test_class.publicize.php | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test-post-fields-publicize-connections.php b/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test-post-fields-publicize-connections.php index d0fb64c39ff84..e7a5357e53218 100644 --- a/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test-post-fields-publicize-connections.php +++ b/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test-post-fields-publicize-connections.php @@ -153,7 +153,7 @@ public static function setup_connections_wpcom() { public static function setup_connections_jetpack() { set_transient( - 'jetpack_social_connections', + 'jetpack_social_connection_list', array( // Normally connected facebook. 'facebook' => array( diff --git a/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test_post-fields-publicize-connections-inactive.php b/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test_post-fields-publicize-connections-inactive.php index ce6dac2fbca42..a8ae33f2e90fe 100644 --- a/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test_post-fields-publicize-connections-inactive.php +++ b/projects/plugins/jetpack/tests/php/core-api/wpcom-fields/test_post-fields-publicize-connections-inactive.php @@ -50,7 +50,7 @@ public static function wpSetUpBeforeClass( $factory ) { self::$user_id = $factory->user->create( array( 'role' => 'administrator' ) ); set_transient( - 'jetpack_social_connections', + 'jetpack_social_connection_list', array( // Normally connected facebook. 'facebook' => array( diff --git a/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php b/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php index 5c89e913f5466..236c34466d414 100644 --- a/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php +++ b/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php @@ -255,6 +255,7 @@ public function test_publicize_get_all_connections_for_user() { 'id_number' => array( 'connection_data' => array( 'user_id' => 0, + 'id' => '456', ), ), ); @@ -262,6 +263,7 @@ public function test_publicize_get_all_connections_for_user() { 'id_number_2' => array( 'connection_data' => array( 'user_id' => 1, + 'id' => '456', ), ), ); From e9a6ae6344593f3b929b485ab3c810b05e777e8a Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Thu, 12 Dec 2024 19:23:37 +0530 Subject: [PATCH 11/14] external_display may be empty in tests --- projects/packages/publicize/src/class-publicize-base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index e3fc4b7421592..899ebceb218fa 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -605,7 +605,7 @@ public function get_display_name( $service_name, $connection ) { return $cmeta['external_display']; } - $connection_display = $cmeta['external_display']; + $connection_display = $cmeta['external_display'] ?? ''; if ( empty( $connection_display ) ) { $connection_display = $cmeta['external_name']; From d6471f524a60f69859f576d2da437def8710562b Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 13 Dec 2024 09:26:50 +0530 Subject: [PATCH 12/14] external_name may be empty in tests --- projects/packages/publicize/src/class-publicize-base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index 899ebceb218fa..fae98d58c0c21 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -608,7 +608,7 @@ public function get_display_name( $service_name, $connection ) { $connection_display = $cmeta['external_display'] ?? ''; if ( empty( $connection_display ) ) { - $connection_display = $cmeta['external_name']; + $connection_display = $cmeta['external_name'] ?? ''; } return $connection_display; From 1b6db8e930d50a33d90a33c23cb15ac8ab21a89d Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 13 Dec 2024 09:28:02 +0530 Subject: [PATCH 13/14] Revert "Sync WPCOM_Features" This reverts commit 1cccf43e2c6633ff8b83c0326918f2623d26fd2b. --- .../wpcomsh/wpcom-features/class-wpcom-features.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php b/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php index e50c986ec5e45..94cb012d150b3 100644 --- a/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php +++ b/projects/plugins/wpcomsh/wpcom-features/class-wpcom-features.php @@ -1089,8 +1089,12 @@ class WPCOM_Features { ), ), self::SOCIAL_CONNECTIONS_MANAGEMENT => array( - self::WPCOM_ALL_SITES, - self::JETPACK_ALL_SITES, + array( + // This feature isn't launched yet, so we're ensuring that it's not available on any plans. + 'before' => '1900-01-01', + self::WPCOM_ALL_SITES, + self::JETPACK_ALL_SITES, + ), ), self::SOCIAL_EDITOR_PREVIEW => array( array( From a1e6be491c07fe9adb4fe344c002c265b11c6ab9 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 13 Dec 2024 09:43:23 +0530 Subject: [PATCH 14/14] Fix tests --- .../publicize/src/class-publicize-base.php | 4 +- .../publicize/test_class.publicize.php | 47 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/projects/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index fae98d58c0c21..e3fc4b7421592 100644 --- a/projects/packages/publicize/src/class-publicize-base.php +++ b/projects/packages/publicize/src/class-publicize-base.php @@ -605,10 +605,10 @@ public function get_display_name( $service_name, $connection ) { return $cmeta['external_display']; } - $connection_display = $cmeta['external_display'] ?? ''; + $connection_display = $cmeta['external_display']; if ( empty( $connection_display ) ) { - $connection_display = $cmeta['external_name'] ?? ''; + $connection_display = $cmeta['external_name']; } return $connection_display; diff --git a/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php b/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php index 236c34466d414..f8094d212c10f 100644 --- a/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php +++ b/projects/plugins/jetpack/tests/php/modules/publicize/test_class.publicize.php @@ -253,18 +253,22 @@ public function test_publicize_post_type_is_publicizeable_cpt() { public function test_publicize_get_all_connections_for_user() { $facebook_connection = array( 'id_number' => array( - 'connection_data' => array( + 'connection_data' => array( 'user_id' => 0, 'id' => '456', ), + 'external_display' => 'Test', + 'external_name' => 'test', ), ); $twitter_connection = array( 'id_number_2' => array( - 'connection_data' => array( + 'connection_data' => array( 'user_id' => 1, 'id' => '456', ), + 'external_display' => 'Test', + 'external_name' => 'test', ), ); @@ -277,23 +281,54 @@ public function test_publicize_get_all_connections_for_user() { $publicize = publicize_init(); + $fb_result = array( + 'connection_id' => '456', + 'display_name' => 'Test', + 'external_handle' => '', + 'external_id' => '', + 'profile_link' => false, + 'profile_picture' => '', + 'service_label' => 'Facebook', + 'service_name' => 'facebook', + 'shared' => true, + 'status' => 'ok', + 'user_id' => 0, + ); + + $twitter_result = array( + 'connection_id' => '456', + 'display_name' => 'Test', + 'external_handle' => '', + 'external_id' => '', + 'profile_link' => 'https://twitter.com/est', + 'profile_picture' => '', + 'service_label' => 'Twitter', + 'service_name' => 'twitter', + 'shared' => false, + 'status' => 'ok', + 'user_id' => 1, + ); + // When logged out, assert that blog-level connections are returned. wp_set_current_user( 0 ); - $this->assertSame( array( 'facebook' => $facebook_connection ), $publicize->get_all_connections_for_user() ); + $this->assertSame( + array( $fb_result ), + $publicize->get_all_connections_for_user() + ); // When logged in, assert that blog-level connections AND any connections for the current user are returned. wp_set_current_user( 1 ); $this->assertSame( array( - 'facebook' => $facebook_connection, - 'twitter' => $twitter_connection, + $fb_result, + $twitter_result, ), $publicize->get_all_connections_for_user() ); // There are no connections for user 2, so we should only get blog-level connections. wp_set_current_user( 2 ); - $this->assertSame( array( 'facebook' => $facebook_connection ), $publicize->get_all_connections_for_user() ); + $this->assertSame( array( $fb_result ), $publicize->get_all_connections_for_user() ); } /**