| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 // utility functions 2 3 var wpCookies = { 4 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL. 5 6 each : function(obj, cb, scope) { 7 var n, l; 8 9 if ( !obj ) 10 return 0; 11 12 scope = scope || obj; 13 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 ) 17 return 0; 18 } 19 } else { 20 for ( n in obj ) { 21 if ( obj.hasOwnProperty(n) ) { 22 if ( cb.call(scope, obj[n], n, obj) === false ) { 23 return 0; 24 } 25 } 26 } 27 } 28 return 1; 29 }, 30 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; 37 38 if ( all ) { 39 this.each( all.split('&'), function(pair) { 40 pair = pair.split('='); 41 ret = ret || {}; 42 ret[pair[0]] = pair[1]; 43 }); 44 } 45 return ret; 46 }, 47 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 = ''; 55 56 this.each(values_obj, function(val, key) { 57 str += (!str ? '' : '&') + key + '=' + val; 58 }); 59 60 this.set(name, str, expires, path, domain, secure); 61 }, 62 63 /** 64 * Get a cookie. 65 */ 66 get : function(name) { 67 var cookie = document.cookie, e, p = name + "=", b; 68 69 if ( !cookie ) 70 return; 71 72 b = cookie.indexOf("; " + p); 73 74 if ( b == -1 ) { 75 b = cookie.indexOf(p); 76 77 if ( b != 0 ) 78 return null; 79 80 } else { 81 b += 2; 82 } 83 84 e = cookie.indexOf(";", b); 85 86 if ( e == -1 ) 87 e = cookie.length; 88 89 return decodeURIComponent( cookie.substring(b + p.length, e) ); 90 }, 91 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" : ""); 115 }, 116 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); 124 } 125 }; 126 127 // Returns the value as string. Second arg or empty string is returned when value is not set. 128 function getUserSetting( name, def ) { 129 var obj = getAllUserSettings(); 130 131 if ( obj.hasOwnProperty(name) ) 132 return obj[name]; 133 134 if ( typeof def != 'undefined' ) 135 return def; 136 137 return ''; 138 } 139 140 // Both name and value must be only ASCII letters, numbers or underscore 141 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. 142 function setUserSetting( name, value, _del ) { 143 if ( 'object' !== typeof userSettings ) 144 return false; 145 146 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url, 147 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); 148 149 if ( _del ) { 150 delete all[n]; 151 } else { 152 all[n] = v; 153 } 154 155 wpCookies.setHash(cookie, all, 31536000, path); 156 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path); 157 158 return name; 159 } 160 161 function deleteUserSetting( name ) { 162 return setUserSetting( name, '', 1 ); 163 } 164 165 // Returns all settings as js object. 166 function getAllUserSettings() { 167 if ( 'object' !== typeof userSettings ) 168 return {}; 169 170 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {}; 171 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |