PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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.7, at jetpack_vendor/automattic/jetpack-publicize/src/class-services.php

144 lines 3.4 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 }
76
77 /**
78 * Filters the list of Publicize services available to the site.
79 *
80 * @since 0.84.3
81 *
82 * @param array $services List of services.
83 */
84 return (array) apply_filters( 'jetpack_publicize_services', $services );
85 }
86
87 /**
88 * Fetch services from the REST API and cache them.
89 *
90 * @return array
91 */
92 public static function fetch_and_cache_services() {
93 $proxy = new Proxy_Requests( 'publicize/services' );
94
95 $request = new WP_REST_Request( 'GET' );
96
97 $response = $proxy->proxy_request_to_wpcom_as_user( $request );
98
99 if ( is_wp_error( $response ) ) {
100 // @todo log error.
101 return array();
102 }
103
104 if ( is_array( $response ) ) {
105 /**
106 * Let us set the connect URL to null.
107 *
108 * Reason:
109 * We do not want to cache the connect URL, as it's user-specific,
110 * but the services are for all users.
111 * The intention is to get the connect URL on demand via an API call when needed.
112 */
113 $services = array_map(
114 function ( $service ) {
115 return array_merge(
116 $service,
117 array(
118 'url' => null,
119 )
120 );
121 },
122 $response
123 );
124
125 if ( ! set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS ) ) {
126 // If the transient has beeen set in another request, the call to set_transient can fail.
127 // If so, we can delete the transient and try again.
128 self::clear_cache();
129
130 set_transient( self::SERVICES_TRANSIENT, $services, DAY_IN_SECONDS );
131 }
132 }
133
134 return $response;
135 }
136
137 /**
138 * Clear the services cache.
139 */
140 public static function clear_cache() {
141 delete_transient( self::SERVICES_TRANSIENT );
142 }
143 }
144