Posts, Post Types: Use WP_Query internally in get_pages.

Convert `get_pages` to use `WP_Query` internally. Using WP_Query means that a lot of code has been removed however existing parameters supported by get_pages are transformed in to query arguments. The custom caching solution found in the old version of this function is replaced with the caching found in WP_Query (added in [53941]). This change adds consistency to the codebase, as improvements and changes to `WP_Query` will filter down to the `get_pages` function. 

Props mikeschinkel, spacedmonkey, nacin, scribu, filosofo, jane, garyc40, markoheijnen, grandslambert, kevinB, wlindley, dbernar1, atimmer, mdawaffe, helen, benjibee, johnbillion, peterwilsoncc, costdev, flixos90, joemcgill.
Fixes #12821.

git-svn-id: https://develop.svn.wordpress.org/trunk@55569 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-03-21 12:47:20 +00:00
parent 308c9104fc
commit eb6bf15bc7
2 changed files with 340 additions and 164 deletions

View File

@@ -66,7 +66,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
// Force last_changed to increment.
clean_post_cache( $pages[0]->ID );
$this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
get_post( $pages[0]->ID );
$num_queries = $wpdb->num_queries;
// last_changed bumped so num_queries should increment.
@@ -172,6 +172,60 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
$this->assertSameSets( array( $posts[0] ), $found_ids );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_include_ignore_meta_key() {
$posts = self::factory()->post->create_many(
2,
array(
'post_type' => 'page',
)
);
$pages = get_pages(
array(
'include' => $posts,
'meta_key' => 'foo',
'meta_value' => 'bar',
)
);
$page_ids = wp_list_pluck( $pages, 'ID' );
$this->assertSameSets( $posts, $page_ids );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_include_ignore_exclude() {
$includes = self::factory()->post->create_many(
2,
array(
'post_type' => 'page',
)
);
$excludes = self::factory()->post->create_many(
2,
array(
'post_type' => 'page',
)
);
$pages = get_pages(
array(
'include' => $includes,
'exclude' => $excludes,
)
);
$page_ids = wp_list_pluck( $pages, 'ID' );
$this->assertSameSets( $includes, $page_ids );
}
/**
* @ticket 40669
*/
@@ -719,4 +773,253 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_post_type() {
register_post_type( 'wptests_pt', array( 'hierarchical' => true ) );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
$pages = get_pages(
array(
'post_type' => 'wptests_pt',
)
);
$this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_author() {
$author_1 = self::factory()->user->create(
array(
'user_login' => 'author1',
'role' => 'author',
)
);
$posts = self::factory()->post->create_many(
2,
array(
'post_type' => 'page',
'post_author' => $author_1,
)
);
$pages = get_pages(
array(
'authors' => $author_1,
)
);
$this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_post_status() {
register_post_status(
'foo',
array(
'public' => true,
)
);
$posts = self::factory()->post->create_many(
2,
array(
'post_type' => 'page',
'post_status' => 'foo',
)
);
$pages = get_pages(
array(
'post_status' => 'foo',
)
);
$this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_offset() {
$posts = self::factory()->post->create_many( 4, array( 'post_type' => 'page' ) );
$pages = get_pages(
array(
'offset' => 2,
'number' => 2,
)
);
$this->assertSameSets( array( $posts[2], $posts[3] ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_authors() {
$author_1 = self::factory()->user->create(
array(
'user_login' => 'author1',
'role' => 'author',
)
);
$post_1 = self::factory()->post->create(
array(
'post_title' => 'Page 1',
'post_type' => 'page',
'post_author' => $author_1,
'post_date' => '2007-01-01 00:00:00',
)
);
$author_2 = self::factory()->user->create(
array(
'user_login' => 'author2',
'role' => 'author',
)
);
$post_2 = self::factory()->post->create(
array(
'post_title' => 'Page 2',
'post_type' => 'page',
'post_author' => $author_2,
'post_date' => '2007-01-01 00:00:00',
)
);
$pages = get_pages(
array(
'authors' => "{$author_1}, {$author_2}",
)
);
$this->assertSameSets( array( $post_1, $post_2 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_get_pages_authors_names() {
$author_1 = self::factory()->user->create(
array(
'user_login' => 'author1',
'role' => 'author',
)
);
$post_1 = self::factory()->post->create(
array(
'post_title' => 'Page 1',
'post_type' => 'page',
'post_author' => $author_1,
'post_date' => '2007-01-01 00:00:00',
)
);
$author_2 = self::factory()->user->create(
array(
'user_login' => 'author2',
'role' => 'author',
)
);
$post_2 = self::factory()->post->create(
array(
'post_title' => 'Page 2',
'post_type' => 'page',
'post_author' => $author_2,
'post_date' => '2007-01-01 00:00:00',
)
);
$pages = get_pages(
array(
'authors' => 'author1, author2',
)
);
$this->assertSameSets( array( $post_1, $post_2 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_orderby() {
global $wpdb;
// 'rand' is a valid value.
get_pages( array( 'sort_column' => 'rand' ) );
$this->assertStringContainsString( 'ORDER BY RAND()', $wpdb->last_query, 'Check order is random' );
// This isn't allowed.
get_pages( array( 'sort_order' => 'rand' ) );
$this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present' );
$this->assertStringNotContainsString( 'RAND()', $wpdb->last_query, 'Check order is random is not present' );
$this->assertStringContainsString( 'DESC', $wpdb->last_query, 'Check DESC is random is not present' );
// 'none' is a valid value.
get_pages( array( 'sort_column' => 'none' ) );
$this->assertStringNotContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is not present' );
$this->assertStringNotContainsString( 'DESC', $wpdb->last_query, 'Check DESC is not present' );
$this->assertStringNotContainsString( 'ASC', $wpdb->last_query, 'Check ASC is not present' );
// False is a valid value.
get_pages( array( 'sort_column' => false ) );
$this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present if sort_column equal false is passed.' );
// Empty array() is a valid value.
get_pages( array( 'sort_column' => array() ) );
$this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present if sort_column equals an empty array is passed.' );
}
/**
* @ticket 12821
* @covers ::get_pages
*/
public function test_order() {
global $wpdb;
get_pages(
array(
'sort_column' => 'post_type',
)
);
$this->assertStringContainsString(
"ORDER BY $wpdb->posts.post_type ASC",
$wpdb->last_query,
'Check order is post type'
);
get_pages(
array(
'sort_column' => 'title',
'sort_order' => 'foo',
)
);
$this->assertStringContainsString(
"ORDER BY $wpdb->posts.post_title DESC",
$wpdb->last_query,
'Check order is default'
);
get_pages(
array(
'sort_order' => 'asc',
'sort_column' => 'date',
)
);
$this->assertStringContainsString(
"ORDER BY $wpdb->posts.post_date ASC",
$wpdb->last_query,
'Check order is post date'
);
}
}