Skip to content

Commit 5930f08

Browse files
committed
Formatting: Deprecate the addslashes_gpc() function.
This deprecates `addslashes_gpc()` in favor of `wp_slash()`, as the former is just a wrapper for the latter. The three remaining uses of `addslashes_gpc()` (in `WP_Query`) have been replaced with `wp_slash()`. Unit tests are added to verify that they have the same behavior. Developed in WordPress#10771 Follow-up to [23591], [23555]. Props rutviksavsani, audrasjb, westonruter, mindctrl, johnbillion. See #21767. Fixes #64539. git-svn-id: https://develop.svn.wordpress.org/trunk@61590 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e36be00 commit 5930f08

4 files changed

Lines changed: 54 additions & 16 deletions

File tree

src/wp-includes/class-wp-query.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,7 @@ public function get_posts() {
23862386
// Author/user stuff.
23872387

23882388
if ( ! empty( $query_vars['author'] ) && '0' != $query_vars['author'] ) {
2389-
$query_vars['author'] = addslashes_gpc( '' . urldecode( $query_vars['author'] ) );
2389+
$query_vars['author'] = wp_slash( '' . urldecode( $query_vars['author'] ) );
23902390
$authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $query_vars['author'] ) ) );
23912391
sort( $authors );
23922392
foreach ( $authors as $author ) {
@@ -2505,7 +2505,7 @@ public function get_posts() {
25052505
$orderby_array = array();
25062506
if ( is_array( $query_vars['orderby'] ) ) {
25072507
foreach ( $query_vars['orderby'] as $_orderby => $order ) {
2508-
$orderby = addslashes_gpc( urldecode( $_orderby ) );
2508+
$orderby = wp_slash( urldecode( $_orderby ) );
25092509
$parsed = $this->parse_orderby( $orderby );
25102510

25112511
if ( ! $parsed ) {
@@ -2518,7 +2518,7 @@ public function get_posts() {
25182518

25192519
} else {
25202520
$query_vars['orderby'] = urldecode( $query_vars['orderby'] );
2521-
$query_vars['orderby'] = addslashes_gpc( $query_vars['orderby'] );
2521+
$query_vars['orderby'] = wp_slash( $query_vars['orderby'] );
25222522

25232523
foreach ( explode( ' ', $query_vars['orderby'] ) as $i => $orderby ) {
25242524
$parsed = $this->parse_orderby( $orderby );

src/wp-includes/deprecated.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6480,6 +6480,24 @@ function wp_print_auto_sizes_contain_css_fix() {
64806480
<?php
64816481
}
64826482

6483+
/**
6484+
* Adds slashes to a string or recursively adds slashes to strings within an array.
6485+
*
6486+
* This function is just a wrapper for `wp_slash()`. It was originally related to
6487+
* magic quotes functionality which was deprecated in PHP 5.3.0 and removed in PHP 5.4.0.
6488+
*
6489+
* @since 0.71
6490+
* @deprecated 7.0.0 Use wp_slash() instead.
6491+
* @see wp_slash()
6492+
*
6493+
* @param string|array $gpc String or array of data to slash.
6494+
* @return string|array Slashed `$gpc`.
6495+
*/
6496+
function addslashes_gpc( $gpc ) {
6497+
_deprecated_function( __FUNCTION__, '7.0.0', 'wp_slash()' );
6498+
return wp_slash( $gpc );
6499+
}
6500+
64836501
/**
64846502
* Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
64856503
*
@@ -6508,4 +6526,3 @@ function wp_sanitize_script_attributes( $attributes ) {
65086526
}
65096527
return $attributes_string;
65106528
}
6511-

src/wp-includes/formatting.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,18 +2837,6 @@ function untrailingslashit( $value ) {
28372837
return rtrim( $value, '/\\' );
28382838
}
28392839

2840-
/**
2841-
* Adds slashes to a string or recursively adds slashes to strings within an array.
2842-
*
2843-
* @since 0.71
2844-
*
2845-
* @param string|array $gpc String or array of data to slash.
2846-
* @return string|array Slashed `$gpc`.
2847-
*/
2848-
function addslashes_gpc( $gpc ) {
2849-
return wp_slash( $gpc );
2850-
}
2851-
28522840
/**
28532841
* Navigates through an array, object, or scalar, and removes slashes from the values.
28542842
*

tests/phpunit/tests/formatting/wpSlash.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,37 @@ public function test_add_even_more_slashes() {
101101
$this->assertSame( array( 'a' => $new ), wp_slash( array( 'a' => $old ) ) ); // Keyed array.
102102
$this->assertSame( array( $new ), wp_slash( array( $old ) ) ); // Non-keyed.
103103
}
104+
105+
/**
106+
* Tests that addslashes_gpc() returns the same result as wp_slash() for strings.
107+
*
108+
* @ticket 64539
109+
* @covers ::addslashes_gpc
110+
* @expectedDeprecated addslashes_gpc
111+
*/
112+
public function test_addslashes_gpc_matches_wp_slash_for_strings() {
113+
$input = "String with 'quotes' and \"double quotes\"";
114+
$this->assertSame( wp_slash( $input ), addslashes_gpc( $input ) );
115+
}
116+
117+
/**
118+
* Tests that addslashes_gpc() returns the same result as wp_slash() for arrays.
119+
*
120+
* @ticket 64539
121+
* @covers ::addslashes_gpc
122+
* @expectedDeprecated addslashes_gpc
123+
*/
124+
public function test_addslashes_gpc_matches_wp_slash_for_arrays() {
125+
$input = array(
126+
'field1' => "Value with 'apostrophe'",
127+
'field2' => 'Value with "quotes"',
128+
'field3' => 'user@example.com',
129+
'nested' => array(
130+
'key1' => 'Nested value with \\ backslash',
131+
'key2' => array( 'deeply', 'nested', 'array' ),
132+
),
133+
);
134+
135+
$this->assertSame( wp_slash( $input ), addslashes_gpc( $input ) );
136+
}
104137
}

0 commit comments

Comments
 (0)