Changeset 55816
- Timestamp:
- 05/16/2023 06:50:11 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r55764 r55816 5436 5436 * Under the hood, the function uses {@see wp_increase_content_media_count()} every time it is called for an element 5437 5437 * within the main content. If the element is the very first content element, the `loading` attribute will be omitted. 5438 * This default threshold of 1 content elementto omit the `loading` attribute for can be customized using the5438 * This default threshold of 3 content elements to omit the `loading` attribute for can be customized using the 5439 5439 * {@see 'wp_omit_loading_attr_threshold'} filter. 5440 5440 * … … 5486 5486 * Gets the threshold for how many of the first content media elements to not lazy-load. 5487 5487 * 5488 * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 1.5488 * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3. 5489 5489 * The filter is only run once per page load, unless the `$force` parameter is used. 5490 5490 * … … 5507 5507 * 5508 5508 * @since 5.9.0 5509 * @since 6.3.0 The default threshold was changed from 1 to 3. 5509 5510 * 5510 * @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 1.5511 * @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 3. 5511 5512 */ 5512 $omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 1);5513 $omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 3 ); 5513 5514 } 5514 5515 -
trunk/tests/phpunit/tests/media.php
r55467 r55816 3586 3586 $this->assertSame( 'lazy', wp_get_loading_attr_default( 'wp_get_attachment_image' ) ); 3587 3587 3588 // Return `false` if in the loop and in the main query and it is the first element. 3589 $this->assertFalse( wp_get_loading_attr_default( $context ) ); 3588 // Return `false` in the main query for first three element. 3589 $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected first image to not be lazy-loaded.' ); 3590 $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected second image to not be lazy-loaded.' ); 3591 $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected third image to not be lazy-loaded.' ); 3590 3592 3591 3593 // Return 'lazy' if in the loop and in the main query for any subsequent elements. … … 3619 3621 $this->reset_omit_loading_attr_filter(); 3620 3622 3621 // Use the filter to alter the threshold for not lazy-loading to the first three elements. 3622 add_filter( 3623 'wp_omit_loading_attr_threshold', 3624 function() { 3625 return 3; 3626 } 3627 ); 3623 // Use the filter to alter the threshold for not lazy-loading to the first five elements. 3624 $this->force_omit_loading_attr_threshold( 5 ); 3628 3625 3629 3626 while ( have_posts() ) { 3630 3627 the_post(); 3631 3628 3632 // Due to the filter, now the first three elements should not be lazy-loaded, i.e. return `false`.3633 for ( $i = 0; $i < 3; $i++ ) {3629 // Due to the filter, now the first five elements should not be lazy-loaded, i.e. return `false`. 3630 for ( $i = 0; $i < 5; $i++ ) { 3634 3631 $this->assertFalse( wp_get_loading_attr_default( 'the_content' ) ); 3635 3632 } … … 3656 3653 3657 3654 // Use a threshold of 2. 3658 add_filter( 3659 'wp_omit_loading_attr_threshold', 3660 function() { 3661 return 2; 3662 } 3663 ); 3655 $this->force_omit_loading_attr_threshold( 2 ); 3664 3656 3665 3657 // Following the threshold of 2, the first two content media elements should not be lazy-loaded. … … 3691 3683 $this->reset_omit_loading_attr_filter(); 3692 3684 3693 // Apply filter, ensure default value of 1.3685 // Apply filter, ensure default value of 3. 3694 3686 $omit_threshold = wp_omit_loading_attr_threshold(); 3695 $this->assertSame( 1, $omit_threshold ); 3696 3697 // Add a filter that changes the value to 3. However, the filter is not applied a subsequent time in a single 3698 // page load by default, so the value is still 1. 3699 add_filter( 3700 'wp_omit_loading_attr_threshold', 3701 function() { 3702 return 3; 3703 } 3704 ); 3687 $this->assertSame( 3, $omit_threshold ); 3688 3689 // Add a filter that changes the value to 1. However, the filter is not applied a subsequent time in a single 3690 // page load by default, so the value is still 3. 3691 $this->force_omit_loading_attr_threshold( 1 ); 3692 3705 3693 $omit_threshold = wp_omit_loading_attr_threshold(); 3706 $this->assertSame( 1, $omit_threshold );3694 $this->assertSame( 3, $omit_threshold ); 3707 3695 3708 3696 // Only by enforcing a fresh check, the filter gets re-applied. 3709 3697 $omit_threshold = wp_omit_loading_attr_threshold( true ); 3710 $this->assertSame( 3, $omit_threshold );3698 $this->assertSame( 1, $omit_threshold ); 3711 3699 } 3712 3700 … … 3726 3714 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3727 3715 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 3716 $this->force_omit_loading_attr_threshold( 1 ); 3728 3717 3729 3718 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); … … 3778 3767 } 3779 3768 ); 3769 $this->force_omit_loading_attr_threshold( 1 ); 3780 3770 3781 3771 $content_img = get_image_tag( self::$large_id, '', '', '', 'large' ); … … 4013 4003 } 4014 4004 4005 /** 4006 * Change the omit loading attribute threshold value. 4007 * 4008 * @param int $threshold Threshold value to change. 4009 */ 4010 public function force_omit_loading_attr_threshold( $threshold ) { 4011 add_filter( 4012 'wp_omit_loading_attr_threshold', 4013 static function() use ( $threshold ) { 4014 return $threshold; 4015 } 4016 ); 4017 } 4015 4018 } 4016 4019
Note: See TracChangeset
for help on using the changeset viewer.