← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-profile.php
+76
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,76 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * REST API endpoint for user profile. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Class WPCOM_REST_API_V2_Endpoint_Profile | |
| 14 | + */ | |
| 15 | +class WPCOM_REST_API_V2_Endpoint_Profile extends WP_REST_Controller { | |
| 16 | + /** | |
| 17 | + * Constructor. | |
| 18 | + */ | |
| 19 | + public function __construct() { | |
| 20 | + $this->namespace = 'wpcom/v2'; | |
| 21 | + $this->rest_base = 'profile'; | |
| 22 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Register routes. | |
| 27 | + */ | |
| 28 | + public function register_routes() { | |
| 29 | + register_rest_route( | |
| 30 | + $this->namespace, | |
| 31 | + $this->rest_base . '/', | |
| 32 | + array( | |
| 33 | + array( | |
| 34 | + 'methods' => WP_REST_Server::READABLE, | |
| 35 | + 'callback' => array( $this, 'get_item' ), | |
| 36 | + 'permission_callback' => array( $this, 'get_item_permissions_check' ), | |
| 37 | + ), | |
| 38 | + ) | |
| 39 | + ); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Checks if a given request has access to user profile. | |
| 44 | + * | |
| 45 | + * @param WP_REST_Request $request Full details about the request. | |
| 46 | + * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. | |
| 47 | + */ | |
| 48 | + public function get_item_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 49 | + if ( ! current_user_can( 'read' ) ) { | |
| 50 | + return new WP_Error( | |
| 51 | + 'rest_forbidden', | |
| 52 | + __( 'Sorry, you are not allowed to view your user profile on this site.', 'jetpack' ), | |
| 53 | + array( 'status' => rest_authorization_required_code() ) | |
| 54 | + ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + return true; | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Retrieves the user profile. | |
| 62 | + * | |
| 63 | + * @param WP_REST_Request $request Full details about the request. | |
| 64 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | |
| 65 | + */ | |
| 66 | + public function get_item( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 67 | + return rest_ensure_response( | |
| 68 | + array( | |
| 69 | + 'admin_color' => get_user_option( 'admin_color' ), | |
| 70 | + 'locale' => get_user_locale(), | |
| 71 | + ) | |
| 72 | + ); | |
| 73 | + } | |
| 74 | +} | |
| 75 | + | |
| 76 | +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Profile' ); | |