Skip to content

Commit

Permalink
Site Level User Profile: add /wpcom/v2/profile endpoint to get admin …
Browse files Browse the repository at this point in the history
…color and locale (#38879)
  • Loading branch information
fushar authored Aug 19, 2024
1 parent 3224c19 commit 05233c2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* REST API endpoint for user profile.
*
* @package automattic/jetpack
*/

/**
* Class WPCOM_REST_API_V2_Endpoint_Profile
*/
class WPCOM_REST_API_V2_Endpoint_Profile extends WP_REST_Controller {
/**
* Constructor.
*/
public function __construct() {
$this->namespace = 'wpcom/v2';
$this->rest_base = 'profile';
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}

/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
$this->rest_base . '/',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
)
);
}

/**
* Checks if a given request has access to user profile.
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
*/
public function get_item_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! current_user_can( 'read' ) ) {
return new WP_Error(
'rest_forbidden',
__( 'Sorry, you are not allowed to view your user profile on this site.', 'jetpack' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Retrieves the user profile.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_item( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return rest_ensure_response(
array(
'admin_color' => get_user_option( 'admin_color' ),
'locale' => get_user_locale(),
)
);
}
}

wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Profile' );
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/site-profile-language
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Add /wpcom/v2/profile endpoint that returns user profile: admin color and locale

0 comments on commit 05233c2

Please sign in to comment.