get_external_services_list( 'publicize', get_current_blog_id() ); $items = array(); foreach ( $services as $service ) { // Set the fields as per the schema in Services_Controller. $items[] = array( 'id' => $service['ID'], 'description' => $service['description'], 'label' => $service['label'], 'status' => $service['status'] ?? 'ok', 'supports' => array( 'additional_users' => $service['multiple_external_user_ID_support'], 'additional_users_only' => $service['external_users_only'], ), 'url' => $service['connect_URL'], ); } return $items; } /** * Get all services. * * @param array $args Arguments * - 'ignore_cache': bool Whether to ignore the cache and fetch the connections from the API. * @return array */ public static function get_all( $args = array() ) { if ( Publicize_Utils::is_wpcom() ) { $services = self::wpcom_get_all(); } else { $ignore_cache = $args['ignore_cache'] ?? false; $services = get_transient( self::SERVICES_TRANSIENT ); if ( $ignore_cache || false === $services ) { $services = self::fetch_and_cache_services(); } } /** * Filters the list of Publicize services available to the site. * * @since 0.84.3 * * @param array $services List of services. */ return (array) apply_filters( 'jetpack_publicize_services', $services ); } /** * Fetch services from the REST API and cache them. * * @return array */ public static function fetch_and_cache_services() { $proxy = new Proxy_Requests( 'publicize/services' ); $request = new WP_REST_Request( 'GET' ); $response = $proxy->proxy_request_to_wpcom_as_user( $request ); if ( is_wp_error( $response ) ) { // @todo log error. return array(); } if ( is_array( $response ) ) { /** * Let us set the connect URL to null. * * Reason: * We do not want to cache the connect URL, as it's user-specific, * but the services are for all users. * The intention is to get the connect URL on demand via an API call when needed. */ $services = array_map( function ( $service ) { return array_merge( $service, array( 'url' => null, ) ); }, $response ); if ( ! set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS ) ) { // If the transient has beeen set in another request, the call to set_transient can fail. // If so, we can delete the transient and try again. self::clear_cache(); set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS ); } } return $response; } /** * Clear the services cache. */ public static function clear_cache() { delete_transient( self::SERVICES_TRANSIENT ); } }