PluginProbe
Gutenberg / 10.1.0
Gutenberg v10.1.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 / search.php

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

162 lines 4.9 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 // 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 $classnames = gutenberg_classnames_for_block_core_search( $attributes );
32 $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
33 $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
34 $show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
35 $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
36 $label_markup = '';
37 $input_markup = '';
38 $button_markup = '';
39 $width_styles = '';
40
41 if ( $show_label ) {
42 if ( ! empty( $attributes['label'] ) ) {
43 $label_markup = sprintf(
44 '<label for="%s" class="wp-block-search__label">%s</label>',
45 $input_id,
46 $attributes['label']
47 );
48 } else {
49 $label_markup = sprintf(
50 '<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>',
51 $input_id,
52 __( 'Search' )
53 );
54 }
55 }
56
57 if ( $show_input ) {
58 $input_markup = sprintf(
59 '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />',
60 $input_id,
61 esc_attr( get_search_query() ),
62 esc_attr( $attributes['placeholder'] )
63 );
64 }
65
66 if ( $show_button ) {
67 $button_internal_markup = '';
68 $button_classes = '';
69
70 if ( ! $use_icon_button ) {
71 if ( ! empty( $attributes['buttonText'] ) ) {
72 $button_internal_markup = $attributes['buttonText'];
73 }
74 } else {
75 $button_classes .= 'has-icon';
76 $button_internal_markup =
77 '<svg id="search-icon" class="search-icon" viewBox="0 0 24 24" width="24" height="24">
78 <path d="M13.5 6C10.5 6 8 8.5 8 11.5c0 1.1.3 2.1.9 3l-3.4 3 1 1.1 3.4-2.9c1 .9 2.2 1.4 3.6 1.4 3 0 5.5-2.5 5.5-5.5C19 8.5 16.5 6 13.5 6zm0 9.5c-2.2 0-4-1.8-4-4s1.8-4 4-4 4 1.8 4 4-1.8 4-4 4z"></path>
79 </svg>';
80 }
81
82 $button_markup = sprintf(
83 '<button type="submit"class="wp-block-search__button ' . $button_classes . '">%s</button>',
84 $button_internal_markup
85 );
86 }
87
88 if ( ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] ) ) {
89 if ( ! empty( $attributes['buttonPosition'] ) && 'button-only' !== $attributes['buttonPosition'] ) {
90 $width_styles = ' style="width: ' . $attributes['width'] . $attributes['widthUnit'] . ';"';
91 }
92 }
93
94 $field_markup = sprintf(
95 '<div class="wp-block-search__inside-wrapper"%s>%s</div>',
96 $width_styles,
97 $input_markup . $button_markup
98 );
99 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
100
101 return sprintf(
102 '<form role="search" method="get" action="%s" %s>%s</form>',
103 esc_url( home_url( '/' ) ),
104 $wrapper_attributes,
105 $label_markup . $field_markup
106 );
107 }
108
109 /**
110 * Registers the `core/search` block on the server.
111 */
112 function gutenberg_register_block_core_search() {
113 register_block_type_from_metadata(
114 __DIR__ . '/search',
115 array(
116 'render_callback' => 'gutenberg_render_block_core_search',
117 )
118 );
119 }
120 add_action( 'init', 'gutenberg_register_block_core_search', 20 );
121
122 /**
123 * Builds the correct top level classnames for the 'core/search' block.
124 *
125 * @param array $attributes The block attributes.
126 *
127 * @return string The classnames used in the block.
128 */
129 function gutenberg_classnames_for_block_core_search( $attributes ) {
130 $classnames = array();
131
132 if ( ! empty( $attributes['buttonPosition'] ) ) {
133 if ( 'button-inside' === $attributes['buttonPosition'] ) {
134 $classnames[] = 'wp-block-search__button-inside';
135 }
136
137 if ( 'button-outside' === $attributes['buttonPosition'] ) {
138 $classnames[] = 'wp-block-search__button-outside';
139 }
140
141 if ( 'no-button' === $attributes['buttonPosition'] ) {
142 $classnames[] = 'wp-block-search__no-button';
143 }
144
145 if ( 'button-only' === $attributes['buttonPosition'] ) {
146 $classnames[] = 'wp-block-search__button-only';
147 }
148 }
149
150 if ( isset( $attributes['buttonUseIcon'] ) ) {
151 if ( ! empty( $attributes['buttonPosition'] ) && 'no-button' !== $attributes['buttonPosition'] ) {
152 if ( $attributes['buttonUseIcon'] ) {
153 $classnames[] = 'wp-block-search__icon-button';
154 } else {
155 $classnames[] = 'wp-block-search__text-button';
156 }
157 }
158 }
159
160 return implode( ' ', $classnames );
161 }
162