Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
VideoPress_Rest_Api_V1_Site
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_endpoints
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 permissions_callback
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_site_info
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * VideoPress Site Info Endpoint
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use WP_REST_Response;
11
12/**
13 * VideoPress rest api class for fetching site information
14 */
15class VideoPress_Rest_Api_V1_Site {
16    /**
17     * Initializes the endpoints
18     *
19     * @return void
20     */
21    public static function init() {
22        add_action( 'rest_api_init', array( static::class, 'register_rest_endpoints' ) );
23    }
24
25    /**
26     * Register the REST API routes.
27     *
28     * @return void
29     */
30    public static function register_rest_endpoints() {
31        register_rest_route(
32            'videopress/v1',
33            'site',
34            array(
35                'methods'             => \WP_REST_Server::READABLE,
36                'callback'            => static::class . '::get_site_info',
37                'permission_callback' => static::class . '::permissions_callback',
38            )
39        );
40    }
41
42    /**
43     * Checks whether the user has permission to see the site information.
44     *
45     * The response is the full WordPress.com site payload (site options,
46     * plan/product entitlements, etc.), which is administrative data, so the
47     * endpoint requires `manage_options`. The VideoPress dashboard is the only
48     * legitimate consumer of this route, and its admin page requires the same
49     * capability.
50     *
51     * @return boolean
52     */
53    public static function permissions_callback() {
54        return current_user_can( 'manage_options' );
55    }
56
57    /**
58     * Returns all the site information usually provided by Jetpack, without relying on Jetpack
59     *
60     * @return WP_Rest_Response The response object.
61     */
62    public static function get_site_info() {
63        $data = Site::get_site_info();
64        return rest_ensure_response( $data );
65    }
66}