Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
JSON_Endpoint
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 permission_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setup
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 get_data
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * JSON REST API endpoint for Global Styles plugin.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Global_Styles;
9
10/**
11 * REST API endpoint for Global Styles plugin.
12 */
13class JSON_Endpoint extends \WP_REST_Controller {
14
15    /**
16     * Namespace for the REST endpoint.
17     *
18     * @var string
19     */
20    private $rest_namespace;
21
22    /**
23     * Route name for the REST endpoint.
24     *
25     * @var string
26     */
27    private $rest_route;
28
29    /**
30     * Object holding the data description to work with.
31     *
32     * @var Data_Set
33     */
34    private $data_set;
35
36    /**
37     * Permission check callback.
38     *
39     * @var callable
40     */
41    private $permission_cb;
42
43    /**
44     * Constructor
45     *
46     * @param string   $rest_namespace Namespace for the REST endpoint.
47     * @param string   $rest_route Route name.
48     * @param Data_Set $data_set Description of the data to work with.
49     * @param callable $permission_cb Permission check callback.
50     */
51    public function __construct( $rest_namespace, $rest_route, $data_set, $permission_cb ) {
52        $this->rest_namespace = $rest_namespace;
53        $this->rest_route     = $rest_route;
54        $this->data_set       = $data_set;
55        $this->permission_cb  = $permission_cb;
56    }
57
58    /**
59     * Callback to determine whether the request can proceed.
60     *
61     * @return boolean
62     */
63    public function permission_callback() {
64        return call_user_func( $this->permission_cb );
65    }
66
67    /**
68     * Initialize the routes. To be called on `rest_api_init'
69     *
70     * @return void
71     */
72    public function setup() {
73        register_rest_route(
74            $this->rest_namespace,
75            $this->rest_route,
76            array(
77                array(
78                    'methods'             => \WP_REST_Server::READABLE,
79                    'callback'            => array( $this, 'get_data' ),
80                    'permission_callback' => array( $this, 'permission_callback' ),
81                ),
82            )
83        );
84        register_rest_route(
85            $this->rest_namespace,
86            $this->rest_route,
87            array(
88                array(
89                    'methods'             => \WP_REST_Server::CREATABLE,
90                    'callback'            => array( $this, 'update_data' ),
91                    'permission_callback' => array( $this, 'permission_callback' ),
92                ),
93            )
94        );
95    }
96
97    /**
98     * Process the incoming request to get data.
99     *
100     * @return Array
101     */
102    public function get_data() {
103        return $this->data_set->get_data();
104    }
105
106    /**
107     * Process the incoming request to update data.
108     *
109     * @param \WP_REST_Request $request Incoming request.
110     * @return Boolean False if data hasn't changed or update failed, true otherwise.
111     */
112    public function update_data( \WP_REST_Request $request ) {
113        $incoming_data = $request->get_json_params();
114        return $this->data_set->save_data( $incoming_data );
115    }
116}