Skip to content

Commit

Permalink
setting the right theme slug for newly created patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Apr 4, 2023
1 parent e8071e5 commit b6b2d86
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 58 deletions.
41 changes: 0 additions & 41 deletions admin/create-theme/theme-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,45 +189,4 @@ static function clean_serialized_markup( $markup ) {
return $markup;
}

// update pattern and template-parts theme attribute
public static function update_patterns_and_templates_theme_attribute( $template, $theme_slug ) {
$new_content = $template->content;
$template_blocks = parse_blocks( $template->content );

$blocks = self::update_blocks_theme_attribute( $template_blocks, $theme_slug );
$blocks = serialize_blocks( $blocks );

$template->content = self::clean_serialized_markup( $blocks );
return $template;
}

static function update_blocks_theme_attribute( $nested_blocks, $theme_slug ) {
$new_blocks = array();
foreach ( $nested_blocks as $block ) {
$inner_blocks = $block['innerBlocks'];
switch ( $block['blockName'] ) {
case 'core/pattern':
case 'core/template-part':
$block = self::update_block_theme_attribute( $block, $theme_slug );
break;
}
// recursive call for inner blocks
if ( ! empty( $block['innerBlocks'] ) ) {
$block['innerBlocks'] = self::update_blocks_theme_attribute( $inner_blocks, $theme_slug );
}
$new_blocks[] = $block;
}
return $new_blocks;
}

static function update_block_theme_attribute( $block, $theme_slug ) {
if ( 'core/pattern' === $block['blockName'] || 'core/template-part' === $block['blockName'] ) {
// Adds the theme attribute to the block only if is set, otherwise it won't be added
if ( ! empty( $block['attrs']['theme'] ) ) {
$block['attrs']['theme'] = $theme_slug;
}
}
return $block;
}

}
6 changes: 3 additions & 3 deletions admin/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

class Theme_Patterns {
public static function pattern_from_template( $template ) {
$theme_slug = wp_get_theme()->get( 'TextDomain' );
public static function pattern_from_template( $template, $new_slug = null ) {
$theme_slug = $new_slug ? $new_slug : wp_get_theme()->get( 'TextDomain' );
$pattern_slug = $theme_slug . '/' . $template->slug;
$pattern_content = (
'<?php
Expand Down Expand Up @@ -64,7 +64,7 @@ static function escape_text_for_pattern( $text ) {

public static function create_pattern_link( $attributes ) {
$block_attributes = array_filter( $attributes );
$attributes_json = json_encode( $block_attributes );
$attributes_json = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES );
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
}
}
8 changes: 2 additions & 6 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public static function add_templates_to_local( $export_type ) {

foreach ( $theme_templates->templates as $template ) {
$template_data = Theme_Blocks::make_template_images_local( $template );
$template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $theme_slug );

// If there are images in the template, add it as a pattern
if ( ! empty( $template_data->media ) ) {
Expand All @@ -125,8 +124,7 @@ public static function add_templates_to_local( $export_type ) {
// If there are external images, add it as a pattern
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
'theme' => $new_slug,
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

Expand Down Expand Up @@ -155,7 +153,6 @@ public static function add_templates_to_local( $export_type ) {

foreach ( $theme_templates->parts as $template_part ) {
$template_data = Theme_Blocks::make_template_images_local( $template_part );
$template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $theme_slug );

// If there are images in the template, add it as a pattern
if ( ! empty( $template_data->media ) ) {
Expand All @@ -167,8 +164,7 @@ public static function add_templates_to_local( $export_type ) {
// If there are external images, add it as a pattern
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
'theme' => $new_slug,
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

Expand Down
12 changes: 4 additions & 8 deletions admin/create-theme/theme-zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {

foreach ( $theme_templates->templates as $template ) {
$template_data = Theme_Blocks::make_template_images_local( $template );
$template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $new_slug );

// If there are images in the template, add it as a pattern
if ( count( $template_data->media ) > 0 ) {
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
'theme' => $new_slug,
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

Expand All @@ -132,14 +130,12 @@ public static function add_templates_to_zip( $zip, $export_type, $new_slug ) {

foreach ( $theme_templates->parts as $template_part ) {
$template_data = Theme_Blocks::make_template_images_local( $template_part );
$template_data = Theme_Blocks::update_patterns_and_templates_theme_attribute( $template_data, $new_slug );

// If there are images in the template, add it as a pattern
if ( count( $template_data->media ) > 0 ) {
$pattern = Theme_Patterns::pattern_from_template( $template_data );
$pattern = Theme_Patterns::pattern_from_template( $template_data, $new_slug );
$pattern_link_attributes = array(
'slug' => $pattern['slug'],
'theme' => $new_slug,
'slug' => $pattern['slug'],
);
$template_data->content = Theme_Patterns::create_pattern_link( $pattern_link_attributes );

Expand Down

0 comments on commit b6b2d86

Please sign in to comment.