Skip to content

Commit 4a8dc16

Browse files
committed
Account for route matching being case insensitive
1 parent f8a00b4 commit 4a8dc16

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,12 @@ static function ( $host ) {
285285
* @noinspection PhpDocMissingThrowsInspection
286286
*/
287287
function image_prioritizer_filter_rest_request_before_callbacks( $response, array $handler, WP_REST_Request $request ) {
288-
if ( $request->get_method() !== 'POST' || OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== trim( $request->get_route(), '/' ) ) {
288+
if (
289+
$request->get_method() !== 'POST'
290+
||
291+
// The strtolower() is due to \WP_REST_Server::match_request_to_handler() using case-insensitive pattern match.
292+
OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE !== strtolower( trim( $request->get_route(), '/' ) )
293+
) {
289294
return $response;
290295
}
291296

plugins/image-prioritizer/tests/test-helper.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo
760760
};
761761

762762
return array(
763-
'invalid_external_bg_image' => array(
763+
'invalid_external_bg_image' => array(
764764
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
765765
$url_metric_data = $get_sample_url_metric_data();
766766

@@ -786,7 +786,7 @@ public function data_provider_to_test_image_prioritizer_filter_rest_request_befo
786786
},
787787
),
788788

789-
'valid_external_bg_image' => array(
789+
'valid_external_bg_image' => array(
790790
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
791791
$url_metric_data = $get_sample_url_metric_data();
792792
$image_url = home_url( '/good.jpg' );
@@ -846,7 +846,26 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
846846
},
847847
),
848848

849-
'not_store_post_request' => array(
849+
'invalid_external_bg_image_variant_route' => array(
850+
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
851+
$url_metric_data = $get_sample_url_metric_data();
852+
853+
$url_metric_data['lcpElementExternalBackgroundImage'] = array(
854+
'url' => 'https://bad-origin.example.com/image.jpg',
855+
'tag' => 'DIV',
856+
'id' => null,
857+
'class' => null,
858+
);
859+
$request = $create_request( $url_metric_data );
860+
$request->set_route( str_replace( 'store', 'STORE', $request->get_route() ) );
861+
return $request;
862+
},
863+
'assert' => function ( WP_REST_Request $request ): void {
864+
$this->assertArrayNotHasKey( 'lcpElementExternalBackgroundImage', $request );
865+
},
866+
),
867+
868+
'not_store_post_request' => array(
850869
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
851870
$url_metric_data = $get_sample_url_metric_data();
852871
$url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/';
@@ -860,7 +879,7 @@ static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
860879
},
861880
),
862881

863-
'not_store_request' => array(
882+
'not_store_request' => array(
864883
'set_up' => static function () use ( $get_sample_url_metric_data, $create_request ): WP_REST_Request {
865884
$url_metric_data = $get_sample_url_metric_data();
866885
$url_metric_data['lcpElementExternalBackgroundImage'] = 'https://totally-different.example.com/';

0 commit comments

Comments
 (0)