diff --git a/projects/packages/publicize/changelog/update-social-script-data-services-wpcom-check b/projects/packages/publicize/changelog/update-social-script-data-services-wpcom-check new file mode 100644 index 0000000000000..2334298f97e5a --- /dev/null +++ b/projects/packages/publicize/changelog/update-social-script-data-services-wpcom-check @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Script data: Don't call service endpoint on wpcom diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index 1b285f1d09801..b4e7dded6b62d 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -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; @@ -21,6 +21,8 @@ */ class Publicize_Script_Data { + const SERVICES_TRANSIENT = 'jetpack_social_services_list'; + /** * Get the publicize instance - properly typed * @@ -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(); } /** diff --git a/projects/packages/publicize/src/class-services.php b/projects/packages/publicize/src/class-services.php new file mode 100644 index 0000000000000..26e49b096b40b --- /dev/null +++ b/projects/packages/publicize/src/class-services.php @@ -0,0 +1,84 @@ +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; + } +}