Make WordPress Core

Opened 3 years ago

Last modified 5 months ago

#56519 new defect (bug)

Inner blocks serialization bug in serialize_block function

Reported by: saqibsarwar's profile saqibsarwar Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.1
Component: Editor Keywords: has-patch needs-testing
Focuses: Cc:

Description

Hi,

The serialize_block function https://developer.wordpress.org/reference/functions/serialize_block/ fails when the number of inner blocks increases.

The following custom function solved it for me.

<?php
function custom_serialize_block( $block ) {
        $block_content = '';

        foreach ( $block['innerContent'] as $chunk ) {
                if ( is_string( $chunk ) ) {
                        $block_content .= $chunk;
                } else {
                        foreach ( $block['innerBlocks'] as $inner_block ) {
                                $block_content .= custom_serialize_block( $inner_block );
                        }
                }
        }

        if ( ! is_array( $block['attrs'] ) ) {
                $block['attrs'] = [];
        }

        return get_comment_delimited_block_content(
                $block['blockName'],
                $block['attrs'],
                $block_content
        );
}

Attachments (1)

inner-block-serialization-bug.patch (751 bytes) - added by dontfeedthecode 5 months ago.
Patch created from saqibsarwar's resolution to ticket 56519

Download all attachments as: .zip

Change History (6)

#1 @dingo_d
3 years ago

  • Focuses performance coding-standards removed
  • Keywords needs-patch added; has-patch removed

#2 @markparnell
3 years ago

  • Component changed from General to Editor
  • Version changed from 6.0.1 to 5.3.1

@dontfeedthecode
5 months ago

Patch created from saqibsarwar's resolution to ticket 56519

#3 @dontfeedthecode
5 months ago

  • Keywords has-patch added; needs-patch removed

I've created a patch from the fix provider by @saqibsarwar and attached it here, happy to assist if any further testing is required.

#4 @dontfeedthecode
5 months ago

  • Keywords needs-testing added

This ticket was mentioned in PR #8973 on WordPress/wordpress-develop by @dontfeedthecode.


5 months ago
#5

This PR introduces a fix provided by saqibsarwar 3 years ago or so, ticket attached to the PR. I've created a patch for this and am providing this PR as the issue has become dormant with no further input from the reporter however the solution itself is sound.

### Problem
The original serialize_block() function relied on index-based matching between innerContent and innerBlocks. This approach breaks when there are nested blocks within non-string innerContent elements, leading to incorrect or incomplete serialization of deeply nested structures.

### Solution
The updated serialize_block() function iterates over innerContent and dynamically processes nested innerBlocks without relying on positional indexing. For each non-string chunk in innerContent, it recursively serializes all child innerBlocks, ensuring accurate rendering and output for arbitrarily nested blocks.

### Benefits
Fixes serialization issues for complex/nested block layouts
Removes dependency on matching innerContent and innerBlocks indexes
Ensures consistent and correct block output, especially for custom blocks with dynamic inner structures

Trac ticket: https://core.trac.wordpress.org/ticket/56519

Note: See TracTickets for help on using tickets.