PluginProbe
Gutenberg / 18.9.0
Gutenberg v18.9.0
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / button.php

button.php in Gutenberg 18.9.0, at build/block-library/blocks/button.php

94 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/button` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/button` block on the server,
10 *
11 * @since 6.6.0
12 *
13 * @param array $attributes The block attributes.
14 * @param string $content The block content.
15 * @param WP_Block $block The block object.
16 *
17 * @return string The block content.
18 */
19 function gutenberg_render_block_core_button( $attributes, $content ) {
20 /*
21 * The current Gutenberg plugin supports WordPress 6.4, but the next_token()
22 * method does not exist in WordPress 6.4. Therefore, if Gutenberg is used
23 * as a plugin, use the Gutenberg class that has the `next_token()` method.
24 *
25 * TODO: After the Gutenberg plugin drops support for WordPress 6.4, this
26 * conditional statement will be removed and the core class `WP_HTML_Tag_Processor`
27 * should be used.
28 */
29 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && class_exists( 'Gutenberg_HTML_Tag_Processor_6_7' ) ) {
30 $p = new Gutenberg_HTML_Tag_Processor_6_7( $content );
31 } else {
32 $p = new WP_HTML_Tag_Processor( $content );
33 }
34
35 /*
36 * The button block can render an `<a>` or `<button>` and also has a
37 * `<div>` wrapper. Find the a or button tag.
38 */
39 $tag = null;
40 while ( $p->next_tag() ) {
41 $tag = $p->get_tag();
42 if ( 'A' === $tag || 'BUTTON' === $tag ) {
43 break;
44 }
45 }
46
47 /*
48 * If this happens, the likelihood is there's no block content,
49 * or the block has been modified by a plugin.
50 */
51 if ( null === $tag ) {
52 return $content;
53 }
54
55 // If the next token is the closing tag, the button is empty.
56 $is_empty = true;
57 while ( $p->next_token() && $tag !== $p->get_token_name() && $is_empty ) {
58 if ( '#comment' !== $p->get_token_type() ) {
59 /**
60 * Anything else implies this is not empty.
61 * This might include any text content (including a space),
62 * inline images or other HTML.
63 */
64 $is_empty = false;
65 }
66 }
67
68 /*
69 * When there's no text, render nothing for the block.
70 * See https://github.com/WordPress/gutenberg/issues/17221 for the
71 * reasoning behind this.
72 */
73 if ( $is_empty ) {
74 return '';
75 }
76
77 return $content;
78 }
79
80 /**
81 * Registers the `core/button` block on server.
82 *
83 * @since 6.6.0
84 */
85 function gutenberg_register_block_core_button() {
86 register_block_type_from_metadata(
87 __DIR__ . '/button',
88 array(
89 'render_callback' => 'gutenberg_render_block_core_button',
90 )
91 );
92 }
93 add_action( 'init', 'gutenberg_register_block_core_button', 20 );
94