WordPress.org

Make WordPress Core

Changeset 33110


Ignore:
Timestamp:
07/07/2015 04:44:08 PM (3 years ago)
Author:
ocean90
Message:

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

props jamesgol, ericmann.
fixes #30380.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r32943 r33110  
    623623            if ( !isset( $alloptions[$transient_option] ) ) {
    624624                $transient_timeout = '_transient_timeout_' . $transient;
    625                 if ( get_option( $transient_timeout ) < time() ) {
     625                $timeout = get_option( $transient_timeout );
     626                if ( false !== $timeout && $timeout < time() ) {
    626627                    delete_option( $transient_option  );
    627628                    delete_option( $transient_timeout );
  • trunk/tests/phpunit/tests/option/transient.php

    r28965 r33110  
    8484        $this->assertFalse( get_transient( $key ) );
    8585    }
     86
     87    /**
     88     * If get_option( $transient_timeout ) returns false, don't bother trying to delete the transient.
     89     *
     90     * @ticket 30380
     91     */
     92    function test_nonexistent_key_dont_delete_if_false() {
     93        // Create a bogus a transient
     94        $key = 'test_transient';
     95        set_transient( $key, 'test', 60 * 10 );
     96        $this->assertEquals( 'test', get_transient( $key ) );
     97
     98        // Useful variables for tracking
     99        $transient_timeout = '_transient_timeout_' . $key;
     100
     101        // Mock an action for tracking action calls
     102        $a = new MockAction();
     103
     104        // Make sure the timeout option returns false
     105        add_filter( 'option_' . $transient_timeout, '__return_false' );
     106
     107        // Add some actions to make sure options are _not_ deleted
     108        add_action( 'delete_option', array( $a, 'action' ) );
     109
     110        // Act
     111        get_transient( $key );
     112
     113        // Make sure delete option was not called for both the transient and the timeout
     114        $this->assertEquals( 0, $a->get_call_count() );
     115    }
     116
     117    /**
     118     * @ticket 30380
     119     */
     120    function test_nonexistent_key_old_timeout() {
     121        // Create a transient
     122        $key = 'test_transient';
     123        set_transient( $key, 'test', 60 * 10 );
     124        $this->assertEquals( 'test', get_transient( $key ) );
     125
     126        // Make sure the timeout option returns false
     127        $timeout = '_transient_timeout_' . $key;
     128        $transient_option = '_transient_' . $key;
     129        add_filter( 'option_' . $timeout, '__return_zero' );
     130
     131        // Mock an action for tracking action calls
     132        $a = new MockAction();
     133
     134        // Add some actions to make sure options are deleted
     135        add_action( 'delete_option', array( $a, 'action' ) );
     136
     137        // Act
     138        get_transient( $key );
     139
     140        // Make sure delete option was called for both the transient and the timeout
     141        $this->assertEquals( 2, $a->get_call_count() );
     142
     143        $expected = array(
     144            array(
     145                'action' => 'action',
     146                'tag'    => 'delete_option',
     147                'args'   => array( $transient_option ),
     148            ),
     149            array(
     150                'action' => 'action',
     151                'tag'    => 'delete_option',
     152                'args'   => array( $timeout ),
     153            ),
     154        );
     155        $this->assertEquals( $expected, $a->get_events() );
     156    }
    86157}
Note: See TracChangeset for help on using the changeset viewer.