mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 21:08:44 +01:00
Posts, Post Types: Add caching to _find_post_by_old_slug
and _find_post_by_old_date
functions.
Cache result of database queries in `_find_post_by_old_slug` and `_find_post_by_old_date` functions. This means that repeated requests to a url where the page is not found, will result in hitting a cache for sites running persistent object caching. Props Spacedmonkey, dd32, mukesh27, pbearne, flixos90. Fixes #36723. git-svn-id: https://develop.svn.wordpress.org/trunk@53549 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d91e148452
commit
dfcfe0b21d
@ -1150,7 +1150,16 @@ function _find_post_by_old_slug( $post_type ) {
|
||||
$query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
|
||||
}
|
||||
|
||||
$id = (int) $wpdb->get_var( $query );
|
||||
$key = md5( $query );
|
||||
$last_changed = wp_cache_get_last_changed( 'posts' );
|
||||
$cache_key = "find_post_by_old_slug:$key:$last_changed";
|
||||
$cache = wp_cache_get( $cache_key, 'posts' );
|
||||
if ( false !== $cache ) {
|
||||
$id = $cache;
|
||||
} else {
|
||||
$id = (int) $wpdb->get_var( $query );
|
||||
wp_cache_set( $cache_key, $id, 'posts' );
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
@ -1183,11 +1192,20 @@ function _find_post_by_old_date( $post_type ) {
|
||||
|
||||
$id = 0;
|
||||
if ( $date_query ) {
|
||||
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
|
||||
|
||||
if ( ! $id ) {
|
||||
// Check to see if an old slug matches the old date.
|
||||
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
|
||||
$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) );
|
||||
$key = md5( $query );
|
||||
$last_changed = wp_cache_get_last_changed( 'posts' );
|
||||
$cache_key = "find_post_by_old_date:$key:$last_changed";
|
||||
$cache = wp_cache_get( $cache_key, 'posts' );
|
||||
if ( false !== $cache ) {
|
||||
$id = $cache;
|
||||
} else {
|
||||
$id = (int) $wpdb->get_var( $query );
|
||||
if ( ! $id ) {
|
||||
// Check to see if an old slug matches the old date.
|
||||
$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
|
||||
}
|
||||
wp_cache_set( $cache_key, $id, 'posts' );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
/**
|
||||
* @group rewrite
|
||||
* @covers wp_old_slug_redirect
|
||||
*/
|
||||
class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
|
||||
protected $old_date_redirect_url;
|
||||
@ -86,6 +87,73 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
|
||||
$this->assertSame( $permalink, $this->old_date_redirect_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36723
|
||||
*/
|
||||
public function test_old_date_slug_redirect_cache() {
|
||||
$old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
|
||||
|
||||
$time = '2004-01-03 00:00:00';
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => self::$post_id,
|
||||
'post_date' => $time,
|
||||
'post_date_gmt' => get_gmt_from_date( $time ),
|
||||
'post_name' => 'bar-baz',
|
||||
)
|
||||
);
|
||||
|
||||
$permalink = user_trailingslashit( get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $old_permalink );
|
||||
wp_old_slug_redirect();
|
||||
$num_queries = get_num_queries();
|
||||
$this->assertSame( $permalink, $this->old_date_redirect_url );
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_date_redirect_url );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36723
|
||||
*/
|
||||
public function test_old_date_redirect_cache_invalidation() {
|
||||
global $wpdb;
|
||||
$old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
|
||||
|
||||
$time = '2004-01-03 00:00:00';
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => self::$post_id,
|
||||
'post_date' => $time,
|
||||
'post_date_gmt' => get_gmt_from_date( $time ),
|
||||
'post_name' => 'bar-baz',
|
||||
)
|
||||
);
|
||||
|
||||
$permalink = user_trailingslashit( get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $old_permalink );
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_date_redirect_url );
|
||||
|
||||
$time = '2014-02-01 00:00:00';
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $this->post_id,
|
||||
'post_date' => $time,
|
||||
'post_date_gmt' => get_gmt_from_date( $time ),
|
||||
'post_name' => 'bar-baz',
|
||||
)
|
||||
);
|
||||
|
||||
$num_queries = get_num_queries();
|
||||
$this->go_to( $permalink );
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_date_redirect_url );
|
||||
$this->assertGreaterThan( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
public function test_old_date_redirect_attachment() {
|
||||
$old_permalink = get_attachment_link( self::$attachment_id );
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
/**
|
||||
* @group rewrite
|
||||
* @ticket 33920
|
||||
* @covers wp_old_slug_redirect
|
||||
*/
|
||||
class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
|
||||
protected $old_slug_redirect_url;
|
||||
@ -52,6 +53,63 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
|
||||
$this->assertSame( $permalink, $this->old_slug_redirect_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36723
|
||||
*/
|
||||
public function test_old_slug_redirect_cache() {
|
||||
$old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
|
||||
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $this->post_id,
|
||||
'post_name' => 'bar-baz',
|
||||
)
|
||||
);
|
||||
|
||||
$permalink = user_trailingslashit( get_permalink( $this->post_id ) );
|
||||
|
||||
$this->go_to( $old_permalink );
|
||||
|
||||
wp_old_slug_redirect();
|
||||
$num_queries = get_num_queries();
|
||||
$this->assertSame( $permalink, $this->old_slug_redirect_url );
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_slug_redirect_url );
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36723
|
||||
*/
|
||||
public function test_old_slug_redirect_cache_invalidation() {
|
||||
$old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
|
||||
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $this->post_id,
|
||||
'post_name' => 'bar-baz',
|
||||
)
|
||||
);
|
||||
|
||||
$permalink = user_trailingslashit( get_permalink( $this->post_id ) );
|
||||
|
||||
$this->go_to( $old_permalink );
|
||||
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_slug_redirect_url );
|
||||
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $this->post_id,
|
||||
'post_name' => 'foo-bar',
|
||||
)
|
||||
);
|
||||
$num_queries = get_num_queries();
|
||||
wp_old_slug_redirect();
|
||||
$this->assertSame( $permalink, $this->old_slug_redirect_url );
|
||||
$this->assertSame( $num_queries + 1, get_num_queries() );
|
||||
}
|
||||
|
||||
public function test_old_slug_redirect_attachment() {
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
|
Loading…
x
Reference in New Issue
Block a user