PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / search.php

search.php in Gutenberg 7.4.0, at build/block-library/blocks/search.php

99 lines 2.3 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/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 gutenberg_render_block_core_search( $attributes ) {
16 static $instance_id = 0;
17
18 $input_id = 'wp-block-search__input-' . ++$instance_id;
19 $label_markup = '';
20 $button_markup = '';
21
22 if ( ! empty( $attributes['label'] ) ) {
23 $label_markup = sprintf(
24 '<label for="%s" class="wp-block-search__label">%s</label>',
25 $input_id,
26 $attributes['label']
27 );
28 } else {
29 $label_markup = sprintf(
30 '<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>',
31 $input_id,
32 __( 'Search' )
33 );
34 }
35
36 $input_markup = sprintf(
37 '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />',
38 $input_id,
39 esc_attr( get_search_query() ),
40 esc_attr( $attributes['placeholder'] )
41 );
42
43 if ( ! empty( $attributes['buttonText'] ) ) {
44 $button_markup = sprintf(
45 '<button type="submit" class="wp-block-search__button">%s</button>',
46 $attributes['buttonText']
47 );
48 }
49
50 $class = 'wp-block-search';
51 if ( isset( $attributes['className'] ) ) {
52 $class .= ' ' . $attributes['className'];
53 }
54 if ( isset( $attributes['align'] ) ) {
55 $class .= ' align' . $attributes['align'];
56 }
57
58 return sprintf(
59 '<form class="%s" role="search" method="get" action="%s">%s</form>',
60 $class,
61 esc_url( home_url( '/' ) ),
62 $label_markup . $input_markup . $button_markup
63 );
64 }
65
66 /**
67 * Registers the `core/search` block on the server.
68 */
69 function gutenberg_register_block_core_search() {
70 register_block_type(
71 'core/search',
72 array(
73 'attributes' => array(
74 'align' => array(
75 'type' => 'string',
76 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
77 ),
78 'className' => array(
79 'type' => 'string',
80 ),
81 'label' => array(
82 'type' => 'string',
83 'default' => __( 'Search' ),
84 ),
85 'placeholder' => array(
86 'type' => 'string',
87 'default' => '',
88 ),
89 'buttonText' => array(
90 'type' => 'string',
91 'default' => __( 'Search' ),
92 ),
93 ),
94 'render_callback' => 'gutenberg_render_block_core_search',
95 )
96 );
97 }
98 add_action( 'init', 'gutenberg_register_block_core_search', 20 );
99