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/all pattern exporting #747

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Got replacing all synced patterns working as exected
  • Loading branch information
pbking committed Nov 22, 2024
commit b299d79880e5a6204b82724ab1aee882d092c528
9 changes: 9 additions & 0 deletions includes/class-create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ function CBT_get_theme_block_patterns()
$pattern_data['pattern_file'] = $pattern_file;
$pattern_data['content'] = CBT_render_pattern($pattern_file);

// if the pattern is synced add the ID

if ( $pattern_data['synced'] === 'yes' ) {
$pattern_post = get_page_by_path(sanitize_title($pattern_data['slug']), OBJECT, 'wp_block');
if ($pattern_post) {
$pattern_data['id'] = $pattern_post->ID;
}
}

$all_patterns[] = $pattern_data;
}
}
Expand Down
20 changes: 17 additions & 3 deletions includes/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public static function create_pattern_link( $attributes ) {
public static function replace_local_synced_pattern_references( $pattern ) {

// If we save patterns we have to update the templates (or none of the templates).
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, null );
// However, we can't save it here because it will overwrite changes we make to the templates RE: Patterns.
// CBT_Theme_Templates::add_templates_to_local( 'all', null, null, null );

// List all template and pattern files in the theme
$base_dir = get_stylesheet_directory();
Expand All @@ -85,10 +86,14 @@ public static function replace_local_synced_pattern_references( $pattern ) {
$templates = glob( $base_dir . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . '*.html' );
$template_parts = glob( $base_dir . DIRECTORY_SEPARATOR . 'template-parts' . DIRECTORY_SEPARATOR . '*.html' );



$needle = 'wp:block {"ref":' . $pattern['id'];
$replacement = 'wp:pattern {"slug":"' . $pattern['slug'] . '"';
// Replace references to the local patterns in the theme
foreach ( array_merge( $patterns, $templates, $template_parts, $synced_patterns ) 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_content = str_replace( $needle, $replacement, $file_content );
file_put_contents( $file, $file_content );
}

Expand Down Expand Up @@ -151,6 +156,15 @@ public static function add_patterns_to_theme( $options = null ) {
}
}
}

// now replace all instances of synced blocks with pattern blocks
$patterns = CBT_get_theme_block_patterns();
$patterns = array_filter($patterns, function ($pattern) {
return $pattern['synced'] === 'yes';
});
foreach ($patterns as $pattern) {
self::replace_local_synced_pattern_references($pattern);
}
}

public static function add_synced_pattern_to_theme($pattern)
Expand All @@ -167,7 +181,7 @@ public static function add_synced_pattern_to_theme($pattern)
file_put_contents( $pattern_file, $pattern->content);

// Replace references in the templates
self::replace_local_synced_pattern_references($pattern);
// self::replace_local_synced_pattern_references($pattern);

// Remove it from the database to ensure that these patterns are loaded from the theme.
//wp_delete_post($pattern->id, true);
Expand Down