Make WordPress Core


Ignore:
Timestamp:
01/10/2019 09:05:50 PM (8 years ago)
Author:
flixos90
Message:

General: Fix problematic string to array parsing.

WordPress has historically often used code like preg_split( '/[\s,]+/', $var ) to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element.

This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string.

Props david.binda, sstoqnov.
Fixes #43977.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions.php

    r44481 r44546  
    533533
    534534                update_option( 'blog_charset', $orig_blog_charset );
     535        }
     536
     537        /**
     538         * @ticket 43977
     539         * @dataProvider data_wp_parse_list
     540         */
     541        function test_wp_parse_list( $expected, $actual ) {
     542                $this->assertSame( $expected, array_values( wp_parse_list( $actual ) ) );
     543        }
     544
     545        function data_wp_parse_list() {
     546                return array(
     547                        array( array( '1', '2', '3', '4' ), '1,2,3,4' ),
     548                        array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
     549                        array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana' ),
     550                        array( array( '1', '2', 'apple', 'banana' ), '1, 2,apple,banana' ),
     551                        array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,,banana' ),
     552                        array( array( '1', '2', 'apple', 'banana' ), ',1,2,apple,banana' ),
     553                        array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana,' ),
     554                        array( array( '1', '2', 'apple', 'banana' ), '1,2 ,apple,banana' ),
     555                        array( array(), '' ),
     556                        array( array(), ',' ),
     557                        array( array(), ',,' ),
     558                );
    535559        }
    536560
Note: See TracChangeset for help on using the changeset viewer.