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 { |
| 71 | 71 | */ |
| 72 | 72 | protected $endpoints = array(); |
| 73 | 73 | |
| | 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 | |
| 74 | 89 | /** |
| 75 | 90 | * Options defined for the routes. |
| 76 | 91 | * |
| … |
… |
class WP_REST_Server { |
| 839 | 854 | } |
| 840 | 855 | } |
| 841 | 856 | |
| | 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 | |
| 842 | 899 | /** |
| 843 | 900 | * Retrieves the route map. |
| 844 | 901 | * |
| … |
… |
class WP_REST_Server { |
| 862 | 919 | * `'/path/regex' => array( array( $callback, $bitmask ), ...)`. |
| 863 | 920 | */ |
| 864 | 921 | 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 | |
| 865 | 927 | $endpoints = $this->endpoints; |
| 866 | 928 | |
| 867 | 929 | if ( $route_namespace ) { |
| … |
… |
class WP_REST_Server { |
| 929 | 991 | } |
| 930 | 992 | } |
| 931 | 993 | |
| | 994 | $this->set_route_map( $route_namespace, $endpoints ); |
| 932 | 995 | return $endpoints; |
| 933 | 996 | } |
| 934 | 997 | |