Changeset 55847
- Timestamp:
- 05/22/2023 07:11:36 PM (16 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r55822 r55847 108 108 */ 109 109 public $current_post = -1; 110 111 /** 112 * Whether the caller is before the loop. 113 * 114 * @since 6.3.0 115 * @var bool 116 */ 117 public $before_loop = true; 110 118 111 119 /** … … 518 526 $this->current_post = -1; 519 527 $this->in_the_loop = false; 528 $this->before_loop = true; 520 529 unset( $this->request ); 521 530 unset( $this->post ); … … 3632 3641 3633 3642 $this->in_the_loop = true; 3643 $this->before_loop = false; 3634 3644 3635 3645 if ( -1 == $this->current_post ) { // Loop has just started. … … 3672 3682 $this->rewind_posts(); 3673 3683 } elseif ( 0 === $this->post_count ) { 3684 $this->before_loop = false; 3685 3674 3686 /** 3675 3687 * Fires if no results are found in a post query. -
trunk/src/wp-includes/media.php
r55843 r55847 5491 5491 * @since 5.9.0 5492 5492 * 5493 * @global WP_Query $wp_query WordPress Query object. 5494 * 5493 5495 * @param string $context Context for the element for which the `loading` attribute value is requested. 5494 5496 * @return string|bool The default `loading` attribute value. Either 'lazy', 'eager', or a boolean `false`, to indicate … … 5496 5498 */ 5497 5499 function wp_get_loading_attr_default( $context ) { 5500 global $wp_query; 5501 5498 5502 // Skip lazy-loading for the overall block template, as it is handled more granularly. 5499 5503 if ( 'template' === $context ) { … … 5502 5506 5503 5507 // Do not lazy-load images in the header block template part, as they are likely above the fold. 5508 // For classic themes, this is handled in the condition below using the 'get_header' action. 5504 5509 $header_area = WP_TEMPLATE_PART_AREA_HEADER; 5505 5510 if ( "template_part_{$header_area}" === $context ) { … … 5507 5512 } 5508 5513 5509 /* 5510 * Skip programmatically created images within post content as they need to be handled together with the other 5511 * images within the post content. 5512 * Without this clause, they would already be counted below which skews the number and can result in the first 5513 * post content image being lazy-loaded only because there are images elsewhere in the post content. 5514 */ 5515 if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) && doing_filter( 'the_content' ) ) { 5516 return false; 5514 // Special handling for programmatically created image tags. 5515 if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) ) { 5516 /* 5517 * Skip programmatically created images within post content as they need to be handled together with the other 5518 * images within the post content. 5519 * Without this clause, they would already be counted below which skews the number and can result in the first 5520 * post content image being lazy-loaded only because there are images elsewhere in the post content. 5521 */ 5522 if ( doing_filter( 'the_content' ) ) { 5523 return false; 5524 } 5525 5526 // Conditionally skip lazy-loading on images before the loop. 5527 if ( 5528 // Only apply for main query but before the loop. 5529 $wp_query->before_loop && $wp_query->is_main_query() 5530 /* 5531 * Any image before the loop, but after the header has started should not be lazy-loaded, 5532 * except when the footer has already started which can happen when the current template 5533 * does not include any loop. 5534 */ 5535 && did_action( 'get_header' ) && ! did_action( 'get_footer' ) 5536 ) { 5537 return false; 5538 } 5517 5539 } 5518 5540 -
trunk/tests/phpunit/tests/media.php
r55825 r55847 3560 3560 */ 3561 3561 public function test_wp_get_loading_attr_default( $context ) { 3562 global $wp_query, $wp_the_query;3563 3564 3562 // Return 'lazy' by default. 3565 3563 $this->assertSame( 'lazy', wp_get_loading_attr_default( 'test' ) ); … … 3569 3567 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3570 3568 3571 $ wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ));3569 $query = $this->get_new_wp_query_for_published_post(); 3572 3570 $this->reset_content_media_count(); 3573 3571 $this->reset_omit_loading_attr_filter(); … … 3580 3578 3581 3579 // Set as main query. 3582 $ wp_the_query = $wp_query;3580 $this->set_main_query( $query ); 3583 3581 3584 3582 // For contexts other than for the main content, still return 'lazy' even in the loop … … 3614 3612 */ 3615 3613 public function test_wp_omit_loading_attr_threshold_filter() { 3616 global $wp_query, $wp_the_query; 3617 3618 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 3619 $wp_the_query = $wp_query; 3614 $query = $this->get_new_wp_query_for_published_post(); 3615 $this->set_main_query( $query ); 3620 3616 $this->reset_content_media_count(); 3621 3617 $this->reset_omit_loading_attr_filter(); … … 3641 3637 */ 3642 3638 public function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { 3643 global $wp_query, $wp_the_query;3644 3645 3639 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); 3646 3640 $iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; … … 3660 3654 $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' ); 3661 3655 3662 $ wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ));3663 $ wp_the_query = $wp_query;3656 $query = $this->get_new_wp_query_for_published_post(); 3657 $this->set_main_query( $query ); 3664 3658 $this->reset_content_media_count(); 3665 3659 $this->reset_omit_loading_attr_filter(); … … 3697 3691 $omit_threshold = wp_omit_loading_attr_threshold( true ); 3698 3692 $this->assertSame( 1, $omit_threshold ); 3693 } 3694 3695 /** 3696 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header if not main query. 3697 * 3698 * @ticket 58211 3699 * 3700 * @covers ::wp_get_loading_attr_default 3701 * 3702 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3703 * 3704 * @param string $context Context for the element for which the `loading` attribute value is requested. 3705 */ 3706 public function test_wp_get_loading_attr_default_before_loop_if_not_main_query( $context ) { 3707 global $wp_query; 3708 3709 $wp_query = $this->get_new_wp_query_for_published_post(); 3710 $this->reset_content_media_count(); 3711 $this->reset_omit_loading_attr_filter(); 3712 3713 do_action( 'get_header' ); 3714 3715 // Lazy if not main query. 3716 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3717 } 3718 3719 /** 3720 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header in main query but header was not called. 3721 * 3722 * @ticket 58211 3723 * 3724 * @covers ::wp_get_loading_attr_default 3725 * 3726 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3727 * 3728 * @param string $context Context for the element for which the `loading` attribute value is requested. 3729 */ 3730 public function test_wp_get_loading_attr_default_before_loop_in_main_query_but_header_not_called( $context ) { 3731 global $wp_query; 3732 3733 $wp_query = $this->get_new_wp_query_for_published_post(); 3734 $this->set_main_query( $wp_query ); 3735 $this->reset_content_media_count(); 3736 $this->reset_omit_loading_attr_filter(); 3737 3738 // Lazy if header not called. 3739 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3740 } 3741 3742 /** 3743 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header for main query. 3744 * 3745 * @ticket 58211 3746 * 3747 * @covers ::wp_get_loading_attr_default 3748 * 3749 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3750 * 3751 * @param string $context Context for the element for which the `loading` attribute value is requested. 3752 */ 3753 public function test_wp_get_loading_attr_default_before_loop_if_main_query( $context ) { 3754 global $wp_query; 3755 3756 $wp_query = $this->get_new_wp_query_for_published_post(); 3757 $this->set_main_query( $wp_query ); 3758 $this->reset_content_media_count(); 3759 $this->reset_omit_loading_attr_filter(); 3760 3761 do_action( 'get_header' ); 3762 $this->assertFalse( wp_get_loading_attr_default( $context ) ); 3763 } 3764 3765 /** 3766 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value after get_header and after loop. 3767 * 3768 * @ticket 58211 3769 * 3770 * @covers ::wp_get_loading_attr_default 3771 * 3772 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3773 * 3774 * @param string $context Context for the element for which the `loading` attribute value is requested. 3775 */ 3776 public function test_wp_get_loading_attr_default_after_loop( $context ) { 3777 global $wp_query; 3778 3779 $wp_query = $this->get_new_wp_query_for_published_post(); 3780 $this->set_main_query( $wp_query ); 3781 $this->reset_content_media_count(); 3782 $this->reset_omit_loading_attr_filter(); 3783 3784 do_action( 'get_header' ); 3785 3786 while ( have_posts() ) { 3787 the_post(); 3788 } 3789 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3790 } 3791 3792 /** 3793 * Tests that wp_get_loading_attr_default() returns the expected loading attribute if no loop. 3794 * 3795 * @ticket 58211 3796 * 3797 * @covers ::wp_get_loading_attr_default 3798 * 3799 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3800 * 3801 * @param string $context Context for the element for which the `loading` attribute value is requested. 3802 */ 3803 public function test_wp_get_loading_attr_default_no_loop( $context ) { 3804 global $wp_query; 3805 3806 $wp_query = $this->get_new_wp_query_for_published_post(); 3807 $this->set_main_query( $wp_query ); 3808 $this->reset_content_media_count(); 3809 $this->reset_omit_loading_attr_filter(); 3810 3811 // Ensure header and footer is called. 3812 do_action( 'get_header' ); 3813 do_action( 'get_footer' ); 3814 3815 // Load lazy if the there is no loop and footer was called. 3816 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3817 } 3818 3819 /** 3820 * Data provider. 3821 * 3822 * @return array[] 3823 */ 3824 public function data_wp_get_loading_attr_default_before_and_no_loop() { 3825 return array( 3826 array( 'wp_get_attachment_image' ), 3827 array( 'the_post_thumbnail' ), 3828 ); 3699 3829 } 3700 3830 … … 4167 4297 ); 4168 4298 } 4299 4300 /** 4301 * Returns a new WP_Query. 4302 * 4303 * @global WP_Query $wp_query WordPress Query object. 4304 * 4305 * @return WP_Query a new query. 4306 */ 4307 public function get_new_wp_query_for_published_post() { 4308 global $wp_query; 4309 4310 // New query to $wp_query. update global for the loop. 4311 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 4312 4313 return $wp_query; 4314 } 4315 4316 /** 4317 * Sets a query as main query. 4318 * 4319 * @global WP_Query $wp_the_query WordPress Query object. 4320 * 4321 * @param WP_Query $query query to be set as main query. 4322 */ 4323 public function set_main_query( $query ) { 4324 global $wp_the_query; 4325 $wp_the_query = $query; 4326 } 4169 4327 } 4170 4328 -
trunk/tests/phpunit/tests/query.php
r54891 r55847 898 898 $this->assertFalse( $q->is_tag( 'non-existent-tag' ) ); 899 899 } 900 901 /** 902 * Test if $before_loop is true before loop. 903 * 904 * @ticket 58211 905 */ 906 public function test_before_loop_value_set_true_before_the_loop() { 907 // Get a new query with 3 posts. 908 $query = $this->get_new_wp_query_with_posts( 3 ); 909 910 $this->assertTrue( $query->before_loop ); 911 } 912 913 /** 914 * Test $before_loop value is set to false when the loop starts. 915 * 916 * @ticket 58211 917 * 918 * @covers WP_Query::the_post 919 */ 920 public function test_before_loop_value_set_to_false_in_loop_with_post() { 921 // Get a new query with 2 posts. 922 $query = $this->get_new_wp_query_with_posts( 2 ); 923 924 while ( $query->have_posts() ) { 925 // $before_loop should be set false as soon as the_post is called for the first time. 926 $query->the_post(); 927 928 $this->assertFalse( $query->before_loop ); 929 break; 930 } 931 } 932 933 /** 934 * Test $before_loop value is set to false when there is no post in the loop. 935 * 936 * @ticket 58211 937 * 938 * @covers WP_Query::have_posts 939 */ 940 public function test_before_loop_set_false_after_loop_with_no_post() { 941 // New query without any posts in the result. 942 $query = new WP_Query( 943 array( 944 'category_name' => 'non-existent-category', 945 ) 946 ); 947 948 // There will not be any posts, so the loop will never actually enter. 949 while ( $query->have_posts() ) { 950 $query->the_post(); 951 } 952 953 // Still, this should be false as there are no results and entering the loop was attempted. 954 $this->assertFalse( $query->before_loop ); 955 } 956 957 /** 958 * Get a new query with a given number of posts. 959 * 960 * @param int $no_of_posts Number of posts to be added in the query. 961 */ 962 public function get_new_wp_query_with_posts( $no_of_posts ) { 963 $post_ids = self::factory()->post->create_many( $no_of_posts ); 964 $query = new WP_Query( array( 'post__in' => $post_ids ) ); 965 return $query; 966 } 900 967 }
Note: See TracChangeset
for help on using the changeset viewer.