wp/wp-includes/blocks/heading.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Appending the wp-block-heading to before rendering the stored `core/heading` block contents.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Adds a wp-block-heading class to the heading block content.
       
    10  *
       
    11  * For example, the following block content:
       
    12  *  <h2 class="align-left">Hello World</h2>
       
    13  *
       
    14  * Would be transformed to:
       
    15  *  <h2 class="align-left wp-block-heading">Hello World</h2>
       
    16  *
       
    17  * @since 6.2.0
       
    18  *
       
    19  * @param array  $attributes Attributes of the block being rendered.
       
    20  * @param string $content Content of the block being rendered.
       
    21  *
       
    22  * @return string The content of the block being rendered.
       
    23  */
       
    24 function block_core_heading_render( $attributes, $content ) {
       
    25 	if ( ! $content ) {
       
    26 		return $content;
       
    27 	}
       
    28 
       
    29 	$p = new WP_HTML_Tag_Processor( $content );
       
    30 
       
    31 	$header_tags = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' );
       
    32 	while ( $p->next_tag() ) {
       
    33 		if ( in_array( $p->get_tag(), $header_tags, true ) ) {
       
    34 			$p->add_class( 'wp-block-heading' );
       
    35 			break;
       
    36 		}
       
    37 	}
       
    38 
       
    39 	return $p->get_updated_html();
       
    40 }
       
    41 
       
    42 /**
       
    43  * Registers the `core/heading` block on server.
       
    44  *
       
    45  * @since 6.2.0
       
    46  */
       
    47 function register_block_core_heading() {
       
    48 	register_block_type_from_metadata(
       
    49 		__DIR__ . '/heading',
       
    50 		array(
       
    51 			'render_callback' => 'block_core_heading_render',
       
    52 		)
       
    53 	);
       
    54 }
       
    55 
       
    56 add_action( 'init', 'register_block_core_heading' );