From e47ea75cd486d9e1fafe39f0aa61d2d93819ea3e Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 12:23:52 +0530 Subject: [PATCH 1/9] Add filter --- src/wp-includes/media.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 335e4157982a5..d72406bbb4b88 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5627,6 +5627,25 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { $loading_attrs = array(); + /** + * Filters whether to short-circuit loading optimization attributes. + * + * Returning an array from the filter will effectively short-circuit the loading of optimization attributes, + * returning that value instead. + * + * @since 6.4.0 + * + * @param array $loading_attrs The loading optimization attributes. + * @param string $tag_name The tag name. + * @param array $attr Array of the attributes for the tag. + * @param string $context Context for the element for which the loading optimization attribute is requested. + */ + $loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); + + if ( ! empty( $loading_attrs ) ) { + return $loading_attrs; + } + /* * Skip lazy-loading for the overall block template, as it is handled more granularly. * The skip is also applicable for `fetchpriority`. @@ -5789,6 +5808,18 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { } } + /** + * Filters the loading optimization attributes. + * + * @since 6.4.0 + * + * @param array $loading_attrs The loading optimization attributes. + * @param string $tag_name The tag name. + * @param array $attr Array of the attributes for the tag. + * @param string $context Context for the element for which the loading optimization attribute is requested. + */ + $loading_attrs = apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); + return $loading_attrs; } From 1c8cc31e0069e1191e33dfb8684fcc42fbedb82f Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 12:24:35 +0530 Subject: [PATCH 2/9] Unit tests --- tests/phpunit/tests/media.php | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index f90378b111295..4f47411f5eeb8 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -4330,6 +4330,64 @@ public function test_wp_get_loading_optimization_attributes( $context ) { } } + /** + * Tests for pre_wp_get_loading_optimization_attributes filter. + * + * @ticket 58893 + */ + public function test_pre_wp_get_loading_optimization_attributes_filter() { + add_filter( 'pre_wp_get_loading_optimization_attributes', function ( $loading_attrs ) { + $loading_attrs['fetchpriority'] = 'high'; + return $loading_attrs; + }, 10, 1 ); + + $attr = $this->get_width_height_for_high_priority(); + + $this->assertSame( + array( 'fetchpriority' => 'high' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + ); + + // Clean up the filter. + add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' ); + + // Modify the loading attributes with any custom attributes. + add_filter( 'pre_wp_get_loading_optimization_attributes', function ( $loading_attrs ) { + $loading_attrs['custom_attr'] = 'custom_value'; + return $loading_attrs; + }, 10, 1 ); + + $this->assertSame( + array( 'custom_attr' => 'custom_value' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + ); + } + + /** + * Tests for wp_get_loading_optimization_attributes filter. + * + * @ticket 58893 + */ + public function test_wp_get_loading_optimization_attributes_filter() { + $attr = $this->get_width_height_for_high_priority(); + + $this->assertSame( + array( 'loading' => 'lazy' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + ); + + add_filter( 'wp_get_loading_optimization_attributes', function ( $loading_attrs ) { + unset( $loading_attrs['loading'] ); + $loading_attrs['fetchpriority'] = 'high'; + return $loading_attrs; + }, 10, 4 ); + + $this->assertSame( + array( 'fetchpriority' => 'high' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + ); + } + /** * Tests that wp_get_loading_optimization_attributes() returns fetchpriority=high and increases the count for arbitrary contexts in the main loop. * From c205a8abb93f8e53fa0f8ececf77d2e4a3621a14 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 13:43:40 +0530 Subject: [PATCH 3/9] Update media.php --- tests/phpunit/tests/media.php | 45 +++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 4f47411f5eeb8..1211163576771 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -4336,30 +4336,42 @@ public function test_wp_get_loading_optimization_attributes( $context ) { * @ticket 58893 */ public function test_pre_wp_get_loading_optimization_attributes_filter() { - add_filter( 'pre_wp_get_loading_optimization_attributes', function ( $loading_attrs ) { - $loading_attrs['fetchpriority'] = 'high'; - return $loading_attrs; - }, 10, 1 ); + add_filter( + 'pre_wp_get_loading_optimization_attributes', + function ( $loading_attrs ) { + $loading_attrs['fetchpriority'] = 'high'; + return $loading_attrs; + }, + 10, + 1 + ); $attr = $this->get_width_height_for_high_priority(); $this->assertSame( array( 'fetchpriority' => 'high' ), wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not return early fetchpriority attribute' ); // Clean up the filter. add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' ); // Modify the loading attributes with any custom attributes. - add_filter( 'pre_wp_get_loading_optimization_attributes', function ( $loading_attrs ) { - $loading_attrs['custom_attr'] = 'custom_value'; - return $loading_attrs; - }, 10, 1 ); + add_filter( + 'pre_wp_get_loading_optimization_attributes', + function ( $loading_attrs ) { + $loading_attrs['custom_attr'] = 'custom_value'; + return $loading_attrs; + }, + 10, + 1 + ); $this->assertSame( array( 'custom_attr' => 'custom_value' ), wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not return custom attributes' ); } @@ -4374,17 +4386,24 @@ public function test_wp_get_loading_optimization_attributes_filter() { $this->assertSame( array( 'loading' => 'lazy' ), wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'Before the filter it will return the loading attribute' ); - add_filter( 'wp_get_loading_optimization_attributes', function ( $loading_attrs ) { - unset( $loading_attrs['loading'] ); - $loading_attrs['fetchpriority'] = 'high'; - return $loading_attrs; - }, 10, 4 ); + add_filter( + 'wp_get_loading_optimization_attributes', + function ( $loading_attrs ) { + unset( $loading_attrs['loading'] ); + $loading_attrs['fetchpriority'] = 'high'; + return $loading_attrs; + }, + 10, + 1 + ); $this->assertSame( array( 'fetchpriority' => 'high' ), wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'After the filter it will return the fetchpriority attribute' ); } From d9a956b496c4fbf11f675596d3b198e3afb9d8ae Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 13:54:18 +0530 Subject: [PATCH 4/9] Apply suggestions from code review --- tests/phpunit/tests/media.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 1211163576771..9a6705d14f065 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -4345,7 +4345,6 @@ function ( $loading_attrs ) { 10, 1 ); - $attr = $this->get_width_height_for_high_priority(); $this->assertSame( @@ -4390,7 +4389,7 @@ public function test_wp_get_loading_optimization_attributes_filter() { ); add_filter( - 'wp_get_loading_optimization_attributes', + 'wp_get_loading_optimization_attributes', function ( $loading_attrs ) { unset( $loading_attrs['loading'] ); $loading_attrs['fetchpriority'] = 'high'; From 24704391a79d550eaaea97fa0f28c73972c74f91 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 14:04:35 +0530 Subject: [PATCH 5/9] Apply suggestions from code review --- tests/phpunit/tests/media.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 9a6705d14f065..ec2389e1b67b6 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -4335,7 +4335,7 @@ public function test_wp_get_loading_optimization_attributes( $context ) { * * @ticket 58893 */ - public function test_pre_wp_get_loading_optimization_attributes_filter() { + public function pre_wp_get_loading_optimization_attributes_filter() { add_filter( 'pre_wp_get_loading_optimization_attributes', function ( $loading_attrs ) { @@ -4379,7 +4379,7 @@ function ( $loading_attrs ) { * * @ticket 58893 */ - public function test_wp_get_loading_optimization_attributes_filter() { + public function wp_get_loading_optimization_attributes_filter() { $attr = $this->get_width_height_for_high_priority(); $this->assertSame( From fba05bfc56240a8159092887292d928174d38193 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 19 Sep 2023 14:17:22 +0530 Subject: [PATCH 6/9] Apply suggestions from code review --- tests/phpunit/tests/media.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index ec2389e1b67b6..dcd824be3558d 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -4335,10 +4335,10 @@ public function test_wp_get_loading_optimization_attributes( $context ) { * * @ticket 58893 */ - public function pre_wp_get_loading_optimization_attributes_filter() { + public function test_pre_wp_get_loading_optimization_attributes_filter() { add_filter( 'pre_wp_get_loading_optimization_attributes', - function ( $loading_attrs ) { + static function ( $loading_attrs ) { $loading_attrs['fetchpriority'] = 'high'; return $loading_attrs; }, @@ -4359,7 +4359,7 @@ function ( $loading_attrs ) { // Modify the loading attributes with any custom attributes. add_filter( 'pre_wp_get_loading_optimization_attributes', - function ( $loading_attrs ) { + static function ( $loading_attrs ) { $loading_attrs['custom_attr'] = 'custom_value'; return $loading_attrs; }, @@ -4379,7 +4379,7 @@ function ( $loading_attrs ) { * * @ticket 58893 */ - public function wp_get_loading_optimization_attributes_filter() { + public function test_wp_get_loading_optimization_attributes_filter() { $attr = $this->get_width_height_for_high_priority(); $this->assertSame( @@ -4390,7 +4390,7 @@ public function wp_get_loading_optimization_attributes_filter() { add_filter( 'wp_get_loading_optimization_attributes', - function ( $loading_attrs ) { + static function ( $loading_attrs ) { unset( $loading_attrs['loading'] ); $loading_attrs['fetchpriority'] = 'high'; return $loading_attrs; From 8a8a8b45601adadbfaf948807f4f3ce8d114dbf3 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 20 Sep 2023 09:41:18 +0530 Subject: [PATCH 7/9] Update media.php --- src/wp-includes/media.php | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index d72406bbb4b88..6e26389355aa4 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5625,8 +5625,6 @@ function wp_get_webp_info( $filename ) { function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { global $wp_query; - $loading_attrs = array(); - /** * Filters whether to short-circuit loading optimization attributes. * @@ -5635,33 +5633,38 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { * * @since 6.4.0 * - * @param array $loading_attrs The loading optimization attributes. - * @param string $tag_name The tag name. - * @param array $attr Array of the attributes for the tag. - * @param string $context Context for the element for which the loading optimization attribute is requested. + * @param array|false $loading_attrs False by default, or array of loading optimization attributes to short-circuit. + * @param string $tag_name The tag name. + * @param array $attr Array of the attributes for the tag. + * @param string $context Context for the element for which the loading optimization attribute is requested. */ - $loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); + $loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', false, $tag_name, $attr, $context ); - if ( ! empty( $loading_attrs ) ) { + if ( is_array( $loading_attrs ) ) { return $loading_attrs; } + $loading_attrs = array(); + /* * Skip lazy-loading for the overall block template, as it is handled more granularly. * The skip is also applicable for `fetchpriority`. */ if ( 'template' === $context ) { - return $loading_attrs; + /** This filter is documented in wp-includes/media.php */ + return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); } // For now this function only supports images and iframes. if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) { - return $loading_attrs; + /** This filter is documented in wp-includes/media.php */ + return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); } // For any resources, width and height must be provided, to avoid layout shifts. if ( ! isset( $attr['width'], $attr['height'] ) ) { - return $loading_attrs; + /** This filter is documented in wp-includes/media.php */ + return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); } /* @@ -5673,7 +5676,8 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { */ // TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853). if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) { - return $loading_attrs; + /** This filter is documented in wp-includes/media.php */ + return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); } /* @@ -5818,9 +5822,7 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { * @param array $attr Array of the attributes for the tag. * @param string $context Context for the element for which the loading optimization attribute is requested. */ - $loading_attrs = apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); - - return $loading_attrs; + return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); } /** From 6513bf3c320d3705596abd2cb70eb48efe3f612c Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 20 Sep 2023 09:41:51 +0530 Subject: [PATCH 8/9] Update media.php --- tests/phpunit/tests/media.php | 193 +++++++++++++++++++--------------- 1 file changed, 106 insertions(+), 87 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index bd921751b103c..7815e256d6a22 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -552,8 +552,8 @@ public function test_post_galleries_images() { $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; } - $ids1_joined = implode( ',', array_slice( $ids1, 0, 3 ) ); - $ids2_joined = implode( ',', array_slice( $ids2, 3, 3 ) ); + $ids1_joined = join( ',', array_slice( $ids1, 0, 3 ) ); + $ids2_joined = join( ',', array_slice( $ids2, 3, 3 ) ); $blob = <<'; } - $imgs1_joined = implode( "\n", array_slice( $imgs, 0, 3 ) ); - $imgs2_joined = implode( "\n", array_slice( $imgs, 3, 3 ) ); + $imgs1_joined = join( "\n", array_slice( $imgs, 0, 3 ) ); + $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) ); $blob = << @@ -677,8 +677,8 @@ public function test_block_post_gallery_images_json() { } - $ids1_joined = implode( ',', array_slice( $ids, 0, 3 ) ); - $ids2_joined = implode( ',', array_slice( $ids, 3, 3 ) ); + $ids1_joined = join( ',', array_slice( $ids, 0, 3 ) ); + $ids2_joined = join( ',', array_slice( $ids, 3, 3 ) ); $blob = << @@ -718,9 +718,9 @@ public function test_mixed_post_gallery_images() { $imgs[] = '
'; } - $ids1_joined = implode( "\n", array_slice( $ids, 0, 3 ) ); - $ids2_joined = implode( "\n", array_slice( $ids, 3, 3 ) ); - $imgs2_joined = implode( "\n", array_slice( $imgs, 3, 3 ) ); + $ids1_joined = join( "\n", array_slice( $ids, 0, 3 ) ); + $ids2_joined = join( "\n", array_slice( $ids, 3, 3 ) ); + $imgs2_joined = join( "\n", array_slice( $imgs, 3, 3 ) ); $blob = << @@ -805,7 +805,7 @@ public function test_block_post_gallery_innerblock_images() { } - $imgs_joined = implode( "\n", $imgs ); + $imgs_joined = join( "\n", $imgs ); $blob = << @@ -4330,82 +4330,6 @@ public function test_wp_get_loading_optimization_attributes( $context ) { } } - /** - * Tests for pre_wp_get_loading_optimization_attributes filter. - * - * @ticket 58893 - */ - public function test_pre_wp_get_loading_optimization_attributes_filter() { - add_filter( - 'pre_wp_get_loading_optimization_attributes', - static function ( $loading_attrs ) { - $loading_attrs['fetchpriority'] = 'high'; - return $loading_attrs; - }, - 10, - 1 - ); - $attr = $this->get_width_height_for_high_priority(); - - $this->assertSame( - array( 'fetchpriority' => 'high' ), - wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), - 'The filter did not return early fetchpriority attribute' - ); - - // Clean up the filter. - add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' ); - - // Modify the loading attributes with any custom attributes. - add_filter( - 'pre_wp_get_loading_optimization_attributes', - static function ( $loading_attrs ) { - $loading_attrs['custom_attr'] = 'custom_value'; - return $loading_attrs; - }, - 10, - 1 - ); - - $this->assertSame( - array( 'custom_attr' => 'custom_value' ), - wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), - 'The filter did not return custom attributes' - ); - } - - /** - * Tests for wp_get_loading_optimization_attributes filter. - * - * @ticket 58893 - */ - public function test_wp_get_loading_optimization_attributes_filter() { - $attr = $this->get_width_height_for_high_priority(); - - $this->assertSame( - array( 'loading' => 'lazy' ), - wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), - 'Before the filter it will return the loading attribute' - ); - - add_filter( - 'wp_get_loading_optimization_attributes', - static function ( $loading_attrs ) { - unset( $loading_attrs['loading'] ); - $loading_attrs['fetchpriority'] = 'high'; - return $loading_attrs; - }, - 10, - 1 - ); - - $this->assertSame( - array( 'fetchpriority' => 'high' ), - wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), - 'After the filter it will return the fetchpriority attribute' - ); - } - /** * Tests that wp_get_loading_optimization_attributes() returns fetchpriority=high and increases the count for arbitrary contexts in the main loop. * @@ -5516,6 +5440,101 @@ public function test_wp_get_loading_optimization_attributes_image_before_loop_in $this->assertSame( 1, wp_increase_content_media_count( 0 ) ); } + /** + * Tests for pre_wp_get_loading_optimization_attributes filter. + * + * @ticket 58893 + */ + public function test_pre_wp_get_loading_optimization_attributes_filter() { + add_filter( + 'pre_wp_get_loading_optimization_attributes', + static function ( $loading_attrs ) { + $loading_attrs['fetchpriority'] = 'high'; + + return $loading_attrs; + }, + 10, + 1 + ); + + $attr = $this->get_width_height_for_high_priority(); + + $this->assertSame( + array( 'fetchpriority' => 'high' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not return early fetchpriority attribute' + ); + + // Clean up the filter. + add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_false' ); + + $this->assertSame( + array( 'loading' => 'lazy' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not return the default attributes.' + ); + + // Return no loading attributes. + add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' ); + + $this->assertSame( + array(), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not clean up all attributes.' + ); + + // Modify the loading attributes with any custom attributes. + add_filter( + 'pre_wp_get_loading_optimization_attributes', + static function ( $loading_attrs ) { + $loading_attrs['custom_attr'] = 'custom_value'; + + return $loading_attrs; + }, + 10, + 1 + ); + + $this->assertSame( + array( 'custom_attr' => 'custom_value' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'The filter did not return custom attributes.' + ); + } + + /** + * Tests for wp_get_loading_optimization_attributes filter. + * + * @ticket 58893 + */ + public function test_wp_get_loading_optimization_attributes_filter() { + $attr = $this->get_width_height_for_high_priority(); + + $this->assertSame( + array( 'loading' => 'lazy' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'Before the filter it will not return the loading attribute.' + ); + + add_filter( + 'wp_get_loading_optimization_attributes', + static function ( $loading_attrs ) { + unset( $loading_attrs['loading'] ); + $loading_attrs['fetchpriority'] = 'high'; + + return $loading_attrs; + }, + 10, + 1 + ); + + $this->assertSame( + array( 'fetchpriority' => 'high' ), + wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ), + 'After the filter it will not return the fetchpriority attribute.' + ); + } + /** * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter. * From 94250849bda14df4c291354b9aa538b6961cbad7 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 20 Sep 2023 10:50:29 +0530 Subject: [PATCH 9/9] Apply suggestions from code review --- tests/phpunit/tests/media.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 7815e256d6a22..12846477bcb3b 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -5449,6 +5449,10 @@ public function test_pre_wp_get_loading_optimization_attributes_filter() { add_filter( 'pre_wp_get_loading_optimization_attributes', static function ( $loading_attrs ) { + if ( false === $loading_attrs ) { + // Initialize as an empty array. + $loading_attrs = array(); + } $loading_attrs['fetchpriority'] = 'high'; return $loading_attrs; @@ -5487,6 +5491,10 @@ static function ( $loading_attrs ) { add_filter( 'pre_wp_get_loading_optimization_attributes', static function ( $loading_attrs ) { + if ( false === $loading_attrs ) { + // Initialize as an empty array. + $loading_attrs = array(); + } $loading_attrs['custom_attr'] = 'custom_value'; return $loading_attrs;