Make WordPress Core

Ticket #15058: 15058.2.diff

File 15058.2.diff, 3.6 KB (added by MikeHansenMe, 10 years ago)

open to suggestions on the function name

  • tests/phpunit/tests/option/transient.php

     
    8383                update_option( '_transient_timeout_' . $key, $now - 1 );
    8484                $this->assertFalse( get_transient( $key ) );
    8585        }
     86
     87        /**
     88         * ticket 15058
     89         */
     90        function test_transient_long() {
     91                $long_name = "thisisaverylongnamethatwillnotfitincolumnbecauseitistoolongforthecolumn";
     92                set_transient( $long_name, true, 60 );
     93                $this->assertTrue( get_transient( $long_name ) );
     94        }
    8695}
  • tests/phpunit/tests/option/option.php

     
    9999        function test_special_option_name_notoptions() {
    100100                delete_option( 'notoptions' );
    101101        }
     102
     103        function test_option_add_long() {
     104                $long_name = "thisisaverylongnamethatwillnotfitincolumnbecauseitistoolongforthecolumn";
     105                add_option( $long_name, true );
     106                $this->assertTrue( get_option( $long_name ) );
     107        }
     108
     109        function test_option_update_long() {
     110                $long_name = "thisisaverylongnamethatwillnotfitincolumnbecauseitistoolongforthecolumn";
     111                add_option( $long_name, true );
     112                update_option( $long_name, 5 );
     113                $this->assertEquals( 5, get_option( $long_name ) );
     114        }
    102115}
  • src/wp-includes/option.php

     
    2929        if ( empty( $option ) )
    3030                return false;
    3131
     32        $option = long_option_name( $option );
     33
    3234        /**
    3335         * Filter the value of an existing option before it is retrieved.
    3436         *
     
    212214}
    213215
    214216/**
     217 * This function allows long option names to work.
     218 *
     219 * @since 4.1.0
     220 *
     221 * @param string $option     Option name.
     222 * @param int    $max_length Max length of the option name.
     223 * @return string $option Modified option name if longer than $max_length.
     224 */
     225function long_option_name( $option, $max_length = 64 ) {
     226        if( strlen( $option ) > 64 ) {
     227                $option = md5( $option );
     228                $option = "md5_" . $option;
     229        } else {
     230                return $option;
     231        }
     232}
     233
     234/**
    215235 * Update the value of an option that was already added.
    216236 *
    217237 * You do not need to serialize values. If the value needs to be serialized, then
     
    240260        if ( is_object( $value ) )
    241261                $value = clone $value;
    242262
     263        $option = long_option_name( $option );
     264
    243265        $value = sanitize_option( $option, $value );
    244266        $old_value = get_option( $option );
    245267
     
    377399        $serialized_value = maybe_serialize( $value );
    378400        $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
    379401
     402        $option = long_option_name( $option );
     403
    380404        /**
    381405         * Fires before an option is added.
    382406         *
     
    582606        if ( wp_using_ext_object_cache() ) {
    583607                $value = wp_cache_get( $transient, 'transient' );
    584608        } else {
     609
     610                $transient = long_option_name( $transient, 40 );
     611
    585612                $transient_option = '_transient_' . $transient;
    586613                if ( ! defined( 'WP_INSTALLING' ) ) {
    587614                        // If option is not in alloptions, it is not autoloaded and thus has a timeout
     
    645672        if ( wp_using_ext_object_cache() ) {
    646673                $result = wp_cache_set( $transient, $value, 'transient', $expiration );
    647674        } else {
     675
     676                $transient = long_option_name( $transient, 40 );
     677
    648678                $transient_timeout = '_transient_timeout_' . $transient;
    649679                $transient = '_transient_' . $transient;
     680
    650681                if ( false === get_option( $transient ) ) {
    651682                        $autoload = 'yes';
    652683                        if ( $expiration ) {