| @@ -1,0 +1,96 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Site Switcher REST API Endpoint | |
| 4 | + * Jetpack-only endpoint for fetching compact sites list | |
| 5 | + * | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +use Automattic\Jetpack\Connection\Client; | |
| 10 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 11 | + | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 14 | +} | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Register REST API endpoint to fetch compact sites list from WordPress.com | |
| 18 | + */ | |
| 19 | +function jetpack_site_switcher_register_rest_routes() { | |
| 20 | + register_rest_route( | |
| 21 | + 'jetpack/v4', | |
| 22 | + '/sites/compact', | |
| 23 | + array( | |
| 24 | + 'methods' => WP_REST_Server::READABLE, | |
| 25 | + 'callback' => 'jetpack_site_switcher_get_sites', | |
| 26 | + 'permission_callback' => 'jetpack_site_switcher_permission_check', | |
| 27 | + ) | |
| 28 | + ); | |
| 29 | +} | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * Check if the current user is connected to WordPress.com | |
| 33 | + * | |
| 34 | + * @return bool True if user is connected, false otherwise | |
| 35 | + */ | |
| 36 | +function jetpack_site_switcher_permission_check() { | |
| 37 | + if ( ! is_user_logged_in() ) { | |
| 38 | + return false; | |
| 39 | + } | |
| 40 | + | |
| 41 | + $connection_manager = new Connection_Manager(); | |
| 42 | + return $connection_manager->is_user_connected(); | |
| 43 | +} | |
| 44 | +add_action( 'rest_api_init', 'jetpack_site_switcher_register_rest_routes' ); | |
| 45 | + | |
| 46 | +/** | |
| 47 | + * Fetch compact sites list from WordPress.com API | |
| 48 | + * | |
| 49 | + * @return WP_REST_Response|WP_Error | |
| 50 | + */ | |
| 51 | +function jetpack_site_switcher_get_sites() { | |
| 52 | + $response = Client::wpcom_json_api_request_as_user( | |
| 53 | + '/me/sites/compact', | |
| 54 | + 'v1.1', | |
| 55 | + array( 'method' => 'GET' ), | |
| 56 | + null, | |
| 57 | + 'rest' | |
| 58 | + ); | |
| 59 | + | |
| 60 | + if ( is_wp_error( $response ) ) { | |
| 61 | + return new WP_Error( | |
| 62 | + 'jetpack_site_switcher_request_failed', | |
| 63 | + sprintf( | |
| 64 | + /* translators: %s: Error message from the API request */ | |
| 65 | + __( 'Failed to connect to WordPress.com: %s', 'jetpack' ), | |
| 66 | + $response->get_error_message() | |
| 67 | + ), | |
| 68 | + array( 'status' => 500 ) | |
| 69 | + ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + $response_code = wp_remote_retrieve_response_code( $response ); | |
| 73 | + if ( 200 !== $response_code ) { | |
| 74 | + return new WP_Error( | |
| 75 | + 'jetpack_site_switcher_api_error', | |
| 76 | + sprintf( | |
| 77 | + /* translators: %d: HTTP status code */ | |
| 78 | + __( 'WordPress.com API returned error (HTTP %d)', 'jetpack' ), | |
| 79 | + $response_code | |
| 80 | + ), | |
| 81 | + array( 'status' => $response_code ) | |
| 82 | + ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + $body = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 86 | + | |
| 87 | + if ( ! isset( $body['sites'] ) ) { | |
| 88 | + return new WP_Error( | |
| 89 | + 'jetpack_site_switcher_invalid_response', | |
| 90 | + __( 'WordPress.com API response missing sites data', 'jetpack' ), | |
| 91 | + array( 'status' => 500 ) | |
| 92 | + ); | |
| 93 | + } | |
| 94 | + | |
| 95 | + return rest_ensure_response( $body ); | |
| 96 | +} | |