Ticket #42057: 42057.4.diff
File 42057.4.diff, 4.0 KB (added by , 6 years ago) |
---|
-
src/wp-includes/general-template.php
175 175 * search. To give a few examples of what it can be used for. 176 176 * 177 177 * @since 2.7.0 178 * @since 5.2.0 The $args array parameter was added in place of an $echo boolean flag. 178 179 * 179 * @param bool $echo Default to echo and not return the form. 180 * @return string|void String when $echo is false. 180 * @param array $args { 181 * Optional. Array of display arguments. 182 * 183 * @type bool $echo Whether to echo or return the form. Default true. 184 * @type string $aria_label Aria label for the search form. Useful to distinguish 185 * multiple search form in the same page and improve 186 * accessibility. Default empty. 187 * } 188 * @return string|void String when the $echo param is false. 181 189 */ 182 function get_search_form( $ echo = true) {190 function get_search_form( $args = array() ) { 183 191 /** 184 192 * Fires before the search form is retrieved, at the start of get_search_form(). 185 193 * … … 192 200 193 201 $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml'; 194 202 203 /* 204 * Back compat: to ensure previous uses of get_search_form continue to 205 * function as expected, we handle a value for the boolean $echo param removed 206 * in 5.2.0. Then we deal with the $args array and cast its defaults. 207 */ 208 $echo = true; 209 if ( false === $args ) { 210 $echo = false; 211 } 212 213 if ( ! is_array( $args ) ) { 214 // Set an empty array and allow default arguments to take over. 215 $args = array(); 216 } 217 218 // Defaults are to echo and to output no custom label on the form. 219 $defaults = array( 220 'echo' => $echo, 221 'aria_label' => '', 222 ); 223 224 $args = wp_parse_args( $args, $defaults ); 225 195 226 /** 227 * Filters the array of arguments used when generating the search form. 228 * 229 * @since 5.2.0 230 * 231 * @param array $args The array of arguments used when building the search form. 232 */ 233 $args = apply_filters( 'search_form_args', $args ); 234 235 /** 196 236 * Filters the HTML format of the search form. 197 237 * 198 238 * @since 3.6.0 … … 208 248 require( $search_form_template ); 209 249 $form = ob_get_clean(); 210 250 } else { 251 // Build a string containaing an aria-label to use for the search form. 252 if ( isset( $args['aria_label'] ) && ! empty( $args['aria_label'] ) ) { 253 $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" '; 254 } else { 255 /* 256 * If there's no custom aria-label, we can set a default here. At the 257 * moment it's empty as there's uncertainty about what the default should be. 258 */ 259 $aria_label = ''; 260 } 211 261 if ( 'html5' == $format ) { 212 $form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">262 $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '"> 213 263 <label> 214 264 <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span> 215 265 <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search …', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" /> … … 217 267 <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" /> 218 268 </form>'; 219 269 } else { 220 $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">270 $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '"> 221 271 <div> 222 272 <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label> 223 273 <input type="text" value="' . get_search_query() . '" name="s" id="s" /> … … 240 290 $result = $form; 241 291 } 242 292 243 if ( $echo) {293 if ( isset( $args['echo'] ) && true === $args['echo'] ) { 244 294 echo $result; 245 295 } else { 246 296 return $result;