Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
78 / 104 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| VideoPress_Rest_Api_V1_Settings | |
75.00% |
78 / 104 |
|
50.00% |
2 / 4 |
17.06 | |
0.00% |
0 / 1 |
| init | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_endpoints | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
2 | |||
| get_settings | |
40.91% |
9 / 22 |
|
0.00% |
0 / 1 |
4.86 | |||
| update_settings | |
66.67% |
26 / 39 |
|
0.00% |
0 / 1 |
10.37 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * VideoPress Settings Endpoint |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | |
| 10 | use WP_Error; |
| 11 | use WP_REST_Request; |
| 12 | use WP_REST_Response; |
| 13 | use WP_REST_Server; |
| 14 | |
| 15 | /** |
| 16 | * Rest API class for fetching and setting site settings related to VideoPress. |
| 17 | */ |
| 18 | class 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_share_menu_disabled' => array( |
| 69 | 'description' => __( 'If the share menu should be hidden on every video, overriding each video’s own setting', 'jetpack-videopress-pkg' ), |
| 70 | 'type' => 'boolean', |
| 71 | ), |
| 72 | ), |
| 73 | ), |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the value of the VideoPress settings. |
| 80 | * |
| 81 | * @return WP_Rest_Response - The response object. |
| 82 | */ |
| 83 | public static function get_settings() { |
| 84 | $has_connected_owner = Data::has_connected_owner(); |
| 85 | if ( ! $has_connected_owner ) { |
| 86 | return rest_ensure_response( |
| 87 | new WP_Error( |
| 88 | 'owner_not_connected', |
| 89 | 'User not connected.', |
| 90 | array( |
| 91 | 'code' => 503, |
| 92 | 'connect_url' => Admin_UI::get_admin_page_url(), |
| 93 | ) |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | $blog_id = Data::get_blog_id(); |
| 99 | if ( ! $blog_id ) { |
| 100 | return rest_ensure_response( |
| 101 | new WP_Error( 'site_not_registered', 'Site not registered.', 503 ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | $status = 200; |
| 106 | $data = Data::get_videopress_settings(); |
| 107 | |
| 108 | return rest_ensure_response( |
| 109 | new WP_REST_Response( $data, $status ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Updates the value of the VideoPress settings when a new value |
| 115 | * is present on the request body. |
| 116 | * |
| 117 | * @param WP_REST_Request $request the request object. |
| 118 | * @return WP_Rest_Response - The response object. |
| 119 | */ |
| 120 | public static function update_settings( $request ) { |
| 121 | $has_connected_owner = Data::has_connected_owner(); |
| 122 | if ( ! $has_connected_owner ) { |
| 123 | return rest_ensure_response( |
| 124 | new WP_Error( |
| 125 | 'owner_not_connected', |
| 126 | 'User not connected.', |
| 127 | array( |
| 128 | 'code' => 503, |
| 129 | 'connect_url' => Admin_UI::get_admin_page_url(), |
| 130 | ) |
| 131 | ) |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | $blog_id = Data::get_blog_id(); |
| 136 | if ( ! $blog_id ) { |
| 137 | return rest_ensure_response( |
| 138 | new WP_Error( 'site_not_registered', 'Site not registered.', 503 ) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | $private_for_site = $request->get_param( 'videopress_videos_private_for_site' ); |
| 143 | $auto_subtitles_disabled = $request->get_param( 'videopress_auto_subtitles_disabled' ); |
| 144 | $player_preload_disabled = $request->get_param( 'videopress_player_preload_disabled' ); |
| 145 | $inline_player_enabled = $request->get_param( 'videopress_inline_player_enabled' ); |
| 146 | $share_menu_disabled = $request->get_param( 'videopress_share_menu_disabled' ); |
| 147 | |
| 148 | if ( null !== $private_for_site ) { |
| 149 | update_option( 'videopress_private_enabled_for_site', $private_for_site ); |
| 150 | } |
| 151 | |
| 152 | if ( null !== $auto_subtitles_disabled ) { |
| 153 | update_option( 'videopress_auto_subtitles_disabled', $auto_subtitles_disabled ); |
| 154 | } |
| 155 | |
| 156 | if ( null !== $player_preload_disabled ) { |
| 157 | update_option( 'videopress_player_preload_disabled', $player_preload_disabled ); |
| 158 | } |
| 159 | |
| 160 | if ( null !== $inline_player_enabled ) { |
| 161 | update_option( 'videopress_inline_player_enabled', $inline_player_enabled ); |
| 162 | } |
| 163 | |
| 164 | if ( null !== $share_menu_disabled ) { |
| 165 | update_option( 'videopress_share_menu_disabled', $share_menu_disabled ); |
| 166 | } |
| 167 | |
| 168 | return rest_ensure_response( |
| 169 | array( |
| 170 | 'code' => 'success', |
| 171 | 'message' => __( 'VideoPress settings updated successfully.', 'jetpack-videopress-pkg' ), |
| 172 | 'data' => 200, |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | } |