Making WordPress.org

Changeset 12356


Ignore:
Timestamp:
01/04/2023 06:31:00 AM (4 years ago)
Author:
tellyworth
Message:

Plugin Directory: better child block handling in validator.

Fixes #6465.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-block-plugin-checker.php

    r11675 r12356  
    453453                        'data' => $data,
    454454                );
     455        }
     456
     457        /**
     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;   
    455481        }
    456482
     
    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
Note: See TracChangeset for help on using the changeset viewer.