13 * @return string The search block markup. |
13 * @return string The search block markup. |
14 */ |
14 */ |
15 function render_block_core_search( $attributes ) { |
15 function render_block_core_search( $attributes ) { |
16 static $instance_id = 0; |
16 static $instance_id = 0; |
17 |
17 |
18 $input_id = 'wp-block-search__input-' . ++$instance_id; |
18 // Older versions of the Search block defaulted the label and buttonText |
|
19 // attributes to `__( 'Search' )` meaning that many posts contain `<!-- |
|
20 // wp:search /-->`. Support these by defaulting an undefined label and |
|
21 // buttonText to `__( 'Search' )`. |
|
22 $attributes = wp_parse_args( |
|
23 $attributes, |
|
24 array( |
|
25 'label' => __( 'Search' ), |
|
26 'buttonText' => __( 'Search' ), |
|
27 ) |
|
28 ); |
|
29 |
|
30 $input_id = 'wp-block-search__input-' . ++$instance_id; |
|
31 $label_markup = ''; |
|
32 $button_markup = ''; |
19 |
33 |
20 if ( ! empty( $attributes['label'] ) ) { |
34 if ( ! empty( $attributes['label'] ) ) { |
21 $label_markup = sprintf( |
35 $label_markup = sprintf( |
22 '<label for="%s" class="wp-block-search__label">%s</label>', |
36 '<label for="%s" class="wp-block-search__label">%s</label>', |
23 $input_id, |
37 $input_id, |
24 $attributes['label'] |
38 $attributes['label'] |
25 ); |
39 ); |
|
40 } else { |
|
41 $label_markup = sprintf( |
|
42 '<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>', |
|
43 $input_id, |
|
44 __( 'Search' ) |
|
45 ); |
26 } |
46 } |
27 |
47 |
28 $input_markup = sprintf( |
48 $input_markup = sprintf( |
29 '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />', |
49 '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />', |
30 $input_id, |
50 $input_id, |
31 esc_attr( get_search_query() ), |
51 esc_attr( get_search_query() ), |
32 esc_attr( $attributes['placeholder'] ) |
52 esc_attr( $attributes['placeholder'] ) |
33 ); |
53 ); |
34 |
54 |
41 |
61 |
42 $class = 'wp-block-search'; |
62 $class = 'wp-block-search'; |
43 if ( isset( $attributes['className'] ) ) { |
63 if ( isset( $attributes['className'] ) ) { |
44 $class .= ' ' . $attributes['className']; |
64 $class .= ' ' . $attributes['className']; |
45 } |
65 } |
|
66 if ( isset( $attributes['align'] ) ) { |
|
67 $class .= ' align' . $attributes['align']; |
|
68 } |
46 |
69 |
47 return sprintf( |
70 return sprintf( |
48 '<form class="%s" role="search" method="get" action="%s">%s</form>', |
71 '<form class="%s" role="search" method="get" action="%s">%s</form>', |
49 $class, |
72 esc_attr( $class ), |
50 esc_url( home_url( '/' ) ), |
73 esc_url( home_url( '/' ) ), |
51 $label_markup . $input_markup . $button_markup |
74 $label_markup . $input_markup . $button_markup |
52 ); |
75 ); |
53 } |
76 } |
54 |
77 |
55 /** |
78 /** |
56 * Registers the `core/search` block on the server. |
79 * Registers the `core/search` block on the server. |
57 */ |
80 */ |
58 function register_block_core_search() { |
81 function register_block_core_search() { |
59 register_block_type( |
82 register_block_type_from_metadata( |
60 'core/search', |
83 __DIR__ . '/search', |
61 array( |
84 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', |
85 'render_callback' => 'render_block_core_search', |
78 ) |
86 ) |
79 ); |
87 ); |
80 } |
88 } |
81 |
|
82 add_action( 'init', 'register_block_core_search' ); |
89 add_action( 'init', 'register_block_core_search' ); |