Make WordPress Core

Changeset 61590


Ignore:
Timestamp:
02/04/2026 06:51:51 PM (3 months ago)
Author:
westonruter
Message:

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 https://github.com/WordPress/wordpress-develop/pull/10771

Follow-up to [23591], [23555].

Props rutviksavsani, audrasjb, westonruter, mindctrl, johnbillion.
See #21767.
Fixes #64539.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r61470 r61590  
    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 );
     
    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
     
    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 ) {
  • trunk/src/wp-includes/deprecated.php

    r61518 r61590  
    64826482
    64836483/**
     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 */
     6496function addslashes_gpc( $gpc ) {
     6497    _deprecated_function( __FUNCTION__, '7.0.0', 'wp_slash()' );
     6498    return wp_slash( $gpc );
     6499}
     6500
     6501/**
    64846502 * Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
    64856503 *
     
    65096527    return $attributes_string;
    65106528}
    6511 
  • trunk/src/wp-includes/formatting.php

    r61504 r61590  
    28392839
    28402840/**
    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 
    2852 /**
    28532841 * Navigates through an array, object, or scalar, and removes slashes from the values.
    28542842 *
  • trunk/tests/phpunit/tests/formatting/wpSlash.php

    r56547 r61590  
    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}
Note: See TracChangeset for help on using the changeset viewer.