-
Notifications
You must be signed in to change notification settings - Fork 23
Fix blocks containing character references #921
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
Changes from 6 commits
5a86850
8e07a0e
685e5f2
55eb159
2656c93
53d306e
3832361
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@headstartwp/headstartwp": patch | ||
| --- | ||
|
|
||
| Fix how data-wp-block attribute is set to avoid generating incorrect/insecure markup |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| use Exception; | ||
| use WP_Block; | ||
| use WP_HTML_Tag_Processor; | ||
| use HeadlessWP\Fixed_WP_HTML_Tag_Processor; | ||
|
|
||
| /** | ||
| * The Gutenberg integration class | ||
|
|
@@ -304,12 +305,37 @@ public function process_block_with_dom_document_api( $html, $block_name, $block_ | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the block attributes in the HTML | ||
| * | ||
| * This is a workaround to avoid the issue with the HTML_Tag_Processor API not handling JSON with HTML in attributes. | ||
| * | ||
| * @see https://github.com/10up/headstartwp/pull/921 | ||
| * | ||
| * @param string $placeholder The placeholder for the block attributes | ||
| * @param string $html The block markup | ||
| * @param string $block_attrs_serialized The block attributes serialized to a JSON string | ||
| * | ||
| * @return string The processed html | ||
| */ | ||
| public function set_block_attributes_tag_api( $placeholder, $html, $block_attrs_serialized ) { | ||
| $search = sprintf( '/data-wp-block="%s"/', preg_quote( $placeholder, '/' ) ); | ||
| $replace = sprintf( 'data-wp-block="%s"', htmlspecialchars( $block_attrs_serialized ) ); | ||
|
|
||
| // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | ||
| return preg_replace( | ||
| $search, | ||
| $replace, | ||
| $html | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Process the block with the WP_HTML_Tag_Processor | ||
| * | ||
| * @param string $html The block markup | ||
| * @param string $block_name The block name | ||
| * @param string $block_attrs_serialized The serialized block attributes | ||
| * @param string $block_attrs_serialized The block attributes serialized to a JSON string | ||
| * @param array $block The block schema | ||
| * @param WP_Block $block_instance The block instance | ||
| * | ||
|
|
@@ -321,7 +347,14 @@ public function process_block_with_html_tag_api( $html, $block_name, $block_attr | |
|
|
||
| if ( ! $this->bypass_block_attributes( $block_name, $block_instance ) && $doc->next_tag() ) { | ||
| $doc->set_attribute( 'data-wp-block-name', $block_name ); | ||
| $doc->set_attribute( 'data-wp-block', $block_attrs_serialized ); | ||
| $placeholder = '___HEADSTARTWP_BLOCK_ATTRS___'; | ||
| $doc->set_attribute( 'data-wp-block', $placeholder ); | ||
|
|
||
| $intermediate_html = $doc->get_updated_html(); | ||
| $intermediate_html = $this->set_block_attributes_tag_api( $placeholder, $intermediate_html, $block_attrs_serialized ); | ||
|
|
||
| $doc = new WP_HTML_Tag_Processor( $intermediate_html ); | ||
| $doc->next_tag(); | ||
|
|
||
| /** | ||
| * Filter the block before rendering | ||
|
|
@@ -347,7 +380,7 @@ public function process_block_with_html_tag_api( $html, $block_name, $block_attr | |
| * | ||
| * @param string $html The block markup | ||
| * @param string $block_name The block name | ||
| * @param string $serialized_attributes Serialized attributes | ||
| * @param string $serialized_attributes The block attributes serialized to a JSON string | ||
| * @param array $block The block array | ||
| * @param WP_Block $block_instance The block instance | ||
| * | ||
|
|
@@ -365,14 +398,8 @@ public function process_dom_document_block( | |
| // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | ||
| $root_node = $document->documentElement; | ||
|
|
||
| $attrs = $document->createAttribute( 'data-wp-block' ); | ||
| $attrs->value = $serialized_attributes; | ||
|
|
||
| $block_name_obj = $document->createAttribute( 'data-wp-block-name' ); | ||
| $block_name_obj->value = $block_name; | ||
|
|
||
| $root_node->appendChild( $attrs ); | ||
| $root_node->appendChild( $block_name_obj ); | ||
| $root_node->setAttribute( 'data-wp-block-name', $block_name ); | ||
| $root_node->setAttribute( 'data-wp-block', $serialized_attributes ); | ||
|
nicholasio marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Filter the block's DOMElement before rendering | ||
|
|
@@ -476,14 +503,14 @@ public function render_block( $html, $block, $block_instance ) { | |
| /** | ||
| * Filter out the block attributes after serialization | ||
| * | ||
| * @param string $encoded_attrs The serialized block attributes | ||
| * @param string $encoded_attrs The block attributes serialized to a JSON string | ||
| * @param array $attrs The block attributes | ||
| * @param array $block The block schema | ||
| * @param WP_Block $block_instance The block instance | ||
| */ | ||
| $block_attrs_serialized = apply_filters( | ||
| 'tenup_headless_wp_render_blocks_attrs_serialized', | ||
| esc_attr( wp_json_encode( $block_attrs ) ), | ||
| wp_json_encode( $block_attrs ), | ||
| $block_attrs, | ||
| $block, | ||
| $block_instance | ||
|
|
@@ -498,7 +525,7 @@ public function render_block( $html, $block, $block_instance ) { | |
| */ | ||
| $use_html_tag_api = apply_filters( 'tenup_headless_wp_render_block_use_tag_processor', false ); | ||
|
|
||
| if ( class_exists( WP_HTML_Tag_Processor::class ) && $use_html_tag_api ) { | ||
| if ( $use_html_tag_api ) { | ||
|
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. Bug: WP HTML Tag Processor Class Check RemovalThe removal of the |
||
| return $this->process_block_with_html_tag_api( | ||
| $html, | ||
| $block_name, | ||
|
|
||
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.
Bug: Unused Import Causes Potential Fatal Error
The
HeadlessWP\Fixed_WP_HTML_Tag_Processorclass is imported but never used. The code continues to useWP_HTML_Tag_Processorfor processing, and a separate workaround method (set_block_attributes_tag_api) was implemented instead. This unused import is dead code, likely a remnant of an incomplete fix, and could cause a fatal error if the class does not exist.Locations (1)
wp/headless-wp/includes/classes/Integrations/Gutenberg.php#L14-L15