[wp-trac] [WordPress Trac] #47375: Blocks API: Add server-side `serialize_block()`

WordPress Trac noreply at wordpress.org
Fri May 24 18:56:53 UTC 2019


#47375: Blocks API: Add server-side `serialize_block()`
-------------------------+-----------------------------
 Reporter:  dmsnell      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Editor       |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 We have the ability to `parse_blocks()` on the server and transform
 serialized block content in HTML form into a list of block objects or
 nodes. We lack the ability to transform in the opposite direction.

 `serialize_block()` takes the output from `parse_blocks()` and returns an
 HTML string which would produce the same content as at the beginning of
 the process. in code terms…

 {{{#!php
 <?php
 $post_content === implode( "\n\n", array_map( 'serialize_block',
 parse_blocks( $post_content ) ) );
 }}}

 An initial use-case I have found is the desire to transform the saved
 contents of `$post_content` from a block-structural point of view. I want
 to "extract" the inner contents of a block from its surrounding block
 comment delimiters without touching the other parts of `$post_content`.

 Another use-case would be any similar structural changes. Consider the
 case where we want to update a post and remove all `align: left`
 attributes from the comment delimiters…

 {{{#!php
 <?php
 $blocks = parse_blocks( $post_content );

 foreach( $blocks as $block ) {
         if ( isset( $block['attrs']['align'] ) && 'left' ===
 $block['attrs']['align'] ) {
                 unset( $block['attrs']['align'] );
         }
 }

 return implode( "\n\n", array_map( 'serialize_block', $blocks ) );
 }}}

 Essentially this copies existing logic which exists inside the editor into
 the backend.

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


More information about the wp-trac mailing list