← All changes
|
_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php
+139
-10
12.0.3
→
16.3-a.5
View file →
| @@ -4,9 +4,11 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -require_once __DIR__ . '/publicize-connections.php'; | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 9 | 11 | |
| 10 | 12 | /** |
| 11 | 13 | * Publicize: List Connection Test Result Data |
| 12 | 14 | * |
| @@ -11,12 +13,23 @@ | ||
| 11 | 13 | * Publicize: List Connection Test Result Data |
| 12 | 14 | * |
| 13 | 15 | * All the same data as the Publicize Connections Endpoint, plus test results. |
| 14 | 16 | * |
| 17 | + * @deprecated 14.4 Deprecated in favor of /wpcom/v2/publicize/connections?test_connections=1 | |
| 18 | + * | |
| 15 | 19 | * @since 6.8 |
| 16 | 20 | */ |
| 17 | -class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends WPCOM_REST_API_V2_Endpoint_List_Publicize_Connections { | |
| 21 | +class WPCOM_REST_API_V2_Endpoint_List_Publicize_Connection_Test_Results extends WP_REST_Controller { | |
| 22 | + | |
| 18 | 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 | + /** | |
| 19 | 32 | * Constructor. |
| 20 | 33 | */ |
| 21 | 34 | public function __construct() { |
| 22 | 35 | $this->namespace = 'wpcom/v2'; |
| @@ -57,8 +70,12 @@ | ||
| 57 | 70 | 'test_success' => array( |
| 58 | 71 | 'description' => __( 'Did the Jetpack Social connection test pass?', 'jetpack' ), |
| 59 | 72 | 'type' => 'boolean', |
| 60 | 73 | ), |
| 74 | + 'error_code' => array( | |
| 75 | + 'description' => __( 'Jetpack Social connection error code', 'jetpack' ), | |
| 76 | + 'type' => 'string', | |
| 77 | + ), | |
| 61 | 78 | 'test_message' => array( |
| 62 | 79 | 'description' => __( 'Jetpack Social connection success or error message', 'jetpack' ), |
| 63 | 80 | 'type' => 'string', |
| 64 | 81 | ), |
| @@ -81,8 +98,114 @@ | ||
| 81 | 98 | return $this->add_additional_fields_schema( $schema ); |
| 82 | 99 | } |
| 83 | 100 | |
| 84 | 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 | + /** | |
| 85 | 208 | * Get list of Publicize Connections. |
| 86 | 209 | * |
| 87 | 210 | * @param WP_REST_Request $request Full details about the request. |
| 88 | 211 | * |
| @@ -96,21 +219,23 @@ | ||
| 96 | 219 | |
| 97 | 220 | $test_results = $publicize->get_publicize_conns_test_results(); |
| 98 | 221 | $test_results_by_unique_id = array(); |
| 99 | 222 | foreach ( $test_results as $test_result ) { |
| 100 | - $test_results_by_unique_id[ $test_result['unique_id'] ] = $test_result; | |
| 223 | + $test_results_by_unique_id[ $test_result['connectionID'] ] = $test_result; | |
| 101 | 224 | } |
| 102 | 225 | |
| 103 | 226 | $mapping = array( |
| 104 | - 'test_success' => 'connectionTestPassed', | |
| 105 | - 'test_message' => 'connectionTestMessage', | |
| 106 | - 'can_refresh' => 'userCanRefresh', | |
| 107 | - 'refresh_text' => 'refreshText', | |
| 108 | - 'refresh_url' => 'refreshURL', | |
| 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', | |
| 109 | 234 | ); |
| 110 | 235 | |
| 111 | 236 | foreach ( $items as &$item ) { |
| 112 | - $test_result = $test_results_by_unique_id[ $item['id'] ]; | |
| 237 | + $test_result = $test_results_by_unique_id[ $item['connection_id'] ]; | |
| 113 | 238 | |
| 114 | 239 | foreach ( $mapping as $field => $test_result_field ) { |
| 115 | 240 | $item[ $field ] = $test_result[ $test_result_field ]; |
| 116 | 241 | } |
| @@ -115,9 +240,13 @@ | ||
| 115 | 240 | $item[ $field ] = $test_result[ $test_result_field ]; |
| 116 | 241 | } |
| 117 | 242 | } |
| 118 | 243 | |
| 119 | - if ( 'linkedin' === $item['id'] && 'must_reauth' === $test_result['connectionTestPassed'] ) { | |
| 244 | + if ( | |
| 245 | + isset( $item['id'] ) | |
| 246 | + && 'linkedin' === $item['id'] | |
| 247 | + && 'must_reauth' === $test_result['connectionTestPassed'] | |
| 248 | + ) { | |
| 120 | 249 | $item['test_success'] = 'must_reauth'; |
| 121 | 250 | } |
| 122 | 251 | |
| 123 | 252 | $response = rest_ensure_response( $items ); |