Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.67% covered (warning)
68.67%
57 / 83
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoPress_Rest_Api_V1_Settings
68.67% covered (warning)
68.67%
57 / 83
50.00% covered (danger)
50.00%
2 / 4
14.72
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%
30 / 30
100.00% covered (success)
100.00%
1 / 1
2
 get_settings
40.91% covered (danger)
40.91%
9 / 22
0.00% covered (danger)
0.00%
0 / 1
4.86
 update_settings
56.67% covered (warning)
56.67%
17 / 30
0.00% covered (danger)
0.00%
0 / 1
7.03
1<?php
2/**
3 * VideoPress Settings Endpoint
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use WP_Error;
11use WP_REST_Request;
12use WP_REST_Response;
13use WP_REST_Server;
14
15/**
16 * Rest API class for fetching and setting site settings related to VideoPress.
17 */
18class VideoPress_Rest_Api_V1_Settings {
19    /**
20     * Initializes the endpoints
21     *
22     * @return void
23     */
24    public static function init() {
25        add_action( 'rest_api_init', array( static::class, 'register_rest_endpoints' ) );
26    }
27
28    /**
29     * Register the REST API routes.
30     *
31     * @return void
32     */
33    public static function register_rest_endpoints() {
34        register_rest_route(
35            'videopress/v1',
36            'settings',
37            array(
38                array(
39                    'methods'             => WP_REST_Server::READABLE,
40                    'callback'            => array( static::class, 'get_settings' ),
41                    'permission_callback' => function () {
42                        return current_user_can( 'manage_options' );
43                    },
44                ),
45                array(
46                    'methods'             => WP_REST_Server::EDITABLE,
47                    'callback'            => array( static::class, 'update_settings' ),
48                    'permission_callback' => function () {
49                        return Data::can_perform_action() && current_user_can( 'manage_options' );
50                    },
51                    'args'                => array(
52                        'videopress_videos_private_for_site' => array(
53                            'description' => __( 'If the VideoPress videos should be private by default', 'jetpack-videopress-pkg' ),
54                            'type'        => 'boolean',
55                        ),
56                        'videopress_auto_subtitles_disabled' => array(
57                            'description' => __( 'If auto-generated subtitles should be skipped for new videos', 'jetpack-videopress-pkg' ),
58                            'type'        => 'boolean',
59                        ),
60                    ),
61                ),
62            )
63        );
64    }
65
66    /**
67     * Returns the value of the VideoPress settings.
68     *
69     * @return WP_Rest_Response - The response object.
70     */
71    public static function get_settings() {
72        $has_connected_owner = Data::has_connected_owner();
73        if ( ! $has_connected_owner ) {
74            return rest_ensure_response(
75                new WP_Error(
76                    'owner_not_connected',
77                    'User not connected.',
78                    array(
79                        'code'        => 503,
80                        'connect_url' => Admin_UI::get_admin_page_url(),
81                    )
82                )
83            );
84        }
85
86        $blog_id = Data::get_blog_id();
87        if ( ! $blog_id ) {
88            return rest_ensure_response(
89                new WP_Error( 'site_not_registered', 'Site not registered.', 503 )
90            );
91        }
92
93        $status = 200;
94        $data   = Data::get_videopress_settings();
95
96        return rest_ensure_response(
97            new WP_REST_Response( $data, $status )
98        );
99    }
100
101    /**
102     * Updates the value of the VideoPress settings when a new value
103     * is present on the request body.
104     *
105     * @param WP_REST_Request $request the request object.
106     * @return WP_Rest_Response - The response object.
107     */
108    public static function update_settings( $request ) {
109        $has_connected_owner = Data::has_connected_owner();
110        if ( ! $has_connected_owner ) {
111            return rest_ensure_response(
112                new WP_Error(
113                    'owner_not_connected',
114                    'User not connected.',
115                    array(
116                        'code'        => 503,
117                        'connect_url' => Admin_UI::get_admin_page_url(),
118                    )
119                )
120            );
121        }
122
123        $blog_id = Data::get_blog_id();
124        if ( ! $blog_id ) {
125            return rest_ensure_response(
126                new WP_Error( 'site_not_registered', 'Site not registered.', 503 )
127            );
128        }
129
130        $private_for_site        = $request->get_param( 'videopress_videos_private_for_site' );
131        $auto_subtitles_disabled = $request->get_param( 'videopress_auto_subtitles_disabled' );
132
133        if ( null !== $private_for_site ) {
134            update_option( 'videopress_private_enabled_for_site', $private_for_site );
135        }
136
137        if ( null !== $auto_subtitles_disabled ) {
138            update_option( 'videopress_auto_subtitles_disabled', $auto_subtitles_disabled );
139        }
140
141        return rest_ensure_response(
142            array(
143                'code'    => 'success',
144                'message' => __( 'VideoPress settings updated successfully.', 'jetpack-videopress-pkg' ),
145                'data'    => 200,
146            )
147        );
148    }
149}