Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoPress_Rest_Api_V1_Features
95.65% covered (success)
95.65%
22 / 23
75.00% covered (warning)
75.00%
3 / 4
7
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_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_features
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2/**
3 * VideoPress Features Endpoint
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use Automattic\Jetpack\My_Jetpack\Product;
11use Automattic\Jetpack\Status\Host;
12
13/**
14 * VideoPress REST API class for fetching site features from WPCOM.
15 */
16class VideoPress_Rest_Api_V1_Features {
17    /**
18     * Initializes the endpoints
19     *
20     * @return void
21     */
22    public static function init() {
23        add_action( 'rest_api_init', array( static::class, 'register_rest_endpoints' ) );
24    }
25
26    /**
27     * Register the REST API routes.
28     *
29     * @return void
30     */
31    public static function register_rest_endpoints() {
32        register_rest_route(
33            'videopress/v1',
34            'features',
35            array(
36                'methods'             => \WP_REST_Server::READABLE,
37                'callback'            => static::class . '::get_features',
38                'permission_callback' => static::class . '::permissions_callback',
39            )
40        );
41    }
42
43    /**
44     * Checks whether the user has permissions to see the features.
45     *
46     * @return boolean
47     */
48    public static function permissions_callback() {
49        return current_user_can( 'read' );
50    }
51
52    /**
53     * Returns VideoPress feature flags fetched fresh from WPCOM.
54     *
55     * Uses My Jetpack's Product::get_site_features_from_wpcom() which calls
56     * WPCOM /sites/%d/features API with a 15-second transient cache.
57     *
58     * @return \WP_REST_Response The response object.
59     */
60    public static function get_features() {
61        $features = Product::get_site_features_from_wpcom();
62
63        if ( is_wp_error( $features ) ) {
64            return rest_ensure_response( $features );
65        }
66
67        $active = $features['active'] ?? array();
68
69        return rest_ensure_response(
70            array(
71                'isVideoPressSupported'          => true, // Always true due to free tier.
72                // Check videopress-1tb-storage (Jetpack) or videopress (WordPress.com).
73                'isVideoPress1TBSupported'       => in_array( 'videopress-1tb-storage', $active, true )
74                    || ( ( new Host() )->is_wpcom_platform() && in_array( 'videopress', $active, true ) ),
75                'isVideoPressUnlimitedSupported' => in_array( 'videopress-unlimited-storage', $active, true ),
76            )
77        );
78    }
79}