Skip to content
Merged
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
62 changes: 29 additions & 33 deletions includes/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static function pattern_from_wp_block( $pattern_post ) {
$pattern->slug = wp_get_theme()->get( 'TextDomain' ) . '/' . $pattern->name;
$pattern_category_list = get_the_terms( $pattern->id, 'wp_pattern_category' );
$pattern->categories = ! empty( $pattern_category_list ) ? join( ', ', wp_list_pluck( $pattern_category_list, 'name' ) ) : '';
$pattern->sync_status = get_post_meta( $pattern->id, 'wp_pattern_sync_status', true );
$pattern->content = <<<PHP
<?php
/**
Expand Down Expand Up @@ -71,7 +70,7 @@ public static function create_pattern_link( $attributes ) {
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
}

public static function replace_local_pattern_references( $pattern ) {
public static function replace_local_pattern_references( $pattern, $options = null ) {
// Find any references to pattern in templates
$templates_to_update = array();
$args = array(
Expand All @@ -89,7 +88,7 @@ public static function replace_local_pattern_references( $pattern ) {
$templates_to_update = array_unique( $templates_to_update );

// Only update templates that reference the pattern
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, null, $templates_to_update );
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, $options, $templates_to_update );

// List all template and pattern files in the theme
$base_dir = get_stylesheet_directory();
Expand Down Expand Up @@ -160,41 +159,38 @@ public static function add_patterns_to_theme( $options = null ) {
$pattern = self::prepare_pattern_for_export( $pattern, $options );
$pattern_exists = false;

// Check pattern is synced before adding to theme.
if ( 'unsynced' !== $pattern->sync_status ) {
// Check pattern name doesn't already exist before creating the file.
$existing_patterns = glob( $patterns_dir . DIRECTORY_SEPARATOR . '*.php' );
foreach ( $existing_patterns as $existing_pattern ) {
if ( strpos( $existing_pattern, $pattern->name . '.php' ) !== false ) {
$pattern_exists = true;
}
}

if ( $pattern_exists ) {
return new WP_Error(
'pattern_already_exists',
sprintf(
/* Translators: Pattern name. */
__(
'A pattern with this name already exists: "%s".',
'create-block-theme'
),
$pattern->name
)
);
// Check pattern name doesn't already exist before creating the file.
$existing_patterns = glob( $patterns_dir . DIRECTORY_SEPARATOR . '*.php' );
foreach ( $existing_patterns as $existing_pattern ) {
if ( strpos( $existing_pattern, $pattern->name . '.php' ) !== false ) {
$pattern_exists = true;
}
}

// Create the pattern file.
file_put_contents(
$patterns_dir . DIRECTORY_SEPARATOR . $pattern->name . '.php',
$pattern->content
if ( $pattern_exists ) {
return new WP_Error(
'pattern_already_exists',
sprintf(
/* Translators: Pattern name. */
__(
'A pattern with this name already exists: "%s".',
'create-block-theme'
),
$pattern->name
)
);
}

self::replace_local_pattern_references( $pattern );
// Create the pattern file.
file_put_contents(
$patterns_dir . DIRECTORY_SEPARATOR . $pattern->name . '.php',
$pattern->content
);

// Remove it from the database to ensure that these patterns are loaded from the theme.
wp_delete_post( $pattern->id, true );
}
self::replace_local_pattern_references( $pattern, $options );

// Remove it from the database to ensure that these patterns are loaded from the theme.
wp_delete_post( $pattern->id, true );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/editor-sidebar/save-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export const SaveThemePanel = () => {
/>
<CheckboxControl
__nextHasNoMarginBottom
label={ __( 'Save Synced Patterns', 'create-block-theme' ) }
label={ __( 'Save Patterns', 'create-block-theme' ) }
help={ __(
'Any synced patterns created in the Editor will be moved to the theme. Note that this will delete all synced patterns from the Editor and any references in templates will be made relative to the theme.',
'All patterns created in the Editor will be moved to the theme. Note that this will delete all patterns from the Editor and any references in templates will be made relative to the theme.',
'create-block-theme'
) }
checked={ preference.savePatterns }
Expand Down