pristas-peter / wp-graphql-gutenberg

Query gutenberg blocks with wp-graphql
https://wp-graphql-gutenberg.netlify.app
GNU General Public License v3.0
299 stars 61 forks source link

blocksJSON doesn't return reusable block data #142

Open andrewmumblebee opened 2 years ago

andrewmumblebee commented 2 years ago

When using the blocksJSON property in a query against a post/page then the data is returned correctly for most blocks, but if there is a reusable block in use then the innerBlocks of it isn't populated.

We don't drill down into our blocks through graphQL as we don't generally know the max depth of blocks an editor might use, and it saves us creating a complex nested query.

So the blocksJSON property has been a savior for us.

To fix this i've changed this part https://github.com/pristas-peter/wp-graphql-gutenberg/blob/f015b8997085961e4fe71ef5960647994c514ac5/src/Blocks/Block.php#L186

To this

public function __construct( $data, $post_id, $registry, $order, $parent ) {

    $innerBlocks = $data['innerBlocks'];

    // handle mapping reusable blocks to innerblocks.
    if ( $data['blockName'] === 'core/block' && ! empty( $data['attrs']['ref'] ) ) {
        $ref            = $data['attrs']['ref'];
                $reusablePost = get_post( $ref );

        if ( ! empty( $reusablePost ) ) {
            $innerBlocks = parse_blocks( $reusablePost->post_content );
        }
    }

    $this->innerBlocks = self::create_blocks( $innerBlocks, $post_id, $registry, $this );

   ... rest of code

Then the innerBlocks of a reusable block should be the same as the blocks that were stored in the post created for it.