Skip to content

Commit 9e6ea32

Browse files
committed
Framework: Drop server-side block serialization, wpautop
A parsed block has no awareness of where inner blocks exist in its innerHTML, so it cannot safely reserialize. There are a few options: - Since we merely skip wpautop for known blocks, we could avoid reserialization and return the block's original HTML verbatim if we had access to its outerHTML. See nylen/phpegjs#3 - Move wpautop behavior for freeform content to the editor client. This may align well with desires to transparently upgrade legacy paragraph content to paragraph blocks. This would also allow the server to avoid any preprocessing before showing a post on the front-end, assuming that the saved content has had wpautop applied already. Acknowledging that this effectively reverts large parts of #2806
1 parent d1df878 commit 9e6ea32

File tree

3 files changed

+0
-104
lines changed

3 files changed

+0
-104
lines changed

lib/blocks.php

-67
Original file line numberDiff line numberDiff line change
@@ -189,70 +189,3 @@ function do_blocks( $content ) {
189189
return $content_after_blocks;
190190
}
191191
add_filter( 'the_content', 'do_blocks', 9 ); // BEFORE do_shortcode().
192-
193-
/**
194-
* Given a string, returns content normalized with automatic paragraphs applied
195-
* to text not identified as a block. Since this executes the block parser, it
196-
* should not be used in a performance-critical flow such as content display.
197-
* Block content will not have automatic paragraphs applied.
198-
*
199-
* @since 1.7.0
200-
*
201-
* @param string $content Original content.
202-
* @return string Content formatted with automatic paragraphs applied
203-
* to unknown blocks.
204-
*/
205-
function gutenberg_wpautop_block_content( $content ) {
206-
$blocks = gutenberg_parse_blocks( $content );
207-
foreach ( $blocks as $i => $block ) {
208-
if ( isset( $block['blockName'] ) ) {
209-
continue;
210-
}
211-
212-
$content = $block['innerHTML'];
213-
214-
// wpautop will trim leading whitespace and return whitespace-only text
215-
// as an empty string. Preserve to apply leading whitespace as prefix.
216-
preg_match( '/^(\s+)/', $content, $prefix_match );
217-
$prefix = empty( $prefix_match ) ? '' : $prefix_match[0];
218-
219-
$content = $prefix . wpautop( $content, false );
220-
221-
// To normalize as text where wpautop would not be applied, restore
222-
// double newline to wpautop'd text if not at the end of content.
223-
$is_last_block = ( count( $blocks ) === $i + 1 );
224-
if ( ! $is_last_block ) {
225-
$content = str_replace( "</p>\n", "</p>\n\n", $content );
226-
}
227-
228-
$blocks[ $i ]['innerHTML'] = $content;
229-
}
230-
231-
return gutenberg_serialize_blocks( $blocks );
232-
}
233-
234-
/**
235-
* Filters saved post data to apply wpautop to freeform block content.
236-
*
237-
* @since 1.7.0
238-
*
239-
* @param array $data An array of slashed post data.
240-
* @return array An array of post data with wpautop applied to freeform
241-
* block content.
242-
*/
243-
function gutenberg_wpautop_insert_post_data( $data ) {
244-
if ( ! empty( $data['post_content'] ) && gutenberg_content_has_blocks( $data['post_content'] ) ) {
245-
// WP_REST_Posts_Controller slashes post data before inserting/updating
246-
// a post. This data gets unslashed by `wp_insert_post` right before
247-
// saving to the DB. The PEG parser needs unslashed input in order to
248-
// properly parse JSON attributes.
249-
$content = wp_unslash( $data['post_content'] );
250-
$content = gutenberg_wpautop_block_content( $content );
251-
$content = wp_slash( $content );
252-
253-
$data['post_content'] = $content;
254-
}
255-
256-
return $data;
257-
}
258-
add_filter( 'wp_insert_post_data', 'gutenberg_wpautop_insert_post_data' );

lib/client-assets.php

-6
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,6 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
737737
);
738738
}
739739

740-
// Set initial content to apply autop on unknown blocks, preserving this
741-
// behavior for classic content while otherwise disabling for blocks.
742-
if ( ! $is_new_post && is_array( $post_to_edit['content'] ) ) {
743-
$post_to_edit['content']['raw'] = gutenberg_wpautop_block_content( $post_to_edit['content']['raw'] );
744-
}
745-
746740
// Set the post type name.
747741
$post_type = get_post_type( $post );
748742

phpunit/class-wpautop-test.php

-31
This file was deleted.

0 commit comments

Comments
 (0)