Making WordPress.org

Ticket #6465: 6465.diff

File 6465.diff, 1.4 KB (added by tellyworth, 2 years ago)

Add is_top_level_block function with parent/ancestor logic

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

     
    455455        }
    456456
    457457        /**
     458         * Determine whether a block is top-level or a child block.
     459         *
     460         * @param object $block The block in question.
     461         *
     462         * @return bool True if the block is top-level; false if it is a child block that can only be used within other blocks in the plugin.
     463         */
     464        public function is_top_level_block( $block ) {
     465                $ancestors = $block->parent ?? [];
     466                $ancestors = array_merge( $ancestors, $block->ancestor ?? [] );
     467
     468                // If it has no parent or ancestor blocks, it's top-level.
     469                if ( empty( $ancestors ) ) {
     470                        return true;
     471                }
     472
     473                foreach ( $ancestors as $ancestor_block_name ) {
     474                        // If it has an ancestor outside of this plugin, like `core/column`, then it's also top-level.
     475                        if ( ! isset( $this->blocks[ $ancestor_block_name ] ) ) {
     476                                return true;
     477                        }
     478                }
     479
     480                return false;   
     481        }
     482
     483        /**
    458484         * Check functions below here. Must be named `check_*()`.
    459485         */
    460486
     
    791817
    792818                $top_level_blocks = array_filter(
    793819                        $this->blocks,
    794                         function( $block ) {
    795                                 return ! isset( $block->parent ) || is_null( $block->parent );
    796                         }
     820                        array( $this, 'is_top_level_block' )
    797821                );
    798822
    799823                if ( count( $top_level_blocks ) > 1 ) {