Make WordPress Core

Ticket #39473: 39473-2023-1.patch

File 39473-2023-1.patch, 3.1 KB (added by mreishus, 3 years ago)

refreshed for 2023; handling new $route_namespace parameter

  • src/wp-includes/rest-api/class-wp-rest-server.php

    From 0c976c2869a7fb5b29cc7f6377526f22d8bb3669 Mon Sep 17 00:00:00 2001
    From: Matthew Reishus <mreishus@users.noreply.github.com>
    Date: Thu, 12 Jan 2023 17:50:23 -0600
    Subject: [PATCH 1/1] get_routes() caches generated routes
    
    ---
     .../rest-api/class-wp-rest-server.php         | 63 +++++++++++++++++++
     1 file changed, 63 insertions(+)
    
    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..0aeb10c02c 100644
    a b class WP_REST_Server { 
    7171         */
    7272        protected $endpoints = array();
    7373
     74        /**
     75         * This variable stores namespaces as keys and route maps as values.
     76         * The route map is an associative array with path regexes as the keys. The
     77         * value is an indexed array with the callback function/method as the first
     78         * item, and a bitmask of HTTP methods as the second item (see the class
     79         * constants).
     80         *
     81         * $endpoints is the raw endpoints.
     82         * $route_map is the parsed, filtered and sanitized map from a regex to an endpoint.
     83         *
     84         * @access protected
     85         * @var array Map of namespaces to route maps.
     86         */
     87        protected $route_map_for_namespace = array();
     88
    7489        /**
    7590         * Options defined for the routes.
    7691         *
    class WP_REST_Server { 
    839854                }
    840855        }
    841856
     857        /**
     858         * Retrieves the internally stored route map.
     859         *
     860         * @access public
     861         *
     862         * @param string $route_namespace The namespace to store the routes for.
     863         * @return array Route map of the routes.
     864         */
     865        public function get_route_map( $route_namespace = '' ) {
     866                if ( isset( $this->route_map_for_namespace[ $route_namespace ] ) ) {
     867                        return $this->route_map_for_namespace[ $route_namespace ];
     868                }
     869                return array();
     870        }
     871
     872        /**
     873         * Sets the internally stored route map.
     874         *
     875         * @access protected
     876         *
     877         * @param string $route_namespace Store the route map for a specific
     878         * namespace, or empty string for the default namespace containing all
     879         * routes.
     880         * @param array $route_map Route map of the routes.
     881         */
     882        protected function set_route_map( $route_namespace, $route_map ) {
     883                $this->route_map_for_namespace[ $route_namespace ] = $route_map;
     884        }
     885
     886        /**
     887         * Clears the internally stored route map.
     888         *
     889         * @access protected
     890         *
     891         * @param string $route_namespace Store the route map for a specific
     892         * namespace, or empty string for the default namespace containing all
     893         * routes.
     894         */
     895        public function clear_route_map( $route_namespace ) {
     896                $this->route_map_for_namespace[ $route_namespace ] = array();
     897        }
     898
    842899        /**
    843900         * Retrieves the route map.
    844901         *
    class WP_REST_Server { 
    862919         *               `'/path/regex' => array( array( $callback, $bitmask ), ...)`.
    863920         */
    864921        public function get_routes( $route_namespace = '' ) {
     922                $cached_routes = $this->get_route_map( $route_namespace );
     923                if ( ! empty( $cached_routes ) ) {
     924                        return $cached_routes;
     925                }
     926
    865927                $endpoints = $this->endpoints;
    866928
    867929                if ( $route_namespace ) {
    class WP_REST_Server { 
    929991                        }
    930992                }
    931993
     994                $this->set_route_map( $route_namespace, $endpoints );
    932995                return $endpoints;
    933996        }
    934997