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

Try: Add synced patterns to theme on save #675

Merged
merged 25 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
30c683f
Add savePatterns option
mikachan Jun 27, 2024
06cbb20
Add basic add_patterns_to_theme function
mikachan Jun 27, 2024
7e13fed
Switch order of save options
mikachan Jun 27, 2024
4bdd3ed
Add prepare_pattern_for_export function
mikachan Jun 27, 2024
f12304f
Copy media to theme filesystem
mikachan Jun 27, 2024
da1b2aa
Fix pattern slug
mikachan Jun 27, 2024
4995876
Add replace_local_pattern_references
mikachan Jun 27, 2024
b538305
Tidy up pattern_from_wp_block
mikachan Jun 29, 2024
4dfb09e
Add pattern categories
mikachan Jul 4, 2024
003f275
Default to empty string for categories list
mikachan Jul 4, 2024
e74d5e8
Potentially save pattern sync status
mikachan Jul 10, 2024
fd2a550
Merge branch 'trunk' into try/export-patterns
mikachan Jul 31, 2024
2b812e2
Merge branch 'trunk' into try/export-patterns
mikachan Aug 1, 2024
b3e8b78
Merge branch 'trunk' into try/export-patterns
mikachan Aug 14, 2024
e6d6570
Refactor PHP content of pattern_from_template
mikachan Aug 14, 2024
6cf4438
Refactor PHP content of pattern_from_wp_block
mikachan Aug 14, 2024
e986584
Delete synced patterns after adding to theme
mikachan Aug 14, 2024
2c6a642
Refactor and add error handling
mikachan Aug 14, 2024
ea94e10
Update save patterns option description
mikachan Aug 15, 2024
f017b94
Remove pattern- prefix
mikachan Aug 15, 2024
6197656
Update option description
mikachan Aug 15, 2024
6de813f
Redirect to patterns page if editing a pattern
mikachan Aug 15, 2024
95c068a
Add check for preference.savePatterns
mikachan Aug 15, 2024
9961ced
Update templates that reference the pattern
mikachan Aug 16, 2024
c901be3
Merge branch 'trunk' into try/export-patterns
mikachan Aug 16, 2024
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
5 changes: 5 additions & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ function rest_save_theme( $request ) {
CBT_Theme_Styles::clear_user_styles_customizations();
}

if ( isset( $options['savePatterns'] ) && true === $options['savePatterns'] ) {
CBT_Theme_Patterns::add_patterns_to_theme( $options );
// clear pattern customisations?
}

wp_cache_flush();

return new WP_REST_Response(
Expand Down
100 changes: 100 additions & 0 deletions includes/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ public static function pattern_from_template( $template, $new_slug = null ) {
);
}

public static function pattern_from_wp_block( $pattern_post ) {
$pattern = new stdClass();
$pattern->id = $pattern_post->ID;
$pattern->title = $pattern_post->post_title;
$pattern->name = sanitize_title_with_dashes( $pattern_post->post_title );
$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 );
mikachan marked this conversation as resolved.
Show resolved Hide resolved
$pattern->content = (
'<?php
/**
* Title: ' . $pattern->title . '
* Slug: ' . $pattern->slug . '
* Categories: ' . $pattern->categories . '
* Inserter: no
*/
?>
' . $pattern_post->post_content
);

return $pattern;
}

