-
Notifications
You must be signed in to change notification settings - Fork 384
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
Fix interactivity API enabled navigation block #7650
Conversation
851e7ef
to
cf16557
Compare
Plugin builds for 1739125 are ready 🛎️!
Checksums
Warning These builds are for testing purposes only and should not be used in production. |
bd6d2c6
to
decc397
Compare
@@ -339,6 +339,8 @@ public function ampify_navigation_block( $block_content, $block ) { | |||
add_action( 'wp_print_footer_scripts', [ $this, 'dequeue_block_navigation_view_script' ], 0 ); | |||
} | |||
|
|||
$is_interactive_block = false !== strpos( $block_content, 'data-wp-interactive' ); |
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.
This could be made more robust. What if someone uses data-wp-interactive
in the text of the block?
I guess that's true for the other existing code below.
At the very least:
$is_interactive_block = false !== strpos( $block_content, 'data-wp-interactive' ); | |
$is_interactive_block = false !== strpos( $block_content, ' data-wp-interactive' ); |
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.
What about checking this attribute in the <nav>
attributes? Something similar to:
$is_interactive_block = str_contains(
preg_match( '/(?<=<nav)\s[^>]+/', $block_content, $matches ) ? $matches[0] : '',
' data-wp-interactive',
);
Surprisingly adding space before data-wp-interactive
is failing here, because it's added as a newline in the outputted block content HTML.
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.
Also, since it's a navigation block, should we expect such a string in block text?
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.
Yeah, that preg_match()
seems like it would do the trick.
Also, since it's a navigation block, should we expect such a string in block text?
Maybe if someone likes to blog about different attributes and has a link to a page all about this one 😄
// Delete `data-wp-*` attributes. | ||
if ( $is_interactive_block ) { | ||
$block_content = preg_replace( '/\sdata-wp-[^=]+="[^"]*"/', '', $block_content ); | ||
$block_content = preg_replace( '/\sdata-wp-[^=]+=\'[^\']*\'/', '', $block_content ); | ||
} | ||
|
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.
These can remain, right? They just won't do anything.
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.
Yes, they won't, apart from increasing content length by a few bytes 😅
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.
Just that their removal could cause problems due to the string replacement not using parsed HTML, and they take additional (inconsequential) server time to remove. IMO, I think we can just leave them.
Co-authored-by: Weston Ruter <westonruter@google.com>
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.
🎉
Summary
Fixes #7647
Checklist