WordPress.org

Make WordPress Core

Changeset 51681


Ignore:
Timestamp:
08/27/2021 07:08:21 PM (3 months ago)
Author:
desrosj
Message:

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.

Location:
branches/5.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/wp-includes/blocks.php

    r51382 r51681  
    489489 * the result in an HTML comment.
    490490 *
     491 * This function must produce output that remains in sync with the output of
     492 * the serializeAttributes JavaScript function in the block editor in order
     493 * to ensure consistent operation between PHP and JavaScript.
     494 *
    491495 * @since 5.3.1
    492496 *
     
    495499 */
    496500function serialize_block_attributes( $block_attributes ) {
    497     $encoded_attributes = json_encode( $block_attributes );
     501    $encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
    498502    $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes );
    499503    $encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes );
  • branches/5.8/tests/phpunit/tests/blocks/serialization.php

    r48937 r51681  
    4848            // Block with attribute values that may conflict with HTML comment.
    4949            array( '<!-- wp:attributes {"key":"\\u002d\\u002d\\u003c\\u003e\\u0026\\u0022"} /-->' ),
     50
     51            // Block with attribute values that should not be escaped.
     52            array( '<!-- wp:attributes {"key":"€1.00 / 3 for €2.00"} /-->' ),
    5053        );
    5154    }
Note: See TracChangeset for help on using the changeset viewer.