Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 3
n/a
0 / 0
CRAP
n/a
0 / 0
WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results
n/a
0 / 0
n/a
0 / 0
20
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 register_routes
n/a
0 / 0
n/a
0 / 0
2
 get_item_schema
n/a
0 / 0
n/a
0 / 0
1
 get_connection_schema_properties
n/a
0 / 0
n/a
0 / 0
1
 get_connections
n/a
0 / 0
n/a
0 / 0
5
 get_items_permission_check
n/a
0 / 0
n/a
0 / 0
3
 get_items
n/a
0 / 0
n/a
0 / 0
7
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
8if ( ! 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 */
21class 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        global $publicize;
46
47        // Match the Publicize package controllers, which only register when Publicize is set up.
48        if ( ! $publicize ) {
49            return;
50        }
51
52        register_rest_route(
53            $this->namespace,
54            '/' . $this->rest_base,
55            array(
56                array(
57                    'methods'             => WP_REST_Server::READABLE,
58                    'callback'            => array( $this, 'get_items' ),
59                    'permission_callback' => array( $this, 'get_items_permission_check' ),
60                ),
61                'schema' => array( $this, 'get_public_item_schema' ),
62            )
63        );
64    }
65
66    /**
67     * Adds the test results properties to the Connection schema.
68     *
69     * @return array
70     */
71    public function get_item_schema() {
72        $schema = array(
73            '$schema'    => 'http://json-schema.org/draft-04/schema#',
74            'title'      => 'jetpack-publicize-connection-test-results',
75            'type'       => 'object',
76            'properties' => $this->get_connection_schema_properties() + array(
77                'test_success' => array(
78                    'description' => __( 'Did the Jetpack Social connection test pass?', 'jetpack' ),
79                    'type'        => 'boolean',
80                ),
81                'error_code'   => array(
82                    'description' => __( 'Jetpack Social connection error code', 'jetpack' ),
83                    'type'        => 'string',
84                ),
85                'test_message' => array(
86                    'description' => __( 'Jetpack Social connection success or error message', 'jetpack' ),
87                    'type'        => 'string',
88                ),
89                'can_refresh'  => array(
90                    'description' => __( 'Can the current user refresh the Jetpack Social connection?', 'jetpack' ),
91                    'type'        => 'boolean',
92                ),
93                'refresh_text' => array(
94                    'description' => __( 'Message instructing the user to refresh their Connection to the Jetpack Social service', 'jetpack' ),
95                    'type'        => 'string',
96                ),
97                'refresh_url'  => array(
98                    'description' => __( 'URL for refreshing the Connection to the Jetpack Social service', 'jetpack' ),
99                    'type'        => 'string',
100                    'format'      => 'uri',
101                ),
102            ),
103        );
104
105        return $this->add_additional_fields_schema( $schema );
106    }
107
108    /**
109     * Helper for generating schema. Used by this endpoint and by the
110     * Connection Test Result endpoint.
111     *
112     * @internal
113     * @return array
114     */
115    protected function get_connection_schema_properties() {
116        return array(
117            'id'                   => array(
118                'description' => __( 'Unique identifier for the Jetpack Social connection', 'jetpack' ),
119                'type'        => 'string',
120            ),
121            'service_name'         => array(
122                'description' => __( 'Alphanumeric identifier for the Jetpack Social service', 'jetpack' ),
123                'type'        => 'string',
124            ),
125            'display_name'         => array(
126                'description' => __( 'Display name of the connected account', 'jetpack' ),
127                'type'        => 'string',
128            ),
129            'username'             => array(
130                'description' => __( 'Username of the connected account', 'jetpack' ),
131                'type'        => 'string',
132            ),
133            'profile_display_name' => array(
134                'description' => __( 'The name to display in the profile of the connected account', 'jetpack' ),
135                'type'        => 'string',
136            ),
137            'profile_picture'      => array(
138                'description' => __( 'Profile picture of the connected account', 'jetpack' ),
139                'type'        => 'string',
140            ),
141            'global'               => array(
142                'description' => __( 'Is this connection available to all users?', 'jetpack' ),
143                'type'        => 'boolean',
144            ),
145            'external_id'          => array(
146                'description' => __( 'The external ID of the connected account', 'jetpack' ),
147                'type'        => 'string',
148            ),
149        );
150    }
151
152    /**
153     * Helper for retrieving Connections. Used by this endpoint and by
154     * the Connection Test Result endpoint.
155     *
156     * @internal
157     * @return array
158     */
159    protected function get_connections() {
160        global $publicize;
161
162        $items = array();
163
164        foreach ( (array) $publicize->get_services( 'connected' ) as $service_name => $connections ) {
165            foreach ( $connections as $connection ) {
166                $connection_meta = $publicize->get_connection_meta( $connection );
167                $connection_data = $connection_meta['connection_data'];
168
169                $items[] = array(
170                    'id'                   => (string) $publicize->get_connection_unique_id( $connection ),
171                    'connection_id'        => (string) $publicize->get_connection_id( $connection ),
172                    'service_name'         => $service_name,
173                    'display_name'         => $publicize->get_display_name( $service_name, $connection ),
174                    'username'             => $publicize->get_username( $service_name, $connection ),
175                    'profile_display_name' => ! empty( $connection_meta['profile_display_name'] ) ? $connection_meta['profile_display_name'] : '',
176                    'profile_picture'      => ! empty( $connection_meta['profile_picture'] ) ? $connection_meta['profile_picture'] : '',
177                    // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- We expect an integer, but do loose comparison below in case some other type is stored.
178                    'global'               => 0 == $connection_data['user_id'],
179                    'external_id'          => $connection_meta['external_id'] ?? '',
180                );
181            }
182        }
183
184        return $items;
185    }
186
187    /**
188     * Verify that user can access Publicize data
189     *
190     * @return true|WP_Error
191     */
192    public function get_items_permission_check() {
193        global $publicize;
194
195        if ( ! $publicize ) {
196            return new WP_Error(
197                'publicize_not_available',
198                __( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack' ),
199                array( 'status' => rest_authorization_required_code() )
200            );
201        }
202
203        if ( $publicize->current_user_can_access_publicize_data() ) {
204            return true;
205        }
206
207        return new WP_Error(
208            'invalid_user_permission_publicize',
209            __( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack' ),
210            array( 'status' => rest_authorization_required_code() )
211        );
212    }
213
214    /**
215     * Get list of Publicize Connections.
216     *
217     * @param WP_REST_Request $request Full details about the request.
218     *
219     * @see Publicize::get_publicize_conns_test_results()
220     * @return WP_REST_Response suitable for 1-page collection
221     */
222    public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
223        global $publicize;
224
225        $items = $this->get_connections();
226
227        $test_results              = $publicize->get_publicize_conns_test_results();
228        $test_results_by_unique_id = array();
229        foreach ( $test_results as $test_result ) {
230            $test_results_by_unique_id[ $test_result['connectionID'] ] = $test_result;
231        }
232
233        $mapping = array(
234            'test_success'  => 'connectionTestPassed',
235            'test_message'  => 'connectionTestMessage',
236            'error_code'    => 'connectionTestErrorCode',
237            'can_refresh'   => 'userCanRefresh',
238            'refresh_text'  => 'refreshText',
239            'refresh_url'   => 'refreshURL',
240            'connection_id' => 'connectionID',
241        );
242
243        foreach ( $items as &$item ) {
244            $test_result = $test_results_by_unique_id[ $item['connection_id'] ];
245
246            foreach ( $mapping as $field => $test_result_field ) {
247                $item[ $field ] = $test_result[ $test_result_field ];
248            }
249        }
250
251        if (
252            isset( $item['id'] )
253            && 'linkedin' === $item['id']
254            && 'must_reauth' === $test_result['connectionTestPassed']
255        ) {
256            $item['test_success'] = 'must_reauth';
257        }
258
259        $response = rest_ensure_response( $items );
260
261        $response->header( 'X-WP-Total', count( $items ) );
262        $response->header( 'X-WP-TotalPages', 1 );
263
264        return $response;
265    }
266}
267wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results' );