diff --git a/modules/images/webp-uploads/helper.php b/modules/images/webp-uploads/helper.php index e27dda992a..af5c82068c 100644 --- a/modules/images/webp-uploads/helper.php +++ b/modules/images/webp-uploads/helper.php @@ -241,6 +241,29 @@ function webp_uploads_get_attachment_sources( $attachment_id, $size = 'thumbnail return array(); } +/** + * Verifies if the request is for a frontend context within the
tag. + * + * @since n.e.x.t + * + * @return bool True if in the within a frontend request, false otherwise. + */ +function webp_uploads_in_frontend_body() { + global $wp_query; + + // Check if this request is generally outside (or before) any frontend context. + if ( ! isset( $wp_query ) || defined( 'REST_REQUEST' ) || defined( 'XMLRPC_REQUEST' ) || is_feed() ) { + return false; + } + + // Check if we're anywhere before 'template_redirect' or within the 'wp_head' action. + if ( ! did_action( 'template_redirect' ) || doing_action( 'wp_head' ) ) { + return false; + } + + return true; +} + /** * Check whether the additional image is larger than the original image. * diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 0f778b262d..60f1492ee2 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -444,6 +444,11 @@ function webp_uploads_remove_sources_files( $attachment_id ) { * @return string The content with the updated references to the images. */ function webp_uploads_update_image_references( $content ) { + // Bail early if request is not for the frontend. + if ( ! webp_uploads_in_frontend_body() ) { + return $content; + } + // This content does not have any tag on it, move forward. if ( ! preg_match_all( '/<(img)\s[^>]+>/', $content, $img_tags, PREG_SET_ORDER ) ) { return $content; diff --git a/tests/modules/images/webp-uploads/helper-tests.php b/tests/modules/images/webp-uploads/helper-tests.php index bff6dac2ea..a9338782db 100644 --- a/tests/modules/images/webp-uploads/helper-tests.php +++ b/tests/modules/images/webp-uploads/helper-tests.php @@ -454,4 +454,57 @@ public function it_should_never_discard_additional_image_if_filter_is_false( $or $output = webp_uploads_should_discard_additional_image_file( $original_filesize, $additional_filesize ); $this->assertFalse( $output ); } + + public function test_webp_uploads_in_frontend_body_without_wp_query() { + unset( $GLOBALS['wp_query'] ); + + $this->assertFalse( webp_uploads_in_frontend_body() ); + } + + public function test_webp_uploads_in_frontend_body_with_feed() { + $this->mock_empty_action( 'template_redirect' ); + $GLOBALS['wp_query']->is_feed = true; + + $this->assertFalse( webp_uploads_in_frontend_body() ); + } + + public function test_webp_uploads_in_frontend_body_without_template_redirect() { + $this->assertFalse( webp_uploads_in_frontend_body() ); + } + + public function test_webp_uploads_in_frontend_body_before_template_redirect() { + $result = webp_uploads_in_frontend_body(); + $this->mock_empty_action( 'template_redirect' ); + + $this->assertFalse( $result ); + } + + public function test_webp_uploads_in_frontend_body_after_template_redirect() { + $this->mock_empty_action( 'template_redirect' ); + $result = webp_uploads_in_frontend_body(); + + $this->assertTrue( $result ); + } + + public function test_webp_uploads_in_frontend_body_within_wp_head() { + $this->mock_empty_action( 'template_redirect' ); + + // Call function within a 'wp_head' callback. + remove_all_actions( 'wp_head' ); + $result = null; + add_action( + 'wp_head', + function() use ( &$result ) { + $result = webp_uploads_in_frontend_body(); + } + ); + do_action( 'wp_head' ); + + $this->assertFalse( $result ); + } + + private function mock_empty_action( $action ) { + remove_all_actions( $action ); + do_action( $action ); + } } diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index e4bc666433..7fcbc35a2b 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -330,6 +330,9 @@ public function it_should_remove_the_backup_sizes_and_sources_if_the_attachment_ * @test */ public function it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library() { + // Run critical hooks to satisfy webp_uploads_in_frontend_body() conditions. + $this->mock_frontend_body_hooks(); + $paragraph = 'Donec accumsan, sapien et
, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.
Donec accumsan, sapien et
, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.