[wp-trac] [WordPress Trac] #60671: If you hook some blocks with Block Hooks API, slashes are stripped from template part block attributes

WordPress Trac noreply at wordpress.org
Fri Mar 1 16:18:19 UTC 2024


#60671: If you hook some blocks with Block Hooks API, slashes are stripped from
template part block attributes
--------------------------+-----------------------------
 Reporter:  karolmanijak  |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Revisions     |    Version:  trunk
 Severity:  major         |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 == Description

 When you use Block Hooks API and hook any block and then edit template
 part which includes some escaped characters, then they're incorrectly
 escaped and two revisions are created.

 In a consequence of incorrect escape, the blocks may become corrupted.

 == Steps to reproduce

 1. In your functions.php hook some block (I used an [example from
 docs](https://make.wordpress.org/core/2023/10/15/introducing-block-hooks-
 for-dynamic-blocks/):


 {{{
 function example_block_hooks( $hooked_blocks, $position, $anchor_block,
 $context ) {

         // Template/Template Part hooks.
         if ( $context instanceof WP_Block_Template ) {

                 // Hooks the "Like" button block before the Post Title in
 the Single template.
                 if (
                         'core/post-title' === $anchor_block &&
                         'before' === $position &&
                         'single' === $context->slug
                 ) {
                         $hooked_blocks[] = 'ockham/like-button';
                 }

                 // Hooks the Login/Logout link block after the Navigation
 block if the context of the template part is a header.
                 if (
                         'core/group' === $anchor_block &&
                         'last_child' === $position &&
                         'header' === $context->area
                 ) {
                         $hooked_blocks[] = 'core/loginout';
                 }
         }

         // Pattern hooks.
         if ( is_array( $context ) && isset( $context['slug'] ) ) {

                 // Hooks into the Post Meta pattern in the Twenty Twenty-
 Four theme.
                 if (
                         'core/post-terms' === $anchor_block &&
                         'after' === $position &&
                         'twentytwentyfour/post-meta' === $context['slug']
                 ) {
                         $hooked_blocks[] = 'ockham/like-button';
                 }
         }

         return $hooked_blocks;
 }
 add_filter( 'hooked_block_types', 'example_block_hooks', 10, 4 );
 }}}


 2. Go to Editor -> Patterns -> General -> Post Meta
 3. Remove the content and create any paragraph with some content e.g.
 "test"
 4. Go to Inspector Controls of paragraph -> Advanced
 5. Add ADDITIONAL CSS CLASS(ES): "some__bem--class" - this is to add some
 data to attributes
 6. Add BLOCK NAME: "mimicking __ bem -- class" - this is to add some data
 to attributes
 7. Switch to "Template Part" tab in Inspector Controls and check the
 number of revisions
 8. Save post

 **Actual**:
 - the number of revisions raised by 2
 - you can check the revisions. Second revision "escapes" the special
 characters (dashes) and remove the backslash `\`

 **Expected:**
 - the number of revisions raised by 1
 - there's no incorrect escaping

 9. You can refresh the page and you can see block name is corrupted:
 - Actual: "mimicking __ bem u002du002d class"
 - Expected: "mimicking __ bem -- class"

 This is small example but it may be more destructive in real scenarios.


 == More info

 Reproducible in WordPress 6.5 Beta only (couldn't reproduce it in
 WordPress 6.4.3 even with Gutenberg 17.7 enabled).

 Originally raised in
 https://github.com/woocommerce/woocommerce/issues/45176.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60671>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list