Transients: Allow a non-expiring transient to be updated with an expiry.
props shahpranaf, sandyr. fixes #22807. git-svn-id: https://develop.svn.wordpress.org/trunk@27719 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
07eb830a9b
commit
fbb2e1dc89
@ -642,9 +642,22 @@ function set_transient( $transient, $value, $expiration = 0 ) {
|
|||||||
}
|
}
|
||||||
$result = add_option( $transient, $value, '', $autoload );
|
$result = add_option( $transient, $value, '', $autoload );
|
||||||
} else {
|
} else {
|
||||||
if ( $expiration )
|
// If expiration is requested, but the transient has no timeout option,
|
||||||
update_option( $transient_timeout, time() + $expiration );
|
// delete, then re-create transient rather than update.
|
||||||
$result = update_option( $transient, $value );
|
$update = true;
|
||||||
|
if ( $expiration ) {
|
||||||
|
if ( false === get_option( $transient_timeout ) ) {
|
||||||
|
delete_option( $transient );
|
||||||
|
add_option( $transient_timeout, time() + $expiration, '', 'no' );
|
||||||
|
$result = add_option( $transient, $value, '', 'no' );
|
||||||
|
$update = false;
|
||||||
|
} else {
|
||||||
|
update_option( $transient_timeout, time() + $expiration );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( $update ) {
|
||||||
|
$result = update_option( $transient, $value );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,4 +33,47 @@ class Tests_Option_Transient extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( $value, get_transient( $key ) );
|
$this->assertEquals( $value, get_transient( $key ) );
|
||||||
$this->assertTrue( delete_transient( $key ) );
|
$this->assertTrue( delete_transient( $key ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 22807
|
||||||
|
*/
|
||||||
|
function test_transient_data_with_timeout() {
|
||||||
|
$key = rand_str();
|
||||||
|
$value = rand_str();
|
||||||
|
$value2 = rand_str();
|
||||||
|
|
||||||
|
$this->assertFalse( get_option( '_transient_timeout_' . $key ) );
|
||||||
|
$now = time();
|
||||||
|
|
||||||
|
$this->assertTrue( set_transient( $key, $value, 100 ) );
|
||||||
|
|
||||||
|
// Ensure the transient timeout is set for 100-101 seconds in the future.
|
||||||
|
$this->assertGreaterThanOrEqual( $now + 100, get_option( '_transient_timeout_' . $key ) );
|
||||||
|
$this->assertLessThanOrEqual( $now + 101, get_option( '_transient_timeout_' . $key ) );
|
||||||
|
|
||||||
|
// Update the timeout to a second in the past and watch the transient be invalidated.
|
||||||
|
update_option( '_transient_timeout_' . $key, $now - 1 );
|
||||||
|
$this->assertFalse( get_transient( $key ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 22807
|
||||||
|
*/
|
||||||
|
function test_transient_add_timeout() {
|
||||||
|
$key = rand_str();
|
||||||
|
$value = rand_str();
|
||||||
|
$value2 = rand_str();
|
||||||
|
$this->assertTrue( set_transient( $key, $value ) );
|
||||||
|
$this->assertEquals( $value, get_transient( $key ) );
|
||||||
|
|
||||||
|
$this->assertFalse( get_option( '_transient_timeout_' . $key ) );
|
||||||
|
|
||||||
|
$now = time();
|
||||||
|
// Add timeout to existing timeout-less transient.
|
||||||
|
$this->assertTrue( set_transient( $key, $value2, 1 ) );
|
||||||
|
$this->assertGreaterThanOrEqual( $now, get_option( '_transient_timeout_' . $key ) );
|
||||||
|
|
||||||
|
update_option( '_transient_timeout_' . $key, $now - 1 );
|
||||||
|
$this->assertFalse( get_transient( $key ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user