PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 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 All 508 releases
← All changes | _inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php +260 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,260 @@
1 +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 +/**
3 + * Fetch information about Publicize connections on a site, including tests and connection status.
4 + *
5 + * @package automattic/jetpack
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 + exit( 0 );
10 +}
11 +
12 +/**
13 + * Publicize: List Connection Test Result Data
14 + *
15 + * All the same data as the Publicize Connections Endpoint, plus test results.
16 + *
17 + * @deprecated 14.4 Deprecated in favor of /wpcom/v2/publicize/connections?test_connections=1
18 + *
19 + * @since 6.8
20 + */
21 +class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends WP_REST_Controller {
22 +
23 + /**
24 + * Flag to help WordPress.com decide where it should look for
25 + * Publicize data. Ignored for direct requests to Jetpack sites.
26 + *
27 + * @var bool $wpcom_is_wpcom_only_endpoint
28 + */
29 + public $wpcom_is_wpcom_only_endpoint = true;
30 +
31 + /**
32 + * Constructor.
33 + */
34 + public function __construct() {
35 + $this->namespace = 'wpcom/v2';
36 + $this->rest_base = 'publicize/connection-test-results';
37 +
38 + add_action( 'rest_api_init', array( $this, 'register_routes' ) );
39 + }
40 +
41 + /**
42 + * Called automatically on `rest_api_init()`.
43 + */
44 + public function register_routes() {
45 + register_rest_route(
46 + $this->namespace,
47 + '/' . $this->rest_base,
48 + array(
49 + array(
50 + 'methods' => WP_REST_Server::READABLE,
51 + 'callback' => array( $this, 'get_items' ),
52 + 'permission_callback' => array( $this, 'get_items_permission_check' ),
53 + ),
54 + 'schema' => array( $this, 'get_public_item_schema' ),
55 + )
56 + );
57 + }
58 +
59 + /**
60 + * Adds the test results properties to the Connection schema.
61 + *
62 + * @return array
63 + */
64 + public function get_item_schema() {
65 + $schema = array(
66 + '$schema' => 'http://json-schema.org/draft-04/schema#',
67 + 'title' => 'jetpack-publicize-connection-test-results',
68 + 'type' => 'object',
69 + 'properties' => $this->get_connection_schema_properties() + array(
70 + 'test_success' => array(
71 + 'description' => __( 'Did the Jetpack Social connection test pass?', 'jetpack' ),
72 + 'type' => 'boolean',
73 + ),
74 + 'error_code' => array(
75 + 'description' => __( 'Jetpack Social connection error code', 'jetpack' ),
76 + 'type' => 'string',
77 + ),
78 + 'test_message' => array(
79 + 'description' => __( 'Jetpack Social connection success or error message', 'jetpack' ),
80 + 'type' => 'string',
81 + ),
82 + 'can_refresh' => array(
83 + 'description' => __( 'Can the current user refresh the Jetpack Social connection?', 'jetpack' ),
84 + 'type' => 'boolean',
85 + ),
86 + 'refresh_text' => array(
87 + 'description' => __( 'Message instructing the user to refresh their Connection to the Jetpack Social service', 'jetpack' ),
88 + 'type' => 'string',
89 + ),
90 + 'refresh_url' => array(
91 + 'description' => __( 'URL for refreshing the Connection to the Jetpack Social service', 'jetpack' ),
92 + 'type' => 'string',
93 + 'format' => 'uri',
94 + ),
95 + ),
96 + );
97 +
98 + return $this->add_additional_fields_schema( $schema );
99 + }
100 +
101 + /**
102 + * Helper for generating schema. Used by this endpoint and by the
103 + * Connection Test Result endpoint.
104 + *
105 + * @internal
106 + * @return array
107 + */
108 + protected function get_connection_schema_properties() {
109 + return array(
110 + 'id' => array(
111 + 'description' => __( 'Unique identifier for the Jetpack Social connection', 'jetpack' ),
112 + 'type' => 'string',
113 + ),
114 + 'service_name' => array(
115 + 'description' => __( 'Alphanumeric identifier for the Jetpack Social service', 'jetpack' ),
116 + 'type' => 'string',
117 + ),
118 + 'display_name' => array(
119 + 'description' => __( 'Display name of the connected account', 'jetpack' ),
120 + 'type' => 'string',
121 + ),
122 + 'username' => array(
123 + 'description' => __( 'Username of the connected account', 'jetpack' ),
124 + 'type' => 'string',
125 + ),
126 + 'profile_display_name' => array(
127 + 'description' => __( 'The name to display in the profile of the connected account', 'jetpack' ),
128 + 'type' => 'string',
129 + ),
130 + 'profile_picture' => array(
131 + 'description' => __( 'Profile picture of the connected account', 'jetpack' ),
132 + 'type' => 'string',
133 + ),
134 + 'global' => array(
135 + 'description' => __( 'Is this connection available to all users?', 'jetpack' ),
136 + 'type' => 'boolean',
137 + ),
138 + 'external_id' => array(
139 + 'description' => __( 'The external ID of the connected account', 'jetpack' ),
140 + 'type' => 'string',
141 + ),
142 + );
143 + }
144 +
145 + /**
146 + * Helper for retrieving Connections. Used by this endpoint and by
147 + * the Connection Test Result endpoint.
148 + *
149 + * @internal
150 + * @return array
151 + */
152 + protected function get_connections() {
153 + global $publicize;
154 +
155 + $items = array();
156 +
157 + foreach ( (array) $publicize->get_services( 'connected' ) as $service_name => $connections ) {
158 + foreach ( $connections as $connection ) {
159 + $connection_meta = $publicize->get_connection_meta( $connection );
160 + $connection_data = $connection_meta['connection_data'];
161 +
162 + $items[] = array(
163 + 'id' => (string) $publicize->get_connection_unique_id( $connection ),
164 + 'connection_id' => (string) $publicize->get_connection_id( $connection ),
165 + 'service_name' => $service_name,
166 + 'display_name' => $publicize->get_display_name( $service_name, $connection ),
167 + 'username' => $publicize->get_username( $service_name, $connection ),
168 + 'profile_display_name' => ! empty( $connection_meta['profile_display_name'] ) ? $connection_meta['profile_display_name'] : '',
169 + 'profile_picture' => ! empty( $connection_meta['profile_picture'] ) ? $connection_meta['profile_picture'] : '',
170 + // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- We expect an integer, but do loose comparison below in case some other type is stored.
171 + 'global' => 0 == $connection_data['user_id'],
172 + 'external_id' => $connection_meta['external_id'] ?? '',
173 + );
174 + }
175 + }
176 +
177 + return $items;
178 + }
179 +
180 + /**
181 + * Verify that user can access Publicize data
182 + *
183 + * @return true|WP_Error
184 + */
185 + public function get_items_permission_check() {
186 + global $publicize;
187 +
188 + if ( ! $publicize ) {
189 + return new WP_Error(
190 + 'publicize_not_available',
191 + __( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack' ),
192 + array( 'status' => rest_authorization_required_code() )
193 + );
194 + }
195 +
196 + if ( $publicize->current_user_can_access_publicize_data() ) {
197 + return true;
198 + }
199 +
200 + return new WP_Error(
201 + 'invalid_user_permission_publicize',
202 + __( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack' ),
203 + array( 'status' => rest_authorization_required_code() )
204 + );
205 + }
206 +
207 + /**
208 + * Get list of Publicize Connections.
209 + *
210 + * @param WP_REST_Request $request Full details about the request.
211 + *
212 + * @see Publicize::get_publicize_conns_test_results()
213 + * @return WP_REST_Response suitable for 1-page collection
214 + */
215 + public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
216 + global $publicize;
217 +
218 + $items = $this->get_connections();
219 +
220 + $test_results = $publicize->get_publicize_conns_test_results();
221 + $test_results_by_unique_id = array();
222 + foreach ( $test_results as $test_result ) {
223 + $test_results_by_unique_id[ $test_result['connectionID'] ] = $test_result;
224 + }
225 +
226 + $mapping = array(
227 + 'test_success' => 'connectionTestPassed',
228 + 'test_message' => 'connectionTestMessage',
229 + 'error_code' => 'connectionTestErrorCode',
230 + 'can_refresh' => 'userCanRefresh',
231 + 'refresh_text' => 'refreshText',
232 + 'refresh_url' => 'refreshURL',
233 + 'connection_id' => 'connectionID',
234 + );
235 +
236 + foreach ( $items as &$item ) {
237 + $test_result = $test_results_by_unique_id[ $item['connection_id'] ];
238 +
239 + foreach ( $mapping as $field => $test_result_field ) {
240 + $item[ $field ] = $test_result[ $test_result_field ];
241 + }
242 + }
243 +
244 + if (
245 + isset( $item['id'] )
246 + && 'linkedin' === $item['id']
247 + && 'must_reauth' === $test_result['connectionTestPassed']
248 + ) {
249 + $item['test_success'] = 'must_reauth';
250 + }
251 +
252 + $response = rest_ensure_response( $items );
253 +
254 + $response->header( 'X-WP-Total', count( $items ) );
255 + $response->header( 'X-WP-TotalPages', 1 );
256 +
257 + return $response;
258 + }
259 +}
260 +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results' );