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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoPress_Rest_Api_V1_Stats
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_endpoints
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 permissions_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_stats
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 get_featured_stats
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * VideoPress Stats Endpoints
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use WP_REST_Response;
11
12/**
13 * VideoPress stats rest api class
14 */
15class VideoPress_Rest_Api_V1_Stats {
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        /**
32         * Basic stats.
33         */
34        register_rest_route(
35            'videopress/v1',
36            'stats',
37            array(
38                'methods'             => \WP_REST_Server::READABLE,
39                'callback'            => static::class . '::get_stats',
40                'permission_callback' => static::class . '::permissions_callback',
41            )
42        );
43
44        /**
45         * Stats to be featured by My Jetpack.
46         */
47        register_rest_route(
48            'videopress/v1',
49            'stats/featured',
50            array(
51                'methods'             => \WP_REST_Server::READABLE,
52                'callback'            => static::class . '::get_featured_stats',
53                'permission_callback' => static::class . '::permissions_callback',
54            )
55        );
56    }
57
58    /**
59     * Checks wether the user have permissions to see stats
60     *
61     * @return boolean
62     */
63    public static function permissions_callback() {
64        return current_user_can( 'manage_options' );
65    }
66
67    /**
68     * Endpoint for getting the general VideoPress stats for the site.
69     *
70     * Returns the plays for all the videos, for today and since the beggining of times.
71     *
72     * @return WP_Rest_Response - The response object.
73     */
74    public static function get_stats() {
75        $today_plays = Stats::get_today_plays();
76
77        if ( is_wp_error( $today_plays ) ) {
78            // TODO: Improve status code.
79            return rest_ensure_response( $today_plays );
80        }
81
82        $data = array(
83            'plays' => array(
84                'today' => $today_plays,
85            ),
86        );
87
88        return rest_ensure_response( $data );
89    }
90
91    /**
92     * Provides the stats that will be featured by My Jetpack.
93     *
94     * @return WP_Rest_Response - The response object.
95     */
96    public static function get_featured_stats() {
97        $featured_stats = Stats::get_featured_stats();
98        return rest_ensure_response( $featured_stats );
99    }
100}