WP_HTML_Tag_Processor example

'style' => block_core_social_link_get_color_styles( $block->context ),
        )
    );

    $link  = '<li ' . $wrapper_attributes . '>';
    $link .= '<a href="' . esc_url( $url ) . '" class="wp-block-social-link-anchor">';
    $link .= $icon;
    $link .= '<span class="wp-block-social-link-label' . ( $show_labels ? '' : ' screen-reader-text' ) . '">';
    $link .= esc_html( $label );
    $link .= '</span></a></li>';

    $processor = new WP_HTML_Tag_Processor( $link );
    $processor->next_tag( 'a' );
    if ( $open_in_new_tab ) {
        $processor->set_attribute( 'rel', esc_attr( $rel ) . ' noopener nofollow' );
        $processor->set_attribute( 'target', '_blank' );
    } elseif ( '' !== $rel ) {
        $processor->set_attribute( 'rel', esc_attr( $rel ) );
    }
    return $processor->get_updated_html();
}

/** * Registers the `core/social-link` blocks. */
$style_attribute = '';
        if ( array_key_exists( 'style', $colors_supports ) ) {
            $style_attribute = $colors_supports['style'];
        }

        $inner_blocks_html = '';
        foreach ( $block->inner_blocks as $inner_block ) {
            $inner_blocks_html .= $inner_block->render();
        }

        if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
            $tag_processor = new WP_HTML_Tag_Processor( $html );
            while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) {
                $tag_processor->add_class( 'current-menu-ancestor' );
            }
            $html = $tag_processor->get_updated_html();
        }

        $wrapper_attributes = get_block_wrapper_attributes(
            array(
                'class' => $css_classes,
                'style' => $style_attribute,
            )
        );
'<ul %1$s>',
                $container_attributes
            );
        }

        if ( ! $is_list_item && $is_list_open ) {
            $is_list_open       = false;
            $inner_blocks_html .= '</ul>';
        }

        $inner_block_content = $inner_block->render();
        $p                   = new WP_HTML_Tag_Processor( $inner_block_content );
        if ( $p->next_tag(
            array(
                'name'       => 'LI',
                'class_name' => 'has-child',
            )
        ) ) {
            $has_submenus = true;
        }
        if ( ! empty( $inner_block_content ) ) {
            if ( in_array( $inner_block->name, $needs_list_item_wrapper, true ) ) {
                $inner_blocks_html .= '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>';
            }
return $block_content;
    }

    // return early if no settings are found on the block attributes.     $block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null );
    if ( empty( $block_settings ) ) {
        return $block_content;
    }

    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.     // Add the class name to the first element, presuming it's the wrapper, if it exists.     $tags = new WP_HTML_Tag_Processor( $block_content );
    if ( $tags->next_tag() ) {
        $tags->add_class( _wp_get_presets_class_name( $block ) );
    }

    return $tags->get_updated_html();
}

/** * Render the block level presets stylesheet. * * @internal * * @since 6.2.0 * @since 6.3.0 Updated preset styles to use Selectors API. * @access private * * @param string|null $pre_render The pre-rendered content. Default null. * @param array $block The block being rendered. * * @return null */

        } elseif ( $has_global_styles_duotone ) {
            $slug         = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.             $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.             $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
            // CSS custom property, SVG filter, and block CSS.             self::enqueue_global_styles_preset( $filter_id$duotone_selector$filter_value );
        }

        // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper.         $tags = new WP_HTML_Tag_Processor( $block_content );
        if ( $tags->next_tag() ) {
            $tags->add_class( $filter_id );
        }
        return $tags->get_updated_html();
    }

    /** * Appends the used block duotone filter declarations to the inline block supports CSS. * * Uses the declarations saved in earlier calls to self::enqueue_block_css. * * @since 6.3.0 */
$button              = '';
    $query_params_markup = '';
    $inline_styles       = styles_for_block_core_search( $attributes );
    $color_classes       = get_color_classes_for_block_core_search( $attributes );
    $typography_classes  = get_typography_classes_for_block_core_search( $attributes );
    $is_button_inside    = ! empty( $attributes['buttonPosition'] ) &&
        'button-inside' === $attributes['buttonPosition'];
    // Border color classes need to be applied to the elements that have a border color.     $border_color_classes = get_border_color_classes_for_block_core_search( $attributes );

    $label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
    $label            = new WP_HTML_Tag_Processor( sprintf( '<label %1$s>%2$s</label>', $inline_styles['label']$label_inner_html ) );
    if ( $label->next_tag() ) {
        $label->set_attribute( 'for', $input_id );
        $label->add_class( 'wp-block-search__label' );
        if ( $show_label && ! empty( $attributes['label'] ) ) {
            if ( ! empty( $typography_classes ) ) {
                $label->add_class( $typography_classes );
            }
        } else {
            $label->add_class( 'screen-reader-text' );
        }
    }

    

function block_core_heading_render( $attributes$content ) {
    if ( ! $content ) {
        return $content;
    }

    $p = new WP_HTML_Tag_Processor( $content );

    $header_tags = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' );
    while ( $p->next_tag() ) {
        if ( in_array( $p->get_tag()$header_tags, true ) ) {
            $p->add_class( 'wp-block-heading' );
            break;
        }
    }

    return $p->get_updated_html();
}

