Skip to content

Commit

Permalink
Fix Template Part auto-drafting to support sub-directories. (#25063)
Browse files Browse the repository at this point in the history
* refactor for nested files

* merge arrays outside of function

* lint wants function doc
  • Loading branch information
Addison-Stavlo authored Sep 4, 2020
1 parent 8ba0a42 commit 959542c
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 959542c

Please sign in to comment.