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

Updated correct template part location when creating patterns on save #413

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
56 changes: 56 additions & 0 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public static function add_templates_to_local( $export_type ) {

// If there are images in the template, add it as a pattern
if ( ! empty( $template_data->media ) ) {

// get all template parts to create template links
$template_parts = get_block_templates( array(), 'wp_template_part' );
$template_links = self::create_template_links( $template_parts );

// remove template strings from template data content
$template_data->content = str_replace( array_values( $template_links ), '', $template_data->content );

// If there is no templates folder, create it.
if ( ! is_dir( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' ) ) {
wp_mkdir_p( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' );
Expand All @@ -135,6 +143,9 @@ public static function add_templates_to_local( $export_type ) {
get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . $template_data->slug . '.php',
$pattern['content']
);

// Set the template structure to ensure the header and footer parts are included correctly
$template_data = self::restructure_template_links( $template_data, $template_parts, $template_links );
}

// Write the template content
Expand Down Expand Up @@ -187,4 +198,49 @@ public static function add_templates_to_local( $export_type ) {
Theme_Media::add_media_to_local( $template_data->media );
}
}

/**
* An array of strings that represent the template parts
*
* @param array $template_parts
* @return array $template_links
*
* @author Elliott Richmond <[email protected]>
*/
public static function create_template_links( $template_parts ) {
$template_links = array();
foreach ( $template_parts as $template_part ) {
$template_link_attributes = array(
'slug' => $template_part->slug,
);
$attributes_json = json_encode( $template_link_attributes, JSON_UNESCAPED_SLASHES );
$template_links[] = '<!-- wp:template-part ' . $attributes_json . ' /-->';
}
return $template_links;
}

/**
* Restructured template data content to include the template parts from the root folder
*
* @param object $template_data
* @param array $template_parts
* @param array $template_links
* @return object $template_data
*
* @author Elliott Richmond <[email protected]>
*/
public static function restructure_template_links( $template_data, $template_parts, $template_links ) {
foreach ( $template_parts as $k => $template_part ) {
switch ( $template_part->slug ) {
case 'header':
$prepend = $template_links[ $k ];
break;
case 'footer':
$append = $template_links[ $k ];
break;
}
}
$template_data->content = $prepend . $template_data->content . $append;
return $template_data;
}
}
Loading
Loading