Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.58% covered (warning)
76.58%
85 / 111
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoPress_Rest_Api_V1_Settings
76.58% covered (warning)
76.58%
85 / 111
50.00% covered (danger)
50.00%
2 / 4
17.89
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%
46 / 46
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
69.05% covered (warning)
69.05%
29 / 42
0.00% covered (danger)
0.00%
0 / 1
11.40
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                        'videopress_player_preload_disabled' => array(
61                            'description' => __( 'If embedded players should wait for playback before preloading video data', 'jetpack-videopress-pkg' ),
62                            'type'        => 'boolean',
63                        ),
64                        'videopress_inline_player_enabled' => array(
65                            'description' => __( 'If videos should render an inline player from one shared script instead of one frame per video', 'jetpack-videopress-pkg' ),
66                            'type'        => 'boolean',
67                        ),
68                        'videopress_download_disabled'     => array(
69                            'description' => __( 'If downloads should be turned off for every video, overriding each video’s own setting', 'jetpack-videopress-pkg' ),
70                            'type'        => 'boolean',
71                        ),
72                        'videopress_share_menu_disabled'   => array(
73                            'description' => __( 'If the share menu should be hidden on every video, overriding each video’s own setting', 'jetpack-videopress-pkg' ),
74                            'type'        => 'boolean',
75                        ),
76                    ),
77                ),
78            )
79        );
80    }
81
82    /**
83     * Returns the value of the VideoPress settings.
84     *
85     * @return WP_Rest_Response - The response object.
86     */
87    public static function get_settings() {
88        $has_connected_owner = Data::has_connected_owner();
89        if ( ! $has_connected_owner ) {
90            return rest_ensure_response(
91                new WP_Error(
92                    'owner_not_connected',
93                    'User not connected.',
94                    array(
95                        'code'        => 503,
96                        'connect_url' => Admin_UI::get_admin_page_url(),
97                    )
98                )
99            );
100        }
101
102        $blog_id = Data::get_blog_id();
103        if ( ! $blog_id ) {
104            return rest_ensure_response(
105                new WP_Error( 'site_not_registered', 'Site not registered.', 503 )
106            );
107        }
108
109        $status = 200;
110        $data   = Data::get_videopress_settings();
111
112        return rest_ensure_response(
113            new WP_REST_Response( $data, $status )
114        );
115    }
116
117    /**
118     * Updates the value of the VideoPress settings when a new value
119     * is present on the request body.
120     *
121     * @param WP_REST_Request $request the request object.
122     * @return WP_Rest_Response - The response object.
123     */
124    public static function update_settings( $request ) {
125        $has_connected_owner = Data::has_connected_owner();
126        if ( ! $has_connected_owner ) {
127            return rest_ensure_response(
128                new WP_Error(
129                    'owner_not_connected',
130                    'User not connected.',
131                    array(
132                        'code'        => 503,
133                        'connect_url' => Admin_UI::get_admin_page_url(),
134                    )
135                )
136            );
137        }
138
139        $blog_id = Data::get_blog_id();
140        if ( ! $blog_id ) {
141            return rest_ensure_response(
142                new WP_Error( 'site_not_registered', 'Site not registered.', 503 )
143            );
144        }
145
146        $private_for_site        = $request->get_param( 'videopress_videos_private_for_site' );
147        $auto_subtitles_disabled = $request->get_param( 'videopress_auto_subtitles_disabled' );
148        $player_preload_disabled = $request->get_param( 'videopress_player_preload_disabled' );
149        $inline_player_enabled   = $request->get_param( 'videopress_inline_player_enabled' );
150        $download_disabled       = $request->get_param( 'videopress_download_disabled' );
151        $share_menu_disabled     = $request->get_param( 'videopress_share_menu_disabled' );
152
153        if ( null !== $private_for_site ) {
154            update_option( 'videopress_private_enabled_for_site', $private_for_site );
155        }
156
157        if ( null !== $auto_subtitles_disabled ) {
158            update_option( 'videopress_auto_subtitles_disabled', $auto_subtitles_disabled );
159        }
160
161        if ( null !== $player_preload_disabled ) {
162            update_option( 'videopress_player_preload_disabled', $player_preload_disabled );
163        }
164
165        if ( null !== $inline_player_enabled ) {
166            update_option( 'videopress_inline_player_enabled', $inline_player_enabled );
167        }
168
169        if ( null !== $download_disabled ) {
170            update_option( 'videopress_download_disabled', $download_disabled );
171        }
172
173        if ( null !== $share_menu_disabled ) {
174            update_option( 'videopress_share_menu_disabled', $share_menu_disabled );
175        }
176
177        return rest_ensure_response(
178            array(
179                'code'    => 'success',
180                'message' => __( 'VideoPress settings updated successfully.', 'jetpack-videopress-pkg' ),
181                'data'    => 200,
182            )
183        );
184    }
185}