Skip to content

Commit

Permalink
Social: Use connections REST endpoint for initial state (#40677)
Browse files Browse the repository at this point in the history
* Create connections class for caching

* Update script data to use connections from the REST endpoint

* changelog

* Restore deprecated connection fields for time being

* Disable caching for now

* Fix display_name for Mastodon

* Remove the unused caching logic
  • Loading branch information
manzoorwanijk committed Dec 26, 2024
1 parent 8e4dd7c commit 9af33a7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Social: Use connections REST endpoint for initial state
94 changes: 94 additions & 0 deletions projects/packages/publicize/src/class-connections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Publicize Connections class.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Connection;
use Automattic\Jetpack\Publicize\REST_API\Connections_Controller;
use Automattic\Jetpack\Status\Host;

/**
* Publicize Connections class.
*/
class Connections {

const CONNECTIONS_TRANSIENT = 'jetpack_social_connections_list';

/**
* Get all connections.
*
* @param array $args Arguments
* - 'clear_cache': bool Whether to clear the cache.
* - 'test_connections': bool Whether to run connection tests.
* @return array
*/
public static function get_all( $args = array() ) {

$run_tests = $args['test_connections'] ?? false;

$is_wpcom = ( new Host() )->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;
}
}
4 changes: 2 additions & 2 deletions projects/packages/publicize/src/class-publicize-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
26 changes: 15 additions & 11 deletions projects/packages/publicize/src/class-publicize-script-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
class Publicize_Script_Data {

const SERVICES_TRANSIENT = 'jetpack_social_services_list';

/**
* Get the publicize instance - properly typed
*
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -238,17 +235,24 @@ public static function get_api_paths() {

$is_wpcom = ( new Host() )->is_wpcom_platform();

$commom_paths = array(
'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1',
);

$specific_paths = array();

if ( $is_wpcom ) {
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 );
}

/**
Expand Down

0 comments on commit 9af33a7

Please sign in to comment.