| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Settings REST Validator which provides a filter to allow other systems to short-circuit the |
| 4 |
* settings REST API response before the data is saved. |
| 5 |
* |
| 6 |
* @package SolidWP\Performance |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace SolidWP\Performance\Admin; |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
use WP_HTTP_Response; |
| 15 |
use WP_REST_Request; |
| 16 |
use WP_REST_Response; |
| 17 |
|
| 18 |
/** |
| 19 |
* The Settings REST Validator which provides a filter to allow other systems to short-circuit the |
| 20 |
* settings REST API response before the data is saved. |
| 21 |
* |
| 22 |
* @package SolidWP\Performance |
| 23 |
*/ |
| 24 |
final class Rest_Validator { |
| 25 |
|
| 26 |
/** |
| 27 |
* If our settings are being saved via /wp/v2/settings, allow us to short-circuit the response for more |
| 28 |
* in-depth validation. |
| 29 |
* |
| 30 |
* @filter rest_request_before_callbacks |
| 31 |
* |
| 32 |
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. |
| 33 |
* Usually a WP_REST_Response or WP_Error. |
| 34 |
* @param array $handler Route handler used for the request. |
| 35 |
* @param WP_REST_Request $request Request used to generate the response. |
| 36 |
* |
| 37 |
* @return mixed|null |
| 38 |
*/ |
| 39 |
public function validate( $response, array $handler, WP_REST_Request $request ) { |
| 40 |
// If there's already a response something else already processed this. |
| 41 |
if ( $response ) { |
| 42 |
return $response; |
| 43 |
} |
| 44 |
|
| 45 |
if ( $request->get_route() !== '/wp/v2/settings' || $request->get_method() !== 'POST' ) { |
| 46 |
return $response; |
| 47 |
} |
| 48 |
|
| 49 |
$params = $request->get_params(); |
| 50 |
$settings = $params[ Settings_Page::SETTINGS_SLUG ] ?? []; |
| 51 |
|
| 52 |
if ( ! $settings ) { |
| 53 |
return $response; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Filters the response before executing any REST API callbacks allowing us to short-circuit the response with |
| 58 |
* our own validation. Return a WP_Error object to short-circuit. |
| 59 |
* |
| 60 |
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. |
| 61 |
* Usually a WP_REST_Response or WP_Error. |
| 62 |
* @param array $settings The settings array. |
| 63 |
* @param array $handler Route handler used for the request. |
| 64 |
* @param WP_REST_Request $request Request used to generate the response. |
| 65 |
* |
| 66 |
* @return mixed|null |
| 67 |
*/ |
| 68 |
return apply_filters( 'solidwp/performance/settings/before_save', $response, $settings, $handler, $request ); |
| 69 |
} |
| 70 |
} |
| 71 |
|