PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / heading.php

heading.php in Gutenberg 22.9.0, at build/scripts/block-library/heading.php

57 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 gutenberg_register_block_core_heading() {
48 register_block_type_from_metadata(
49 __DIR__ . '/heading',
50 array(
51 'render_callback' => 'gutenberg_block_core_heading_render',
52 )
53 );
54 }
55
56 add_action( 'init', 'gutenberg_register_block_core_heading', 20 );
57