|
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 render_block_core_button( $attributes, $content ) { |
|
20 $p = new WP_HTML_Tag_Processor( $content ); |
|
21 |
|
22 /* |
|
23 * The button block can render an `<a>` or `<button>` and also has a |
|
24 * `<div>` wrapper. Find the a or button tag. |
|
25 */ |
|
26 $tag = null; |
|
27 while ( $p->next_tag() ) { |
|
28 $tag = $p->get_tag(); |
|
29 if ( 'A' === $tag || 'BUTTON' === $tag ) { |
|
30 break; |
|
31 } |
|
32 } |
|
33 |
|
34 /* |
|
35 * If this happens, the likelihood is there's no block content, |
|
36 * or the block has been modified by a plugin. |
|
37 */ |
|
38 if ( null === $tag ) { |
|
39 return $content; |
|
40 } |
|
41 |
|
42 // If the next token is the closing tag, the button is empty. |
|
43 $is_empty = true; |
|
44 while ( $p->next_token() && $tag !== $p->get_token_name() && $is_empty ) { |
|
45 if ( '#comment' !== $p->get_token_type() ) { |
|
46 /** |
|
47 * Anything else implies this is not empty. |
|
48 * This might include any text content (including a space), |
|
49 * inline images or other HTML. |
|
50 */ |
|
51 $is_empty = false; |
|
52 } |
|
53 } |
|
54 |
|
55 /* |
|
56 * When there's no text, render nothing for the block. |
|
57 * See https://github.com/WordPress/gutenberg/issues/17221 for the |
|
58 * reasoning behind this. |
|
59 */ |
|
60 if ( $is_empty ) { |
|
61 return ''; |
|
62 } |
|
63 |
|
64 return $content; |
|
65 } |
|
66 |
|
67 /** |
|
68 * Registers the `core/button` block on server. |
|
69 * |
|
70 * @since 6.6.0 |
|
71 */ |
|
72 function register_block_core_button() { |
|
73 register_block_type_from_metadata( |
|
74 __DIR__ . '/button', |
|
75 array( |
|
76 'render_callback' => 'render_block_core_button', |
|
77 ) |
|
78 ); |
|
79 } |
|
80 add_action( 'init', 'register_block_core_button' ); |