-
Notifications
You must be signed in to change notification settings - Fork 54
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
Export patterns #693
Export patterns #693
Changes from all commits
070c444
29c8dbc
082be0b
bbf98cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,44 @@ class CBT_Theme_Patterns { | |
public static function pattern_from_template( $template, $new_slug = null ) { | ||
$theme_slug = $new_slug ? $new_slug : wp_get_theme()->get( 'TextDomain' ); | ||
$pattern_slug = $theme_slug . '/' . $template->slug; | ||
$pattern_content = ( | ||
'<?php | ||
/** | ||
* Title: ' . $template->slug . ' | ||
* Slug: ' . $pattern_slug . ' | ||
* Categories: hidden | ||
* Inserter: no | ||
*/ | ||
?> | ||
' . $template->content | ||
); | ||
$pattern_content = <<<PHP | ||
<?php | ||
/** | ||
* Title: {$pattern->title} | ||
* Slug: {$pattern->slug} | ||
* Categories: {$pattern->categories} | ||
*/ | ||
?> | ||
{$pattern_post->post_content} | ||
PHP; | ||
return array( | ||
'slug' => $pattern_slug, | ||
'content' => $pattern_content, | ||
); | ||
} | ||
|
||
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->content = <<<PHP | ||
<?php | ||
/** | ||
* Title: {$pattern->title} | ||
* Slug: {$pattern->slug} | ||
* Categories: {$pattern->categories} | ||
*/ | ||
?> | ||
{$pattern_post->post_content} | ||
PHP; | ||
|
||
return $pattern; | ||
} | ||
|
||
public static function escape_alt_for_pattern( $html ) { | ||
if ( empty( $html ) ) { | ||
return $html; | ||
|
@@ -47,4 +68,91 @@ 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 ); | ||
|
||
/** | ||
* if the pattern is not synced then remove if from | ||
* database so patterns are loaded from the theme | ||
* and not the database | ||
*/ | ||
|
||
if ( stripos( $pattern->content, 'pattern-overrides' ) !== true ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a neat way to check if the pattern is not synced. I already mentioned here, but I also found |
||
wp_delete_post( $pattern->id, true ); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should use
$template
rather than$pattern_post
- I'm guessing this was accidentally copied over frompattern_from_wp_block
.$template->categories
might need to be created, too.