Make WordPress Core

Ticket #57455: patchfile57455.patch

File patchfile57455.patch, 5.2 KB (added by mreishus, 17 months ago)
  • src/wp-includes/rest-api.php

    From cf7401dd73402d1311b9873a81fc1f1dbfd54c9a Mon Sep 17 00:00:00 2001
    From: Matthew Reishus <mreishus@users.noreply.github.com>
    Date: Thu, 12 Jan 2023 13:08:54 -0600
    Subject: [PATCH] respond_to_request: store matched handlers across other
     methods
    
    ---
     src/wp-includes/rest-api.php                  |  8 ++++--
     .../rest-api/class-wp-rest-response.php       | 27 +++++++++++++++++++
     .../rest-api/class-wp-rest-server.php         | 20 +++++++-------
     3 files changed, 44 insertions(+), 11 deletions(-)
    
    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index 9898501cba..d7af8aec84 100644
    a b function rest_send_allow_header( $response, $server, $request ) { 
    808808                return $response;
    809809        }
    810810
    811         $routes = $server->get_routes();
     811        $all_handlers = $response->get_all_methods_matched_handlers();
     812        if ( empty( $all_handlers ) ) {
     813                $routes = $server->get_routes();
     814                $all_handlers = $routes[ $matched_route ];
     815        }
    812816
    813817        $allowed_methods = array();
    814818
    815819        // Get the allowed methods across the routes.
    816         foreach ( $routes[ $matched_route ] as $_handler ) {
     820        foreach ( $all_handlers as $_handler ) {
    817821                foreach ( $_handler['methods'] as $handler_method => $value ) {
    818822
    819823                        if ( ! empty( $_handler['permission_callback'] ) ) {
  • src/wp-includes/rest-api/class-wp-rest-response.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-response.php b/src/wp-includes/rest-api/class-wp-rest-response.php
    index c6ea11be83..0c90f98ca5 100644
    a b class WP_REST_Response extends WP_HTTP_Response { 
    4040         */
    4141        protected $matched_handler = null;
    4242
     43        /**
     44         * All handlers that match the request, even if the method doesn't match.
     45         * For example, this could include both GET and POST handlers for a route.
     46         * Used when calculating the Allow header.
     47         *
     48         * @var null|array
     49         */
     50        protected $all_methods_matched_handlers = null;
     51
    4352        /**
    4453         * Adds a link to the response.
    4554         *
    class WP_REST_Response extends WP_HTTP_Response { 
    204213                $this->matched_handler = $handler;
    205214        }
    206215
     216        /**
     217         * Retrieves all possible handlers that match the route, even for other HTTP methods.
     218         *
     219         * @return null|array Handlers that could be used to
     220         */
     221        public function get_all_methods_matched_handlers() {
     222                return $this->all_methods_matched_handlers;
     223        }
     224
     225        /**
     226         * Sets all possible handlers that match the route.
     227         *
     228         * @param array $handlers An array of handlers matching the route across all HTTP methods.
     229         */
     230        public function set_all_methods_matched_handlers( $handlers ) {
     231                $this->all_methods_matched_handlers = $handlers;
     232        }
     233
    207234        /**
    208235         * Checks if the response is an error, i.e. >= 400 response code.
    209236         *
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php
    index 00c34cd621..b8b8d216ad 100644
    a b class WP_REST_Server { 
    994994                        return $this->error_to_response( $matched );
    995995                }
    996996
    997                 list( $route, $handler ) = $matched;
     997                list( $route, $handler, $handlers_all_methods ) = $matched;
    998998
    999999                if ( ! is_callable( $handler['callback'] ) ) {
    10001000                        $error = new WP_Error(
    class WP_REST_Server { 
    10161016                        }
    10171017                }
    10181018
    1019                 return $this->respond_to_request( $request, $route, $handler, $error );
     1019                return $this->respond_to_request( $request, $route, $handler, $error, $handlers_all_methods );
    10201020        }
    10211021
    10221022        /**
    class WP_REST_Server { 
    10751075                                }
    10761076
    10771077                                if ( ! is_callable( $callback ) ) {
    1078                                         return array( $route, $handler );
     1078                                        return array( $route, $handler, $handlers );
    10791079                                }
    10801080
    10811081                                $request->set_url_params( $args );
    class WP_REST_Server { 
    10911091
    10921092                                $request->set_default_params( $defaults );
    10931093
    1094                                 return array( $route, $handler );
     1094                                return array( $route, $handler, $handlers );
    10951095                        }
    10961096                }
    10971097
    class WP_REST_Server { 
    11081108         * @access private
    11091109         * @since 5.6.0
    11101110         *
    1111          * @param WP_REST_Request $request  The request object.
    1112          * @param string          $route    The matched route regex.
    1113          * @param array           $handler  The matched route handler.
    1114          * @param WP_Error|null   $response The current error object if any.
     1111         * @param WP_REST_Request $request               The request object.
     1112         * @param string          $route                 The matched route regex.
     1113         * @param array           $handler               The matched route handler.
     1114         * @param WP_Error|null   $response              The current error object if any.
     1115         * @param array           $all_methods_handlers  The matched route handlers including other methods.
    11151116         * @return WP_REST_Response
    11161117         */
    1117         protected function respond_to_request( $request, $route, $handler, $response ) {
     1118        protected function respond_to_request( $request, $route, $handler, $response, $all_methods_handlers ) {
    11181119                /**
    11191120                 * Filters the response before executing any REST API callbacks.
    11201121                 *
    class WP_REST_Server { 
    12041205
    12051206                $response->set_matched_route( $route );
    12061207                $response->set_matched_handler( $handler );
     1208                $response->set_all_methods_matched_handlers( $all_methods_handlers );
    12071209
    12081210                return $response;
    12091211        }