Make WordPress Core

Changeset 44116


Ignore:
Timestamp:
12/13/2018 05:39:59 PM (8 years ago)
Author:
desrosj
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.

Props pento.

Merges [43751] to trunk.

See #45109.

Location:
trunk
Files:
4 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/blocks.php

    r44109 r44116  
    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}
  • trunk/src/wp-settings.php

    r44108 r44116  
    247247require( ABSPATH . WPINC . '/class-wp-block-type.php' );
    248248require( ABSPATH . WPINC . '/class-wp-block-type-registry.php' );
     249require( ABSPATH . WPINC . '/class-wp-block-parser.php' );
    249250require( ABSPATH . WPINC . '/blocks.php' );
    250251
  • trunk/tools/webpack/packages.js

    r44114 r44116  
    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.