Skip to content

Commit

Permalink
Fixes for 5.3.4 (#3412)
Browse files Browse the repository at this point in the history
* Fix query params where a + is in the name

Need to fix both the match and the parameter passing

* Bundle 5.3.4

* Update locale files
  • Loading branch information
johngodley authored Sep 14, 2022
1 parent ebd62bd commit 0c0f69d
Show file tree
Hide file tree
Showing 34 changed files with 572 additions and 433 deletions.
39 changes: 39 additions & 0 deletions e2e/__tests__/url.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,44 @@
"agent": true
}
}
],
[
"Test query param exact with +",
{
"source": {
"url": "/exact-query?random+thing"
},
"target": {
"location": "/plain-redirect",
"status": 301,
"agent": true
}
}
],
[
"Test query param ignore and pass with +",
{
"source": {
"url": "/plain-query-pass?random+thing"
},
"target": {
"location": "/plain-redirect?random+thing",
"status": 301,
"agent": true
}
}
],
[
"Test query param ignore and multiple pass with +",
{
"source": {
"url": "/plain-query-pass?random+thing&another[]=thing"
},
"target": {
"location": "/plain-redirect?another[]=thing&random+thing",
"status": 301,
"agent": true
}
}
]
]
2 changes: 1 addition & 1 deletion locale/json/redirection-cs_CZ.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-de_DE.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-el.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-en_AU.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-en_CA.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-en_GB.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-en_NZ.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-en_ZA.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-es_CO.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-es_EC.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-es_ES.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-es_MX.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-es_VE.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-fa_IR.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-fr_CA.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-fr_FR.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-gl_ES.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-hr.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-it_IT.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-ja.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-nl_NL.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-pt_BR.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-ru_RU.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion locale/json/redirection-sv_SE.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions locale/redirection.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# This file is distributed under the same license as the Redirection plugin.
msgid ""
msgstr ""
"Project-Id-Version: Redirection 5.3.3\n"
"Project-Id-Version: Redirection 5.3.4\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/redirection\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2022-09-07T07:49:10+00:00\n"
"POT-Creation-Date: 2022-09-13T14:45:15+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.6.0\n"

Expand Down
70 changes: 64 additions & 6 deletions models/url/url-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,18 @@ public static function build_url( $url, $query_array ) {
return $value;
}, $query_array ) );

$query = preg_replace( '@%5B\d*%5D@', '[]', $query ); // Make these look like []

foreach ( $query_array as $key => $value ) {
if ( $value === null ) {
$query = str_replace( $key . '=', $key, $query );
$search = str_replace( '%20', '+', rawurlencode( $key ) . '=' );
$replace = str_replace( '%20', '+', rawurlencode( $key ) );

$query = str_replace( $search, $replace, $query );
}
}

$query = preg_replace( '@%5B\d*%5D@', '[]', $query ); // Make these look like []
$query = str_replace( '%252B', '+', $query );

if ( $query ) {
// Get any fragment
Expand Down Expand Up @@ -222,23 +227,70 @@ private function is_exact_match( $url, $params ) {
private function get_url_query( $url ) {
$params = [];
$query = $this->get_query_after( $url );
$internal = $this->parse_str( $query );

wp_parse_str( $query ? $query : '', $params );

// For exactness and due to the way parse_str works we go through and check any query param without a value
foreach ( $params as $key => $value ) {
if ( is_string( $value ) && strlen( $value ) === 0 && strpos( $url, $value . '=' ) === false ) {
if ( is_string( $value ) && strlen( $value ) === 0 && strpos( $url, $key . '=' ) === false ) {
$params[ $key ] = null;
}
}

// A work-around until we replace parse_str with internal function
foreach ( $internal as $pos => $internal_param ) {
if ( $internal_param['parse_str'] !== $internal_param['name'] ) {
foreach ( $params as $key => $value ) {
if ( $key === $internal_param['parse_str'] ) {
unset( $params[ $key ] );
unset( $internal[ $pos ] );
$params[ $internal_param['name'] ] = $value;
}
}
}
}

if ( $this->is_exact_match( $url, $params ) ) {
$this->match_exact = $query;
}

return $params;
}

/**
* A replacement for parse_str, which behaves oddly in some situations (spaces and no param value)
*
* TODO: use this in preference to parse_str
*
* @param string $query Query.
* @return string
*/
private function parse_str( $query ) {
$params = [];

if ( strlen( $query ) === 0 ) {
return $params;
}

$parts = explode( '&', $query ? $query : '' );

foreach ( $parts as $part ) {
$param = explode( '=', $part );
$parse_str = [];

wp_parse_str( $part, $parse_str );

$params[] = [
'name' => str_replace( [ '[', ']', '%5B', '%5D' ], '', str_replace( '+', ' ', $param[0] ) ),
'value' => isset( $param[1] ) ? str_replace( '+', ' ', $param[1] ) : null,
'parse_str' => implode( '', array_keys( $parse_str ) ),
];
}

return $params;
}

/**
* Does the URL contain query parameters?
*
Expand Down Expand Up @@ -322,9 +374,11 @@ public function get_query_same( array $source_query, array $target_query, $is_ig
}
} elseif ( is_string( $source_value ) && is_string( $target_value ) ) {
$add = $this->is_string_match( $source_value, $target_value, $is_ignore_case ) ? $source_value : false;
} elseif ( $source_value === null && $target_value === null ) {
$add = null;
}

if ( ! empty( $add ) || is_numeric( $add ) || $add === '' ) {
if ( ! empty( $add ) || is_numeric( $add ) || $add === '' || $add === null ) {
$same[ $original_key ] = $add;
}
}
Expand All @@ -348,17 +402,21 @@ public function get_query_diff( array $source_query, array $target_query, $depth

$diff = [];
foreach ( $source_query as $key => $value ) {
if ( isset( $target_query[ $key ] ) && is_array( $value ) && is_array( $target_query[ $key ] ) ) {
if ( array_key_exists( $key, $target_query ) && is_array( $value ) && is_array( $target_query[ $key ] ) ) {
$add = $this->get_query_diff( $source_query[ $key ], $target_query[ $key ], $depth + 1 );

if ( ! empty( $add ) ) {
$diff[ $key ] = $add;
}
} elseif ( ! isset( $target_query[ $key ] ) || ! is_string( $value ) || ! is_string( $target_query[ $key ] ) || $target_query[ $key ] !== $source_query[ $key ] ) {
} elseif ( ! array_key_exists( $key, $target_query ) || ! $this->is_value( $value ) || ! $this->is_value( $target_query[ $key ] ) || $target_query[ $key ] !== $source_query[ $key ] ) {
$diff[ $key ] = $value;
}
}

return $diff;
}

private function is_value( $value ) {
return is_string( $value ) || $value === null;
}
}
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redirection",
"version": "5.3.3",
"version": "5.3.4",
"description": "Redirection is a WordPress plugin to manage 301 redirections and keep track of 404 errors without requiring knowledge of Apache .htaccess files.",
"main": "redirection.php",
"browser": {
Expand Down Expand Up @@ -72,19 +72,19 @@
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@redux-devtools/extension": "^3.2.3",
"@types/react": "^18.0.18",
"@types/react": "^18.0.19",
"@types/react-dom": "^18.0.6",
"@types/react-highlight-words": "^0.16.4",
"@types/react-redux": "^7.1.24",
"@wordpress/base-styles": "^4.7.0",
"@wordpress/eslint-plugin": "^13.0.0",
"@wordpress/base-styles": "^4.8.0",
"@wordpress/eslint-plugin": "^13.1.0",
"@wordpress/jest-preset-default": "^9.0.0",
"@wordpress/prettier-config": "^2.0.0",
"apidoc": "^0.53.0",
"autoprefixer": "^10.4.8",
"autoprefixer": "^10.4.9",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^29.0.2",
"babel-jest": "^29.0.3",
"babel-loader": "^8.2.5",
"chai": "^4.3.6",
"css-loader": "^6.7.1",
Expand All @@ -102,18 +102,18 @@
"he": "^1.2.0",
"html-webpack-plugin": "^5.5.0",
"imports-loader": "^4.0.1",
"jest": "^29.0.2",
"jest-environment-jsdom": "^29.0.2",
"jest": "^29.0.3",
"jest-environment-jsdom": "^29.0.3",
"json-loader": "^0.5.7",
"mocha": "^10.0.0",
"node-fetch": "2",
"node-sass": "^7.0.1",
"node-sass": "^7.0.3",
"null-loader": "^4.0.1",
"path": "^0.12.7",
"postcss": "^8.4.16",
"postcss-focus": "^5.0.1",
"postcss-loader": "^7.0.1",
"postcss-preset-env": "^7.8.0",
"postcss-preset-env": "^7.8.1",
"postcss-reporter": "^7.0.5",
"prettier": "npm:[email protected]",
"redux-devtools-extension": "^2.13.9",
Expand All @@ -130,13 +130,13 @@
"webpack-bundle-analyzer": "^4.6.1",
"webpack-bundle-size-analyzer": "^3.1.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.10.1"
"webpack-dev-server": "^4.11.0"
},
"dependencies": {
"@emotion/react": "^11.10.4",
"@wordpress/element": "^4.14.0",
"@wordpress/i18n": "^4.16.0",
"classnames": "^2.3.1",
"@wordpress/element": "^4.15.0",
"@wordpress/i18n": "^4.17.0",
"classnames": "^2.3.2",
"deep-equal": "^2.0.5",
"lru": "^3.1.0",
"qs": "^6.11.0",
Expand All @@ -151,7 +151,7 @@
"react-textarea-autosize": "^8.3.4",
"redux": "^4.2.0",
"redux-thunk": "^2.4.1",
"typescript": "^4.8.2",
"typescript": "^4.8.3",
"url": "^0.11.0",
"use-debounce": "^8.0.4"
},
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ The plugin works in a similar manner to how WordPress handles permalinks and sho

A x.1 version increase introduces new or updated features and can be considered to contain 'breaking' changes. A x.x.1 increase is purely a bug fix and introduces no new features, and can be considered as containing no breaking changes.

= 5.3.4 - 14th September 2022 =
* Fix query parameter name with a + not matching

= 5.3.3 - 7th September 2022 =
* Fix default HTTP header not being set when first used
* Fix incorrect column heading in CSV
Expand Down
4 changes: 2 additions & 2 deletions redirection-version.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

define( 'REDIRECTION_VERSION', '5.3.3' );
define( 'REDIRECTION_BUILD', 'df596e91f760a31732345ea55e190a72' );
define( 'REDIRECTION_VERSION', '5.3.4' );
define( 'REDIRECTION_BUILD', '04d536d277e6e20820f24f21d395db08' );
define( 'REDIRECTION_MIN_WP', '5.4' );
4 changes: 2 additions & 2 deletions redirection.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Redirection
Plugin URI: https://redirection.me/
Description: Manage all your 301 redirects and monitor 404 errors
Version: 5.3.3
Version: 5.3.4
Author: John Godley
Text Domain: redirection
Domain Path: /locale
Expand Down
18 changes: 18 additions & 0 deletions tests/models/test-url-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public function testQueryArrayCaseInsensitive() {
$this->assertTrue( $url->is_match( '/?hascase[something]=test', new Red_Source_Flags( [ 'flag_case' => true ] ) ) );
}

public function testQueryMatchPlus() {
$url = new Red_Url_Query( '/?a+1', new Red_Source_Flags() );
$this->assertTrue( $url->is_match( '/?a+1', new Red_Source_Flags( [] ) ) );
}

public function testQueryMatchPlusExtra() {
$url = new Red_Url_Query( '/?a+1', new Red_Source_Flags() );
$this->assertTrue( $url->is_match( '/?a+1&test=3', new Red_Source_Flags( [ 'flag_query' => 'pass' ] ) ) );
}

public function testQueryPassNoParams() {
$this->assertEquals( '/cats', Red_Url_Query::add_to_target( '/cats', '/target', new Red_Source_Flags( [ 'flag_query' => 'pass' ] ) ) );
}
Expand Down Expand Up @@ -130,6 +140,14 @@ public function testQueryPassNoValue() {
$this->assertEquals( '/?this', Red_Url_Query::add_to_target( '/', '/?this', new Red_Source_Flags( [ 'flag_query' => 'pass', 'flag_case' => true ] ) ) );
}

public function testQueryPassNoValuePlus() {
$this->assertEquals( '/?this+that', Red_Url_Query::add_to_target( '/', '/?this+that', new Red_Source_Flags( [ 'flag_query' => 'pass', 'flag_case' => true ] ) ) );
}

public function testQueryPassNoValuePlusArray() {
$this->assertEquals( '/?array[]=1&this+that', Red_Url_Query::add_to_target( '/', '/?array[]=1&this+that', new Red_Source_Flags( [ 'flag_query' => 'pass', 'flag_case' => true ] ) ) );
}

public function testFragment() {
$this->assertEquals( '/?arrs1[]=1&arrs1[]=2&stuff=1#fragment', Red_Url_Query::add_to_target( '/#fragment', '/?arrs1[]=1&arrs1[]=2&stuff=1', new Red_Source_Flags( [ 'flag_query' => 'pass' ] ) ) );
}
Expand Down
Loading

0 comments on commit 0c0f69d

Please sign in to comment.