-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Social: Use connections REST endpoint for initial state (#40677)
* 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
1 parent
8e4dd7c
commit 9af33a7
Showing
4 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/publicize/changelog/update-unify-social-connections-schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters