Merge pull request #23196 from woocommerce/fix/23195

Replace full stops before running through `parse_str`
This commit is contained in:
Mike Jolley 2019-04-18 11:01:59 +01:00 committed by GitHub
commit b686150655
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -686,7 +686,22 @@ function wc_query_string_form_fields( $values = null, $exclude = array(), $curre
$values = array();
if ( ! empty( $url_parts['query'] ) ) {
parse_str( $url_parts['query'], $values );
// This is to preserve full-stops and spaces in the query string when ran through parse_str.
$replace_chars = array(
'.' => '{dot}',
'+' => '{plus}',
'%20' => '{space}',
);
$query_string = str_replace( array_keys( $replace_chars ), array_values( $replace_chars ), $url_parts['query'] );
// Parse the string.
parse_str( $query_string, $parsed_query_string );
// Convert the full-stops back and add to values array.
foreach ( $parsed_query_string as $key => $value ) {
$values[ str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $key ) ] = $value;
}
}
}
$html = '';

View File

@ -115,4 +115,31 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case {
unset( $_REQUEST['attribute_pa_size'] );
}
/**
* Test wc_query_string_form_fields.
*
* @return void
*/
public function test_wc_query_string_form_fields() {
$actual_html = wc_query_string_form_fields( '?test=1', array(), '', true );
$expected_html = '<input type="hidden" name="test" value="1" />';
$this->assertEquals( $expected_html, $actual_html, var_export( $actual_html, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$actual_html = wc_query_string_form_fields( '?test=1&test2=something', array(), '', true );
$expected_html = '<input type="hidden" name="test" value="1" /><input type="hidden" name="test2" value="something" />';
$this->assertEquals( $expected_html, $actual_html, var_export( $actual_html, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$actual_html = wc_query_string_form_fields( '?test.something=something', array(), '', true );
$expected_html = '<input type="hidden" name="test.something" value="something" />';
$this->assertEquals( $expected_html, $actual_html, var_export( $actual_html, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$actual_html = wc_query_string_form_fields( '?test+something=something', array(), '', true );
$expected_html = '<input type="hidden" name="test+something" value="something" />';
$this->assertEquals( $expected_html, $actual_html, var_export( $actual_html, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$actual_html = wc_query_string_form_fields( '?test%20something=something', array(), '', true );
$expected_html = '<input type="hidden" name="test%20something" value="something" />';
$this->assertEquals( $expected_html, $actual_html, var_export( $actual_html, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
}
}