Skip to content

Commit

Permalink
Publicize: Mave the publicize-connections endpoint to the package
Browse files Browse the repository at this point in the history
This is following on from various discussions we've been having about
how to refactor the API endpoints for connections management. This is a
variation on the approach in #40607 but repurposes the existing endpoint
rather than creating a new backwards compatible one.
  • Loading branch information
pablinos committed Dec 16, 2024
1 parent 5e270fb commit e0f992b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Publicize: Move the publicize-connections endpoint to the package
2 changes: 2 additions & 0 deletions projects/packages/publicize/src/class-publicize-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function on_jetpack_feature_publicize_enabled() {

}

new WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections();

Check failure on line 40 in projects/packages/publicize/src/class-publicize-setup.php

View workflow job for this annotation

GitHub Actions / Static analysis

NOOPError PhanNoopNew Unused result of new object creation expression in new WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections() (this may be called for the side effects of the non-empty constructor or destructor)

// 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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' ),
),
)
);
}

Expand Down Expand Up @@ -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 ),

Check failure on line 196 in projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredMethod Call to undeclared method \Automattic\Jetpack\Publicize\Publicize::get_external_handle
'external_id' => $connection_meta['external_id'] ?? '',
'profile_link' => $publicize->get_profile_link( $service_name, $connection ),
'profile_picture' => $publicize->get_profile_picture( $connection ),

Check failure on line 199 in projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php

View workflow job for this annotation

GitHub Actions / Static analysis

AccessError PhanAccessMethodPrivate Cannot access private method \Automattic\Jetpack\Publicize\Publicize::get_profile_picture defined at src/class-publicize-base.php:611
'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'] ?? '',
);
}
}
Expand All @@ -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 );
}

Expand All @@ -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
*
Expand Down Expand Up @@ -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() )
);
}
Expand All @@ -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' );

Check failure on line 319 in projects/packages/publicize/src/class-wpcom-rest-api-v2-endpoint-list-publicize-connections.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredFunction Call to undeclared function \wpcom_rest_api_v2_load_plugin()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

use Automattic\Jetpack\Connection\Client;

require_once __DIR__ . '/publicize-connections.php';

/**
* Publicize: Share post class.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
* @package automattic/jetpack
*/

require_once __DIR__ . '/publicize-connections.php';

/**
* Publicize: List Connection Test Result Data
*
* All the same data as the Publicize Connections Endpoint, plus test results.
*
* @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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Publicize: Move the publicize-connections endpoint to the package

0 comments on commit e0f992b

Please sign in to comment.