Make WordPress Core

Changeset 19954


Ignore:
Timestamp:
02/19/2012 11:08:10 PM (13 years ago)
Author:
azaozz
Message:

Refresh wpCookie, fixes #20078

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/utils.dev.js

    r16282 r19954  
    44// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
    55
    6     each : function(o, cb, s) {
     6    each : function(obj, cb, scope) {
    77        var n, l;
    88
    9         if (!o)
     9        if ( !obj )
    1010            return 0;
    1111
    12         s = s || o;
     12        scope = scope || obj;
    1313
    14         if (typeof(o.length) != 'undefined') {
    15             for (n=0, l = o.length; n<l; n++) {
    16                 if (cb.call(s, o[n], n, o) === false)
     14        if ( typeof(obj.length) != 'undefined' ) {
     15            for ( n = 0, l = obj.length; n < l; n++ ) {
     16                if ( cb.call(scope, obj[n], n, obj) === false )
    1717                    return 0;
    1818            }
    1919        } else {
    20             for (n in o) {
    21                 if (o.hasOwnProperty(n)) {
    22                     if (cb.call(s, o[n], n, o) === false) {
     20            for ( n in obj ) {
     21                if ( obj.hasOwnProperty(n) ) {
     22                    if ( cb.call(scope, obj[n], n, obj) === false ) {
    2323                        return 0;
    2424                    }
     
    2929    },
    3030
    31     getHash : function(n) {
    32         var v = this.get(n), h;
     31    /**
     32     * Get a multi-values cookie.
     33     * Returns a JS object with the name: 'value' pairs.
     34     */         
     35    getHash : function(name) {
     36        var all = this.get(name), ret;
    3337
    34         if (v) {
    35             this.each(v.split('&'), function(v) {
    36                 v = v.split('=');
    37                 h = h || {};
    38                 h[v[0]] = v[1];
     38        if ( all ) {
     39            this.each( all.split('&'), function(pair) {
     40                pair = pair.split('=');
     41                ret = ret || {};
     42                ret[pair[0]] = pair[1];
    3943            });
    4044        }
    41         return h;
     45        return ret;
    4246    },
    4347
    44     setHash : function(n, v, e, p, d, s) {
    45         var o = '';
     48    /**
     49     * Set a multi-values cookie.
     50     *
     51     * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
     52     */         
     53    setHash : function(name, values_obj, expires, path, domain, secure) {
     54        var str = '';
    4655
    47         this.each(v, function(v, k) {
    48             o += (!o ? '' : '&') + k + '=' + v;
     56        this.each(values_obj, function(val, key) {
     57            str += (!str ? '' : '&') + key + '=' + val;
    4958        });
    5059
    51         this.set(n, o, e, p, d, s);
     60        this.set(name, str, expires, path, domain, secure);
    5261    },
    5362
    54     get : function(n) {
    55         var c = document.cookie, e, p = n + "=", b;
     63    /**
     64     * Get a cookie.
     65     */     
     66    get : function(name) {
     67        var cookie = document.cookie, e, p = name + "=", b;
    5668
    57         if (!c)
     69        if ( !cookie )
    5870            return;
    5971
    60         b = c.indexOf("; " + p);
     72        b = cookie.indexOf("; " + p);
    6173
    62         if (b == -1) {
    63             b = c.indexOf(p);
     74        if ( b == -1 ) {
     75            b = cookie.indexOf(p);
    6476
    65             if (b != 0)
     77            if ( b != 0 )
    6678                return null;
    6779
     
    7082        }
    7183
    72         e = c.indexOf(";", b);
     84        e = cookie.indexOf(";", b);
    7385
    74         if (e == -1)
    75             e = c.length;
     86        if ( e == -1 )
     87            e = cookie.length;
    7688
    77         return decodeURIComponent(c.substring(b + p.length, e));
     89        return decodeURIComponent( cookie.substring(b + p.length, e) );
    7890    },
    7991
    80     set : function(n, v, e, p, d, s) {
    81         document.cookie = n + "=" + encodeURIComponent(v) +
    82             ((e) ? "; expires=" + e.toGMTString() : "") +
    83             ((p) ? "; path=" + p : "") +
    84             ((d) ? "; domain=" + d : "") +
    85             ((s) ? "; secure" : "");
     92    /**
     93     * Set a cookie.
     94     *
     95     * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
     96     * or the number of seconds until expiration
     97     */     
     98    set : function(name, value, expires, path, domain, secure) {
     99        var d = new Date();
     100
     101        if ( typeof(expires) == 'object' && expires.toGMTString ) {
     102            expires = expires.toGMTString();
     103        } else if ( parseInt(expires, 10) ) {
     104            d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
     105            expires = d.toGMTString();
     106        } else {
     107            expires = '';
     108        }
     109
     110        document.cookie = name + "=" + encodeURIComponent(value) +
     111            ((expires) ? "; expires=" + expires : "") +
     112            ((path) ? "; path=" + path : "") +
     113            ((domain) ? "; domain=" + domain : "") +
     114            ((secure) ? "; secure" : "");
    86115    },
    87116
    88     remove : function(n, p) {
    89         var d = new Date();
    90 
    91         d.setTime(d.getTime() - 1000);
    92 
    93         this.set(n, '', d, p, d);
     117    /**
     118     * Remove a cookie.
     119     *
     120     * This is done by setting it to an empty value and setting the expiration time in the past.
     121     */     
     122    remove : function(name, path) {
     123        this.set(name, '', -1000, path);
    94124    }
    95125};
     
    97127// Returns the value as string. Second arg or empty string is returned when value is not set.
    98128function getUserSetting( name, def ) {
    99     var o = getAllUserSettings();
     129    var obj = getAllUserSettings();
    100130
    101     if ( o.hasOwnProperty(name) )
    102         return o[name];
     131    if ( obj.hasOwnProperty(name) )
     132        return obj[name];
    103133
    104134    if ( typeof def != 'undefined' )
     
    110140// Both name and value must be only ASCII letters, numbers or underscore
    111141// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
    112 function setUserSetting( name, value, del ) {
     142function setUserSetting( name, value, _del ) {
    113143    if ( 'object' !== typeof userSettings )
    114144        return false;
    115145
    116     var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p,
     146    var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url,
    117147    n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
    118148
    119     if ( del ) {
    120         delete o[n];
     149    if ( _del ) {
     150        delete all[n];
    121151    } else {
    122         o[n] = v;
     152        all[n] = v;
    123153    }
    124154
    125     d.setTime( d.getTime() + 31536000000 );
    126     p = userSettings.url;
    127 
    128     wpCookies.setHash(c, o, d, p);
    129     wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p);
     155    wpCookies.setHash(cookie, all, 31536000, path);
     156    wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
    130157
    131158    return name;
Note: See TracChangeset for help on using the changeset viewer.