Improve the include / exclude SQL generation in `get_pages()` by using `IN` and `NOT IN` where applicable. Adds unit tests for include / exclude.

Fixes #22074.



git-svn-id: https://develop.svn.wordpress.org/trunk@25168 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-08-29 18:48:29 +00:00
parent dd78079286
commit f458d16c01
2 changed files with 44 additions and 35 deletions

View File

@ -3671,7 +3671,7 @@ function get_pages($args = '') {
$cache = array();
$inclusions = '';
if ( !empty($include) ) {
if ( ! empty( $include ) ) {
$child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
$parent = -1;
$exclude = '';
@ -3679,32 +3679,16 @@ function get_pages($args = '') {
$meta_value = '';
$hierarchical = false;
$incpages = wp_parse_id_list( $include );
if ( ! empty( $incpages ) ) {
foreach ( $incpages as $incpage ) {
if (empty($inclusions))
$inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
else
$inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
}
}
if ( ! empty( $incpages ) )
$inclusions = ' AND ID IN (' . implode( ',', array_map( 'intval', $incpages ) ) . ')';
}
if (!empty($inclusions))
$inclusions .= ')';
$exclusions = '';
if ( !empty($exclude) ) {
if ( ! empty( $exclude ) ) {
$expages = wp_parse_id_list( $exclude );
if ( ! empty( $expages ) ) {
foreach ( $expages as $expage ) {
if (empty($exclusions))
$exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
else
$exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
}
}
if ( ! empty( $expages ) )
$exclusions = ' AND ID NOT IN (' . implode( ',', array_map( 'intval', $expages ) ) . ')';
}
if (!empty($exclusions))
$exclusions .= ')';
$author_query = '';
if (!empty($authors)) {

View File

@ -19,19 +19,19 @@ class Tests_Post extends WP_UnitTestCase {
wp_set_current_user( $this->old_current_user );
parent::tearDown();
}
// helper function: return the timestamp(s) of cron jobs for the specified hook and post
function _next_schedule_for_post($hook, $id) {
return wp_next_scheduled('publish_future_post', array(0=>intval($id)));
}
// helper function, unsets current user globally
function _unset_current_user() {
global $current_user, $user_ID;
$current_user = $user_ID = null;
}
// test simple valid behavior: insert and get a post
function test_vb_insert_get_delete() {
register_post_type( 'cpt', array( 'taxonomies' => array( 'post_tag', 'ctax' ) ) );
@ -707,9 +707,9 @@ class Tests_Post extends WP_UnitTestCase {
*/
function test_insert_programmatic_sanitized() {
$this->_unset_current_user();
register_taxonomy( 'test_tax', 'post' );
$title = rand_str();
$post_data = array(
'post_author' => $this->author_id,
@ -722,7 +722,7 @@ class Tests_Post extends WP_UnitTestCase {
);
$insert_post_id = wp_insert_post( $post_data, true, true );
$this->assertTrue( ( is_int($insert_post_id) && $insert_post_id > 0 ) );
$post = get_post( $insert_post_id );
$this->assertEquals( $post->post_author, $this->author_id );
$this->assertEquals( $post->post_title, $title );
@ -733,9 +733,9 @@ class Tests_Post extends WP_UnitTestCase {
*/
function test_insert_programmatic_without_current_user_success() {
$this->_unset_current_user();
register_taxonomy( 'test_tax', 'post' );
$title = rand_str();
$post_data = array(
'post_author' => $this->author_id,
@ -748,11 +748,11 @@ class Tests_Post extends WP_UnitTestCase {
);
// with sanitize set to false
$insert_post_id = wp_insert_post( $post_data, true, false );
$post = get_post( $insert_post_id );
$this->assertEquals( $post->post_author, $this->author_id );
$this->assertEquals( $post->post_title, $title );
$terms = wp_get_object_terms( $insert_post_id, 'test_tax' );
$this->assertTrue( ( is_array( $terms ) && count( $terms ) == 3 ) );
}
@ -762,9 +762,9 @@ class Tests_Post extends WP_UnitTestCase {
*/
function test_insert_programmatic_without_current_user_fail() {
$this->_unset_current_user();
register_taxonomy( 'test_tax', 'post' );
$title = rand_str();
$post_data = array(
// post_author not set
@ -798,4 +798,29 @@ class Tests_Post extends WP_UnitTestCase {
_unregister_post_type( $post_type );
$this->assertEquals( new stdClass, wp_count_posts( $post_type, 'readable' ) );
}
/**
* @ticket 22074
*/
function test_get_pages_include_exclude() {
$page_ids = array();
foreach ( range( 1, 20 ) as $i )
$page_ids[] = $this->factory->post->create( array( 'post_type' => 'page' ) );
$inc = array_slice( $page_ids, 0, 10 );
sort( $inc );
$exc = array_slice( $page_ids, 10 );
sort( $exc );
$include = get_pages( array( 'include' => $inc ) );
$inc_result = wp_list_pluck( $include, 'ID' );
sort( $inc_result );
$this->assertEquals( $inc, $inc_result );
$exclude = get_pages( array( 'exclude' => $exc ) );
$exc_result = wp_list_pluck( $exclude, 'ID' );
sort( $exc_result );
$this->assertEquals( $inc, $exc_result );
}
}