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

Fix interactivity API enabled navigation block #7650

Merged
merged 7 commits into from
Nov 1, 2023

Conversation

thelovekesh
Copy link
Collaborator

@thelovekesh thelovekesh commented Oct 31, 2023

Summary

Fixes #7647

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

Copy link
Contributor

github-actions bot commented Oct 31, 2023

Plugin builds for 1739125 are ready 🛎️!

Checksums
# Development build checksums
55db57a3222215b432c4dc66e70a5dfe9d89d3e2a0b0e8631dd17acbf9a21246 *amp.zip

# Production build checksums
f04c1f9068549c69880e250b9a00affe00eb3fec3a33bff493fc076ab8833138 *amp.zip

Warning

These builds are for testing purposes only and should not be used in production.

@westonruter westonruter added this to the v2.5.0 milestone Oct 31, 2023
@@ -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' );
Copy link
Member

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:

Suggested change
$is_interactive_block = false !== strpos( $block_content, 'data-wp-interactive' );
$is_interactive_block = false !== strpos( $block_content, ' data-wp-interactive' );

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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?

Copy link
Member

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 😄

includes/embeds/class-amp-core-block-handler.php Outdated Show resolved Hide resolved
includes/embeds/class-amp-core-block-handler.php Outdated Show resolved Hide resolved
Comment on lines 448 to 453
// Delete `data-wp-*` attributes.
if ( $is_interactive_block ) {
$block_content = preg_replace( '/\sdata-wp-[^=]+="[^"]*"/', '', $block_content );
$block_content = preg_replace( '/\sdata-wp-[^=]+=\'[^\']*\'/', '', $block_content );
}

Copy link
Member

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.

Copy link
Collaborator Author

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 😅

Copy link
Member

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.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@westonruter westonruter merged commit 789e099 into develop Nov 1, 2023
34 of 35 checks passed
@westonruter westonruter deleted the fix/navigation-block branch November 1, 2023 03:40
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update AMP_Core_Block_Handler to handle interactivity API enabled blocks
2 participants