Make WordPress Core


Ignore:
Timestamp:
09/27/2022 08:12:00 PM (2 years ago)
Author:
davidbaumwald
Message:

REST API: Ensure args is an array of arrays in register_rest_route().

When calling register_rest_route(), the args parameter for a route should be an array of arrays. However, some plugins/themes have passed an array of strings or key-value pairs which produces a PHP warning when array_intersect_key is used to filter the array keys based on an allowed list of schema keywords.

This change adds a check of the args parameter to ensure it's an array of arrays, presenting a _doing_it_wrong if any element of args is not an array and restructuring to an array of arrays. This change also adds a unit test for the incorrect usage described above, expecting that a _doing_it_wrong is produced.

Props slaFFik, desrosj, apermo, AndrewNZ, aristath, poena, dovyp, timothyblynjacobs, Hinjiriyo, johnmark8080, nateallen.
Fixes #51986.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api.php

    r53217 r54339  
    25192519        );
    25202520    }
     2521
     2522    /**
     2523     * @ticket 51986
     2524     */
     2525    public function test_route_args_is_array_of_arrays() {
     2526        $this->setExpectedIncorrectUsage( 'register_rest_route' );
     2527
     2528        $registered = register_rest_route(
     2529            'my-ns/v1',
     2530            '/my-route',
     2531            array(
     2532                'callback'            => '__return_true',
     2533                'permission_callback' => '__return_true',
     2534                'args'                => array( 'pattern' ),
     2535            )
     2536        );
     2537
     2538        $this->assertTrue( $registered );
     2539    }
    25212540}
Note: See TracChangeset for help on using the changeset viewer.