From f60619b3967cb0b8e99905ef0e33a2bb769dbba6 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 24 Jan 2024 18:07:16 +0000 Subject: [PATCH 01/11] pattern lazy loding. --- src/wp-includes/block-patterns.php | 6 ++---- .../class-wp-block-patterns-registry.php | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 66bdfd68e7caa..7bf766ac85bd0 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -378,12 +378,10 @@ function _register_theme_block_patterns() { } // The actual pattern content is the output of the file. - ob_start(); - include $file_path; - $pattern_data['content'] = ob_get_clean(); - if ( ! $pattern_data['content'] ) { + if ( ! file_exists( $file_path ) ) { continue; } + $pattern_data['file_path'] = $file_path; // Translate the pattern metadata. // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index a11bac06bef02..c821c12d7313d 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -101,7 +101,7 @@ public function register( $pattern_name, $pattern_properties ) { return false; } - if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { + if ( ! isset( $pattern_properties['file_path'] ) || ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { _doing_it_wrong( __METHOD__, __( 'Pattern content must be a string.' ), @@ -191,6 +191,13 @@ public function get_registered( $pattern_name ) { } $pattern = $this->registered_patterns[ $pattern_name ]; + if ( ! $pattern['content'] && isset( $pattern['file_path'] ) ) { + ob_start(); + include $pattern['file_path']; + $pattern['content'] = ob_get_clean(); + $this->registered_patterns[ $pattern_name ]['content'] = $pattern['content']; + } + $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() ); return $pattern; @@ -213,6 +220,16 @@ public function get_all_registered( $outside_init_only = false ) { ); $hooked_blocks = get_hooked_blocks(); foreach ( $patterns as $index => $pattern ) { + if ( ! $pattern['content'] && isset( $pattern['file_path'] ) ) { + ob_start(); + include $pattern['file_path']; + $pattern['content'] = ob_get_clean(); + if ( $outside_init_only ) { + $this->registered_patterns_outside_init[ $index ]['content'] = $pattern['content']; + } else { + $this->registered_patterns[ $index ]['content'] = $pattern['content']; + } + } $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); } return $patterns; From 812719d971e33ef7f7f4dce4aa651a40deba446f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 24 Jan 2024 19:08:11 +0000 Subject: [PATCH 02/11] error fix --- src/wp-includes/block-patterns.php | 4 ---- src/wp-includes/class-wp-block-patterns-registry.php | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 7bf766ac85bd0..1af47d39aca54 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -377,10 +377,6 @@ function _register_theme_block_patterns() { continue; } - // The actual pattern content is the output of the file. - if ( ! file_exists( $file_path ) ) { - continue; - } $pattern_data['file_path'] = $file_path; // Translate the pattern metadata. diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index c821c12d7313d..1e5d98976dbf2 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -101,7 +101,7 @@ public function register( $pattern_name, $pattern_properties ) { return false; } - if ( ! isset( $pattern_properties['file_path'] ) || ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { + if ( ! isset( $pattern_properties['file_path'] ) && ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) ) { _doing_it_wrong( __METHOD__, __( 'Pattern content must be a string.' ), @@ -191,7 +191,7 @@ public function get_registered( $pattern_name ) { } $pattern = $this->registered_patterns[ $pattern_name ]; - if ( ! $pattern['content'] && isset( $pattern['file_path'] ) ) { + if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { ob_start(); include $pattern['file_path']; $pattern['content'] = ob_get_clean(); From 4b083c47bb822585e35cf25c9bfe3fbd07244435 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 24 Jan 2024 22:24:50 +0000 Subject: [PATCH 03/11] fix error --- src/wp-includes/class-wp-block-patterns-registry.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 1e5d98976dbf2..4efbde96cd3eb 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -190,11 +190,11 @@ public function get_registered( $pattern_name ) { return null; } - $pattern = $this->registered_patterns[ $pattern_name ]; + $pattern = $this->registered_patterns[ $pattern_name ]; if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { ob_start(); include $pattern['file_path']; - $pattern['content'] = ob_get_clean(); + $pattern['content'] = ob_get_clean(); $this->registered_patterns[ $pattern_name ]['content'] = $pattern['content']; } @@ -220,7 +220,7 @@ public function get_all_registered( $outside_init_only = false ) { ); $hooked_blocks = get_hooked_blocks(); foreach ( $patterns as $index => $pattern ) { - if ( ! $pattern['content'] && isset( $pattern['file_path'] ) ) { + if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { ob_start(); include $pattern['file_path']; $pattern['content'] = ob_get_clean(); From 89f68b0dd2c8db3e87b0bd887baef1a892fe6158 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 25 Jan 2024 05:39:53 +0000 Subject: [PATCH 04/11] simplyfy condition --- .../class-wp-block-patterns-registry.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 4efbde96cd3eb..59b6b97b0ff80 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -101,13 +101,15 @@ public function register( $pattern_name, $pattern_properties ) { return false; } - if ( ! isset( $pattern_properties['file_path'] ) && ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) ) { - _doing_it_wrong( - __METHOD__, - __( 'Pattern content must be a string.' ), - '5.5.0' - ); - return false; + if ( ! isset( $pattern_properties['file_path'] ) ) { + if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { + _doing_it_wrong( + __METHOD__, + __( 'Pattern content must be a string.' ), + '5.5.0' + ); + return false; + } } $pattern = array_merge( From 221e735c88d5746f762822604f5acc300d95b2f0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 25 Jan 2024 06:12:43 +0000 Subject: [PATCH 05/11] Skip if name not present --- src/wp-includes/class-wp-block-patterns-registry.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 59b6b97b0ff80..d285a121a6f6c 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -222,6 +222,9 @@ public function get_all_registered( $outside_init_only = false ) { ); $hooked_blocks = get_hooked_blocks(); foreach ( $patterns as $index => $pattern ) { + if ( ! isset( $pattern['name'] ) ) { + continue; + } if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { ob_start(); include $pattern['file_path']; From 55658f402a796546fd6fbee40191ac699b61ec0e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 25 Jan 2024 06:27:10 +0000 Subject: [PATCH 06/11] temprary fix --- src/wp-includes/class-wp-block-patterns-registry.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index d285a121a6f6c..0a4ae6fc9ccd6 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -223,6 +223,12 @@ public function get_all_registered( $outside_init_only = false ) { $hooked_blocks = get_hooked_blocks(); foreach ( $patterns as $index => $pattern ) { if ( ! isset( $pattern['name'] ) ) { + unset( $patterns[ $index ] ); + if ( $outside_init_only ) { + unset( $this->registered_patterns_outside_init[ $index ] ); + } else { + unset( $this->registered_patterns[ $index ] ); + } continue; } if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { From 1b4908f093aa017b718c7d003e8dc532c27c2770 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 9 Feb 2024 13:17:24 +0000 Subject: [PATCH 07/11] introduce load_content_from_file_path --- .../class-wp-block-patterns-registry.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 0a4ae6fc9ccd6..e1e75297327e2 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -179,6 +179,20 @@ private function prepare_content( $pattern, $hooked_blocks ) { return $content; } + /** + * Retrieves the content of a block pattern from a file path. + * + * @since 6.5.0 + * + * @param string $file_path The file path to the block pattern. + * @return string The content of the block pattern. + */ + private function load_content_from_file_path( $file_path ) { + ob_start(); + include $file_path; + return ob_get_clean(); + } + /** * Retrieves an array containing the properties of a registered block pattern. * @@ -194,9 +208,7 @@ public function get_registered( $pattern_name ) { $pattern = $this->registered_patterns[ $pattern_name ]; if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { - ob_start(); - include $pattern['file_path']; - $pattern['content'] = ob_get_clean(); + $pattern['content'] = $this->load_content_from_file_path( $pattern['file_path'] ); $this->registered_patterns[ $pattern_name ]['content'] = $pattern['content']; } @@ -232,9 +244,7 @@ public function get_all_registered( $outside_init_only = false ) { continue; } if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { - ob_start(); - include $pattern['file_path']; - $pattern['content'] = ob_get_clean(); + $pattern['content'] = $this->load_content_from_file_path( $pattern['file_path'] ); if ( $outside_init_only ) { $this->registered_patterns_outside_init[ $index ]['content'] = $pattern['content']; } else { From 759ae1626041a7d5ea7d597a7415478133eaaec9 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 14 Feb 2024 13:51:36 +0000 Subject: [PATCH 08/11] shift array values to the end --- .../class-wp-block-patterns-registry.php | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index e1e75297327e2..e37f260351177 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -227,33 +227,26 @@ public function get_registered( $pattern_name ) { * and per style. */ public function get_all_registered( $outside_init_only = false ) { - $patterns = array_values( - $outside_init_only + $patterns = $outside_init_only ? $this->registered_patterns_outside_init - : $this->registered_patterns - ); + : $this->registered_patterns; $hooked_blocks = get_hooked_blocks(); + foreach ( $patterns as $index => $pattern ) { - if ( ! isset( $pattern['name'] ) ) { - unset( $patterns[ $index ] ); - if ( $outside_init_only ) { - unset( $this->registered_patterns_outside_init[ $index ] ); - } else { - unset( $this->registered_patterns[ $index ] ); - } - continue; - } if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { $pattern['content'] = $this->load_content_from_file_path( $pattern['file_path'] ); if ( $outside_init_only ) { $this->registered_patterns_outside_init[ $index ]['content'] = $pattern['content']; + unset( $this->registered_patterns_outside_init[ $index ]['file_path'] ); } else { $this->registered_patterns[ $index ]['content'] = $pattern['content']; + unset( $this->registered_patterns[ $index ]['file_path'] ); } } $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); } - return $patterns; + + return array_values( $patterns ); } /** From e9bf6de7986ba1ae3e9834d897ed21b816a7c9a5 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 16 Feb 2024 13:14:09 +0000 Subject: [PATCH 09/11] get_content --- .../class-wp-block-patterns-registry.php | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index e37f260351177..d66f1f373f207 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -180,17 +180,27 @@ private function prepare_content( $pattern, $hooked_blocks ) { } /** - * Retrieves the content of a block pattern from a file path. + * Retrieves the content of a registered block pattern. * - * @since 6.5.0 + * @since 5.5.0 * - * @param string $file_path The file path to the block pattern. + * @param string $pattern_name Block pattern name including namespace. + * @param bool $outside_init_only Return only patterns registered outside the `init` action. * @return string The content of the block pattern. */ - private function load_content_from_file_path( $file_path ) { - ob_start(); - include $file_path; - return ob_get_clean(); + private function get_content( $pattern_name, $outside_init_only = false ) { + if ( $outside_init_only ) { + $patterns = &$this->registered_patterns_outside_init; + } else { + $patterns = &$this->registered_patterns; + } + if ( ! isset( $patterns[ $pattern_name ]['content'] ) && isset( $patterns[ $pattern_name ]['file_path'] ) ) { + ob_start(); + include $patterns[ $pattern_name ]['file_path']; + $patterns[ $pattern_name ]['content'] = ob_get_clean(); + unset( $patterns[ $pattern_name ]['file_path'] ); + } + return $patterns[ $pattern_name ]['content']; } /** @@ -206,12 +216,8 @@ public function get_registered( $pattern_name ) { return null; } - $pattern = $this->registered_patterns[ $pattern_name ]; - if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { - $pattern['content'] = $this->load_content_from_file_path( $pattern['file_path'] ); - $this->registered_patterns[ $pattern_name ]['content'] = $pattern['content']; - } - + $pattern = $this->registered_patterns[ $pattern_name ]; + $pattern['content'] = $this->get_content( $pattern_name ); $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() ); return $pattern; @@ -233,16 +239,7 @@ public function get_all_registered( $outside_init_only = false ) { $hooked_blocks = get_hooked_blocks(); foreach ( $patterns as $index => $pattern ) { - if ( ! isset( $pattern['content'] ) && isset( $pattern['file_path'] ) ) { - $pattern['content'] = $this->load_content_from_file_path( $pattern['file_path'] ); - if ( $outside_init_only ) { - $this->registered_patterns_outside_init[ $index ]['content'] = $pattern['content']; - unset( $this->registered_patterns_outside_init[ $index ]['file_path'] ); - } else { - $this->registered_patterns[ $index ]['content'] = $pattern['content']; - unset( $this->registered_patterns[ $index ]['file_path'] ); - } - } + $pattern['content'] = $this->get_content( $pattern['name'], $outside_init_only ); $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); } From db636eaf49aa825cf49bc2ece2218ba1c446b6bf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 16 Feb 2024 13:14:55 +0000 Subject: [PATCH 10/11] version correction --- src/wp-includes/class-wp-block-patterns-registry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index d66f1f373f207..038c0121abc25 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -182,7 +182,7 @@ private function prepare_content( $pattern, $hooked_blocks ) { /** * Retrieves the content of a registered block pattern. * - * @since 5.5.0 + * @since 6.5.0 * * @param string $pattern_name Block pattern name including namespace. * @param bool $outside_init_only Return only patterns registered outside the `init` action. From 08acde9280b052d8ce989f0699dd28f470180f19 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:17:26 +0000 Subject: [PATCH 11/11] Update src/wp-includes/class-wp-block-patterns-registry.php Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-block-patterns-registry.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 038c0121abc25..1402819c848ff 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -184,8 +184,8 @@ private function prepare_content( $pattern, $hooked_blocks ) { * * @since 6.5.0 * - * @param string $pattern_name Block pattern name including namespace. - * @param bool $outside_init_only Return only patterns registered outside the `init` action. + * @param string $pattern_name Block pattern name including namespace. + * @param bool $outside_init_only Optional. Return only patterns registered outside the `init` action. Default false. * @return string The content of the block pattern. */ private function get_content( $pattern_name, $outside_init_only = false ) {