Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.89% covered (danger)
47.89%
34 / 71
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Rest_Controller
47.89% covered (danger)
47.89%
34 / 71
60.00% covered (warning)
60.00%
3 / 5
41.74
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_routes
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 stats_video_plays_args
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
1
 permissions_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_stats_video_plays
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3 * The VideoPress REST Controller.
4 *
5 * Registers the `/jetpack/v4/videopress/*` routes backing the
6 * modernized wp-build dashboard. Currently exposes one route — a
7 * user-signed proxy to the WPCOM `sites/{id}/stats/video-plays`
8 * endpoint — needed by the Overview screen's KPI / trends / top-N
9 * cards.
10 *
11 * @package automattic/jetpack-videopress
12 */
13
14namespace Automattic\Jetpack\VideoPress;
15
16use Automattic\Jetpack\Connection\Client;
17use Jetpack_Options;
18use WP_Error;
19use WP_REST_Request;
20use WP_REST_Server;
21
22/**
23 * REST routes for the modernized VideoPress admin UI.
24 */
25class Rest_Controller {
26
27    /**
28     * REST namespace used by this package's modernization routes.
29     *
30     * @var string
31     */
32    const REST_NAMESPACE = 'jetpack/v4/videopress';
33
34    /**
35     * Hook the route registration on `rest_api_init`.
36     *
37     * @return void
38     */
39    public static function init() {
40        add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
41    }
42
43    /**
44     * Register the VideoPress REST routes.
45     *
46     * @return void
47     */
48    public static function register_rest_routes() {
49        register_rest_route(
50            self::REST_NAMESPACE,
51            '/stats/video-plays',
52            array(
53                'methods'             => WP_REST_Server::READABLE,
54                'callback'            => array( __CLASS__, 'get_stats_video_plays' ),
55                'permission_callback' => array( __CLASS__, 'permissions_callback' ),
56                'args'                => self::stats_video_plays_args(),
57            )
58        );
59    }
60
61    /**
62     * Query params accepted by the video-plays proxy. Forwarded verbatim
63     * to WPCOM after permission and shape validation; `complete_stats` is
64     * always forced to `true` (the only mode the Overview cares about)
65     * and is therefore not exposed as an incoming param.
66     *
67     * @return array
68     */
69    private static function stats_video_plays_args() {
70        return array(
71            'period'     => array(
72                'description' => __( 'Period unit: day, week, month, or year.', 'jetpack-videopress-pkg' ),
73                'type'        => 'string',
74                'enum'        => array( 'day', 'week', 'month', 'year' ),
75            ),
76            'num'        => array(
77                'description' => __( 'Number of periods to include.', 'jetpack-videopress-pkg' ),
78                'type'        => 'integer',
79                'minimum'     => 1,
80                'maximum'     => 365,
81            ),
82            'date'       => array(
83                'description' => __( 'Most recent day to include in results (YYYY-MM-DD).', 'jetpack-videopress-pkg' ),
84                'type'        => 'string',
85                'format'      => 'date',
86            ),
87            'start_date' => array(
88                'description' => __( 'Starting date for range queries (YYYY-MM-DD).', 'jetpack-videopress-pkg' ),
89                'type'        => 'string',
90                'format'      => 'date',
91            ),
92        );
93    }
94
95    /**
96     * Permission callback. Admin-gated. The upstream call is blog-signed,
97     * matching the existing `Stats::fetch_video_plays` path; no user-level
98     * WPCOM connection is required.
99     *
100     * @return bool
101     */
102    public static function permissions_callback() {
103        return current_user_can( 'manage_options' );
104    }
105
106    /**
107     * Proxy the video-plays stats endpoint.
108     *
109     * Forwards the whitelisted query params to WPCOM (REST v1.1, blog-signed
110     * — matching the existing `Stats::fetch_video_plays` path) and forces
111     * `complete_stats=true`. In complete-stats mode, each day entry carries
112     * `total.views`, `total.impressions`, and `total.watch_time` (in hours)
113     * plus a per-video `data[]` array whose entries have `post_id`, `title`,
114     * `views`, `impressions`, `watch_time` (hours), and `retention_rate`.
115     * The `plays` field is NOT returned in complete-stats mode.
116     *
117     * @param WP_REST_Request $request Incoming request.
118     * @return mixed Decoded JSON response from WPCOM, or WP_Error on failure.
119     */
120    public static function get_stats_video_plays( WP_REST_Request $request ) {
121        $blog_id = (int) Jetpack_Options::get_option( 'id' );
122        if ( ! $blog_id ) {
123            return new WP_Error(
124                'videopress_stats_not_connected',
125                esc_html__( 'This site is not connected to WordPress.com.', 'jetpack-videopress-pkg' ),
126                array( 'status' => 400 )
127            );
128        }
129
130        $params = array( 'complete_stats' => 'true' );
131        foreach ( array_keys( self::stats_video_plays_args() ) as $key ) {
132            $value = $request->get_param( $key );
133            if ( $value !== null && $value !== '' ) {
134                $params[ $key ] = $value;
135            }
136        }
137
138        $path     = sprintf(
139            'sites/%d/stats/video-plays?%s',
140            $blog_id,
141            http_build_query( $params )
142        );
143        $response = Client::wpcom_json_api_request_as_blog( $path );
144
145        if ( is_wp_error( $response ) ) {
146            return new WP_Error(
147                'videopress_stats_request_failed',
148                $response->get_error_message(),
149                array( 'status' => 500 )
150            );
151        }
152
153        $status = (int) wp_remote_retrieve_response_code( $response );
154        $body   = json_decode( wp_remote_retrieve_body( $response ), true );
155
156        if ( 200 !== $status ) {
157            $message = is_array( $body ) && isset( $body['message'] )
158                ? (string) $body['message']
159                : esc_html__( 'Unable to fetch VideoPress stats.', 'jetpack-videopress-pkg' );
160            return new WP_Error(
161                'videopress_stats_request_failed',
162                $message,
163                array( 'status' => $status ? $status : 500 )
164            );
165        }
166
167        return rest_ensure_response( $body );
168    }
169}