| 112 | /** |
| 113 | * Tools for retrieving and manipulating URLs. |
| 114 | * |
| 115 | * @since 4.2.0 |
| 116 | * @namespace |
| 117 | */ |
| 118 | wp.url = { |
| 119 | /** |
| 120 | * Remove a parameter from a URL query string. |
| 121 | * |
| 122 | * To avoid certain messages from being displayed more than once (like when a |
| 123 | * page is refreshed and a message is displayed based on the `msg=nnn` param), |
| 124 | * you can use this to strip out a whitelisted array of parameters. |
| 125 | * |
| 126 | * @since 4.2.0 |
| 127 | * |
| 128 | * @param {Array} [removableQueryArgs] Optional. The parameter names to remove. |
| 129 | * @param {String} [sourceURL] Optional. The URL to parse; defaults to current URL. |
| 130 | * @returns {String} The URL with the filtered parameters. |
| 131 | */ |
| 132 | removeQueryParam: function ( /* removableQueryArgs, sourceURL */ ) { |
| 133 | /** |
| 134 | * The list of parameters to remove. |
| 135 | * |
| 136 | * @type {Array} |
| 137 | */ |
| 138 | var removableQueryArgs = arguments[0] || window.removableQueryArgs, |
| 139 | /** |
| 140 | * The URL to parse. |
| 141 | * |
| 142 | * @type {String|Window.location.href} |
| 143 | */ |
| 144 | sourceURL = arguments[1] || window.location.href, |
| 145 | /** |
| 146 | * The base URL. |
| 147 | * |
| 148 | * @type {string} |
| 149 | */ |
| 150 | rtn = sourceURL.split( '?' )[0], |
| 151 | /** |
| 152 | * The query string of the URL (param=val). |
| 153 | * |
| 154 | * @type {string} |
| 155 | */ |
| 156 | queryString = ( - 1 !== sourceURL.indexOf( '?' ) ) ? sourceURL.split( '?' )[1] : '', |
| 157 | /** |
| 158 | * A map of the query string. |
| 159 | * |
| 160 | * @type {Object} |
| 161 | */ |
| 162 | params = window.wpUrlParams || wp.url.parseQueryArgs( sourceURL ), |
| 163 | /** |
| 164 | * The filtered parameters in an array of strings. |
| 165 | * |
| 166 | * @type {string[]} |
| 167 | */ |
| 168 | newParams = []; |
| 169 | |
| 170 | if ( '' !== queryString ) { |
| 171 | _.reject( params, function( param, index ) { |
| 172 | if( ! _.contains( removableQueryArgs, index ) ) { |
| 173 | newParams.push( index + '=' + param ); |
| 174 | } |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | return rtn + '?' + newParams.join( '&' ); |
| 179 | }, |
| 180 | |
| 181 | /** |
| 182 | * Parse the query string from a URL into an object of param:value. |
| 183 | * |
| 184 | * @since 4.2.0 |
| 185 | * |
| 186 | * @param {String} [url] Optional. Defaults to current URL if not set. |
| 187 | * @returns {Object} The extracted parameters. |
| 188 | */ |
| 189 | parseQueryArgs: function ( /* url */ ) { |
| 190 | /** |
| 191 | * The URL to parse. |
| 192 | * |
| 193 | * @type {*|Window.location.href} |
| 194 | */ |
| 195 | var url = arguments[0] || window.location.href, |
| 196 | /** |
| 197 | * The URL query string. |
| 198 | * |
| 199 | * @type {string} |
| 200 | */ |
| 201 | queryString = ( - 1 !== url.indexOf( '?' ) ) ? url.split( '?' )[1] : '', |
| 202 | /** |
| 203 | * A collection of parameters and their values as strings. |
| 204 | * |
| 205 | * @type {Array} |
| 206 | */ |
| 207 | params = [], |
| 208 | /** |
| 209 | * A collection of parameters and their values. |
| 210 | * |
| 211 | * @type {Object.<string, string>} |
| 212 | */ |
| 213 | argMap = {}, |
| 214 | /** |
| 215 | * The filtered {@link argMap}. |
| 216 | * |
| 217 | * @type {Object.<string, string>} |
| 218 | */ |
| 219 | filteredArgMap = {}; |
| 220 | |
| 221 | if ( '' !== queryString ) { |
| 222 | params = queryString.split( '&' ); |
| 223 | _.each( params, function ( param ) { |
| 224 | /** |
| 225 | * An array containing the the param[0] and value[1]. |
| 226 | * |
| 227 | * @type {Array} |
| 228 | */ |
| 229 | var p = param.split( '=' ); |
| 230 | argMap[ p[0] ] = p[1]; |
| 231 | } ); |
| 232 | } |
| 233 | |
| 234 | // Filter the object so it only contains our declared values, |
| 235 | // and not inherited ones from the Object prototype. |
| 236 | for ( var arg in argMap ) { |
| 237 | if ( argMap.hasOwnProperty( arg ) ) { |
| 238 | filteredArgMap[arg] = argMap[arg]; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Make the params available to the DOM. |
| 244 | * |
| 245 | * @global |
| 246 | * @type {Object.<string, string>} |
| 247 | */ |
| 248 | return window.wpUrlParams = filteredArgMap; |
| 249 | } |
| 250 | }; |