Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Global_Styles_Status_Rest_API
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 permissions_check
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_global_styles_info
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WPCOM Global Styles Info API.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * This class contains the necessary endpoints to interact with global styles outside the editor context.
10 */
11class Global_Styles_Status_Rest_API extends WP_REST_Controller {
12
13    /**
14     * Class constructor
15     */
16    public function __construct() {
17        $this->namespace                       = 'wpcom/v2';
18        $this->rest_base                       = 'global-styles/status';
19        $this->wpcom_is_site_specific_endpoint = true;
20        $this->wpcom_is_wpcom_only_endpoint    = false;
21        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
22    }
23
24    /**
25     * Here we register the routes this API will expose.
26     *
27     * @return void
28     */
29    public function register_routes() {
30
31        register_rest_route(
32            $this->namespace,
33            '/' . $this->rest_base,
34            array(
35                array(
36                    'methods'             => WP_REST_Server::READABLE,
37                    'callback'            => array( $this, 'get_global_styles_info' ),
38                    'permission_callback' => array( $this, 'permissions_check' ),
39                ),
40            )
41        );
42    }
43
44    /**
45     * Checks if the user has the necessary permissions to get global styles information.
46     *
47     * @return bool|WP_Error
48     */
49    public function permissions_check() {
50        if ( ! current_user_can( 'edit_theme_options' ) ) {
51            return new WP_Error(
52                'rest_cannot_view',
53                __( 'Your user is not permitted to access this resource.', 'jetpack-mu-wpcom' ),
54                array( 'status' => rest_authorization_required_code() )
55            );
56        }
57
58        return true;
59    }
60
61    /**
62     * Returns if the current blog has Global Styles in use and if Global Styles should be limited.
63     *
64     * @return array
65     */
66    public function get_global_styles_info() {
67        return array(
68            'globalStylesInUse'          => wpcom_global_styles_in_use(),
69            'shouldLimitGlobalStyles'    => wpcom_should_limit_global_styles(),
70            'globalStylesInPersonalPlan' => is_global_styles_on_personal_plan(),
71            'variation'                  => get_global_styles_on_personal_variation(),
72        );
73    }
74}
75
76if ( function_exists( 'wpcom_rest_api_v2_load_plugin' ) ) {
77    wpcom_rest_api_v2_load_plugin( 'Global_Styles_Status_Rest_API' );
78}