Skip to content

Commit

Permalink
Parser: Set a parser for the modal block, strip newlines from attri…
Browse files Browse the repository at this point in the history
…bute replacements
  • Loading branch information
ryelle committed Dec 18, 2024
1 parent c61e137 commit 2f7167a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions env/export-content/includes/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct( string $content = '' ) {

// Shared custom blocks.
'wporg/link-wrapper' => new Parsers\HTMLParser( 'a', [ 'href' ] ),
'wporg/modal' => new Parsers\AttributeParser( [ 'label' ] ),
];
}

Expand Down
4 changes: 3 additions & 1 deletion env/export-content/includes/parsers/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ private function get_attribute( string $attribute_name, array $block ) : array {
private function set_attribute( string $attribute_name, array &$block, array $replacements ) {
if ( isset( $block['attrs'][ $attribute_name ] ) && is_string( $block['attrs'][ $attribute_name ] ) ) {
if ( isset( $replacements[ $block['attrs'][ $attribute_name ] ] ) ) {
$block['attrs'][ $attribute_name ] = $replacements[ $block['attrs'][ $attribute_name ] ];
$replace = $replacements[ $block['attrs'][ $attribute_name ] ];
$replace = str_replace( "\n", ' ', $replace );
$block['attrs'][ $attribute_name ] = $replace;
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions env/export-content/tests/block-parser-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function data_block_content_strings() {
"<!-- wp:table -->\n<figure class=\"wp-block-table\"><table><thead><tr><th>Cookie</th><th>Logged-in Users Only?</th></tr></thead><tbody><tr><th>welcome-{blog_id}</th><td>No</td></tr><tr><th>showComments</th><td>No</td></tr></tbody></table></figure>\n<!-- /wp:table -->",
[ 'Cookie', 'Logged-in Users Only?', 'welcome-{blog_id}', 'No', 'showComments' ],
],
[
"<!-- wp:wporg/modal {\"closeButtonColor\":\"white\",\"customCloseButtonColor\":\"#ffffff\",\"href\":\"[download_link]\",\"label\":\"Download WordPress [latest_version]\"} -->\n<!-- wp:group {\"style\":{\"elements\":{\"link\":{\"color\":{\"text\":\"var:preset|color|white\"}}},\"spacing\":{\"padding\":{\"top\":\"var:preset|spacing|40\",\"bottom\":\"var:preset|spacing|30\",\"left\":\"var:preset|spacing|40\",\"right\":\"var:preset|spacing|40\"},\"blockGap\":\"var:preset|spacing|10\"}},\"backgroundColor\":\"blueberry-1\",\"textColor\":\"white\",\"layout\":{\"type\":\"constrained\"}} -->\n<div class=\"wp-block-group has-white-color has-blueberry-1-background-color has-text-color has-background has-link-color\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\"><!-- wp:heading {\"style\":{\"spacing\":{\"margin\":{\"top\":\"0\"}}}} -->\n<h2 class=\"wp-block-heading\" style=\"margin-top:0\">Howdy!</h2>\n<!-- /wp:heading --></div>\n<!-- /wp:group -->\n<!-- /wp:wporg/modal -->",
[ 'Download WordPress [latest_version]', 'Howdy!' ],
],
];
}

Expand Down Expand Up @@ -178,6 +182,11 @@ public function data_block_content_i18n_with_shortcode() {
"<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"[download_link]\">Download WordPress [latest_version]</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
"<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"[download_link]\"><?php\n/* translators: [latest_version] is a shortcode and should not be translated. */\n_e( 'Download WordPress [latest_version]', 'wporg' );\n?></a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
],
[
// Modal, with shortcode in the label (shortcode in attribute).
"<!-- wp:wporg/modal {\"closeButtonColor\":\"white\",\"customCloseButtonColor\":\"#ffffff\",\"href\":\"[download_link]\",\"label\":\"Download WordPress [latest_version]\"} -->\n<!-- wp:group -->\n<div class=\"wp-block-group\"><!-- wp:heading {\"style\":{\"spacing\":{\"margin\":{\"top\":\"0\"}}}} -->\n<h2 class=\"wp-block-heading\" style=\"margin-top:0\">Howdy!</h2>\n<!-- /wp:heading --></div>\n<!-- /wp:group -->\n<!-- /wp:wporg/modal -->",
"<!-- wp:wporg/modal {\"closeButtonColor\":\"white\",\"customCloseButtonColor\":\"#ffffff\",\"href\":\"[download_link]\",\"label\":\"<?php /* translators: [latest_version] is a shortcode and should not be translated. */ _e( 'Download WordPress [latest_version]', 'wporg' ); ?>\"} -->\n<!-- wp:group -->\n<div class=\"wp-block-group\"><!-- wp:heading {\"style\":{\"spacing\":{\"margin\":{\"top\":\"0\"}}}} -->\n<h2 class=\"wp-block-heading\" style=\"margin-top:0\"><?php _e( 'Howdy!', 'wporg' ); ?></h2>\n<!-- /wp:heading --></div>\n<!-- /wp:group -->\n<!-- /wp:wporg/modal -->",
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<!-- /wp:paragraph -->

<!-- wp:group {"style":{"spacing":{"blockGap":"var:preset|spacing|10"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group"><!-- wp:wporg/modal {"closeButtonColor":"white","customCloseButtonColor":"#ffffff","href":"[download_link]","label":"Download WordPress [latest_version]"} -->
<div class="wp-block-group"><!-- wp:wporg/modal {"closeButtonColor":"white","customCloseButtonColor":"#ffffff","href":"[download_link]","label":"<?php /* translators: [latest_version] is a shortcode and should not be translated. */ _e( 'Download WordPress [latest_version]', 'wporg' ); ?>"} -->
<!-- wp:group {"style":{"elements":{"link":{"color":{"text":"var:preset|color|white"}}},"spacing":{"padding":{"top":"var:preset|spacing|40","bottom":"var:preset|spacing|30","left":"var:preset|spacing|40","right":"var:preset|spacing|40"},"blockGap":"var:preset|spacing|10"}},"backgroundColor":"blueberry-1","textColor":"white","layout":{"type":"constrained"}} -->
<div class="wp-block-group has-white-color has-blueberry-1-background-color has-text-color has-background has-link-color" style="padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)"><!-- wp:heading {"style":{"spacing":{"margin":{"top":"0"}}}} -->
<h2 class="wp-block-heading" style="margin-top:0"><?php _e( 'Howdy!', 'wporg' ); ?></h2>
Expand Down

0 comments on commit 2f7167a

Please sign in to comment.