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/packages/publicize/src/class-publicize-base.php b/projects/packages/publicize/src/class-publicize-base.php index b88bd1a6b12ca..e3fc4b7421592 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 * @@ -470,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 * @@ -497,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'] ) ) { @@ -608,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'] ) ) { 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, ); diff --git a/projects/packages/publicize/src/class-publicize.php b/projects/packages/publicize/src/class-publicize.php index beb043db17f16..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. @@ -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, + ); } } } 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 ( 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/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..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,16 +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', ), ); @@ -275,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() ); } /** 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