Ticket #23367: 23367.refresh1.diff
File 23367.refresh1.diff, 8.2 KB (added by , 10 years ago) |
---|
-
src/wp-admin/admin.php
diff --git src/wp-admin/admin.php src/wp-admin/admin.php index 1040995..c93e553 100644
set_screen_options(); 88 88 $date_format = get_option('date_format'); 89 89 $time_format = get_option('time_format'); 90 90 91 wp_enqueue_script( 'wp-util' ); 92 93 add_filter( 'removable_url_params', function( $removable_url_params ) { 94 $my_params = array( 'mdub' ); 95 return array_merge( $my_params, $removable_url_params ); 96 }); 97 $removable_url_params = array( 98 'user_switched', 99 'switched_off', 100 'switched_back', 101 'message', 102 'updated', 103 'settings-updated', 104 'saved', 105 'activated', 106 'activate', 107 'deactivate', 108 'locked', 109 'skipped', 110 'deleted', 111 'trashed', 112 'untrashed', 113 ); 114 /** 115 * Filter the list of URL parameters to dynamically remove. 116 * 117 * @since 4.2.0 118 * 119 * @param array $removable_url_params An array of parameters to remove from the URL. 120 */ 121 $removable_url_params = apply_filters( 'removable_url_params', $removable_url_params ); 122 wp_localize_script( 'common', 'removableQueryArgs', $removable_url_params ); 91 123 wp_enqueue_script( 'common' ); 92 124 93 125 // $pagenow is set in vars.php -
src/wp-admin/js/common.js
diff --git src/wp-admin/js/common.js src/wp-admin/js/common.js index bcdb576..4b54c11 100644
$(document).ready( function() { 838 838 } 839 839 }; 840 840 841 /** 842 * Remove parameters from admin pages after they're loaded so they don't trigger 843 * messages again if the page is refreshed. 844 * 845 * This relies on history.replaceState, so is not available to IE < 10 without a polyfill. 846 * 847 * @link https://core.trac.wordpress.org/ticket/23367 848 * @link http://caniuse.com/#search=pushstate 849 * 850 * @since 4.2.0 851 */ 852 function removeQueryArgsFromUrl() { 853 /** 854 * The list of parameters to check against. 855 * 856 * @type {Array} 857 */ 858 var removableQueryArgs = window.removableQueryArgs || [], 859 /** 860 * The filtered URL. 861 * 862 * @type {string} 863 */ 864 url; 865 866 // Create the wpUrlParams object. 867 wp.url.parseQueryArgs(); 868 869 url = wp.url.removeQueryParam( removableQueryArgs ); 870 871 if ( typeof history.replaceState === 'function' ) { 872 history.replaceState( null, null, url ); 873 } 874 } 875 841 876 window.wpResponsive.init(); 842 877 setPinMenu(); 878 removeQueryArgsFromUrl(); 843 879 844 880 $document.on( 'wp-window-resized.pin-menu postboxes-columnchange.pin-menu postbox-toggled.pin-menu wp-collapse-menu.pin-menu wp-scroll-start.pin-menu', setPinMenu ); 845 881 }); -
src/wp-includes/js/wp-util.js
diff --git src/wp-includes/js/wp-util.js src/wp-includes/js/wp-util.js index 1985b35..4eefe33 100644
window.wp = window.wp || {}; 109 109 } 110 110 }; 111 111 112 /** 113 * Tools for retrieving and manipulating URLs. 114 * 115 * @since 4.2.0 116 * 117 * @type {{removeQueryParam: Function, parseQueryArgs: Function}} 118 */ 119 wp.url = { 120 /** 121 * Remove a parameter from a URL query string. 122 * 123 * To avoid certain messages from being displayed more than once (like when a 124 * page is refreshed and a message is displayed based on the `msg=nnn` param), 125 * you can use this to strip out a whitelisted array of parameters. 126 * 127 * @since 4.2.0 128 * 129 * @param {Array} [removableQueryArgs] Optional. The parameter names to remove. 130 * @param {String} [sourceURL] Optional. The URL to parse; defaults to current URL. 131 * @returns {String} The URL with the filtered parameters. 132 */ 133 removeQueryParam: function ( /* removableQueryArgs, sourceURL */ ) { 134 /** 135 * The list of parameters to remove. 136 * 137 * @type {Array} 138 */ 139 var removableQueryArgs = arguments[0] || window.removableQueryArgs, 140 /** 141 * The URL to parse. 142 * 143 * @type {String|Window.location.href} 144 */ 145 sourceURL = arguments[1] || window.location.href, 146 /** 147 * The base URL. 148 * 149 * @type {string} 150 */ 151 rtn = sourceURL.split( '?' )[0], 152 /** 153 * The query string of the URL (param=val). 154 * 155 * @type {string} 156 */ 157 queryString = ( sourceURL.indexOf( '?' ) !== - 1 ) ? sourceURL.split( '?' )[1] : '', 158 /** 159 * A map of the query string. 160 * 161 * @type {Object} 162 */ 163 params = window.wpUrlParams || wp.url.parseQueryArgs( sourceURL ), 164 /** 165 * The filtered parameters in an array of strings. 166 * 167 * @type {string[]} 168 */ 169 newParams = []; 170 171 if ( '' !== queryString ) { 172 _.reject( params, function( param, index ) { 173 if( ! _.contains( removableQueryArgs, index ) ) { 174 newParams.push( index + '=' + param ); 175 } 176 }); 177 } 178 179 return rtn + '?' + newParams.join( '&' ); 180 }, 181 182 /** 183 * Parse the query string from a URL into an object of param:value. 184 * 185 * @since 4.2.0 186 * 187 * @param {String} [url] Optional. Defaults to current URL if not set. 188 * @returns {Object} The extracted parameters. 189 */ 190 parseQueryArgs: function ( /* url */ ) { 191 /** 192 * The URL to parse. 193 * 194 * @type {*|Window.location.href} 195 */ 196 var url = arguments[0] || window.location.href, 197 /** 198 * The URL query string. 199 * 200 * @type {string} 201 */ 202 queryString = ( - 1 !== url.indexOf( '?' ) ) ? url.split( '?' )[1] : '', 203 /** 204 * A collection of parameters and their values as strings. 205 * 206 * @type {Array} 207 */ 208 params = [], 209 /** 210 * A collection of parameters and their values. 211 * 212 * @type {Object.<string, string>} 213 */ 214 argMap = {}, 215 /** 216 * The filtered {@link argMap}. 217 * 218 * @type {Object.<string, string>} 219 */ 220 filteredArgMap = {}; 221 222 if ( '' !== queryString ) { 223 params = queryString.split( '&' ); 224 _.each( params, function ( param ) { 225 /** 226 * An array containing the the param[0] and value[1]. 227 * 228 * @type {Array} 229 */ 230 var p = param.split( '=' ); 231 argMap[ p[0] ] = p[1]; 232 } ); 233 } 234 235 // Filter the object so it only contains our declared values, 236 // and not inherited ones from the Object prototype. 237 for ( var arg in argMap ) { 238 if ( argMap.hasOwnProperty( arg ) ) { 239 filteredArgMap[arg] = argMap[arg]; 240 } 241 } 242 243 /** 244 * Make the params available to the DOM. 245 * 246 * @global 247 * @type {Object.<string, string>} 248 */ 249 return window.wpUrlParams = filteredArgMap; 250 } 251 }; 112 252 }(jQuery)); -
new file tests/qunit/wp-admin/js/common-remove-querystring-args.js
diff --git tests/qunit/wp-admin/js/common-remove-querystring-args.js tests/qunit/wp-admin/js/common-remove-querystring-args.js new file mode 100644 index 0000000..87c96de
- + 1 /* global wp */ 2 3 jQuery( function ( $ ) { 4 module( 'Remove QueryString Args: InList' ); 5 var removableQueryArgs = [ 6 'user_switched', 7 'switched_off', 8 'switched_back', 9 'message', 10 'updated', 11 'settings-updated', 12 'saved', 13 'activated', 14 'activate', 15 'deactivate', 16 'locked', 17 'skipped', 18 'deleted', 19 'trashed', 20 'untrashed' 21 ]; 22 23 function removeParam( key, sourceURL ) { 24 var rtn = sourceURL.split( '?' )[0], param, params = [], 25 queryString = (sourceURL.indexOf( '?' ) !== - 1) ? sourceURL.split( '?' )[1] : ''; 26 if ( queryString !== '' ) { 27 params = queryString.split( '&' ); 28 for ( var i = params.length - 1; i >= 0; i -= 1 ) { 29 param = params[i].split( '=' )[0]; 30 if ( $.inArray( param, key ) > - 1 ) { 31 params.splice( i, 1 ); 32 } 33 } 34 rtn = rtn + '?' + params.join( '&' ); 35 } 36 return rtn; 37 } 38 39 test( 'should remove listed arg from the URL', function () { 40 var testUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivate=true&plugin_status=all&paged=1&s=', 41 finalUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?plugin_status=all&paged=1&s='; 42 43 equal( removeParam( removableQueryArgs, testUrl ), finalUrl, 'deactivate removed from URL' ); 44 } ); 45 46 test( 'should not remove unlisted arg from the URL', function () { 47 var testUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivated=true&plugin_status=all&paged=1&s=', 48 finalUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivated=true&plugin_status=all&paged=1&s='; 49 50 equal( removeParam( removableQueryArgs, testUrl ), finalUrl, 'deactivated not removed from URL' ); 51 } ); 52 } ); 53 No newline at end of file