Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social | Standardize connections API schema #40589

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Social | Enabled connections management on WPCOM
39 changes: 36 additions & 3 deletions projects/packages/publicize/src/class-publicize-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
34 changes: 17 additions & 17 deletions projects/packages/publicize/src/class-publicize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
);

Expand Down Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Updated publicize connection test restults endpoint to add status field
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just to be more descriptive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned in the PR description

Changed the transient name for connections to ensure to clear old transients to avoid data mismatch

array(
// Normally connected facebook.
'facebook' => array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);

Expand All @@ -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() );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Social | Enabled connections management on WPCOM
Loading