WordPress.org

Make WordPress Core

Ticket #24106: 24106.2.patch

File 24106.2.patch, 2.1 KB (added by SergeyBiryukov, 6 years ago)
  • src/wp-includes/formatting.php

     
    34043404}
    34053405
    34063406/**
    3407  * Add slashes to a string or array of strings.
     3407 * Add slashes to a string or array of strings, in recursive manner.
    34083408 *
    34093409 * This should be used when preparing data for core API that expects slashed data.
    34103410 * This should not be used to escape data going directly into an SQL query.
     
    34163416 */
    34173417function wp_slash( $value ) {
    34183418        if ( is_array( $value ) ) {
    3419                 foreach ( $value as $k => $v ) {
    3420                         if ( is_array( $v ) ) {
    3421                                 $value[$k] = wp_slash( $v );
    3422                         } else {
    3423                                 $value[$k] = addslashes( $v );
    3424                         }
    3425                 }
     3419                $value = array_map( 'wp_slash', $value );
    34263420        } else {
    34273421                $value = addslashes( $value );
    34283422        }
  • tests/phpunit/tests/formatting/StripSlashesDeep.php

     
    22
    33/**
    44 * @group formatting
     5 * @group slashes
    56 */
    67class Tests_Formatting_StripSlashesDeep extends WP_UnitTestCase {
    78        /**
  • tests/phpunit/tests/formatting/WPSlash.php

     
     1<?php
     2
     3/**
     4 * @group formatting
     5 * @group slashes
     6 */
     7class Tests_Formatting_WPSlash extends WP_UnitTestCase {
     8
     9        /**
     10         * @ticket 24106
     11         */
     12        function test_adds_slashes() {
     13                $old = "I can't see, isn't that it?";
     14                $new = "I can\'t see, isn\'t that it?";
     15                $this->assertEquals( $new, wp_slash( $old ) );
     16                $this->assertEquals( "I can\\\\\'t see, isn\\\\\'t that it?", wp_slash( $new ) );
     17                $this->assertEquals( array( 'a' => $new ), wp_slash( array( 'a' => $old ) ) ); // Keyed array
     18                $this->assertEquals( array( $new ), wp_slash( array( $old ) ) ); // Non-keyed
     19        }
     20
     21}