diff --git a/lib/template-parts.php b/lib/template-parts.php index dde7189621d9f..b09bd83ba0b86 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -211,13 +211,28 @@ 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'] ) { + /** + * 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' ) ) { + $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; + } + // 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(); + $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 = 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 ) {