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: Use connections REST endpoint for initial state #40677

Merged
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: 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'];
Copy link
Member Author

Choose a reason for hiding this comment

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

We want to have @[email protected] instead of just jetpack_reach

}

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_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 );
}

/**
Expand Down
Loading