WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.37k stars 4.15k forks source link

Check if class existing in parse_blocks #12551

Open spacedmonkey opened 5 years ago

spacedmonkey commented 5 years ago

Describe the bug If parse_blocks and gutenberg_parse_blocks gives you a filter to change the parser class. See

$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );

After this line, it should check to see if they class existings, because instantiating the class, to stop getting a fatal error.

swissspidy commented 5 years ago

Feels like a fatal error would be a really good indication for a developer that they're doing something wrong, no? :-) Returning an empty array or something would be like error silencing in this case.

spacedmonkey commented 5 years ago

I am more thinking about users if something goes wrong. How about something like this

function gutenberg_parse_blocks( $content ) {

                 $default = 'WP_Block_Parser';
        /**
         * Filter to allow plugins to replace the server-side block parser
         *
         * @since 3.8.0
         *
         * @param string $parser_class Name of block parser class
         */
        $parser_class = apply_filters( 'block_parser_class', $default );
        // Load default block parser for server-side parsing if the default parser class is being used.
                 if( ! class_exists( $parser_class ) ){
                      _doing_it_wrong( __FUNCTION__, 'Unable to find class $parser_class to parse content', '5.0.0' );
              $parser_class = $default;
                }
        if ( 'WP_Block_Parser' === $parser_class ) {
            require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';
        }

        $parser = new $parser_class();
        return $parser->parse( $content );
    }

Just so users never get a white screen and it still renders.

tomdevisser commented 1 year ago

@spacedmonkey After 4 years, has this already been resolved? And if not, could you summarize what's still needed for this issue to be closed?