Make WordPress Core

Changeset 54339

Timestamp:
09/27/2022 08:12:00 PM (23 months 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r53877 r54339  
    102102                ),
    103103                '5.5.0'
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
    104116            );
    105117        }
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r54133 r54339  
    15141514
    15151515                foreach ( $callback['args'] as $key => $opts ) {
     1516
     1517
     1518
     1519
     1520
    15161521                    $arg_data             = array_intersect_key( $opts, $allowed_schema_keywords );
    15171522                    $arg_data['required'] = ! empty( $opts['required'] );
  • trunk/tests/phpunit/tests/rest-api.php

    r53217 r54339  
    25192519        );
    25202520    }
     2521
     2522
     2523
     2524
     2525
     2526
     2527
     2528
     2529
     2530
     2531
     2532
     2533
     2534
     2535
     2536
     2537
     2538
     2539
    25212540}
Note: See TracChangeset for help on using the changeset viewer.