Changeset 29362
- Timestamp:
- 08/02/2014 08:08:52 PM (10 years ago)
- Location:
- trunk/src/wp-includes
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/utils.js
r26203 r29362 4 4 5 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) {6 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL. 7 8 each: function( obj, cb, scope ) { 9 9 var n, l; 10 10 11 if ( ! obj )11 if ( ! obj ) { 12 12 return 0; 13 } 13 14 14 15 scope = scope || obj; 15 16 16 if ( typeof( obj.length) != 'undefined' ) {17 if ( typeof( obj.length ) !== 'undefined' ) { 17 18 for ( n = 0, l = obj.length; n < l; n++ ) { 18 if ( cb.call( scope, obj[n], n, obj) === false )19 if ( cb.call( scope, obj[n], n, obj ) === false ) { 19 20 return 0; 21 } 20 22 } 21 23 } else { 22 24 for ( n in obj ) { 23 25 if ( obj.hasOwnProperty(n) ) { 24 if ( cb.call( scope, obj[n], n, obj) === false ) {26 if ( cb.call( scope, obj[n], n, obj ) === false ) { 25 27 return 0; 26 28 } … … 35 37 * Returns a JS object with the name: 'value' pairs. 36 38 */ 37 getHash : function(name) {38 var all = this.get(name), ret;39 40 if ( all) {41 this.each( all.split('&'), function(pair) {39 getHash: function( name ) { 40 var cookie = this.get( name ), values; 41 42 if ( cookie ) { 43 this.each( cookie.split('&'), function( pair ) { 42 44 pair = pair.split('='); 43 ret = ret|| {};44 ret[pair[0]] = pair[1];45 values = values || {}; 46 values[pair[0]] = pair[1]; 45 47 }); 46 48 } 47 return ret; 49 50 return values; 48 51 }, 49 52 … … 53 56 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). 54 57 */ 55 setHash : function(name, values_obj, expires, path, domain, secure) {58 setHash: function( name, values_obj, expires, path, domain, secure ) { 56 59 var str = ''; 57 60 58 this.each( values_obj, function(val, key) {59 str += ( !str ? '' : '&') + key + '=' + val;61 this.each( values_obj, function( val, key ) { 62 str += ( ! str ? '' : '&' ) + key + '=' + val; 60 63 }); 61 64 62 this.set( name, str, expires, path, domain, secure);65 this.set( name, str, expires, path, domain, secure ); 63 66 }, 64 67 … … 66 69 * Get a cookie. 67 70 */ 68 get : function(name) {71 get: function( name ) { 69 72 var e, b, 70 73 cookie = document.cookie, 71 74 p = name + '='; 72 75 73 if ( ! cookie )76 if ( ! cookie ) { 74 77 return; 75 76 b = cookie.indexOf('; ' + p); 77 78 if ( b == -1 ) { 78 } 79 80 b = cookie.indexOf( '; ' + p ); 81 82 if ( b === -1 ) { 79 83 b = cookie.indexOf(p); 80 84 81 if ( b !== 0 ) 85 if ( b !== 0 ) { 82 86 return null; 83 87 } 84 88 } else { 85 89 b += 2; … … 88 92 e = cookie.indexOf( ';', b ); 89 93 90 if ( e == -1 )94 if ( e === -1 ) { 91 95 e = cookie.length; 92 93 return decodeURIComponent( cookie.substring(b + p.length, e) ); 96 } 97 98 return decodeURIComponent( cookie.substring( b + p.length, e ) ); 94 99 }, 95 100 … … 100 105 * or the number of seconds until expiration 101 106 */ 102 set : function(name, value, expires, path, domain, secure) {107 set: function( name, value, expires, path, domain, secure ) { 103 108 var d = new Date(); 104 109 105 if ( typeof( expires)== 'object' && expires.toGMTString ) {110 if ( typeof( expires ) === 'object' && expires.toGMTString ) { 106 111 expires = expires.toGMTString(); 107 } else if ( parseInt( expires, 10) ) {108 d.setTime( d.getTime() + ( parseInt( expires, 10) * 1000 ) ); // time must be in miliseconds112 } else if ( parseInt( expires, 10 ) ) { 113 d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds 109 114 expires = d.toGMTString(); 110 115 } else { … … 124 129 * This is done by setting it to an empty value and setting the expiration time in the past. 125 130 */ 126 remove : function(name, path) {127 this.set( name, '', -1000, path);131 remove: function( name, path ) { 132 this.set( name, '', -1000, path ); 128 133 } 129 134 }; … … 131 136 // Returns the value as string. Second arg or empty string is returned when value is not set. 132 137 function getUserSetting( name, def ) { 133 var obj = getAllUserSettings(); 134 135 if ( obj.hasOwnProperty(name) ) 136 return obj[name]; 137 138 if ( typeof def != 'undefined' ) 138 var settings = getAllUserSettings(); 139 140 if ( settings.hasOwnProperty( name ) ) { 141 return settings[name]; 142 } 143 144 if ( typeof def !== 'undefined' ) { 139 145 return def; 146 } 140 147 141 148 return ''; … … 144 151 // Both name and value must be only ASCII letters, numbers or underscore 145 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. 146 154 function setUserSetting( name, value, _del ) { 147 if ( 'object' !== typeof userSettings ) 155 if ( 'object' !== typeof userSettings ) { 148 156 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_]/, ''); 157 } 158 159 var uid = userSettings.uid, 160 oldUid = uid.lastIndexOf('-') > 0 ? uid.substring( 0, uid.lastIndexOf('-') ) : 0, 161 settings = wpCookies.getHash( 'wp-settings-' + uid ), 162 path = userSettings.url; 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 if ( oldUid ) { 173 if ( ! settings ) { 174 settings = wpCookies.getHash( 'wp-settings-' + oldUid ); 175 } 176 // Delete old cookies 177 if ( wpCookies.get( 'wp-settings-time-' + oldUid ) ) { 178 wpCookies.remove( 'wp-settings-' + oldUid, path ); 179 wpCookies.remove( 'wp-settings-time-' + oldUid, path ); 180 } 181 } 182 183 settings = settings || {}; 152 184 153 185 if ( _del ) { 154 delete all[n];186 delete settings[name]; 155 187 } 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);188 settings[name] = value; 189 } 190 191 wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path ); 192 wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path ); 161 193 162 194 return name; … … 169 201 // Returns all settings as js object. 170 202 function getAllUserSettings() { 171 if ( 'object' !== typeof userSettings ) 203 if ( 'object' !== typeof userSettings ) { 172 204 return {}; 173 174 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {}; 175 } 205 } 206 207 var uid = userSettings.uid, 208 settings = wpCookies.getHash( 'wp-settings-' + uid ); 209 210 // Try the old format cookie 211 if ( ! settings && uid.lastIndexOf('-') > 0 ) { 212 uid = uid.substring( 0, uid.lastIndexOf('-') ); 213 settings = wpCookies.getHash( 'wp-settings-' + uid ); 214 } 215 216 return settings || {}; 217 } -
trunk/src/wp-includes/option.php
r29311 r29362 714 714 function wp_user_settings() { 715 715 716 if ( ! is_admin() )716 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) { 717 717 return; 718 719 if ( defined('DOING_AJAX') ) 718 } 719 720 if ( ! $user_id = get_current_user_id() ) { 720 721 return; 721 722 if ( ! $user_id = get_current_user_id() ) 722 } 723 724 if ( is_super_admin() && ! is_user_member_of_blog() ) { 723 725 return; 724 725 if ( is_super_admin() && ! is_user_member_of_blog() ) 726 return; 726 } 727 727 728 728 $settings = (string) get_user_option( 'user-settings', $user_id ); 729 730 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) { 731 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] ); 729 $uid = $user_id . '-' . get_current_blog_id(); 730 731 if ( isset( $_COOKIE['wp-settings-' . $uid] ) ) { 732 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $uid] ); 732 733 733 734 // No change or both empty … … 736 737 737 738 $last_saved = (int) get_user_option( 'user-settings-time', $user_id ); 738 $current = isset( $_COOKIE['wp-settings-time-' . $u ser_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; 739 740 740 741 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is … … 748 749 // The cookie is not set in the current browser or the saved value is newer. 749 750 $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) ); 750 setcookie( 'wp-settings-' . $u ser_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );751 setcookie( 'wp-settings-time-' . $u ser_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN, $secure );752 $_COOKIE['wp-settings-' . $u ser_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; 753 754 } 754 755 … … 782 783 function set_user_setting( $name, $value ) { 783 784 784 if ( headers_sent() ) 785 if ( headers_sent() ) { 785 786 return false; 787 } 786 788 787 789 $all_user_settings = get_all_user_settings(); … … 804 806 function delete_user_setting( $names ) { 805 807 806 if ( headers_sent() ) 808 if ( headers_sent() ) { 807 809 return false; 810 } 808 811 809 812 $all_user_settings = get_all_user_settings(); … … 818 821 } 819 822 820 if ( $deleted ) 823 if ( $deleted ) { 821 824 return wp_set_all_user_settings( $all_user_settings ); 825 } 822 826 823 827 return false; … … 834 838 global $_updated_user_settings; 835 839 836 if ( ! $user_id = get_current_user_id() ) 840 if ( ! $user_id = get_current_user_id() ) { 837 841 return array(); 838 839 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) 842 } 843 844 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) { 840 845 return $_updated_user_settings; 846 } 841 847 842 848 $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] ) ) { 844 854 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] ); 845 846 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char 847 parse_str( $cookie, $user_settings );848 855 } 856 857 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) { // '=' cannot be 1st char 858 parse_str( $cookie, $user_settings ); 849 859 } else { 850 860 $option = get_user_option( 'user-settings', $user_id ); 851 if ( $option && is_string( $option) )861 if ( $option && is_string( $option ) ) 852 862 parse_str( $option, $user_settings ); 853 863 } … … 868 878 global $_updated_user_settings; 869 879 870 if ( ! $user_id = get_current_user_id() ) 880 if ( ! $user_id = get_current_user_id() ) { 871 881 return false; 872 873 if ( is_super_admin() && ! is_user_member_of_blog() ) 882 } 883 884 if ( is_super_admin() && ! is_user_member_of_blog() ) { 874 885 return; 886 } 875 887 876 888 $settings = ''; … … 879 891 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value ); 880 892 881 if ( ! empty( $_name ) ) 893 if ( ! empty( $_name ) ) { 882 894 $settings .= $_name . '=' . $_value . '&'; 883 } 884 885 $settings = rtrim($settings, '&'); 895 } 896 } 897 898 $settings = rtrim( $settings, '&' ); 886 899 parse_str( $settings, $_updated_user_settings ); 887 900 … … 898 911 */ 899 912 function delete_all_user_settings() { 900 if ( ! $user_id = get_current_user_id() ) 913 if ( ! $user_id = get_current_user_id() ) { 901 914 return; 902 915 } 916 917 $uid = $user_id . '-' . get_current_blog_id(); 903 918 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 ); 905 920 } 906 921 -
trunk/src/wp-includes/script-loader.php
r29049 r29362 73 73 did_action( 'init' ) && $scripts->localize( 'utils', 'userSettings', array( 74 74 'url' => (string) SITECOOKIEPATH, 75 'uid' => (string) get_current_user_id(),75 'uid' => get_current_user_id() . '-' . get_current_blog_id(), 76 76 'time' => (string) time(), 77 77 ) );
Note: See TracChangeset
for help on using the changeset viewer.