Making WordPress.org

Changeset 12351


Ignore:
Timestamp:
12/20/2022 05:41:00 AM (14 months ago)
Author:
dd32
Message:

wp-themes.com: Don't display patterns with third party blocks.

Unprops dd32 for missing this in [12349].

Props dufresnesteven.
See #6420.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp-themes.com/public_html/wp-content/plugins/pattern-page/inc/pattern-endpoint.php

    r12349 r12351  
    33namespace WordPressdotorg\PatternPreview\PatternEndpoint;
    44
     5/**
     6 * Recurses the blocks and returns block names.
     7 *
     8 * @param WP_Block $block
     9 * @param array    $names
     10 * @return array List of block names.
     11 */
     12function get_all_block_names( $block, $names = array() ) {
     13    // HTML blocks can have a null block name
     14    // It won't have any innerBlocks.
     15    if ( empty( $block['blockName'] ) ) {
     16        return $names;
     17    }
     18
     19    $names[] = $block['blockName'];
     20
     21    // Blocks have inner blocks that we need to recurse through
     22    foreach ( $block['innerBlocks'] as $inner_block ) {
     23        return get_all_block_names( $inner_block, $names );
     24    }
     25
     26    return $names;
     27}
     28
     29/**
     30 * Return whether the block or its innerBlocks contains a non core block.
     31 *
     32 * @param WP_Block $block
     33 * @return boolean
     34 */
     35function has_unsupported_block( $block ) {
     36    $block_names = get_all_block_names( $block );
     37
     38    // We only want "core" blocks.
     39    $unsupported = array_filter(
     40        $block_names,
     41        function ( $block_name ) {
     42            return ! str_starts_with( $block_name, 'core' );
     43        }
     44    );
     45
     46    return ! empty( $unsupported );
     47}
     48
     49/**
     50 * Returns patterns that only contain core blocks.
     51 *
     52 * @param array[] $patterns List of block patterns
     53 * @return array[]
     54 */
     55function get_supported_patterns( $patterns ) {
     56    $supported_patterns = array();
     57
     58    foreach ( $patterns as $pattern ) {
     59        $blocks       = parse_blocks( $pattern['content'] );
     60        $is_supported = true;
     61
     62        // Filter out any core patterns
     63        if ( str_starts_with( $pattern['name'], 'core/' ) ) {
     64            $is_supported = false;
     65        }
     66
     67        // Filter out any "no inserter" patterns
     68        if ( isset( $pattern['inserter'] ) && false === $pattern['inserter'] ) {
     69            $is_supported = false;
     70        }
     71
     72        // Filter out patterns with unsupported blocks
     73        foreach ( $blocks as $block ) {
     74            if ( has_unsupported_block( $block ) ) {
     75                $is_supported = false;
     76            }
     77        }
     78
     79        if ( $is_supported ) {
     80            $supported_patterns[] = $pattern;
     81        }
     82    }
     83
     84    return $supported_patterns;
     85}
     86
    587function endpoint_handler() {
    688    $patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
    7 
    8     $theme_patterns = array_filter(
    9         $patterns,
    10         function ( $pattern ) {
    11             return ! str_starts_with( $pattern['name'], 'core/' ) && ( ! isset( $pattern['inserter'] ) || false !== $pattern['inserter'] );
    12         }
    13     );
     89    $theme_patterns = get_supported_patterns( $patterns );
    1490
    1591    /**
Note: See TracChangeset for help on using the changeset viewer.