From 1660108b59280e9eb99d6ea8bfe1a46481a9c840 Mon Sep 17 00:00:00 2001 From: desrosj Date: Fri, 27 Aug 2021 19:09:58 +0000 Subject: [PATCH] Editor: Ensure block attribute serialization in PHP matches the JavaScript equivalent. The `serializeAttributes()` function in JavaScript uses `JSON.stringify`, which does not encode slashes and unicode characters by default. This resulted in the PHP serialization through `json_encode()` producing different results. This also switches from `json_encode()` to `wp_json_encode()` to prevent failures when any non UTF-8 characters are included. Props kevinfodness, SergeyBiryukov, timothyblynjacobs. Merges [51674] to the 5.8 branch. Fixes #53936. Built from https://develop.svn.wordpress.org/branches/5.8@51681 git-svn-id: http://core.svn.wordpress.org/branches/5.8@51287 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 6 +++++- wp-includes/version.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 0a4c1c9afc..fc7c3f9b0d 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -488,13 +488,17 @@ function get_dynamic_block_names() { * substitution for characters which might otherwise interfere with embedding * the result in an HTML comment. * + * This function must produce output that remains in sync with the output of + * the serializeAttributes JavaScript function in the block editor in order + * to ensure consistent operation between PHP and JavaScript. + * * @since 5.3.1 * * @param array $block_attributes Attributes object. * @return string Serialized attributes. */ function serialize_block_attributes( $block_attributes ) { - $encoded_attributes = json_encode( $block_attributes ); + $encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); $encoded_attributes = preg_replace( '//', '\\u003e', $encoded_attributes ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 486918919f..f8d7feda19 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8.1-alpha-51680'; +$wp_version = '5.8.1-alpha-51681'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.