← All changes
|
_inc/lib/core-api/wpcom-endpoints/publicize-connection-test-results.php
+121
-2
13.0.2
→
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'; |
| @@ -82,8 +95,114 @@ | ||
| 82 | 95 | ), |
| 83 | 96 | ); |
| 84 | 97 | |
| 85 | 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 | + ); | |
| 86 | 205 | } |
| 87 | 206 | |
| 88 | 207 | /** |
| 89 | 208 | * Get list of Publicize Connections. |