Skip to content

Commit

Permalink
Social: Don't call endpoint for services on WPCOM (#40596)
Browse files Browse the repository at this point in the history
* Don't make request if we are on WPCOM

* changelog

* Use transient

* Refactor to own class

* Remove param from script data
  • Loading branch information
gmjuhasz authored Dec 16, 2024
1 parent 6ff35a2 commit 9e30ed6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Script data: Don't call service endpoint on wpcom
26 changes: 4 additions & 22 deletions projects/packages/publicize/src/class-publicize-script-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Current_Plan;
use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings;
use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
use Automattic\Jetpack\Publicize\Services as Publicize_Services;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;
use Jetpack_Options;
Expand All @@ -21,6 +21,8 @@
*/
class Publicize_Script_Data {

const SERVICES_TRANSIENT = 'jetpack_social_services_list';

/**
* Get the publicize instance - properly typed
*
Expand Down Expand Up @@ -224,27 +226,7 @@ public static function get_shares_data() {
* @return array List of external services and their settings.
*/
public static function get_supported_services() {
$site_id = Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
return array();
}
$path = sprintf( '/sites/%d/external-services', $site_id );
$response = Client::wpcom_json_api_request_as_user( $path );
if ( is_wp_error( $response ) ) {
return array();
}
$body = json_decode( wp_remote_retrieve_body( $response ) );

$services = $body->services ?? array();

return array_values(
array_filter(
(array) $services,
function ( $service ) {
return isset( $service->type ) && 'publicize' === $service->type;
}
)
);
return Publicize_Services::get_all();
}

/**
Expand Down
84 changes: 84 additions & 0 deletions projects/packages/publicize/src/class-services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Publicize Services class.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;

/**
* Publicize Services class.
*/
class Services {

const SERVICES_TRANSIENT = 'jetpack_social_services_list';

/**
* Get all services.
*
* @param bool $force_refresh Whether to force a refresh of the services.
* @return array
*/
public static function get_all( $force_refresh = false ) {
if ( defined( 'IS_WPCOM' ) && constant( 'IS_WPCOM' ) ) {
if ( function_exists( 'require_lib' ) ) {
// @phan-suppress-next-line PhanUndeclaredFunction - phan is dumb not to see the function_exists check.
require_lib( 'external-connections' );
}

// @phan-suppress-next-line PhanUndeclaredClassMethod - We are here because we are on WPCOM.
$external_connections = \WPCOM_External_Connections::init();
$services = array_values( $external_connections->get_external_services_list( 'publicize', get_current_blog_id() ) );

return $services;
}

// Checking the cache.
$services = get_transient( self::SERVICES_TRANSIENT );
if ( false !== $services && ! $force_refresh ) {
return $services;
}

return self::fetch_and_cache_services();
}

/**
* Fetch services from the REST API and cache them.
*
* @return array
*/
public static function fetch_and_cache_services() {
// Fetch the services.
$site_id = Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
return array();
}
$path = sprintf( '/sites/%d/external-services', $site_id );
$response = Client::wpcom_json_api_request_as_user( $path );
if ( is_wp_error( $response ) ) {
return array();
}
$body = json_decode( wp_remote_retrieve_body( $response ) );

$services = $body->services ?? array();

$formatted_services = array_values(
array_filter(
(array) $services,
function ( $service ) {
return isset( $service->type ) && 'publicize' === $service->type;
}
)
);

if ( ! empty( $formatted_services ) ) {
set_transient( self::SERVICES_TRANSIENT, $formatted_services, DAY_IN_SECONDS );
}

return $formatted_services;
}
}

0 comments on commit 9e30ed6

Please sign in to comment.