From 699853bbc485f8ddcb7fce21f4f5245ee599c6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 19 Apr 2022 09:26:20 +0000 Subject: [PATCH] REST API: Don't load remote patterns twice in the controller This PR aims to fix an issue with fetching remote patterns multiple times inside the WP_REST_Block_Patterns_Controller::get_items method. Follow-up [53152]. Props antonvlasenko. See #55567. git-svn-id: https://develop.svn.wordpress.org/trunk@53209 602fd350-edb4-49c9-b593-d223f7449a82 --- ...lass-wp-rest-block-patterns-controller.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php index 62d69bcd69..1f1bf679c8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php @@ -16,6 +16,14 @@ */ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { + /** + * Defines whether remote patterns should be loaded. + * + * @since 6.0.0 + * @var bool + */ + private $remote_patterns_loaded; + /** * Constructs the controller. * @@ -81,10 +89,14 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_items( $request ) { - // Load block patterns from w.org. - _load_remote_block_patterns(); // Patterns with the `core` keyword. - _load_remote_featured_patterns(); // Patterns in the `featured` category. - _register_remote_theme_patterns(); // Patterns requested by current theme. + if ( ! $this->remote_patterns_loaded ) { + // Load block patterns from w.org. + _load_remote_block_patterns(); // Patterns with the `core` keyword. + _load_remote_featured_patterns(); // Patterns in the `featured` category. + _register_remote_theme_patterns(); // Patterns requested by current theme. + + $this->remote_patterns_loaded = true; + } $response = array(); $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();