| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ContentControl\RestAPI; |
| 10 | 10 | |
| 11 | -use WP_Rest_Controller, WP_REST_Response, WP_REST_Server, WP_Error; | |
| 11 | +use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error; | |
| 12 | 12 | use function ContentControl\get_all_plugin_options; |
| 13 | 13 | use function ContentControl\update_plugin_options; |
| 14 | 14 | |
| 15 | 15 | defined( 'ABSPATH' ) || exit; |
| @@ -34,8 +34,10 @@ | ||
| 34 | 34 | protected $base = 'settings'; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Register API endpoint routes. |
| 38 | + * | |
| 39 | + * @return void | |
| 38 | 40 | */ |
| 39 | 41 | public function register_routes() { |
| 40 | 42 | register_rest_route( |
| 41 | 43 | $this->namespace, |
| @@ -49,9 +51,9 @@ | ||
| 49 | 51 | [ |
| 50 | 52 | 'methods' => WP_REST_Server::EDITABLE, |
| 51 | 53 | 'callback' => [ $this, 'update_settings' ], |
| 52 | 54 | 'permission_callback' => [ $this, 'update_settings_permissions' ], |
| 53 | - 'args' => $this->get_endpoint_args_for_item_schema( true ), | |
| 55 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 54 | 56 | ], |
| 55 | 57 | 'schema' => [ $this, 'get_schema' ], |
| 56 | 58 | ] |
| 57 | 59 | ); |
| @@ -65,9 +67,9 @@ | ||
| 65 | 67 | public function get_settings() { |
| 66 | 68 | $settings = get_all_plugin_options(); |
| 67 | 69 | |
| 68 | 70 | if ( $settings ) { |
| 69 | - return new WP_REST_Response( $settings, 200 ); | |
| 71 | + return new WP_REST_Response( [ 'settings' => $settings ], 200 ); | |
| 70 | 72 | } else { |
| 71 | 73 | return new WP_Error( '404', __( 'Something went wrong, the settings could not be found.', 'content-control' ), [ 'status' => 404 ] ); |
| 72 | 74 | } |
| 73 | 75 | } |
| @@ -74,14 +76,14 @@ | ||
| 74 | 76 | |
| 75 | 77 | /** |
| 76 | 78 | * Update plugin settings. |
| 77 | 79 | * |
| 78 | - * @param WP_REST_Request $request Request object. | |
| 80 | + * @param \WP_REST_Request<array<string,mixed>> $request Request object. | |
| 79 | 81 | * |
| 80 | - * @return WP_Error|WP_REST_Response | |
| 82 | + * @return \WP_Error|\WP_REST_Response | |
| 81 | 83 | */ |
| 82 | 84 | public function update_settings( $request ) { |
| 83 | - $settings = $request->get_params(); | |
| 85 | + $settings = $request->get_param( 'settings' ); | |
| 84 | 86 | |
| 85 | 87 | $error_message = __( 'Something went wrong, the settings could not be updated.', 'content-control' ); |
| 86 | 88 | |
| 87 | 89 | if ( ! get_all_plugin_options() ) { |
| @@ -87,12 +89,12 @@ | ||
| 87 | 89 | if ( ! get_all_plugin_options() ) { |
| 88 | 90 | return new WP_Error( '500', $error_message, [ 'status' => 500 ] ); |
| 89 | 91 | } |
| 90 | 92 | |
| 91 | - update_plugin_options( $settings ); | |
| 93 | + $updated = update_plugin_options( $settings ); | |
| 92 | 94 | $new_settings = get_all_plugin_options(); |
| 93 | 95 | |
| 94 | - if ( $new_settings ) { | |
| 96 | + if ( $updated ) { | |
| 95 | 97 | return new WP_REST_Response( $new_settings, 200 ); |
| 96 | 98 | } else { |
| 97 | 99 | return new WP_Error( '404', $error_message, [ 'status' => 404 ] ); |
| 98 | 100 | } |
| @@ -109,9 +111,9 @@ | ||
| 109 | 111 | |
| 110 | 112 | /** |
| 111 | 113 | * Get settings schema. |
| 112 | 114 | * |
| 113 | - * @return array | |
| 115 | + * @return array<string,array<string,mixed>> | |
| 114 | 116 | */ |
| 115 | 117 | public function get_schema() { |
| 116 | 118 | if ( $this->schema ) { |
| 117 | 119 | // Bail early if already cached. |
| @@ -124,34 +126,10 @@ | ||
| 124 | 126 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 125 | 127 | 'title' => 'settings', |
| 126 | 128 | 'type' => 'object', |
| 127 | 129 | 'properties' => [ |
| 128 | - 'block_controls' => [ | |
| 129 | - 'type' => 'object', | |
| 130 | - 'properties' => [ | |
| 131 | - 'enable' => [ | |
| 132 | - 'type' => 'boolean', | |
| 133 | - ], | |
| 134 | - 'controls' => [ | |
| 135 | - 'type' => 'object', | |
| 136 | - 'properties' => [ | |
| 137 | - 'device_rules' => [ | |
| 138 | - 'type' => 'object', | |
| 139 | - 'properties' => [ | |
| 140 | - 'enable' => [ | |
| 141 | - 'type' => 'boolean', | |
| 142 | - ], | |
| 143 | - ], | |
| 144 | - ], | |
| 145 | - ], | |
| 146 | - ], | |
| 147 | - 'disabled_blocks' => [ | |
| 148 | - 'type' => 'array', | |
| 149 | - 'items' => [ | |
| 150 | - 'type' => 'string', | |
| 151 | - ], | |
| 152 | - ], | |
| 153 | - ], | |
| 130 | + 'settings' => [ | |
| 131 | + 'type' => 'object', | |
| 154 | 132 | ], |
| 155 | 133 | ], |
| 156 | 134 | ] |
| 157 | 135 | ); |