summaryrefslogtreecommitdiffstats
path: root/wp-includes/blocks/heading.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:56:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:56:49 +0000
commita415c29efee45520ae252d2aa28f1083a521cd7b (patch)
treef4ade4b6668ecc0765de7e1424f7c1427ad433ff /wp-includes/blocks/heading.php
parentInitial commit. (diff)
downloadwordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.tar.xz
wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.zip
Adding upstream version 6.4.3+dfsg1.upstream/6.4.3+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/blocks/heading.php')
-rw-r--r--wp-includes/blocks/heading.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/wp-includes/blocks/heading.php b/wp-includes/blocks/heading.php
new file mode 100644
index 0000000..5a7e8db
--- /dev/null
+++ b/wp-includes/blocks/heading.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Appending the wp-block-heading to before rendering the stored `core/heading` block contents.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Adds a wp-block-heading class to the heading block content.
+ *
+ * For example, the following block content:
+ * <h2 class="align-left">Hello World</h2>
+ *
+ * Would be transformed to:
+ * <h2 class="align-left wp-block-heading">Hello World</h2>
+ *
+ * @param array $attributes Attributes of the block being rendered.
+ * @param string $content Content of the block being rendered.
+ *
+ * @return string The content of the block being rendered.
+ */
+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();
+}
+
+/**
+ * Registers the `core/heading` block on server.
+ */
+function register_block_core_heading() {
+ register_block_type_from_metadata(
+ __DIR__ . '/heading',
+ array(
+ 'render_callback' => 'block_core_heading_render',
+ )
+ );
+}
+
+add_action( 'init', 'register_block_core_heading' );