Make WordPress Core

Changeset 43751


Ignore:
Timestamp:
10/18/2018 04:39:40 AM (6 years ago)
Author:
pento
Message:

Blocks: Introduce the block parser.

The WP_Block_Parser class, and the accompanying parse_blocks() helper function, can be used to parse an array of blocks out of a content string.

WP_Block_Parser is copied from the @wordpress/block-serialization-default-parser package. To ensure it stays in sync with the JavaScript parser, changes should be implemented in the package first, then the package version should be upgraded to include the changes.

See #45109.

Location:
branches/5.0
Files:
215 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/blocks.php

    r43743 r43751  
    113113    return $dynamic_block_names;
    114114}
     115
     116/**
     117 * Parses blocks out of a content string.
     118 *
     119 * @since 5.0.0
     120 *
     121 * @param  string $content Post content.
     122 * @return array  Array of parsed block objects.
     123 */
     124function parse_blocks( $content ) {
     125    /*
     126     * If there are no blocks in the content, return a single block, rather
     127     * than wasting time trying to parse the string.
     128     */
     129    if ( ! has_blocks( $content ) ) {
     130        return array(
     131            array(
     132                'blockName'   => null,
     133                'attrs'       => array(),
     134                'innerBlocks' => array(),
     135                'innerHTML'   => $content,
     136            ),
     137        );
     138    }
     139
     140    /**
     141     * Filter to allow plugins to replace the server-side block parser
     142     *
     143     * @since 5.0.0
     144     *
     145     * @param string $parser_class Name of block parser class
     146     */
     147    $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
     148
     149    $parser = new $parser_class();
     150    return $parser->parse( $content );
     151}
  • branches/5.0/src/wp-settings.php

    r43742 r43751  
    246246require( ABSPATH . WPINC . '/class-wp-block-type.php' );
    247247require( ABSPATH . WPINC . '/class-wp-block-type-registry.php' );
     248require( ABSPATH . WPINC . '/class-wp-block-parser.php' );
    248249require( ABSPATH . WPINC . '/blocks.php' );
    249250
  • branches/5.0/tools/webpack/packages.js

    r43723 r43751  
    117117    };
    118118
     119    const phpFiles = {
     120        'block-serialization-default-parser/parser.php': 'wp-includes/class-wp-block-parser.php',
     121    };
     122
    119123    const externals = {
    120124        react: 'React',
     
    165169    } ) );
    166170
     171    const phpCopies = Object.keys( phpFiles ).map( ( filename ) => ( {
     172        from: join( baseDir, `node_modules/@wordpress/${ filename }` ),
     173        to: join( baseDir, `src/${ phpFiles[ filename ] }` ),
     174    } ) );
     175
    167176    const config = {
    168177        mode,
     
    233242                    ...vendorCopies,
    234243                    ...cssCopies,
     244                    ...phpCopies,
    235245                ],
    236246            ),
Note: See TracChangeset for help on using the changeset viewer.