create-block-theme/admin/create-theme/theme-patterns.php
Jason Crist 20bcc01052
Extracted any logic that may need to be tested from the api class (#522)
* Extracted any logic that may need to be tested from the api class

* Refactored to facilitate testing

* Refactored zip construction to use shared code with saving logic

* Consolidated template and css parsing logic for theme exporting

* removed queryID removal from query blocks

* Fixed the image processing so that the id is removed from inner content correctly

* Added tests that evaluate encoded patterns and test patterns created from templates with media

* Added taxQuery removal tests and fixed taxQuery attribute removal logic

* Refactored image/cover processing to more cleanly handle id classes

* Add test and fixed bugs around escaping <'s and >'s

* localized quote blocks

* Added localization processing to list, verse, table and media-text blocks

---------

Co-authored-by: Vicente Canales <vicente.canales@automattic.com>
Co-authored-by: Madhu Dollu <madhusudhan.dollu@gmail.com>
2024-03-27 09:41:01 -04:00

50 lines
1.4 KiB
PHP

<?php
class 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
);
return array(
'slug' => $pattern_slug,
'content' => $pattern_content,
);
}
public static function escape_alt_for_pattern( $html ) {
if ( empty( $html ) ) {
return $html;
}
$html = new WP_HTML_Tag_Processor( $html );
while ( $html->next_tag( 'img' ) ) {
$alt_attribute = $html->get_attribute( 'alt' );
if ( ! empty( $alt_attribute ) ) {
$html->set_attribute( 'alt', self::escape_text_for_pattern( $alt_attribute ) );
}
}
return $html->__toString();
}
public static function escape_text_for_pattern( $text ) {
if ( $text && trim( $text ) !== '' ) {
$escaped_text = addslashes( $text );
return "<?php echo esc_attr_e( '" . $escaped_text . "', '" . wp_get_theme()->get( 'Name' ) . "' ); ?>";
}
}
public static function create_pattern_link( $attributes ) {
$block_attributes = array_filter( $attributes );
$attributes_json = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES );
return '<!-- wp:pattern ' . $attributes_json . ' /-->';
}
}