Commit 8380f0fda4 for wordpress.org
commit 8380f0fda4603074e595d8180a26b0ba81c79f60
Author: dmsnell <dmsnell@git.wordpress.org>
Date: Wed Nov 11 21:41:56 2026 +0000
Blocks: Non-string documents should parse as empty arrays.
Following the change to rely on `WP_Block_Processor` for parsing inside of `WP_Block_Parser`, a small behavioral change affected non-string inputs. Previously they coerced to strings, but the updated code throws a `TypeError`.
In this patch, non-string and `Stringable` inputs call `_doing_it_wrong()`, Stringables coerce to string, and non-string inputs return an empty array.
Developed in: https://github.com/WordPress/wordpress-develop/pull/15340
Discussed in: https://core.trac.wordpress.org/ticket/66138
Follow-up to r64870.
Props dmsnell, rejaulalomkhan.
See #66138.
Built from https://develop.svn.wordpress.org/trunk@65405
git-svn-id: http://core.svn.wordpress.org/trunk@64544 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-block-parser.php b/wp-includes/class-wp-block-parser.php
index 06342fc5ac..13c5ac2029 100644
--- a/wp-includes/class-wp-block-parser.php
+++ b/wp-includes/class-wp-block-parser.php
@@ -66,11 +66,31 @@ class WP_Block_Parser {
* return an error on invalid inputs.
*
* @since 5.0.0
+ * @since 7.2.0 Triggers a notice when the document is not a string, and returns
+ * an empty array unless the document is a stringable object.
*
* @param string $document Input document being parsed.
* @return array[]
*/
public function parse( $document ) {
+ if ( is_object( $document ) && method_exists( $document, '__toString' ) ) {
+ _doing_it_wrong(
+ __METHOD__,
+ __( 'Convert the document to a string before calling parse().' ),
+ '7.2.0'
+ );
+ $document = (string) $document;
+ }
+
+ if ( ! is_string( $document ) ) {
+ _doing_it_wrong(
+ __METHOD__,
+ __( 'The document must be a string.' ),
+ '7.2.0'
+ );
+ return array();
+ }
+
$this->document = $document;
$this->offset = 0;
$this->output = array();
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 1aa525b7fc..8bdee0d376 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta5-65404';
+$wp_version = '7.2-beta5-65405';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.