~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/js/utils.js

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* global userSettings */
 
2
/* exported getUserSetting, setUserSetting, deleteUserSetting */
 
3
// utility functions
 
4
 
 
5
var wpCookies = {
 
6
// The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
 
7
 
 
8
        each: function( obj, cb, scope ) {
 
9
                var n, l;
 
10
 
 
11
                if ( ! obj ) {
 
12
                        return 0;
 
13
                }
 
14
 
 
15
                scope = scope || obj;
 
16
 
 
17
                if ( typeof( obj.length ) !== 'undefined' ) {
 
18
                        for ( n = 0, l = obj.length; n < l; n++ ) {
 
19
                                if ( cb.call( scope, obj[n], n, obj ) === false ) {
 
20
                                        return 0;
 
21
                                }
 
22
                        }
 
23
                } else {
 
24
                        for ( n in obj ) {
 
25
                                if ( obj.hasOwnProperty(n) ) {
 
26
                                        if ( cb.call( scope, obj[n], n, obj ) === false ) {
 
27
                                                return 0;
 
28
                                        }
 
29
                                }
 
30
                        }
 
31
                }
 
32
                return 1;
 
33
        },
 
34
 
 
35
        /**
 
36
         * Get a multi-values cookie.
 
37
         * Returns a JS object with the name: 'value' pairs.
 
38
         */
 
39
        getHash: function( name ) {
 
40
                var cookie = this.get( name ), values;
 
41
 
 
42
                if ( cookie ) {
 
43
                        this.each( cookie.split('&'), function( pair ) {
 
44
                                pair = pair.split('=');
 
45
                                values = values || {};
 
46
                                values[pair[0]] = pair[1];
 
47
                        });
 
48
                }
 
49
 
 
50
                return values;
 
51
        },
 
52
 
 
53
        /**
 
54
         * Set a multi-values cookie.
 
55
         *
 
56
         * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
 
57
         */
 
58
        setHash: function( name, values_obj, expires, path, domain, secure ) {
 
59
                var str = '';
 
60
 
 
61
                this.each( values_obj, function( val, key ) {
 
62
                        str += ( ! str ? '' : '&' ) + key + '=' + val;
 
63
                });
 
64
 
 
65
                this.set( name, str, expires, path, domain, secure );
 
66
        },
 
67
 
 
68
        /**
 
69
         * Get a cookie.
 
70
         */
 
71
        get: function( name ) {
 
72
                var e, b,
 
73
                        cookie = document.cookie,
 
74
                        p = name + '=';
 
75
 
 
76
                if ( ! cookie ) {
 
77
                        return;
 
78
                }
 
79
 
 
80
                b = cookie.indexOf( '; ' + p );
 
81
 
 
82
                if ( b === -1 ) {
 
83
                        b = cookie.indexOf(p);
 
84
 
 
85
                        if ( b !== 0 ) {
 
86
                                return null;
 
87
                        }
 
88
                } else {
 
89
                        b += 2;
 
90
                }
 
91
 
 
92
                e = cookie.indexOf( ';', b );
 
93
 
 
94
                if ( e === -1 ) {
 
95
                        e = cookie.length;
 
96
                }
 
97
 
 
98
                return decodeURIComponent( cookie.substring( b + p.length, e ) );
 
99
        },
 
100
 
 
101
        /**
 
102
         * Set a cookie.
 
103
         *
 
104
         * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
 
105
         * or the number of seconds until expiration
 
106
         */
 
107
        set: function( name, value, expires, path, domain, secure ) {
 
108
                var d = new Date();
 
109
 
 
110
                if ( typeof( expires ) === 'object' && expires.toGMTString ) {
 
111
                        expires = expires.toGMTString();
 
112
                } else if ( parseInt( expires, 10 ) ) {
 
113
                        d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds
 
114
                        expires = d.toGMTString();
 
115
                } else {
 
116
                        expires = '';
 
117
                }
 
118
 
 
119
                document.cookie = name + '=' + encodeURIComponent( value ) +
 
120
                        ( expires ? '; expires=' + expires : '' ) +
 
121
                        ( path    ? '; path=' + path       : '' ) +
 
122
                        ( domain  ? '; domain=' + domain   : '' ) +
 
123
                        ( secure  ? '; secure'             : '' );
 
124
        },
 
125
 
 
126
        /**
 
127
         * Remove a cookie.
 
128
         *
 
129
         * This is done by setting it to an empty value and setting the expiration time in the past.
 
130
         */
 
131
        remove: function( name, path, domain, secure ) {
 
132
                this.set( name, '', -1000, path, domain, secure );
 
133
        }
 
134
};
 
135
 
 
136
// Returns the value as string. Second arg or empty string is returned when value is not set.
 
137
function getUserSetting( name, def ) {
 
138
        var settings = getAllUserSettings();
 
139
 
 
140
        if ( settings.hasOwnProperty( name ) ) {
 
141
                return settings[name];
 
142
        }
 
143
 
 
144
        if ( typeof def !== 'undefined' ) {
 
145
                return def;
 
146
        }
 
147
 
 
148
        return '';
 
149
}
 
150
 
 
151
// Both name and value must be only ASCII letters, numbers or underscore
 
152
// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
 
153
// The value is converted and stored as string.
 
154
function setUserSetting( name, value, _del ) {
 
155
        if ( 'object' !== typeof userSettings ) {
 
156
                return false;
 
157
        }
 
158
 
 
159
        var uid = userSettings.uid,
 
160
                settings = wpCookies.getHash( 'wp-settings-' + uid ),
 
161
                path = userSettings.url,
 
162
                secure = !! userSettings.secure;
 
163
 
 
164
        name = name.toString().replace( /[^A-Za-z0-9_]/, '' );
 
165
 
 
166
        if ( typeof value === 'number' ) {
 
167
                value = parseInt( value, 10 );
 
168
        } else {
 
169
                value = value.toString().replace( /[^A-Za-z0-9_]/, '' );
 
170
        }
 
171
 
 
172
        settings = settings || {};
 
173
 
 
174
        if ( _del ) {
 
175
                delete settings[name];
 
176
        } else {
 
177
                settings[name] = value;
 
178
        }
 
179
 
 
180
        wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
 
181
        wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
 
182
 
 
183
        return name;
 
184
}
 
185
 
 
186
function deleteUserSetting( name ) {
 
187
        return setUserSetting( name, '', 1 );
 
188
}
 
189
 
 
190
// Returns all settings as js object.
 
191
function getAllUserSettings() {
 
192
        if ( 'object' !== typeof userSettings ) {
 
193
                return {};
 
194
        }
 
195
 
 
196
        return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
 
197
}