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 diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php new file mode 100644 index 0000000000000..de30d41e264ae --- /dev/null +++ b/projects/packages/publicize/src/class-connections.php @@ -0,0 +1,94 @@ +is_wpcom_simple(); + + if ( $is_wpcom ) { + $connections = Connections_Controller::get_connections( $run_tests ); + } else { + $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. + * + * @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 ); + + // TODO Implement caching here. + + return $connections; + } +} 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'] ) ) { 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 ); } /**