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