public static function escape_alt_for_pattern( $html ) {
if ( empty( $html ) ) {
return $html;
Expand All @@ -47,4 +71,80 @@ public static function create_pattern_link( $attributes ) {
$attributes_json = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES );
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
}

public static function replace_local_pattern_references( $pattern ) {
// List all template and pattern files in the theme
$base_dir = get_stylesheet_directory();
$patterns = glob( $base_dir . DIRECTORY_SEPARATOR . 'patterns' . DIRECTORY_SEPARATOR . '*.php' );
$templates = glob( $base_dir . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . '*.html' );
$template_parts = glob( $base_dir . DIRECTORY_SEPARATOR . 'template-parts' . DIRECTORY_SEPARATOR . '*.html' );

// Replace references to the local patterns in the theme
foreach ( array_merge( $patterns, $templates, $template_parts ) as $file ) {
$file_content = file_get_contents( $file );
$file_content = str_replace( 'wp:block {"ref":' . $pattern->id . '}', 'wp:pattern {"slug":"' . $pattern->slug . '"}', $file_content );
file_put_contents( $file, $file_content );
}
}

public static function prepare_pattern_for_export( $pattern, $options = null ) {
if ( ! $options ) {
$options = array(
'localizeText' => false,
'removeNavRefs' => true,
'localizeImages' => true,
);
}

$pattern = CBT_Theme_Templates::eliminate_environment_specific_content( $pattern, $options );

if ( array_key_exists( 'localizeText', $options ) && $options['localizeText'] ) {
$pattern = CBT_Theme_Templates::escape_text_in_template( $pattern );
}

if ( array_key_exists( 'localizeImages', $options ) && $options['localizeImages'] ) {
$pattern = CBT_Theme_Media::make_template_images_local( $pattern );

// Write the media assets if there are any
if ( $pattern->media ) {
CBT_Theme_Media::add_media_to_local( $pattern->media );
}
}

return $pattern;
}

/**
* Copy the local patterns as well as any media to the theme filesystem.
*/
public static function add_patterns_to_theme( $options = null ) {
$base_dir = get_stylesheet_directory();
$patterns_dir = $base_dir . DIRECTORY_SEPARATOR . 'patterns';

$pattern_query = new WP_Query(
array(
'post_type' => 'wp_block',
'posts_per_page' => -1,
)
);

if ( $pattern_query->have_posts() ) {
// If there is no patterns folder, create it.
if ( ! is_dir( $patterns_dir ) ) {
wp_mkdir_p( $patterns_dir );
}

foreach ( $pattern_query->posts as $pattern ) {
$pattern = self::pattern_from_wp_block( $pattern );
$pattern = self::prepare_pattern_for_export( $pattern, $options );
$pattern_file = $patterns_dir . 'pattern-' . $pattern->name . '.php';
file_put_contents(
$patterns_dir . DIRECTORY_SEPARATOR . 'pattern-' . $pattern->name . '.php',
$pattern->content
);

self::replace_local_pattern_references( $pattern );
}
}
}
}
38 changes: 30 additions & 8 deletions src/editor-sidebar/save-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const SaveThemePanel = () => {
saveTemplates: _preference?.saveTemplates ?? true,
processOnlySavedTemplates:
_preference?.processOnlySavedTemplates ?? true,
savePatterns: _preference?.savePatterns ?? true,
saveFonts: _preference?.saveFonts ?? true,
removeNavRefs: _preference?.removeNavRefs ?? false,
localizeText: _preference?.localizeText ?? false,
Expand Down Expand Up @@ -135,27 +136,44 @@ export const SaveThemePanel = () => {
handleTogglePreference( 'processOnlySavedTemplates' )
}
/>
<CheckboxControl
label={ __( 'Save My Patterns', 'create-block-theme' ) }
help={ __(
'Save custom patterns created in the Editor, listed under "My patterns", to the theme.',
'create-block-theme'
) }
checked={ preference.savePatterns }
onChange={ () => handleTogglePreference( 'savePatterns' ) }
/>
<CheckboxControl
label={ __( 'Localize Text', 'create-block-theme' ) }
help={ __(
'Any text in a template will be copied to a pattern and localized.',
'Any text in a template or pattern will be localized in a pattern.',
'create-block-theme'
) }
disabled={ ! preference.saveTemplates }
disabled={
! preference.saveTemplates && ! preference.savePatterns
}
checked={
preference.saveTemplates && preference.localizeText
( preference.saveTemplates ||
preference.savePatterns ) &&
preference.localizeText
}
onChange={ () => handleTogglePreference( 'localizeText' ) }
/>
<CheckboxControl
label={ __( 'Localize Images', 'create-block-theme' ) }
help={ __(
'Any images in a template will be copied to a local /assets folder and referenced from there via a pattern.',
'Any images in a template or pattern will be copied to a local /assets folder and referenced from there via a pattern.',
'create-block-theme'
) }
disabled={ ! preference.saveTemplates }
disabled={
! preference.saveTemplates && ! preference.savePatterns
}
checked={
preference.saveTemplates && preference.localizeImages
( preference.saveTemplates ||
preference.savePatterns ) &&
preference.localizeImages
}
onChange={ () =>
handleTogglePreference( 'localizeImages' )
Expand All @@ -170,9 +188,13 @@ export const SaveThemePanel = () => {
'Remove Navigation Refs from the theme returning your navigation to the default state.',
'create-block-theme'
) }
disabled={ ! preference.saveTemplates }
disabled={
! preference.saveTemplates && ! preference.savePatterns
}
checked={
preference.saveTemplates && preference.removeNavRefs
( preference.saveTemplates ||
preference.savePatterns ) &&
preference.removeNavRefs
}
onChange={ () => handleTogglePreference( 'removeNavRefs' ) }
/>
Expand Down
Loading