diff --git a/projects/packages/publicize/changelog/try-move-publicize-connections-endpoint b/projects/packages/publicize/changelog/try-move-publicize-connections-endpoint new file mode 100644 index 0000000000000..bc1a2d0bf680d --- /dev/null +++ b/projects/packages/publicize/changelog/try-move-publicize-connections-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Publicize: Move the publicize-connections endpoint to the package diff --git a/projects/packages/publicize/src/class-publicize-setup.php b/projects/packages/publicize/src/class-publicize-setup.php index 383ef289fab5e..7e3e686b6fec0 100644 --- a/projects/packages/publicize/src/class-publicize-setup.php +++ b/projects/packages/publicize/src/class-publicize-setup.php @@ -37,6 +37,8 @@ public static function on_jetpack_feature_publicize_enabled() { } + new WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections(); + // Adding on a higher priority to make sure we're the first field registered. // The priority parameter can be removed once we deprecate WPCOM_REST_API_V2_Post_Publicize_Connections_Field add_action( 'rest_api_init', array( new Connections_Post_Field(), 'register_fields' ), 5 ); diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php b/projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php similarity index 55% rename from projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php rename to projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php index 67fa5b25ced9e..b0cd1aa6a7e77 100644 --- a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/publicize-connections.php +++ b/projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php @@ -4,6 +4,15 @@ * * @package automattic/jetpack */ +namespace Automattic\Jetpack\Publicize; + +use Automattic\Jetpack\Connection\Client; +use Automattic\Jetpack\Connection\Manager; +use WP_Error; +use WP_REST_Controller; +use WP_REST_Request; +use WP_REST_Response; +use WP_REST_Server; /** * Publicize: List Connections @@ -65,39 +74,85 @@ public function register_routes() { * @return array */ protected function get_connection_schema_properties() { - return array( + $deprecated_fields = array( 'id' => array( - 'description' => __( 'Unique identifier for the Jetpack Social connection', 'jetpack' ), - 'type' => 'string', - ), - 'service_name' => array( - 'description' => __( 'Alphanumeric identifier for the Jetpack Social service', 'jetpack' ), - 'type' => 'string', - ), - 'display_name' => array( - 'description' => __( 'Display name of the connected account', 'jetpack' ), 'type' => 'string', + 'description' => __( 'Unique identifier for the Jetpack Social connection.', 'jetpack-publicize-pkg' ) . ' ' . sprintf( + /* translators: %s is the new field name */ + __( 'Deprecated in favor of %s.', 'jetpack-publicize-pkg' ), + 'connection_id' + ), ), 'username' => array( - 'description' => __( 'Username of the connected account', 'jetpack' ), 'type' => 'string', + 'description' => __( 'Username of the connected account.', 'jetpack-publicize-pkg' ) . ' ' . sprintf( + /* translators: %s is the new field name */ + __( 'Deprecated in favor of %s.', 'jetpack-publicize-pkg' ), + 'external_handle' + ), ), 'profile_display_name' => array( - 'description' => __( 'The name to display in the profile of the connected account', 'jetpack' ), - 'type' => 'string', - ), - 'profile_picture' => array( - 'description' => __( 'Profile picture of the connected account', 'jetpack' ), 'type' => 'string', + 'description' => __( 'The name to display in the profile of the connected account.', 'jetpack-publicize-pkg' ) . ' ' . sprintf( + /* translators: %s is the new field name */ + __( 'Deprecated in favor of %s.', 'jetpack-publicize-pkg' ), + 'display_name' + ), ), 'global' => array( - 'description' => __( 'Is this connection available to all users?', 'jetpack' ), 'type' => 'boolean', + 'description' => __( 'Is this connection available to all users?', 'jetpack-publicize-pkg' ) . ' ' . sprintf( + /* translators: %s is the new field name */ + __( 'Deprecated in favor of %s.', 'jetpack-publicize-pkg' ), + 'shared' + ), ), - 'external_id' => array( - 'description' => __( 'The external ID of the connected account', 'jetpack' ), - 'type' => 'string', - ), + ); + + return array_merge( + $deprecated_fields, + array( + 'connection_id' => array( + 'type' => 'string', + 'description' => __( 'Connection ID of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'display_name' => array( + 'type' => 'string', + 'description' => __( 'Display name of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'external_handle' => array( + 'type' => 'string', + 'description' => __( 'The external handle or username of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'external_id' => array( + 'type' => 'string', + 'description' => __( 'The external ID of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'profile_link' => array( + 'type' => 'string', + 'description' => __( 'Profile link of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'profile_picture' => array( + 'type' => 'string', + 'description' => __( 'URL of the profile picture of the connected account.', 'jetpack-publicize-pkg' ), + ), + 'service_label' => array( + 'type' => 'string', + 'description' => __( 'Human-readable label for the Jetpack Social service.', 'jetpack-publicize-pkg' ), + ), + 'service_name' => array( + 'type' => 'string', + 'description' => __( 'Alphanumeric identifier for the Jetpack Social service.', 'jetpack-publicize-pkg' ), + ), + 'shared' => array( + 'type' => 'boolean', + 'description' => __( 'Whether the connection is shared with other users.', 'jetpack-publicize-pkg' ), + ), + 'user_id' => array( + 'type' => 'integer', + 'description' => __( 'ID of the user the connection belongs to.', 'jetpack-publicize-pkg' ), + ), + ) ); } @@ -133,18 +188,26 @@ protected function get_connections() { foreach ( $connections as $connection ) { $connection_meta = $publicize->get_connection_meta( $connection ); $connection_data = $connection_meta['connection_data']; + $connection_id = $publicize->get_connection_id( $connection ); $items[] = array( - 'id' => (string) $publicize->get_connection_unique_id( $connection ), - 'connection_id' => (string) $publicize->get_connection_id( $connection ), - 'service_name' => $service_name, + 'connection_id' => $connection_id, 'display_name' => $publicize->get_display_name( $service_name, $connection ), + 'external_handle' => $publicize->get_external_handle( $service_name, $connection ), + 'external_id' => $connection_meta['external_id'] ?? '', + 'profile_link' => $publicize->get_profile_link( $service_name, $connection ), + 'profile_picture' => $publicize->get_profile_picture( $connection ), + 'service_label' => Publicize::get_service_label( $service_name ), + 'service_name' => $service_name, + 'shared' => ! $connection_data['user_id'], + 'user_id' => (int) $connection_data['user_id'], + + // Deprecated fields. + 'id' => (string) $publicize->get_connection_unique_id( $connection ), 'username' => $publicize->get_username( $service_name, $connection ), 'profile_display_name' => ! empty( $connection_meta['profile_display_name'] ) ? $connection_meta['profile_display_name'] : '', - 'profile_picture' => ! empty( $connection_meta['profile_picture'] ) ? $connection_meta['profile_picture'] : '', // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- We expect an integer, but do loose comparison below in case some other type is stored. 'global' => 0 == $connection_data['user_id'], - 'external_id' => $connection_meta['external_id'] ?? '', ); } } @@ -162,7 +225,9 @@ protected function get_connections() { public function get_items( $request ) { $items = array(); - foreach ( $this->get_connections() as $item ) { + $connections = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? $this->get_connections() : $this->get_connections_from_wpcom(); + + foreach ( $connections as $item ) { $items[] = $this->prepare_item_for_response( $item, $request ); } @@ -173,6 +238,30 @@ public function get_items( $request ) { return $response; } + /** + * Proxy the request to the WPCOM API + * + * @return array Connection objects in the same shape as from `get_connections` + */ + protected function get_connections_from_wpcom() { + $site_id = Manager::get_site_id( true ); + if ( ! $site_id ) { + return array(); + } + + $response = Client::wpcom_json_api_request_as_user( sprintf( '/sites/%d/publicize/connections', $site_id ), 'v2', array( 'method' => 'GET' ) ); + + if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { + // TODO log error. + return array(); + } + + $body = wp_remote_retrieve_body( $response ); + + $items = json_decode( $body, true ); + return $items ? $items : array(); + } + /** * Filters out data based on ?_fields= request parameter * @@ -209,7 +298,7 @@ public function get_items_permission_check() { if ( ! $publicize ) { return new WP_Error( 'publicize_not_available', - __( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack' ), + __( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack-publicize-pkg' ), array( 'status' => rest_authorization_required_code() ) ); } @@ -220,9 +309,12 @@ public function get_items_permission_check() { return new WP_Error( 'invalid_user_permission_publicize', - __( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack' ), + __( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack-publicize-pkg' ), array( 'status' => rest_authorization_required_code() ) ); } } -wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections' ); + +if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + wpcom_rest_api_v2_load_plugin( 'Automattic\Jetpack\Publicize\WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections' ); +} diff --git a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-publicize-share-post.php b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-publicize-share-post.php index f9522d94231e6..764b00f4a85d2 100644 --- a/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-publicize-share-post.php +++ b/projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-publicize-share-post.php @@ -10,8 +10,6 @@ use Automattic\Jetpack\Connection\Client; -require_once __DIR__ . '/publicize-connections.php'; - /** * Publicize: Share post class. */ 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..012bee756d672 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 @@ -5,8 +5,6 @@ * @package automattic/jetpack */ -require_once __DIR__ . '/publicize-connections.php'; - /** * Publicize: List Connection Test Result Data * @@ -14,7 +12,7 @@ * * @since 6.8 */ -class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections { +class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends Automattic\Jetpack\Publicize\WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections { /** * Constructor. */ diff --git a/projects/plugins/jetpack/changelog/try-move-publicize-connections-endpoint b/projects/plugins/jetpack/changelog/try-move-publicize-connections-endpoint new file mode 100644 index 0000000000000..ea450e15a2a54 --- /dev/null +++ b/projects/plugins/jetpack/changelog/try-move-publicize-connections-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Publicize: Move the publicize-connections endpoint to the package