Skip to content

Commit cf56ccb

Browse files
committed
Blocks: Introduce WP_Block_Processor for efficiently parsing blocks.
The Block Processor follows the HTML API in providing a streaming, near-zero-overhead, lazy, re-entrant parser for traversing block structure. This class provides an alternate interface to `parse_blocks()` which is more amenable to a number of common server-side operations on posts, especially those needing to operate on only a part of a full post. Developed in #9105 Discussed in https://core.trac.wordpress.org/ticket/61401 Props dmsnell, gziolo, jonsurrell, soean, tjnowell, westonruter. Fixes #61401. git-svn-id: https://develop.svn.wordpress.org/trunk@60939 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 464ed1c commit cf56ccb

File tree

6 files changed

+3370
-2
lines changed

6 files changed

+3370
-2
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
#############################################################################
230230
SELECTIVE EXCLUSIONS
231231
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.
232-
232+
233233
These exclusions are listed ordered by alphabetic sniff name.
234234
#############################################################################
235235
-->
@@ -266,8 +266,9 @@
266266

267267
<!-- Goto is an effective way to handle errors in decoders which expect valid bytes
268268
without impacting the fast path while avoiding bloating the code with redundant
269-
and risky handling code. Exclude forbidding goto in UTF-8 fallback code. -->
269+
and risky handling code. Exclude forbidding goto in parser code. -->
270270
<exclude-pattern>/wp-includes/compat-utf8\.php</exclude-pattern>
271+
<exclude-pattern>/wp-includes/class-wp-block-processor\.php</exclude-pattern>
271272
</rule>
272273

273274
<!-- Exclude sample config from modernization to prevent breaking CI workflows based on WP-CLI scaffold.

src/wp-includes/blocks.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,17 @@ function render_block( $parsed_block ) {
23762376
/**
23772377
* Parses blocks out of a content string.
23782378
*
2379+
* Given an HTML document, this function fully-parses block content, producing
2380+
* a tree of blocks and their contents, as well as top-level non-block content,
2381+
* which will appear as a block with no `blockName`.
2382+
*
2383+
* This function can be memory heavy for certain documents, particularly those
2384+
* with deeply-nested blocks or blocks with extensive attribute values. Further,
2385+
* this function must parse an entire document in one atomic operation.
2386+
*
2387+
* If the entire parsed document is not necessary, consider using {@see WP_Block_Processor}
2388+
* instead, as it provides a streaming and low-overhead interface for finding blocks.
2389+
*
23792390
* @since 5.0.0
23802391
*
23812392
* @param string $content Post content.

0 commit comments

Comments
 (0)