Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 77 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| VideoPress_Rest_Api_V1_Settings | |
0.00% |
0 / 77 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_endpoints | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
6 | |||
| get_settings | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| update_settings | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | 'required' => true, |
| 56 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 57 | ), |
| 58 | ), |
| 59 | ), |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the value of the VideoPress settings. |
| 66 | * |
| 67 | * @return WP_Rest_Response - The response object. |
| 68 | */ |
| 69 | public static function get_settings() { |
| 70 | $has_connected_owner = Data::has_connected_owner(); |
| 71 | if ( ! $has_connected_owner ) { |
| 72 | return rest_ensure_response( |
| 73 | new WP_Error( |
| 74 | 'owner_not_connected', |
| 75 | 'User not connected.', |
| 76 | array( |
| 77 | 'code' => 503, |
| 78 | 'connect_url' => Admin_UI::get_admin_page_url(), |
| 79 | ) |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | $blog_id = Data::get_blog_id(); |
| 85 | if ( ! $blog_id ) { |
| 86 | return rest_ensure_response( |
| 87 | new WP_Error( 'site_not_registered', 'Site not registered.', 503 ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $status = 200; |
| 92 | $data = Data::get_videopress_settings(); |
| 93 | |
| 94 | return rest_ensure_response( |
| 95 | new WP_REST_Response( $data, $status ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Updates the value of the VideoPress settings when a new value |
| 101 | * is present on the request body. |
| 102 | * |
| 103 | * @param WP_REST_Request $request the request object. |
| 104 | * @return WP_Rest_Response - The response object. |
| 105 | */ |
| 106 | public static function update_settings( $request ) { |
| 107 | $has_connected_owner = Data::has_connected_owner(); |
| 108 | if ( ! $has_connected_owner ) { |
| 109 | return rest_ensure_response( |
| 110 | new WP_Error( |
| 111 | 'owner_not_connected', |
| 112 | 'User not connected.', |
| 113 | array( |
| 114 | 'code' => 503, |
| 115 | 'connect_url' => Admin_UI::get_admin_page_url(), |
| 116 | ) |
| 117 | ) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | $blog_id = Data::get_blog_id(); |
| 122 | if ( ! $blog_id ) { |
| 123 | return rest_ensure_response( |
| 124 | new WP_Error( 'site_not_registered', 'Site not registered.', 503 ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | $json_params = $request->get_json_params(); |
| 129 | |
| 130 | // We are sure that the param is set because it's required by the request |
| 131 | update_option( 'videopress_private_enabled_for_site', boolval( $json_params['videopress_videos_private_for_site'] ) ); |
| 132 | |
| 133 | return rest_ensure_response( |
| 134 | array( |
| 135 | 'code' => 'success', |
| 136 | 'message' => __( 'VideoPress settings updated successfully.', 'jetpack-videopress-pkg' ), |
| 137 | 'data' => 200, |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | } |