Skip to content

Commit c72602a

Browse files
committed
Remove Pattern Saving Option
1 parent 0c2d483 commit c72602a

File tree

3 files changed

+8
-203
lines changed

3 files changed

+8
-203
lines changed

includes/class-create-block-theme-api.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,6 @@ function rest_save_theme( $request ) {
394394
CBT_Theme_Styles::clear_user_styles_customizations();
395395
}
396396

397-
if ( isset( $options['savePatterns'] ) && true === $options['savePatterns'] ) {
398-
$response = CBT_Theme_Patterns::add_patterns_to_theme( $options );
399-
400-
if ( is_wp_error( $response ) ) {
401-
return $response;
402-
}
403-
}
404-
405397
wp_get_theme()->cache_delete();
406398

407399
return new WP_REST_Response(

includes/create-theme/theme-patterns.php

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,6 @@ public static function pattern_from_template( $template, $new_slug = null ) {
2121
);
2222
}
2323

24-
public static function pattern_from_wp_block( $pattern_post ) {
25-
$pattern = new stdClass();
26-
$pattern->id = $pattern_post->ID;
27-
$pattern->title = $pattern_post->post_title;
28-
$pattern->name = sanitize_title_with_dashes( $pattern_post->post_title );
29-
$pattern->slug = wp_get_theme()->get( 'TextDomain' ) . '/' . $pattern->name;
30-
$pattern_category_list = get_the_terms( $pattern->id, 'wp_pattern_category' );
31-
$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 );
33-
$pattern->content = <<<PHP
34-
<?php
35-
/**
36-
* Title: {$pattern->title}
37-
* Slug: {$pattern->slug}
38-
* Categories: {$pattern->categories}
39-
*/
40-
?>
41-
{$pattern_post->post_content}
42-
PHP;
43-
44-
return $pattern;
45-
}
46-
4724
public static function escape_alt_for_pattern( $html ) {
4825
if ( empty( $html ) ) {
4926
return $html;
@@ -71,132 +48,4 @@ public static function create_pattern_link( $attributes ) {
7148
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
7249
}
7350

74-
public static function replace_local_pattern_references( $pattern ) {
75-
// Find any references to pattern in templates
76-
$templates_to_update = array();
77-
$args = array(
78-
'post_type' => array( 'wp_template', 'wp_template_part' ),
79-
'posts_per_page' => -1,
80-
's' => 'wp:block {"ref":' . $pattern->id . '}',
81-
);
82-
$find_pattern_refs = new WP_Query( $args );
83-
if ( $find_pattern_refs->have_posts() ) {
84-
foreach ( $find_pattern_refs->posts as $post ) {
85-
$slug = $post->post_name;
86-
array_push( $templates_to_update, $slug );
87-
}
88-
}
89-
$templates_to_update = array_unique( $templates_to_update );
90-
91-
// Only update templates that reference the pattern
92-
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, $options, $templates_to_update );
93-
94-
// List all template and pattern files in the theme
95-
$base_dir = get_stylesheet_directory();
96-
$patterns = glob( $base_dir . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . '*.php' );
97-
$templates = glob( $base_dir . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . '*.html' );
98-
$template_parts = glob( $base_dir . DIRECTORY_SEPARATOR . 'template-parts' . DIRECTORY_SEPARATOR . '*.html' );
99-
100-
// Replace references to the local patterns in the theme
101-
foreach ( array_merge( $patterns, $templates, $template_parts ) as $file ) {
102-
$file_content = file_get_contents( $file );
103-
$file_content = str_replace( 'wp:block {"ref":' . $pattern->id . '}', 'wp:pattern {"slug":"' . $pattern->slug . '"}', $file_content );
104-
file_put_contents( $file, $file_content );
105-
}
106-
107-
CBT_Theme_Templates::clear_user_templates_customizations();
108-
CBT_Theme_Templates::clear_user_template_parts_customizations();
109-
}
110-
111-
public static function prepare_pattern_for_export( $pattern, $options = null ) {
112-
if ( ! $options ) {
113-
$options = array(
114-
'localizeText' => false,
115-
'removeNavRefs' => true,
116-
'localizeImages' => true,
117-
);
118-
}
119-
120-
$pattern = CBT_Theme_Templates::eliminate_environment_specific_content( $pattern, $options );
121-
122-
if ( array_key_exists( 'localizeText', $options ) && $options['localizeText'] ) {
123-
$pattern = CBT_Theme_Templates::escape_text_in_template( $pattern );
124-
}
125-
126-
if ( array_key_exists( 'localizeImages', $options ) && $options['localizeImages'] ) {
127-
$pattern = CBT_Theme_Media::make_template_images_local( $pattern );
128-
129-
// Write the media assets if there are any
130-
if ( $pattern->media ) {
131-
CBT_Theme_Media::add_media_to_local( $pattern->media );
132-
}
133-
}
134-
135-
return $pattern;
136-
}
137-
138-
/**
139-
* Copy the local patterns as well as any media to the theme filesystem.
140-
*/
141-
public static function add_patterns_to_theme( $options = null ) {
142-
$base_dir = get_stylesheet_directory();
143-
$patterns_dir = $base_dir . DIRECTORY_SEPARATOR . 'patterns';
144-
145-
$pattern_query = new WP_Query(
146-
array(
147-
'post_type' => 'wp_block',
148-
'posts_per_page' => -1,
149-
)
150-
);
151-
152-
if ( $pattern_query->have_posts() ) {
153-
// If there is no patterns folder, create it.
154-
if ( ! is_dir( $patterns_dir ) ) {
155-
wp_mkdir_p( $patterns_dir );
156-
}
157-
158-
foreach ( $pattern_query->posts as $pattern ) {
159-
$pattern = self::pattern_from_wp_block( $pattern );
160-
$pattern = self::prepare_pattern_for_export( $pattern, $options );
161-
$pattern_exists = false;
162-
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-
);
185-
}
186-
187-
// Create the pattern file.
188-
$pattern_file = $patterns_dir . $pattern->name . '.php';
189-
file_put_contents(
190-
$patterns_dir . DIRECTORY_SEPARATOR . $pattern->name . '.php',
191-
$pattern->content
192-
);
193-
194-
self::replace_local_pattern_references( $pattern );
195-
196-
// Remove it from the database to ensure that these patterns are loaded from the theme.
197-
wp_delete_post( $pattern->id, true );
198-
}
199-
}
200-
}
201-
}
20251
}

src/editor-sidebar/save-panel.js

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const SaveThemePanel = () => {
3434
saveTemplates: _preference?.saveTemplates ?? true,
3535
processOnlySavedTemplates:
3636
_preference?.processOnlySavedTemplates ?? true,
37-
savePatterns: _preference?.savePatterns ?? true,
3837
saveFonts: _preference?.saveFonts ?? true,
3938
removeNavRefs: _preference?.removeNavRefs ?? false,
4039
localizeText: _preference?.localizeText ?? false,
@@ -70,21 +69,8 @@ export const SaveThemePanel = () => {
7069
)
7170
);
7271

73-
const searchParams = new URLSearchParams(
74-
window?.location?.search
75-
);
76-
// If user is editing a pattern and savePatterns is true, redirect back to the patterns page.
77-
if (
78-
preference.savePatterns &&
79-
searchParams.get( 'postType' ) === 'wp_block' &&
80-
searchParams.get( 'postId' )
81-
) {
82-
window.location =
83-
'/wp-admin/site-editor.php?postType=wp_block';
84-
} else {
85-
// If user is not editing a pattern, reload the editor.
86-
window.location.reload();
87-
}
72+
// reload the editor.
73+
window.location.reload();
8874
} )
8975
.catch( ( error ) => {
9076
const errorMessage =
@@ -155,30 +141,16 @@ export const SaveThemePanel = () => {
155141
handleTogglePreference( 'processOnlySavedTemplates' )
156142
}
157143
/>
158-
<CheckboxControl
159-
__nextHasNoMarginBottom
160-
label={ __( 'Save Synced Patterns', 'create-block-theme' ) }
161-
help={ __(
162-
'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-
'create-block-theme'
164-
) }
165-
checked={ preference.savePatterns }
166-
onChange={ () => handleTogglePreference( 'savePatterns' ) }
167-
/>
168144
<CheckboxControl
169145
__nextHasNoMarginBottom
170146
label={ __( 'Localize Text', 'create-block-theme' ) }
171147
help={ __(
172148
'Any text in a template or pattern will be localized in a pattern.',
173149
'create-block-theme'
174150
) }
175-
disabled={
176-
! preference.saveTemplates && ! preference.savePatterns
177-
}
151+
disabled={ ! preference.saveTemplates }
178152
checked={
179-
( preference.saveTemplates ||
180-
preference.savePatterns ) &&
181-
preference.localizeText
153+
preference.saveTemplates && preference.localizeText
182154
}
183155
onChange={ () => handleTogglePreference( 'localizeText' ) }
184156
/>
@@ -189,13 +161,9 @@ export const SaveThemePanel = () => {
189161
'Any images in a template or pattern will be copied to a local /assets folder and referenced from there via a pattern.',
190162
'create-block-theme'
191163
) }
192-
disabled={
193-
! preference.saveTemplates && ! preference.savePatterns
194-
}
164+
disabled={ ! preference.saveTemplates }
195165
checked={
196-
( preference.saveTemplates ||
197-
preference.savePatterns ) &&
198-
preference.localizeImages
166+
preference.saveTemplates && preference.localizeImages
199167
}
200168
onChange={ () =>
201169
handleTogglePreference( 'localizeImages' )
@@ -211,13 +179,9 @@ export const SaveThemePanel = () => {
211179
'Remove Navigation Refs from the theme returning your navigation to the default state.',
212180
'create-block-theme'
213181
) }
214-
disabled={
215-
! preference.saveTemplates && ! preference.savePatterns
216-
}
182+
disabled={ ! preference.saveTemplates }
217183
checked={
218-
( preference.saveTemplates ||
219-
preference.savePatterns ) &&
220-
preference.removeNavRefs
184+
preference.saveTemplates && preference.removeNavRefs
221185
}
222186
onChange={ () => handleTogglePreference( 'removeNavRefs' ) }
223187
/>

0 commit comments

Comments
 (0)