From 8401ea1925b4a1ea882aa522a6d7d688aeb02258 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 3 Sep 2020 20:51:33 -0400 Subject: [PATCH 1/3] refactor for nested files --- lib/template-parts.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index dde7189621d9f..9dc38b9e5c08c 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -212,12 +212,20 @@ function filter_rest_wp_template_part_query( $args, $request ) { // Ensure auto-drafts of all theme supplied template parts are created. if ( wp_get_theme()->get( 'TextDomain' ) === $request['theme'] ) { // Get file paths for all theme supplied template parts. - $template_part_files = glob( get_stylesheet_directory() . '/block-template-parts/*.html' ); - $template_part_files = is_array( $template_part_files ) ? $template_part_files : array(); + function get_template_part_paths( $base_directory, $path_list = array() ) { + if ( file_exists( $base_directory . '/block-template-parts' ) ) { + $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/block-template-parts' ) ); + $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); + foreach ( $nested_html_files as $path => $file ) { + $path_list[] = $path; + } + } + return $path_list; + } + + $template_part_files = get_template_part_paths( get_stylesheet_directory() ); if ( is_child_theme() ) { - $child_template_part_files = glob( get_template_directory() . '/block-template-parts/*.html' ); - $child_template_part_files = is_array( $child_template_part_files ) ? $child_template_part_files : array(); - $template_part_files = array_merge( $template_part_files, $child_template_part_files ); + $template_part_files = get_template_part_paths( get_template_directory(), $template_part_files ); } // Build and save each template part. foreach ( $template_part_files as $template_part_file ) { From c2ba442b2391c067a36237cefeb1a082f57e181c Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 3 Sep 2020 21:31:29 -0400 Subject: [PATCH 2/3] merge arrays outside of function --- lib/template-parts.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 9dc38b9e5c08c..3080e4f82366b 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -212,7 +212,8 @@ function filter_rest_wp_template_part_query( $args, $request ) { // Ensure auto-drafts of all theme supplied template parts are created. if ( wp_get_theme()->get( 'TextDomain' ) === $request['theme'] ) { // Get file paths for all theme supplied template parts. - function get_template_part_paths( $base_directory, $path_list = array() ) { + function get_template_part_paths( $base_directory ) { + $path_list = array(); if ( file_exists( $base_directory . '/block-template-parts' ) ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/block-template-parts' ) ); $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); @@ -225,7 +226,7 @@ function get_template_part_paths( $base_directory, $path_list = array() ) { $template_part_files = get_template_part_paths( get_stylesheet_directory() ); if ( is_child_theme() ) { - $template_part_files = get_template_part_paths( get_template_directory(), $template_part_files ); + $template_part_files = array_merge( $template_part_files, get_template_part_paths( get_template_directory() ) ); } // Build and save each template part. foreach ( $template_part_files as $template_part_file ) { From 21c803b46a92e343ea63a24baaee42fe084d305a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 3 Sep 2020 21:42:11 -0400 Subject: [PATCH 3/3] lint wants function doc --- lib/template-parts.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 3080e4f82366b..b09bd83ba0b86 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -211,7 +211,12 @@ function filter_rest_wp_template_part_query( $args, $request ) { // Ensure auto-drafts of all theme supplied template parts are created. if ( wp_get_theme()->get( 'TextDomain' ) === $request['theme'] ) { - // Get file paths for all theme supplied template parts. + /** + * Finds all nested template part file paths in a theme's directory. + * + * @param string $base_directory The theme's file path. + * @return array $path_list A list of paths to all template part files. + */ function get_template_part_paths( $base_directory ) { $path_list = array(); if ( file_exists( $base_directory . '/block-template-parts' ) ) { @@ -224,6 +229,7 @@ function get_template_part_paths( $base_directory ) { return $path_list; } + // Get file paths for all theme supplied template parts. $template_part_files = get_template_part_paths( get_stylesheet_directory() ); if ( is_child_theme() ) { $template_part_files = array_merge( $template_part_files, get_template_part_paths( get_template_directory() ) );