return ''; // Return early if no custom logo is set, avoiding extraneous wrapper div.     }

    if ( ! $attributes['isLink'] ) {
        // Remove the link.         $custom_logo = preg_replace( '#<a.*?>(.*?)</a>#i', '\1', $custom_logo );
    }

    if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
        // Add the link target after the rel="home".         // Add an aria-label for informing that the page opens in a new tab.         $processor = new WP_HTML_Tag_Processor( $custom_logo );
        $processor->next_tag( 'a' );
        if ( 'home' === $processor->get_attribute( 'rel' ) ) {
            $processor->set_attribute( 'aria-label', __( '(Home link, opens in a new tab)' ) );
            $processor->set_attribute( 'target', $attributes['linkTarget'] );
        }
        $custom_logo = $processor->get_updated_html();
    }

    $classnames = array();
    if ( empty( $attributes['width'] ) ) {
        $classnames[] = 'is-default-size';
    }
array(
                'context'  => 'block-supports',
                'prettify' => false,
            )
        );

        $outer_class_names[] = $container_content_class;
    }

    // Return early if only child layout exists.     if ( ! $support_layout && ! empty( $outer_class_names ) ) {
        $content = new WP_HTML_Tag_Processor( $block_content );
        $content->next_tag();
        $content->add_class( implode( ' ', $outer_class_names ) );
        return (string) $content;
    }

    $global_settings = wp_get_global_settings();
    $fallback_layout = ! empty( _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) ) ? _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) : _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
    $used_layout     = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;

    $class_names        = array();
    $layout_definitions = wp_get_layout_definitions();
    
$gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;

        // Get spacing CSS variable from preset value if provided.         if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
            $index_to_splice = strrpos( $gap, '|' ) + 1;
            $slug            = _wp_to_kebab_case( substr( $gap$index_to_splice ) );
            $gap             = "var(--wp--preset--spacing--$slug)";
        }
    }

    $unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );
    $processed_content        = new WP_HTML_Tag_Processor( $content );
    $processed_content->next_tag();
    $processed_content->add_class( $unique_gallery_classname );

    // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default     // gap on the gallery.     $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
    $gap_value    = $gap ? $gap : $fallback_gap;
    $gap_column   = $gap_value;

    if ( is_array( $gap_value ) ) {
        $gap_row    = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;
        

    if ( null === $link_color && null === $hover_link_color ) {
        return $block_content;
    }

    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.     // Add the class name to the first element, presuming it's the wrapper, if it exists.     $tags = new WP_HTML_Tag_Processor( $block_content );
    if ( $tags->next_tag() ) {
        $tags->add_class( wp_get_elements_class_name( $block ) );
    }

    return $tags->get_updated_html();
}

/** * Renders the elements stylesheet. * * In the case of nested blocks we want the parent element styles to be rendered before their descendants. * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant: * we want the descendant style to take priority, and this is done by loading it after, in DOM order. * * @since 6.0.0 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. * @access private * * @param string|null $pre_render The pre-rendered content. Default null. * @param array $block The block being rendered. * @return null */
$content = substr( $content, 0, $offset ) . $image . substr( $content$offset );
        }
    } else {
        if ( in_the_loop() ) {
            update_post_thumbnail_cache();
        }
        $current_featured_image = get_the_post_thumbnail_url();
        if ( ! $current_featured_image ) {
            return $content;
        }

        $processor = new WP_HTML_Tag_Processor( $content );
        $processor->next_tag();

        $styles         = $processor->get_attribute( 'style' );
        $merged_styles  = ! empty( $styles ) ? $styles . ';' : '';
        $merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';

        $processor->set_attribute( 'style', $merged_styles );
        $content = $processor->get_updated_html();
    }

    return $content;
}
$filename
                )
                : __( 'PDF embed' );

            return str_replace( $matches['attribute']sprintf( 'aria-label="%s"', $label )$matches[0] );
        },
        $content
    );

    // If it uses the Interactivity API, add the directives.     if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $should_load_view_script ) {
        $processor = new WP_HTML_Tag_Processor( $content );
        $processor->next_tag();
        $processor->set_attribute( 'data-wp-interactive', '' );
        $processor->next_tag( 'object' );
        $processor->set_attribute( 'data-wp-bind--hidden', '!selectors.core.file.hasPdfPreview' );
        $processor->set_attribute( 'hidden', true );
        return $processor->get_updated_html();
    }

    return $content;
}

/** * Renders the `core/image` block on the server, * adding a data-id attribute to the element if core/gallery has added on pre-render. * * @param array $attributes The block attributes. * @param string $content The block content. * @return string Returns the block content with the data-id attribute added. */
function render_block_core_image( $attributes$content ) {

    $processor = new WP_HTML_Tag_Processor( $content );
    $processor->next_tag( 'img' );

    if ( $processor->get_attribute( 'src' ) === null ) {
        return '';
    }

    if ( isset( $attributes['data-id'] ) ) {
        // Add the data-id="$id" attribute to the img element         // to provide backwards compatibility for the Gallery Block,         // which now wraps Image Blocks within innerBlocks.         // The data-id attribute is added in a core/gallery `render_block_data` hook.

        wp_style_engine_get_stylesheet_from_css_rules(
            $position_styles,
            array(
                'context'  => 'block-supports',
                'prettify' => false,
            )
        );

        // Inject class name to block container markup.         $content = new WP_HTML_Tag_Processor( $block_content );
        $content->next_tag();
        foreach ( $wrapper_classes as $class ) {
            $content->add_class( $class );
        }
        return (string) $content;
    }

    return $block_content;
}

// Register the block support.
Home | Imprint | This part of the site doesn't use cookies.