mirror of
git://develop.git.wordpress.org/
synced 2025-05-11 22:35:23 +02:00
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. git-svn-id: https://develop.svn.wordpress.org/branches/5.8@51681 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
cc3fde44f1
commit
b77313d258
@ -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( '/</', '\\u003c', $encoded_attributes );
|
||||
$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes );
|
||||
|
@ -47,6 +47,9 @@ class WP_Test_Block_Serialization extends WP_UnitTestCase {
|
||||
|
||||
// Block with attribute values that may conflict with HTML comment.
|
||||
array( '<!-- wp:attributes {"key":"\\u002d\\u002d\\u003c\\u003e\\u0026\\u0022"} /-->' ),
|
||||
|
||||
// Block with attribute values that should not be escaped.
|
||||
array( '<!-- wp:attributes {"key":"€1.00 / 3 for €2.00"} /-->' ),
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user