From 514c51344f993c010edff305c27084c65e3da70d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 2 Dec 2024 13:17:02 -0800 Subject: [PATCH] Remove od_store_url_metric_validity filter to be re-added in follow-up PR --- plugins/image-prioritizer/helper.php | 60 ------------------- plugins/image-prioritizer/hooks.php | 1 - .../storage/rest-api.php | 29 --------- 3 files changed, 90 deletions(-) diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index f569ecbb0..c93153dcf 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -136,66 +136,6 @@ function image_prioritizer_add_element_item_schema_properties( array $additional return $additional_properties; } -/** - * Validates that the provided background image URL is valid. - * - * @since n.e.x.t - * - * @param bool|WP_Error|mixed $validity Validity. Valid if true or a WP_Error without any errors, or invalid otherwise. - * @param OD_Strict_URL_Metric $url_metric URL Metric, already validated against the JSON Schema. - * @return bool|WP_Error Validity. Valid if true or a WP_Error without any errors, or invalid otherwise. - */ -function image_prioritizer_filter_store_url_metric_validity( $validity, OD_Strict_URL_Metric $url_metric ) { - if ( ! is_bool( $validity ) && ! ( $validity instanceof WP_Error ) ) { - $validity = (bool) $validity; - } - - $data = $url_metric->get( 'lcpElementExternalBackgroundImage' ); - if ( ! is_array( $data ) ) { - return $validity; - } - - $r = wp_safe_remote_head( - $data['url'], - array( - 'redirection' => 3, // Allow up to 3 redirects. - ) - ); - if ( $r instanceof WP_Error ) { - return new WP_Error( - WP_DEBUG ? $r->get_error_code() : 'head_request_failure', - __( 'HEAD request for background image URL failed.', 'image-prioritizer' ) . ( WP_DEBUG ? ' ' . $r->get_error_message() : '' ), - array( - 'code' => 500, - ) - ); - } - $response_code = wp_remote_retrieve_response_code( $r ); - if ( $response_code < 200 || $response_code >= 400 ) { - return new WP_Error( - 'background_image_response_not_ok', - __( 'HEAD request for background image URL did not return with a success status code.', 'image-prioritizer' ), - array( - 'code' => WP_DEBUG ? $response_code : 400, - ) - ); - } - - $content_type = wp_remote_retrieve_header( $r, 'Content-Type' ); - if ( ! is_string( $content_type ) || ! str_starts_with( $content_type, 'image/' ) ) { - return new WP_Error( - 'background_image_response_not_image', - __( 'HEAD request for background image URL did not return an image Content-Type.', 'image-prioritizer' ), - array( - 'code' => 400, - ) - ); - } - - // TODO: Check for the Content-Length and return invalid if it is gigantic? - return $validity; -} - /** * Gets the path to a script or stylesheet. * diff --git a/plugins/image-prioritizer/hooks.php b/plugins/image-prioritizer/hooks.php index 4a47f3564..7587e9e67 100644 --- a/plugins/image-prioritizer/hooks.php +++ b/plugins/image-prioritizer/hooks.php @@ -13,4 +13,3 @@ add_action( 'od_init', 'image_prioritizer_init' ); add_filter( 'od_extension_module_urls', 'image_prioritizer_filter_extension_module_urls' ); add_filter( 'od_url_metric_schema_root_additional_properties', 'image_prioritizer_add_element_item_schema_properties' ); -add_filter( 'od_store_url_metric_validity', 'image_prioritizer_filter_store_url_metric_validity', 10, 2 ); diff --git a/plugins/optimization-detective/storage/rest-api.php b/plugins/optimization-detective/storage/rest-api.php index 8e4828bec..38f3c9dda 100644 --- a/plugins/optimization-detective/storage/rest-api.php +++ b/plugins/optimization-detective/storage/rest-api.php @@ -210,35 +210,6 @@ function od_handle_rest_request( WP_REST_Request $request ) { ); } - /** - * Filters whether a URL Metric is valid for storage. - * - * This allows for custom validation constraints to be applied beyond what can be expressed in JSON Schema. This is - * also necessary because the 'validate_callback' key in a JSON Schema is not respected when gathering the REST API - * endpoint args via the {@see rest_get_endpoint_args_for_schema()} function. Besides this, the REST API doesn't - * support 'validate_callback' for any nested arguments in any case, meaning that custom constraints would be able - * to be applied to multidimensional objects, such as the items inside 'elements'. - * - * This filter only applies when storing a URL Metric via the REST API. It does not run when a stored URL Metric - * loaded from the od_url_metric post type. This means that validation logic enforced via this filter can be more - * expensive, such as doing filesystem checks or HTTP requests. - * - * @since n.e.x.t - * - * @param bool|WP_Error $validity Validity. Valid if true or a WP_Error without any errors, or invalid otherwise. - * @param OD_Strict_URL_Metric $url_metric URL Metric, already validated against the JSON Schema. - */ - $validity = apply_filters( 'od_store_url_metric_validity', true, $url_metric ); - if ( false === $validity || ( $validity instanceof WP_Error && $validity->has_errors() ) ) { - if ( false === $validity ) { - $validity = new WP_Error( 'invalid_url_metric', __( 'Validity of URL Metric was rejected by filter.', 'optimization-detective' ) ); - } - if ( ! isset( $validity->error_data['code'] ) ) { - $validity->error_data['code'] = 400; - } - return $validity; - } - // TODO: This should be changed from store_url_metric($slug, $url_metric) instead be update_post( $slug, $group_collection ). As it stands, store_url_metric() is duplicating logic here. $result = OD_URL_Metrics_Post_Type::store_url_metric( $request->get_param( 'slug' ),