Transients: If `get_option( $transient_timeout )` returns false, don't bother trying to delete the transient in `get_transient()`.

props jamesgol, ericmann.
fixes #30380.

git-svn-id: https://develop.svn.wordpress.org/trunk@33110 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2015-07-07 16:44:08 +00:00
parent 68b5d643d0
commit daa477e3b8
2 changed files with 73 additions and 1 deletions

View File

@ -622,7 +622,8 @@ function get_transient( $transient ) {
$alloptions = wp_load_alloptions(); $alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) { if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' . $transient; $transient_timeout = '_transient_timeout_' . $transient;
if ( get_option( $transient_timeout ) < time() ) { $timeout = get_option( $transient_timeout );
if ( false !== $timeout && $timeout < time() ) {
delete_option( $transient_option ); delete_option( $transient_option );
delete_option( $transient_timeout ); delete_option( $transient_timeout );
$value = false; $value = false;

View File

@ -83,4 +83,75 @@ class Tests_Option_Transient extends WP_UnitTestCase {
update_option( '_transient_timeout_' . $key, $now - 1 ); update_option( '_transient_timeout_' . $key, $now - 1 );
$this->assertFalse( get_transient( $key ) ); $this->assertFalse( get_transient( $key ) );
} }
/**
* If get_option( $transient_timeout ) returns false, don't bother trying to delete the transient.
*
* @ticket 30380
*/
function test_nonexistent_key_dont_delete_if_false() {
// Create a bogus a transient
$key = 'test_transient';
set_transient( $key, 'test', 60 * 10 );
$this->assertEquals( 'test', get_transient( $key ) );
// Useful variables for tracking
$transient_timeout = '_transient_timeout_' . $key;
// Mock an action for tracking action calls
$a = new MockAction();
// Make sure the timeout option returns false
add_filter( 'option_' . $transient_timeout, '__return_false' );
// Add some actions to make sure options are _not_ deleted
add_action( 'delete_option', array( $a, 'action' ) );
// Act
get_transient( $key );
// Make sure delete option was not called for both the transient and the timeout
$this->assertEquals( 0, $a->get_call_count() );
}
/**
* @ticket 30380
*/
function test_nonexistent_key_old_timeout() {
// Create a transient
$key = 'test_transient';
set_transient( $key, 'test', 60 * 10 );
$this->assertEquals( 'test', get_transient( $key ) );
// Make sure the timeout option returns false
$timeout = '_transient_timeout_' . $key;
$transient_option = '_transient_' . $key;
add_filter( 'option_' . $timeout, '__return_zero' );
// Mock an action for tracking action calls
$a = new MockAction();
// Add some actions to make sure options are deleted
add_action( 'delete_option', array( $a, 'action' ) );
// Act
get_transient( $key );
// Make sure delete option was called for both the transient and the timeout
$this->assertEquals( 2, $a->get_call_count() );
$expected = array(
array(
'action' => 'action',
'tag' => 'delete_option',
'args' => array( $transient_option ),
),
array(
'action' => 'action',
'tag' => 'delete_option',
'args' => array( $timeout ),
),
);
$this->assertEquals( $expected, $a->get_events() );
}
} }