Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-drafting for theme supplied template parts. #23254

Merged
merged 7 commits into from
Jun 19, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,34 @@ function filter_rest_wp_template_part_query( $args, $request ) {
'value' => $request['theme'],
);

// 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();
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 );
}
// Build and save each template part.
foreach ( $template_part_files as $template_part_file ) {
$content = file_get_contents( $template_part_file );
// Infer slug from filepath.
$slug = substr(
$template_part_file,
// Starting position of slug.
strpos( $template_part_file, 'block-template-parts/' ) + 21,
// Subtract ending '.html'.
-5
);
// Wrap content with the template part block, parse, and create auto-draft.
$template_part_string = '<!-- wp:template-part {"slug":"' . $slug . '","theme":"' . wp_get_theme()->get( 'TextDomain' ) . '"} -->' . $content . '<!-- /wp:template-part -->';
$template_part_block = parse_blocks( $template_part_string )[0];
create_auto_draft_for_template_part_block( $template_part_block );
}
Addison-Stavlo marked this conversation as resolved.
Show resolved Hide resolved
};

$args['meta_query'] = $meta_query;
}

Expand Down