Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default post type in class-wp-query.php #6393

Closed
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/wp-includes/class-wp-query.php
Expand Up @@ -2542,6 +2542,8 @@ public function get_posts() {
$post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
}
} elseif ( ! empty( $post_type ) && is_array( $post_type ) ) {
// Sort post types to ensure same cache key generation.
sort( $post_type );
jonnynews marked this conversation as resolved.
Show resolved Hide resolved
$post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
} elseif ( ! empty( $post_type ) ) {
$post_type_where = $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
Expand Down Expand Up @@ -4856,6 +4858,26 @@ protected function generate_cache_key( array $args, $sql ) {
$args['suppress_filters']
);

if ( empty( $args['post_type'] ) ) {
if ( $this->is_attachment ) {
$args['post_type'] = 'attachment';
} elseif ( $this->is_page ) {
$args['post_type'] = 'page';
} else {
$args['post_type'] = 'post';
}
} elseif ( 'any' === $args['post_type'] ) {
$args['post_type'] = array_values( get_post_types( array( 'exclude_from_search' => false ) ) );
}
$args['post_type'] = (array) $args['post_type'];
jonnynews marked this conversation as resolved.
Show resolved Hide resolved
// Sort post types to ensure same cache key generation.
sort( $args['post_type'] );

// Add a default orderby value of date to ensure same cache key generation.
if ( ! isset( $q['orderby'] ) ) {
$args['orderby'] = 'date';
}
jonnynews marked this conversation as resolved.
Show resolved Hide resolved

$placeholder = $wpdb->placeholder_escape();
array_walk_recursive(
$args,
Expand All @@ -4874,6 +4896,8 @@ static function ( &$value ) use ( $wpdb, $placeholder ) {
}
);

ksort( $args );

// Replace wpdb placeholder in the SQL statement used by the cache key.
$sql = $wpdb->remove_placeholder_escape( $sql );
$key = md5( serialize( $args ) . $sql );
Expand Down
124 changes: 124 additions & 0 deletions tests/phpunit/tests/query/cacheResults.php
Expand Up @@ -171,6 +171,58 @@ public function test_generate_cache_key_placeholder() {
$this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
}

/**
* @covers WP_Query::generate_cache_key
* @ticket 59442
*/
public function test_generate_cache_key_unregister_post_type() {
global $wpdb;
register_post_type(
'wptests_pt',
array(
'exclude_from_search' => false,
)
);
$query_vars = array(
'post_type' => 'any',
);
$fields = "{$wpdb->posts}.ID";
$query1 = new WP_Query( $query_vars );
$request1 = str_replace( $fields, "{$wpdb->posts}.*", $query1->request );

$reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
$reflection->setAccessible( true );

$cache_key_1 = $reflection->invoke( $query1, $query_vars, $request1 );
unregister_post_type( 'wptests_pt' );
$cache_key_2 = $reflection->invoke( $query1, $query_vars, $request1 );

$this->assertNotSame( $cache_key_1, $cache_key_2, 'Cache key should differs when after unregister post type.' );
jonnynews marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @ticket 59442
*
* @covers WP_Query::generate_cache_key
*
* @dataProvider data_query_cache_duplicate
*/
public function test_generate_cache_key_normalize( $query_vars1, $query_vars2 ) {
jonnynews marked this conversation as resolved.
Show resolved Hide resolved
global $wpdb;

$fields = "{$wpdb->posts}.ID";
$query1 = new WP_Query( $query_vars1 );
$request1 = str_replace( $fields, "{$wpdb->posts}.*", $query1->request );

$reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
$reflection->setAccessible( true );

$cache_key_1 = $reflection->invoke( $query1, $query_vars1, $request1 );
$cache_key_2 = $reflection->invoke( $query1, $query_vars2, $request1 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not yet defined but shouldn't this be using the request value from the second query.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do this in a early versions of the patch. It just made the tests harder to follow. I think this is fine. We can improve the tests later.


$this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
}

/**
* @dataProvider data_query_cache
* @ticket 22176
Expand Down Expand Up @@ -216,6 +268,66 @@ public function test_query_cache( $args ) {
}
}

/**
* Data provider for test_generate_cache_key_normalize().
*
* @return array[]
*/
public function data_query_cache_duplicate() {
jonnynews marked this conversation as resolved.
Show resolved Hide resolved
return array(
'post type empty' => array(
'query_vars1' => array( 'post_type' => '' ),
'query_vars2' => array( 'post_type' => 'post' ),
),
'post type array' => array(
'query_vars1' => array( 'post_type' => array( 'page' ) ),
'query_vars2' => array( 'post_type' => 'page' ),
),
'orderby empty' => array(
'query_vars1' => array( 'orderby' => null ),
'query_vars2' => array( 'orderby' => 'date' ),
),
'different order parameter' => array(
'query_vars1' => array(
'post_type' => 'post',
'posts_per_page' => 15,
),
'query_vars2' => array(
'posts_per_page' => 15,
'post_type' => 'post',
),
),
'same args' => array(
'query_vars1' => array( 'post_type' => 'post' ),
'query_vars2' => array( 'post_type' => 'post' ),
),
'same args any' => array(
'query_vars1' => array( 'post_type' => 'any' ),
'query_vars2' => array( 'post_type' => 'any' ),
),
'any and post types' => array(
'query_vars1' => array( 'post_type' => 'any' ),
'query_vars2' => array( 'post_type' => array( 'post', 'page', 'attachment' ) ),
),
'different order post type' => array(
'query_vars1' => array( 'post_type' => array( 'post', 'page' ) ),
'query_vars2' => array( 'post_type' => array( 'page', 'post' ) ),
),
'cache parameters' => array(
'query_vars1' => array(
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'update_menu_item_cache' => true,
),
'query_vars2' => array(
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'update_menu_item_cache' => false,
),
),
);
}

/**
* Data provider.
*
Expand Down Expand Up @@ -263,6 +375,18 @@ public function data_query_cache() {
'post_type' => 'page',
),
),
'cache true and empty post type' => array(
'args' => array(
'cache_results' => true,
'post_type' => '',
),
),
'cache true and orderby null' => array(
'args' => array(
'cache_results' => true,
'orderby' => null,
),
),
'cache true and ids' => array(
'args' => array(
'cache_results' => true,
Expand Down