parse_blocks example


function filter_block_content( $text$allowed_html = 'post', $allowed_protocols = array() ) {
    $result = '';

    if ( str_contains( $text, '<!--' ) && str_contains( $text, '--->' ) ) {
        $text = preg_replace_callback( '%<!--(.*?)--->%', '_filter_block_content_callback', $text );
    }

    $blocks = parse_blocks( $text );
    foreach ( $blocks as $block ) {
        $block   = filter_block_kses( $block$allowed_html$allowed_protocols );
        $result .= serialize_block( $block );
    }

    return $result;
}

/** * Callback used for regular expression replacement in filter_block_content(). * * @private * @since 6.2.1 * * @param array $matches Array of preg_replace_callback matches. * @return string Replacement string. */
$shortcode_attrs,
                        array(
                            'src' => array_values( array_unique( $srcs ) ),
                        )
                    );
                }
            }
        }
    }

    if ( has_block( 'gallery', $post->post_content ) ) {
        $post_blocks = parse_blocks( $post->post_content );

        while ( $block = array_shift( $post_blocks ) ) {
            $has_inner_blocks = ! empty( $block['innerBlocks'] );

            // Skip blocks with no blockName and no innerHTML.             if ( ! $block['blockName'] ) {
                continue;
            }

            // Skip non-Gallery blocks.             if ( 'core/gallery' !== $block['blockName'] ) {
                

function _inject_theme_attribute_in_block_template_content( $template_content ) {
    $has_updated_content = false;
    $new_content         = '';
    $template_blocks     = parse_blocks( $template_content );

    $blocks = _flatten_blocks( $template_blocks );
    foreach ( $blocks as &$block ) {
        if (
            'core/template-part' === $block['blockName'] &&
            ! isset( $block['attrs']['theme'] )
        ) {
            $block['attrs']['theme'] = get_stylesheet();
            $has_updated_content     = true;
        }
    }

    
// If `core/page-list` is not registered then return empty blocks.     $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();

    if ( class_exists( 'WP_Navigation_Fallback' ) ) {
        $navigation_post = WP_Navigation_Fallback::get_fallback();
    } else {
        $navigation_post = Gutenberg_Navigation_Fallback::get_fallback();
    }

    // Use the first non-empty Navigation as fallback if available.     if ( $navigation_post ) {
        $parsed_blocks  = parse_blocks( $navigation_post->post_content );
        $maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

        // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.         // In this case default to the (Page List) fallback.         $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
    }

    /** * Filters the fallback experience for the Navigation block. * * Returning a falsey value will opt out of the fallback and cause the block not to render. * To customise the blocks provided return an array of blocks - these should be valid * children of the `core/navigation` block. * * @since 5.9.0 * * @param array[] default fallback blocks provided by the default block mechanic. */
$template_slug = $page_slug;
                break;
            default:
                $template_slug = $post_slug;
                break;
        }
    }

    $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) );

    if ( ! empty( $current_template ) ) {
        $template_blocks    = parse_blocks( $current_template[0]->content );
        $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' );

        if ( ! empty( $post_content_block['attrs'] ) ) {
            return $post_content_block['attrs'];
        }
    }

    return array();
}

/** * Returns the contextualized block editor settings for a selected editor context. * * @since 5.8.0 * * @param array $custom_settings Custom settings to use with the given editor type. * @param WP_Block_Editor_Context $block_editor_context The current block editor context. * * @return array The contextualized block editor settings. */

function twenty_twenty_one_print_first_instance_of_block( $block_name$content = null, $instances = 1 ) {
    $instances_count = 0;
    $blocks_content  = '';

    if ( ! $content ) {
        $content = get_the_content();
    }

    // Parse blocks in the content.     $blocks = parse_blocks( $content );

    // Loop blocks.     foreach ( $blocks as $block ) {

        // Sanity check.         if ( ! isset( $block['blockName'] ) ) {
            continue;
        }

        // Check if this the block matches the $block_name.         $is_matching_block = false;

        

    private function get_dynamic_classname( $content ) {
        $blocks = parse_blocks( $content );

        $block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;

        switch ( $block_name ) {
            case 'core/paragraph':
                $classname = 'widget_block widget_text';
                break;
            case 'core/calendar':
                $classname = 'widget_block widget_calendar';
                break;
            case 'core/search':
                
Home | Imprint | This part of the site doesn't use cookies.