Index: wp-includes/rest-api/class-wp-rest-server.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- wp-includes/rest-api/class-wp-rest-server.php	(revision 39676)
+++ wp-includes/rest-api/class-wp-rest-server.php	(revision )
@@ -72,6 +72,17 @@
 	 */
 	protected $endpoints = array();
 
+	/**
+	 * The route map is an associative array with path regexes as the keys. The
+	 * value is an indexed array with the callback function/method as the first
+	 * item, and a bitmask of HTTP methods as the second item (see the class
+	 * constants).
+	 *
+	 * @access protected
+	 * @var array Route map of the routes
+	 */
+	protected $route_map = array();
+
 	/**
 	 * Options defined for the routes.
 	 *
@@ -673,6 +684,28 @@
 		}
 	}
 
+	/**
+	 * Retrieves the route map.
+	 *
+	 * @access public
+	 *
+	 * @return array Route map of the routes.
+	 */
+	public function get_route_map() {
+		return $this->route_map;
+	}
+
+	/**
+	 * Sets the route map.
+	 *
+	 * @access public
+	 *
+	 * @param array $route_map Route map of the routes.
+	 */
+	public function set_route_map( $route_map ) {
+		$this->route_map = $route_map;
+	}
+
 	/**
 	 * Retrieves the route map.
 	 *
@@ -695,66 +728,70 @@
 	 *               `'/path/regex' => array( array( $callback, $bitmask ), ...)`.
 	 */
 	public function get_routes() {
+		$endpoints = $this->get_route_map();
 
-		/**
-		 * Filters the array of available endpoints.
-		 *
-		 * @since 4.4.0
-		 *
-		 * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped
-		 *                         to an array of callbacks for the endpoint. These take the format
-		 *                         `'/path/regex' => array( $callback, $bitmask )` or
-		 *                         `'/path/regex' => array( array( $callback, $bitmask ).
-		 */
-		$endpoints = apply_filters( 'rest_endpoints', $this->endpoints );
+		if ( empty( $endpoints ) ) {
+			/**
+			 * Filters the array of available endpoints.
+			 *
+			 * @since 4.4.0
+			 *
+			 * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped
+			 *                         to an array of callbacks for the endpoint. These take the format
+			 *                         `'/path/regex' => array( $callback, $bitmask )` or
+			 *                         `'/path/regex' => array( array( $callback, $bitmask ).
+			 */
+			$endpoints = apply_filters( 'rest_endpoints', $this->endpoints );
 
-		// Normalise the endpoints.
-		$defaults = array(
-			'methods'       => '',
-			'accept_json'   => false,
-			'accept_raw'    => false,
-			'show_in_index' => true,
-			'args'          => array(),
-		);
+			// Normalise the endpoints.
+			$defaults = array(
+				'methods'       => '',
+				'accept_json'   => false,
+				'accept_raw'    => false,
+				'show_in_index' => true,
+				'args'          => array(),
+			);
 
-		foreach ( $endpoints as $route => &$handlers ) {
+			foreach ( $endpoints as $route => &$handlers ) {
 
-			if ( isset( $handlers['callback'] ) ) {
-				// Single endpoint, add one deeper.
-				$handlers = array( $handlers );
-			}
+				if ( isset( $handlers['callback'] ) ) {
+					// Single endpoint, add one deeper.
+					$handlers = array( $handlers );
+				}
 
-			if ( ! isset( $this->route_options[ $route ] ) ) {
-				$this->route_options[ $route ] = array();
-			}
+				if ( ! isset( $this->route_options[ $route ] ) ) {
+					$this->route_options[ $route ] = array();
+				}
 
-			foreach ( $handlers as $key => &$handler ) {
+				foreach ( $handlers as $key => &$handler ) {
 
-				if ( ! is_numeric( $key ) ) {
-					// Route option, move it to the options.
-					$this->route_options[ $route ][ $key ] = $handler;
-					unset( $handlers[ $key ] );
-					continue;
-				}
+					if ( ! is_numeric( $key ) ) {
+						// Route option, move it to the options.
+						$this->route_options[ $route ][ $key ] = $handler;
+						unset( $handlers[ $key ] );
+						continue;
+					}
 
-				$handler = wp_parse_args( $handler, $defaults );
+					$handler = wp_parse_args( $handler, $defaults );
 
-				// Allow comma-separated HTTP methods.
-				if ( is_string( $handler['methods'] ) ) {
-					$methods = explode( ',', $handler['methods'] );
-				} elseif ( is_array( $handler['methods'] ) ) {
-					$methods = $handler['methods'];
-				} else {
-					$methods = array();
-				}
+					// Allow comma-separated HTTP methods.
+					if ( is_string( $handler['methods'] ) ) {
+						$methods = explode( ',', $handler['methods'] );
+					} elseif ( is_array( $handler['methods'] ) ) {
+						$methods = $handler['methods'];
+					} else {
+						$methods = array();
+					}
 
-				$handler['methods'] = array();
+					$handler['methods'] = array();
 
-				foreach ( $methods as $method ) {
-					$method = strtoupper( trim( $method ) );
-					$handler['methods'][ $method ] = true;
+					foreach ( $methods as $method ) {
+						$method = strtoupper( trim( $method ) );
+						$handler['methods'][ $method ] = true;
+					}
 				}
 			}
+			$this->set_route_map( $endpoints );
 		}
 
 		return $endpoints;
