PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-publicize / src / class-services.php

class-services.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at jetpack_vendor/automattic/jetpack-publicize/src/class-services.php

162 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Publicize Services class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8 namespace Automattic\Jetpack\Publicize;
9
10 use Automattic\Jetpack\Publicize\REST_API\Proxy_Requests;
11 use WP_REST_Request;
12
13 /**
14 * Publicize Services class.
15 */
16 class Services {
17
18 const SERVICES_TRANSIENT = 'jetpack_social_services_list_v2';
19
20 /**
21 * Get the available publicize services. Meant to be called directly only on WPCOM.
22 *
23 * @return array
24 */
25 public static function wpcom_get_all() {
26 // Ensure that we are on WPCOM.
27 Publicize_Utils::assert_is_wpcom( __METHOD__ );
28
29 require_lib( 'external-connections' );
30
31 $external_connections = \WPCOM_External_Connections::init();
32
33 $services = $external_connections->get_external_services_list( 'publicize', get_current_blog_id() );
34
35 $items = array();
36
37 foreach ( $services as $service ) {
38 // Set the fields as per the schema in Services_Controller.
39 $items[] = array(
40 'id' => $service['ID'],
41 'description' => $service['description'],
42 'label' => $service['label'],
43 'status' => $service['status'] ?? 'ok',
44 'supports' => array(
45 'additional_users' => $service['multiple_external_user_ID_support'],
46 'additional_users_only' => $service['external_users_only'],
47 ),
48 'url' => $service['connect_URL'],
49 );
50 }
51
52 return $items;
53 }
54
55 /**
56 * Get all services.
57 *
58 * @param array $args Arguments
59 * - 'ignore_cache': bool Whether to ignore the cache and fetch the connections from the API.
60 * @return array
61 */
62 public static function get_all( $args = array() ) {
63
64 if ( Publicize_Utils::is_wpcom() ) {
65 $services = self::wpcom_get_all();
66 } else {
67
68 $ignore_cache = $args['ignore_cache'] ?? false;
69
70 $services = get_transient( self::SERVICES_TRANSIENT );
71
72 if ( $ignore_cache || false === $services ) {
73 $services = self::fetch_and_cache_services();
74 }
75 // This is here for backwards compatibility
76 // TODO Remove this array_map() call after April 2025 release of Jetpack.
77 $services = array_map(
78 function ( $service ) {
79 global $publicize;
80
81 return array_merge(
82 $service,
83 array(
84 'ID' => $service['id'],
85 'connect_URL' => $publicize->connect_url( $service['id'], 'connect' ),
86 'external_users_only' => $service['supports']['additional_users_only'],
87 'multiple_external_user_ID_support' => $service['supports']['additional_users'],
88 )
89 );
90 },
91 $services
92 );
93 }
94
95 /**
96 * Filters the list of Publicize services available to the site.
97 *
98 * @since 0.84.3
99 *
100 * @param array $services List of services.
101 */
102 return (array) apply_filters( 'jetpack_publicize_services', $services );
103 }
104
105 /**
106 * Fetch services from the REST API and cache them.
107 *
108 * @return array
109 */
110 public static function fetch_and_cache_services() {
111 $proxy = new Proxy_Requests( 'publicize/services' );
112
113 $request = new WP_REST_Request( 'GET' );
114
115 $response = $proxy->proxy_request_to_wpcom_as_user( $request );
116
117 if ( is_wp_error( $response ) ) {
118 // @todo log error.
119 return array();
120 }
121
122 if ( is_array( $response ) ) {
123 /**
124 * Let us set the connect URL to null.
125 *
126 * Reason:
127 * We do not want to cache the connect URL, as it's user-specific,
128 * but the services are for all users.
129 * The intention is to get the connect URL on demand via an API call when needed.
130 */
131 $services = array_map(
132 function ( $service ) {
133 return array_merge(
134 $service,
135 array(
136 'url' => null,
137 )
138 );
139 },
140 $response
141 );
142
143 if ( ! set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS ) ) {
144 // If the transient has beeen set in another request, the call to set_transient can fail.
145 // If so, we can delete the transient and try again.
146 self::clear_cache();
147
148 set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS );
149 }
150 }
151
152 return $response;
153 }
154
155 /**
156 * Clear the services cache.
157 */
158 public static function clear_cache() {
159 delete_transient( self::SERVICES_TRANSIENT );
160 }
161 }
162