Ticket #29095: 29095.patch
| File 29095.patch, 12.2 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/js/utils.js
3 3 // utility functions 4 4 5 5 var wpCookies = { 6 // The following functions are from Cookie.js class in TinyMCE , Moxiecode, used under LGPL.6 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL. 7 7 8 each : function(obj, cb, scope) {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 scope = scope || obj; 15 15 16 if ( typeof( obj.length) != 'undefined' ) {16 if ( typeof( obj.length ) !== 'undefined' ) { 17 17 for ( n = 0, l = obj.length; n < l; n++ ) { 18 if ( cb.call( scope, obj[n], n, obj) === false )18 if ( cb.call( scope, obj[n], n, obj ) === false ) { 19 19 return 0; 20 } 20 21 } 21 22 } else { 22 23 for ( n in obj ) { 23 24 if ( obj.hasOwnProperty(n) ) { 24 if ( cb.call( scope, obj[n], n, obj) === false ) {25 if ( cb.call( scope, obj[n], n, obj ) === false ) { 25 26 return 0; 26 27 } 27 28 } … … 34 35 * Get a multi-values cookie. 35 36 * Returns a JS object with the name: 'value' pairs. 36 37 */ 37 getHash : function(name) {38 var all = this.get(name), ret;38 getHash: function( name ) { 39 var cookie = this.get( name ), values; 39 40 40 if ( all) {41 this.each( all.split('&'), function(pair) {41 if ( cookie ) { 42 this.each( cookie.split('&'), function( pair ) { 42 43 pair = pair.split('='); 43 ret = ret|| {};44 ret[pair[0]] = pair[1];44 values = values || {}; 45 values[pair[0]] = pair[1]; 45 46 }); 46 47 } 47 return ret; 48 49 return values; 48 50 }, 49 51 50 52 /** … … 52 54 * 53 55 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). 54 56 */ 55 setHash : function(name, values_obj, expires, path, domain, secure) {57 setHash: function( name, values_obj, expires, path, domain, secure ) { 56 58 var str = ''; 57 59 58 this.each( values_obj, function(val, key) {59 str += ( !str ? '' : '&') + key + '=' + val;60 this.each( values_obj, function( val, key ) { 61 str += ( ! str ? '' : '&' ) + key + '=' + val; 60 62 }); 61 63 62 this.set( name, str, expires, path, domain, secure);64 this.set( name, str, expires, path, domain, secure ); 63 65 }, 64 66 65 67 /** 66 68 * Get a cookie. 67 69 */ 68 get : function(name) {70 get: function( name ) { 69 71 var e, b, 70 72 cookie = document.cookie, 71 73 p = name + '='; … … 90 92 if ( e == -1 ) 91 93 e = cookie.length; 92 94 93 return decodeURIComponent( cookie.substring( b + p.length, e) );95 return decodeURIComponent( cookie.substring( b + p.length, e ) ); 94 96 }, 95 97 96 98 /** … … 99 101 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat) 100 102 * or the number of seconds until expiration 101 103 */ 102 set : function(name, value, expires, path, domain, secure) {104 set: function( name, value, expires, path, domain, secure ) { 103 105 var d = new Date(); 104 106 105 if ( typeof( expires) == 'object' && expires.toGMTString ) {107 if ( typeof( expires ) == 'object' && expires.toGMTString ) { 106 108 expires = expires.toGMTString(); 107 } else if ( parseInt( expires, 10) ) {108 d.setTime( d.getTime() + ( parseInt( expires, 10) * 1000 ) ); // time must be in miliseconds109 } else if ( parseInt( expires, 10 ) ) { 110 d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds 109 111 expires = d.toGMTString(); 110 112 } else { 111 113 expires = ''; … … 123 125 * 124 126 * This is done by setting it to an empty value and setting the expiration time in the past. 125 127 */ 126 remove : function(name, path) {127 this.set( name, '', -1000, path);128 remove: function( name, path ) { 129 this.set( name, '', -1000, path ); 128 130 } 129 131 }; 130 132 131 133 // Returns the value as string. Second arg or empty string is returned when value is not set. 132 134 function getUserSetting( name, def ) { 133 var obj= getAllUserSettings();135 var settings = getAllUserSettings(); 134 136 135 if ( obj.hasOwnProperty(name) ) 136 return obj[name]; 137 if ( settings.hasOwnProperty( name ) ) { 138 return settings[name]; 139 } 137 140 138 if ( typeof def != 'undefined' )141 if ( typeof def !== 'undefined' ) { 139 142 return def; 143 } 140 144 141 145 return ''; 142 146 } … … 147 151 if ( 'object' !== typeof userSettings ) 148 152 return false; 149 153 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_]/, ''); 154 var uid = userSettings.uid, 155 oldUid = uid.lastIndexOf('-') > 0 ? uid.substring( 0, uid.lastIndexOf('-') ) : 0, 156 settings = wpCookies.getHash( 'wp-settings-' + uid ), 157 path = userSettings.url; 152 158 159 name = name.toString().replace( /[^A-Za-z0-9_]/, '' ); 160 value = value.toString().replace( /[^A-Za-z0-9_]/, '' ); 161 162 // This should be removed in about a year as these cookies will expire by then. 163 if ( oldUid ) { 164 if ( ! settings ) { 165 settings = wpCookies.getHash( 'wp-settings-' + oldUid ); 166 } 167 // Delete old cookies 168 if ( wpCookies.get( 'wp-settings-time-' + oldUid ) ) { 169 wpCookies.remove( 'wp-settings-' + oldUid, path ); 170 wpCookies.remove( 'wp-settings-time-' + oldUid, path ); 171 } 172 } 173 174 settings = settings || {}; 175 153 176 if ( _del ) { 154 delete all[n];177 delete settings[name]; 155 178 } else { 156 all[n] = v;179 settings[name] = value; 157 180 } 158 181 159 wpCookies.setHash( cookie, all, 31536000, path);160 wpCookies.set( 'wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);182 wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path ); 183 wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path ); 161 184 162 185 return name; 163 186 } … … 168 191 169 192 // Returns all settings as js object. 170 193 function getAllUserSettings() { 171 if ( 'object' !== typeof userSettings ) 194 if ( 'object' !== typeof userSettings ) { 172 195 return {}; 196 } 173 197 174 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {}; 198 var uid = userSettings.uid, 199 settings = wpCookies.getHash( 'wp-settings-' + uid ); 200 201 // Try the old format cookie 202 if ( ! settings && uid.lastIndexOf('-') > 0 ) { 203 uid = uid.substring( 0, uid.lastIndexOf('-') ); 204 settings = wpCookies.getHash( 'wp-settings-' + uid ); 205 } 206 207 return settings || {}; 175 208 } -
src/wp-includes/option.php
713 713 */ 714 714 function wp_user_settings() { 715 715 716 if ( ! is_admin() )716 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) { 717 717 return; 718 } 718 719 719 if ( defined('DOING_AJAX') )720 if ( ! $user_id = get_current_user_id() ) { 720 721 return; 722 } 721 723 722 if ( ! $user_id = get_current_user_id() )724 if ( is_super_admin() && ! is_user_member_of_blog() ) { 723 725 return; 726 } 724 727 725 if ( is_super_admin() && ! is_user_member_of_blog() )726 return;727 728 728 $settings = (string) get_user_option( 'user-settings', $user_id ); 729 $uid = $user_id . '-' . get_current_blog_id(); 729 730 730 if ( isset( $_COOKIE['wp-settings-' . $u ser_id] ) ) {731 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $u ser_id] );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 734 735 if ( $cookie == $settings ) … … 735 736 return; 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 741 742 if ( $current > $last_saved ) { … … 747 748 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 755 756 /** … … 781 782 */ 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(); 788 790 $all_user_settings[$name] = $value; … … 803 805 */ 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(); 810 813 $names = (array) $names; … … 817 820 } 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; 824 828 } … … 833 837 function get_all_user_settings() { 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(); 842 } 838 843 839 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) 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] ); 855 } 845 856 846 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char 847 parse_str( $cookie, $user_settings ); 848 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 } 854 864 … … 867 877 function wp_set_all_user_settings( $user_settings ) { 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; 882 } 872 883 873 if ( is_super_admin() && ! is_user_member_of_blog() ) 884 if ( is_super_admin() && ! is_user_member_of_blog() ) { 874 885 return; 886 } 875 887 876 888 $settings = ''; 877 889 foreach ( $user_settings as $name => $value ) { … … 878 890 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name ); 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 . '&'; 895 } 883 896 } 884 897 885 $settings = rtrim( $settings, '&');898 $settings = rtrim( $settings, '&' ); 886 899 parse_str( $settings, $_updated_user_settings ); 887 900 888 901 update_user_option( $user_id, 'user-settings', $settings, false ); … … 897 910 * @since 2.7.0 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; 915 } 902 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 907 922 /** -
src/wp-includes/script-loader.php
72 72 $scripts->add( 'utils', "/wp-includes/js/utils$suffix.js" ); 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 ) ); 78 78