Skip to content

Commit 9f57ccd

Browse files
authored
Save all patterns (#790)
* Save all patterns * remove wrapping funciton * Save all patterns * remove wrapping funciton * remove sync_status
1 parent 1783e34 commit 9f57ccd

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

includes/create-theme/theme-patterns.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public static function pattern_from_wp_block( $pattern_post ) {
2929
$pattern->slug = wp_get_theme()->get( 'TextDomain' ) . '/' . $pattern->name;
3030
$pattern_category_list = get_the_terms( $pattern->id, 'wp_pattern_category' );
3131
$pattern->categories = ! empty( $pattern_category_list ) ? join( ', ', wp_list_pluck( $pattern_category_list, 'name' ) ) : '';
32-
$pattern->sync_status = get_post_meta( $pattern->id, 'wp_pattern_sync_status', true );
3332
$pattern->content = <<<PHP
3433
<?php
3534
/**
@@ -71,7 +70,7 @@ public static function create_pattern_link( $attributes ) {
7170
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
7271
}
7372

74-
public static function replace_local_pattern_references( $pattern ) {
73+
public static function replace_local_pattern_references( $pattern, $options = null ) {
7574
// Find any references to pattern in templates
7675
$templates_to_update = array();
7776
$args = array(
@@ -89,7 +88,7 @@ public static function replace_local_pattern_references( $pattern ) {
8988
$templates_to_update = array_unique( $templates_to_update );
9089

9190
// Only update templates that reference the pattern
92-
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, null, $templates_to_update );
91+
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, $options, $templates_to_update );
9392

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

163-
// Check pattern is synced before adding to theme.
164-
if ( 'unsynced' !== $pattern->sync_status ) {
165-
// Check pattern name doesn't already exist before creating the file.
166-
$existing_patterns = glob( $patterns_dir . DIRECTORY_SEPARATOR . '*.php' );
167-
foreach ( $existing_patterns as $existing_pattern ) {
168-
if ( strpos( $existing_pattern, $pattern->name . '.php' ) !== false ) {
169-
$pattern_exists = true;
170-
}
171-
}
172-
173-
if ( $pattern_exists ) {
174-
return new WP_Error(
175-
'pattern_already_exists',
176-
sprintf(
177-
/* Translators: Pattern name. */
178-
__(
179-
'A pattern with this name already exists: "%s".',
180-
'create-block-theme'
181-
),
182-
$pattern->name
183-
)
184-
);
162+
// Check pattern name doesn't already exist before creating the file.
163+
$existing_patterns = glob( $patterns_dir . DIRECTORY_SEPARATOR . '*.php' );
164+
foreach ( $existing_patterns as $existing_pattern ) {
165+
if ( strpos( $existing_pattern, $pattern->name . '.php' ) !== false ) {
166+
$pattern_exists = true;
185167
}
168+
}
186169

187-
// Create the pattern file.
188-
file_put_contents(
189-
$patterns_dir . DIRECTORY_SEPARATOR . $pattern->name . '.php',
190-
$pattern->content
170+
if ( $pattern_exists ) {
171+
return new WP_Error(
172+
'pattern_already_exists',
173+
sprintf(
174+
/* Translators: Pattern name. */
175+
__(
176+
'A pattern with this name already exists: "%s".',
177+
'create-block-theme'
178+
),
179+
$pattern->name
180+
)
191181
);
182+
}
192183

193-
self::replace_local_pattern_references( $pattern );
184+
// Create the pattern file.
185+
file_put_contents(
186+
$patterns_dir . DIRECTORY_SEPARATOR . $pattern->name . '.php',
187+
$pattern->content
188+
);
194189

195-
// Remove it from the database to ensure that these patterns are loaded from the theme.
196-
wp_delete_post( $pattern->id, true );
197-
}
190+
self::replace_local_pattern_references( $pattern, $options );
191+
192+
// Remove it from the database to ensure that these patterns are loaded from the theme.
193+
wp_delete_post( $pattern->id, true );
198194
}
199195
}
200196
}

src/editor-sidebar/save-panel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ export const SaveThemePanel = () => {
158158
/>
159159
<CheckboxControl
160160
__nextHasNoMarginBottom
161-
label={ __( 'Save Synced Patterns', 'create-block-theme' ) }
161+
label={ __( 'Save Patterns', 'create-block-theme' ) }
162162
help={ __(
163-
'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.',
163+
'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.',
164164
'create-block-theme'
165165
) }
166166
checked={ preference.savePatterns }

0 commit comments

Comments
 (0)