Making WordPress.org

Ticket #5303: single-parent-block.diff

File single-parent-block.diff, 1.1 KB (added by ryelle, 5 years ago)

Check that the plugin only exports a single parent-level block

  • cli/class-block-plugin-checker.php

     
    478478        }
    479479
    480480        /**
     481         * Check for a single parent block
     482         */
     483        function check_for_single_parent() {
     484                if ( empty( $this->blocks ) || ! is_array( $this->blocks ) ) {
     485                        return;
     486                }
     487
     488                $top_level_blocks = array_filter(
     489                        $this->blocks,
     490                        function( $block ) {
     491                                return ! isset( $block->parent ) || is_null( $block->parent );
     492                        }
     493                );
     494
     495                if ( count( $top_level_blocks ) > 1 ) {
     496                        $list = array_map(
     497                                function( $block ) {
     498                                        return ! empty( $block->title ) ? $block->title : $block->name;
     499                                },
     500                                $top_level_blocks
     501                        );
     502                        $this->record_result(
     503                                __FUNCTION__,
     504                                'warning',
     505                                sprintf(
     506                                        /* translators: %s is a list of block names. */
     507                                        __( 'More than one top level block was found: %s', 'wporg-plugins' ),
     508                                        implode( ', ', $list )
     509                                ),
     510                                $block_name
     511                        );
     512                }
     513        }
     514
     515        /**
    481516         * Do the script files all exist?
    482517         */
    483518        function check_for_block_script_files() {