Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
REST_Provider
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 initialize_rest_api
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 get_blog
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_blog_permission_check
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The Stats REST Provider class.
4 *
5 * @package @automattic/jetpack-stats
6 */
7
8namespace Automattic\Jetpack\Stats;
9
10use Automattic\Jetpack\Connection\Rest_Authentication;
11use Automattic\Jetpack\Connection\REST_Connector;
12use WP_Error;
13use WP_REST_Server;
14
15/**
16 * The REST API provider class.
17 *
18 * @since 0.12.0
19 */
20class REST_Provider {
21    /**
22     * Singleton instance.
23     *
24     * @var REST_Provider
25     **/
26    private static $instance = null;
27
28    /**
29     * Private constructor.
30     *
31     * Use the static::init() method to get an instance.
32     */
33    private function __construct() {
34        add_action( 'rest_api_init', array( $this, 'initialize_rest_api' ) );
35    }
36
37    /**
38     * Initialize class and get back a singleton instance.
39     *
40     * @param bool $new_instance Force create new instance.
41     *
42     * @return static
43     */
44    public static function init( $new_instance = false ) {
45        if ( null === self::$instance || $new_instance ) {
46            self::$instance = new static();
47        }
48
49        return self::$instance;
50    }
51
52    /**
53     * Initialize the REST API.
54     *
55     * @return void
56     */
57    public function initialize_rest_api() {
58        register_rest_route(
59            'jetpack/v4',
60            '/stats/blog',
61            array(
62                'methods'             => WP_REST_Server::READABLE,
63                'callback'            => array( $this, 'get_blog' ),
64                'permission_callback' => array( $this, 'get_blog_permission_check' ),
65            )
66        );
67    }
68
69    /**
70     * Get the stats blog data.
71     *
72     * @since 0.12.0
73     *
74     * @return array
75     */
76    public function get_blog() {
77        return XMLRPC_Provider::init()->get_blog();
78    }
79
80    /**
81     * Check permissions for the `/stats/blog` endpoint.
82     *
83     * @return WP_Error|true
84     */
85    public function get_blog_permission_check() {
86        return Rest_Authentication::is_signed_with_blog_token()
87            ? true
88            : new WP_Error( 'invalid_permission_stats_get_blog', REST_Connector::get_user_permissions_error_msg(), array( 'status' => rest_authorization_required_code() ) );
89    }
90}