diff --git a/projects/packages/publicize/src/class-services.php b/projects/packages/publicize/src/class-services.php new file mode 100644 index 0000000000000..9a33cc08792cc --- /dev/null +++ b/projects/packages/publicize/src/class-services.php @@ -0,0 +1,73 @@ +is_wpcom_simple(); + + if ( $is_wpcom ) { + // We don't need to cache services for simple sites. + return Services_Controller::get_supported_services(); + } + + $clear_cache = $args['clear_cache'] ?? false; + + if ( $clear_cache ) { + self::clear_transient(); + } + + $services = get_transient( self::SERVICES_TRANSIENT ); + + // This can be an empty array, so we need to check for false. + if ( false === $services ) { + $services = self::fetch_and_cache_services(); + } + + return $services; + } + + /** + * Fetch services from the REST API and cache them. + * + * @return array + */ + public static function fetch_and_cache_services() { + $services = Services_Controller::get_supported_services(); + + if ( ! empty( $services ) ) { + set_transient( self::SERVICES_TRANSIENT, $services, HOUR_IN_SECONDS * 4 ); + } + + return $services; + } + + /** + * Delete the transient. + */ + protected static function clear_transient() { + delete_transient( self::SERVICES_TRANSIENT ); + } +}