Make WordPress Core

Ticket #29095: 29095.patch

File 29095.patch, 12.2 KB (added by azaozz, 12 years ago)
  • src/wp-includes/js/utils.js

     
    33// utility functions
    44
    55var wpCookies = {
    6 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
     6// The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
    77
    8         each : function(obj, cb, scope) {
     8        each: function( obj, cb, scope ) {
    99                var n, l;
    1010
    11                 if ( !obj )
     11                if ( ! obj )
    1212                        return 0;
    1313
    1414                scope = scope || obj;
    1515
    16                 if ( typeof(obj.length) != 'undefined' ) {
     16                if ( typeof( obj.length ) !== 'undefined' ) {
    1717                        for ( n = 0, l = obj.length; n < l; n++ ) {
    18                                 if ( cb.call(scope, obj[n], n, obj) === false )
     18                                if ( cb.call( scope, obj[n], n, obj ) === false ) {
    1919                                        return 0;
     20                                }
    2021                        }
    2122                } else {
    2223                        for ( n in obj ) {
    2324                                if ( obj.hasOwnProperty(n) ) {
    24                                         if ( cb.call(scope, obj[n], n, obj) === false ) {
     25                                        if ( cb.call( scope, obj[n], n, obj ) === false ) {
    2526                                                return 0;
    2627                                        }
    2728                                }
     
    3435         * Get a multi-values cookie.
    3536         * Returns a JS object with the name: 'value' pairs.
    3637         */
    37         getHash : function(name) {
    38                 var all = this.get(name), ret;
     38        getHash: function( name ) {
     39                var cookie = this.get( name ), values;
    3940
    40                 if ( all ) {
    41                         this.each( all.split('&'), function(pair) {
     41                if ( cookie ) {
     42                        this.each( cookie.split('&'), function( pair ) {
    4243                                pair = pair.split('=');
    43                                 ret = ret || {};
    44                                 ret[pair[0]] = pair[1];
     44                                values = values || {};
     45                                values[pair[0]] = pair[1];
    4546                        });
    4647                }
    47                 return ret;
     48
     49                return values;
    4850        },
    4951
    5052        /**
     
    5254         *
    5355         * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
    5456         */
    55         setHash : function(name, values_obj, expires, path, domain, secure) {
     57        setHash: function( name, values_obj, expires, path, domain, secure ) {
    5658                var str = '';
    5759
    58                 this.each(values_obj, function(val, key) {
    59                         str += (!str ? '' : '&') + key + '=' + val;
     60                this.each( values_obj, function( val, key ) {
     61                        str += ( ! str ? '' : '&' ) + key + '=' + val;
    6062                });
    6163
    62                 this.set(name, str, expires, path, domain, secure);
     64                this.set( name, str, expires, path, domain, secure );
    6365        },
    6466
    6567        /**
    6668         * Get a cookie.
    6769         */
    68         get : function(name) {
     70        get: function( name ) {
    6971                var e, b,
    7072                        cookie = document.cookie,
    7173                        p = name + '=';
     
    9092                if ( e == -1 )
    9193                        e = cookie.length;
    9294
    93                 return decodeURIComponent( cookie.substring(b + p.length, e) );
     95                return decodeURIComponent( cookie.substring( b + p.length, e ) );
    9496        },
    9597
    9698        /**
     
    99101         * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
    100102         * or the number of seconds until expiration
    101103         */
    102         set : function(name, value, expires, path, domain, secure) {
     104        set: function( name, value, expires, path, domain, secure ) {
    103105                var d = new Date();
    104106
    105                 if ( typeof(expires) == 'object' && expires.toGMTString ) {
     107                if ( typeof( expires ) == 'object' && expires.toGMTString ) {
    106108                        expires = expires.toGMTString();
    107                 } else if ( parseInt(expires, 10) ) {
    108                         d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
     109                } else if ( parseInt( expires, 10 ) ) {
     110                        d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds
    109111                        expires = d.toGMTString();
    110112                } else {
    111113                        expires = '';
     
    123125         *
    124126         * This is done by setting it to an empty value and setting the expiration time in the past.
    125127         */
    126         remove : function(name, path) {
    127                 this.set(name, '', -1000, path);
     128        remove: function( name, path ) {
     129                this.set( name, '', -1000, path );
    128130        }
    129131};
    130132
    131133// Returns the value as string. Second arg or empty string is returned when value is not set.
    132134function getUserSetting( name, def ) {
    133         var obj = getAllUserSettings();
     135        var settings = getAllUserSettings();
    134136
    135         if ( obj.hasOwnProperty(name) )
    136                 return obj[name];
     137        if ( settings.hasOwnProperty( name ) ) {
     138                return settings[name];
     139        }
    137140
    138         if ( typeof def != 'undefined' )
     141        if ( typeof def !== 'undefined' ) {
    139142                return def;
     143        }
    140144
    141145        return '';
    142146}
     
    147151        if ( 'object' !== typeof userSettings )
    148152                return false;
    149153
    150         var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url,
    151         n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
     154        var uid = userSettings.uid,
     155                oldUid = uid.lastIndexOf('-') > 0 ? uid.substring( 0, uid.lastIndexOf('-') ) : 0,
     156                settings = wpCookies.getHash( 'wp-settings-' + uid ),
     157                path = userSettings.url;
    152158
     159        name = name.toString().replace( /[^A-Za-z0-9_]/, '' );
     160        value = value.toString().replace( /[^A-Za-z0-9_]/, '' );
     161
     162        // This should be removed in about a year as these cookies will expire by then.
     163        if ( oldUid ) {
     164                if ( ! settings ) {
     165                        settings = wpCookies.getHash( 'wp-settings-' + oldUid );
     166                }
     167                // Delete old cookies
     168                if ( wpCookies.get( 'wp-settings-time-' + oldUid ) ) {
     169                        wpCookies.remove( 'wp-settings-' + oldUid, path );
     170                        wpCookies.remove( 'wp-settings-time-' + oldUid, path );
     171                }
     172        }
     173
     174        settings = settings || {};
     175
    153176        if ( _del ) {
    154                 delete all[n];
     177                delete settings[name];
    155178        } else {
    156                 all[n] = v;
     179                settings[name] = value;
    157180        }
    158181
    159         wpCookies.setHash(cookie, all, 31536000, path);
    160         wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
     182        wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path );
     183        wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path );
    161184
    162185        return name;
    163186}
     
    168191
    169192// Returns all settings as js object.
    170193function getAllUserSettings() {
    171         if ( 'object' !== typeof userSettings )
     194        if ( 'object' !== typeof userSettings ) {
    172195                return {};
     196        }
    173197
    174         return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
     198        var uid = userSettings.uid,
     199                settings = wpCookies.getHash( 'wp-settings-' + uid );
     200
     201        // Try the old format cookie
     202        if ( ! settings && uid.lastIndexOf('-') > 0 ) {
     203                uid = uid.substring( 0, uid.lastIndexOf('-') );
     204                settings = wpCookies.getHash( 'wp-settings-' + uid );
     205        }
     206
     207        return settings || {};
    175208}
  • src/wp-includes/option.php

     
    713713 */
    714714function wp_user_settings() {
    715715
    716         if ( ! is_admin() )
     716        if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
    717717                return;
     718        }
    718719
    719         if ( defined('DOING_AJAX') )
     720        if ( ! $user_id = get_current_user_id() ) {
    720721                return;
     722        }
    721723
    722         if ( ! $user_id = get_current_user_id() )
     724        if ( is_super_admin() && ! is_user_member_of_blog() ) {
    723725                return;
     726        }
    724727
    725         if ( is_super_admin() && ! is_user_member_of_blog() )
    726                 return;
    727 
    728728        $settings = (string) get_user_option( 'user-settings', $user_id );
     729        $uid = $user_id . '-' . get_current_blog_id();
    729730
    730         if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
    731                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
     731        if ( isset( $_COOKIE['wp-settings-' . $uid] ) ) {
     732                $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $uid] );
    732733
    733734                // No change or both empty
    734735                if ( $cookie == $settings )
     
    735736                        return;
    736737
    737738                $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
    738                 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
     739                $current = isset( $_COOKIE['wp-settings-time-' . $uid]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $uid] ) : 0;
    739740
    740741                // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
    741742                if ( $current > $last_saved ) {
     
    747748
    748749        // The cookie is not set in the current browser or the saved value is newer.
    749750        $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
    750         setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
    751         setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
    752         $_COOKIE['wp-settings-' . $user_id] = $settings;
     751        setcookie( 'wp-settings-' . $uid, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
     752        setcookie( 'wp-settings-time-' . $uid, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );
     753        $_COOKIE['wp-settings-' . $uid] = $settings;
    753754}
    754755
    755756/**
     
    781782 */
    782783function set_user_setting( $name, $value ) {
    783784
    784         if ( headers_sent() )
     785        if ( headers_sent() ) {
    785786                return false;
     787        }
    786788
    787789        $all_user_settings = get_all_user_settings();
    788790        $all_user_settings[$name] = $value;
     
    803805 */
    804806function delete_user_setting( $names ) {
    805807
    806         if ( headers_sent() )
     808        if ( headers_sent() ) {
    807809                return false;
     810        }
    808811
    809812        $all_user_settings = get_all_user_settings();
    810813        $names = (array) $names;
     
    817820                }
    818821        }
    819822
    820         if ( $deleted )
     823        if ( $deleted ) {
    821824                return wp_set_all_user_settings( $all_user_settings );
     825        }
    822826
    823827        return false;
    824828}
     
    833837function get_all_user_settings() {
    834838        global $_updated_user_settings;
    835839
    836         if ( ! $user_id = get_current_user_id() )
     840        if ( ! $user_id = get_current_user_id() ) {
    837841                return array();
     842        }
    838843
    839         if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) )
     844        if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
    840845                return $_updated_user_settings;
     846        }
    841847
    842848        $user_settings = array();
    843         if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
     849        $uid = $user_id . '-' . get_current_blog_id();
     850
     851        if ( isset( $_COOKIE['wp-settings-' . $uid] ) ) {
     852                $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $uid] );
     853        } elseif ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
    844854                $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
     855        }
    845856
    846                 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char
    847                         parse_str( $cookie, $user_settings );
    848 
     857        if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
     858                parse_str( $cookie, $user_settings );
    849859        } else {
    850860                $option = get_user_option( 'user-settings', $user_id );
    851                 if ( $option && is_string($option) )
     861                if ( $option && is_string( $option ) )
    852862                        parse_str( $option, $user_settings );
    853863        }
    854864
     
    867877function wp_set_all_user_settings( $user_settings ) {
    868878        global $_updated_user_settings;
    869879
    870         if ( ! $user_id = get_current_user_id() )
     880        if ( ! $user_id = get_current_user_id() ) {
    871881                return false;
     882        }
    872883
    873         if ( is_super_admin() && ! is_user_member_of_blog() )
     884        if ( is_super_admin() && ! is_user_member_of_blog() ) {
    874885                return;
     886        }
    875887
    876888        $settings = '';
    877889        foreach ( $user_settings as $name => $value ) {
     
    878890                $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
    879891                $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
    880892
    881                 if ( ! empty( $_name ) )
     893                if ( ! empty( $_name ) ) {
    882894                        $settings .= $_name . '=' . $_value . '&';
     895                }
    883896        }
    884897
    885         $settings = rtrim($settings, '&');
     898        $settings = rtrim( $settings, '&' );
    886899        parse_str( $settings, $_updated_user_settings );
    887900
    888901        update_user_option( $user_id, 'user-settings', $settings, false );
     
    897910 * @since 2.7.0
    898911 */
    899912function delete_all_user_settings() {
    900         if ( ! $user_id = get_current_user_id() )
     913        if ( ! $user_id = get_current_user_id() ) {
    901914                return;
     915        }
    902916
     917        $uid = $user_id . '-' . get_current_blog_id();
    903918        update_user_option( $user_id, 'user-settings', '', false );
    904         setcookie('wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
     919        setcookie( 'wp-settings-' . $uid, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
    905920}
    906921
    907922/**
  • src/wp-includes/script-loader.php

     
    7272        $scripts->add( 'utils', "/wp-includes/js/utils$suffix.js" );
    7373        did_action( 'init' ) && $scripts->localize( 'utils', 'userSettings', array(
    7474                'url' => (string) SITECOOKIEPATH,
    75                 'uid' => (string) get_current_user_id(),
     75                'uid' => get_current_user_id() . '-' . get_current_blog_id(),
    7676                'time' => (string) time(),
    7777        ) );
    7878