Make WordPress Core


Ignore:
Timestamp:
01/31/2023 04:54:22 PM (2 years ago)
Author:
spacedmonkey
Message:

Query: Use WP_Query in get_page_by_path.

Replace raw database queries in get_page_by_path with a call to WP_Query class. This has a number of benefits, including improved caching and priming of post caches. To maintain backwards compatibility, this function calls WP_Query to gets all matching posts of all post statuses.

Props spacedmonkey, peterwilsoncc, costdev,
Fixes #56689.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/getPageByPath.php

    r51331 r55169  
    104104    }
    105105
     106    /**
     107     * @ticket 56689
     108     *
     109     * @covers ::get_page_by_path
     110     */
     111    public function test_should_match_nested_page_query_count() {
     112        $p1 = self::factory()->post->create(
     113            array(
     114                'post_type' => 'page',
     115                'post_name' => 'foo',
     116            )
     117        );
     118
     119        $p2 = self::factory()->post->create(
     120            array(
     121                'post_type'   => 'page',
     122                'post_name'   => 'bar',
     123                'post_parent' => $p1,
     124            )
     125        );
     126
     127        $p3 = self::factory()->post->create(
     128            array(
     129                'post_type'   => 'page',
     130                'post_name'   => 'baz',
     131                'post_parent' => $p2,
     132            )
     133        );
     134
     135        $queries_before = get_num_queries();
     136        $found          = get_page_by_path( 'foo/bar/baz' );
     137        $queries_after  = get_num_queries();
     138        $cached_post    = wp_cache_get( $p1, 'posts' );
     139
     140        $this->assertSame( 1, $queries_after - $queries_before, 'Only one query should run' );
     141        $this->assertSame( $p3, $found->ID, 'Check to see if the result is correct' );
     142        $this->assertIsObject( $cached_post, 'The cached post is not an object' );
     143    }
     144
     145    /**
     146     * @ticket 56689
     147     *
     148     * @covers ::get_page_by_path
     149     */
     150    public function test_should_match_nested_page_query_count_status() {
     151        $p1 = self::factory()->post->create(
     152            array(
     153                'post_type'   => 'page',
     154                'post_name'   => 'foo',
     155                'post_status' => 'draft',
     156            )
     157        );
     158
     159        $p2 = self::factory()->post->create(
     160            array(
     161                'post_type'   => 'page',
     162                'post_name'   => 'bar',
     163                'post_parent' => $p1,
     164            )
     165        );
     166
     167        $p3 = self::factory()->post->create(
     168            array(
     169                'post_type'   => 'page',
     170                'post_name'   => 'baz',
     171                'post_parent' => $p2,
     172            )
     173        );
     174
     175        $queries_before = get_num_queries();
     176        $found          = get_page_by_path( 'foo/bar/baz' );
     177        $queries_after  = get_num_queries();
     178        $cached_post    = wp_cache_get( $p1, 'posts' );
     179
     180        $this->assertSame( 1, $queries_after - $queries_before, 'Only one query should run' );
     181        $this->assertSame( $p3, $found->ID, 'Check to see if the result is correct' );
     182        $this->assertIsObject( $cached_post, 'The cached post is not an object' );
     183    }
     184
     185    /**
     186     * @ticket 56689
     187     *
     188     * @covers ::get_page_by_path
     189     */
     190    public function test_should_return_null_for_invalid_path() {
     191        $queries_before = get_num_queries();
     192        $get_1          = get_page_by_path( 'should/return/null/for/an/invalid/path' );
     193        $get_2          = get_page_by_path( 'should/return/null/for/an/invalid/path' );
     194        $queries_after  = get_num_queries();
     195
     196        $this->assertNull( $get_1, 'Invalid path should return null.' );
     197        $this->assertSame( 1, $queries_after - $queries_before, 'Only one query should run.' );
     198        $this->assertSame( $get_1, $get_2, 'The cached result should be the same as the uncached result.' );
     199    }
     200
    106201    public function test_should_not_make_partial_match() {
    107202        $p1 = self::factory()->post->create(
Note: See TracChangeset for help on using the changeset viewer.