|
1 <?php |
|
2 /** |
|
3 * Server-side rendering of the `core/search` block. |
|
4 * |
|
5 * @package WordPress |
|
6 */ |
|
7 |
|
8 /** |
|
9 * Dynamically renders the `core/search` block. |
|
10 * |
|
11 * @param array $attributes The block attributes. |
|
12 * |
|
13 * @return string The search block markup. |
|
14 */ |
|
15 function render_block_core_search( $attributes ) { |
|
16 static $instance_id = 0; |
|
17 |
|
18 $input_id = 'wp-block-search__input-' . ++$instance_id; |
|
19 |
|
20 if ( ! empty( $attributes['label'] ) ) { |
|
21 $label_markup = sprintf( |
|
22 '<label for="%s" class="wp-block-search__label">%s</label>', |
|
23 $input_id, |
|
24 $attributes['label'] |
|
25 ); |
|
26 } |
|
27 |
|
28 $input_markup = sprintf( |
|
29 '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />', |
|
30 $input_id, |
|
31 esc_attr( get_search_query() ), |
|
32 esc_attr( $attributes['placeholder'] ) |
|
33 ); |
|
34 |
|
35 if ( ! empty( $attributes['buttonText'] ) ) { |
|
36 $button_markup = sprintf( |
|
37 '<button type="submit" class="wp-block-search__button">%s</button>', |
|
38 $attributes['buttonText'] |
|
39 ); |
|
40 } |
|
41 |
|
42 $class = 'wp-block-search'; |
|
43 if ( isset( $attributes['className'] ) ) { |
|
44 $class .= ' ' . $attributes['className']; |
|
45 } |
|
46 |
|
47 return sprintf( |
|
48 '<form class="%s" role="search" method="get" action="%s">%s</form>', |
|
49 $class, |
|
50 esc_url( home_url( '/' ) ), |
|
51 $label_markup . $input_markup . $button_markup |
|
52 ); |
|
53 } |
|
54 |
|
55 /** |
|
56 * Registers the `core/search` block on the server. |
|
57 */ |
|
58 function register_block_core_search() { |
|
59 register_block_type( |
|
60 'core/search', |
|
61 array( |
|
62 'attributes' => array( |
|
63 'label' => array( |
|
64 'type' => 'string', |
|
65 'default' => __( 'Search' ), |
|
66 ), |
|
67 'placeholder' => array( |
|
68 'type' => 'string', |
|
69 'default' => '', |
|
70 ), |
|
71 'buttonText' => array( |
|
72 'type' => 'string', |
|
73 'default' => __( 'Search' ), |
|
74 ), |
|
75 ), |
|
76 |
|
77 'render_callback' => 'render_block_core_search', |
|
78 ) |
|
79 ); |
|
80 } |
|
81 |
|
82 add_action( 'init', 'register_block_core_search' ); |