Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
36.67% covered (danger)
36.67%
11 / 30
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Profile
40.74% covered (danger)
40.74%
11 / 27
25.00% covered (danger)
25.00%
1 / 4
10.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 get_item_permissions_check
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_item
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * REST API endpoint for user profile.
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Class WPCOM_REST_API_V2_Endpoint_Profile
14 */
15class 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
76wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Profile' );