2019-03-07 04:12:51 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Server-side rendering of the `core/search` block.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically renders the `core/search` block.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return string The search block markup.
|
|
|
|
*/
|
|
|
|
function render_block_core_search( $attributes ) {
|
|
|
|
static $instance_id = 0;
|
|
|
|
|
2020-07-14 07:39:09 -04:00
|
|
|
// Older versions of the Search block defaulted the label and buttonText
|
|
|
|
// attributes to `__( 'Search' )` meaning that many posts contain `<!--
|
|
|
|
// wp:search /-->`. Support these by defaulting an undefined label and
|
|
|
|
// buttonText to `__( 'Search' )`.
|
|
|
|
$attributes = wp_parse_args(
|
|
|
|
$attributes,
|
|
|
|
array(
|
|
|
|
'label' => __( 'Search' ),
|
|
|
|
'buttonText' => __( 'Search' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-19 11:47:58 -04:00
|
|
|
$input_id = 'wp-block-search__input-' . ++$instance_id;
|
|
|
|
$label_markup = '';
|
|
|
|
$button_markup = '';
|
2019-03-07 04:12:51 -05:00
|
|
|
|
|
|
|
if ( ! empty( $attributes['label'] ) ) {
|
|
|
|
$label_markup = sprintf(
|
|
|
|
'<label for="%s" class="wp-block-search__label">%s</label>',
|
|
|
|
$input_id,
|
|
|
|
$attributes['label']
|
|
|
|
);
|
2020-01-08 07:10:04 -05:00
|
|
|
} else {
|
|
|
|
$label_markup = sprintf(
|
|
|
|
'<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>',
|
|
|
|
$input_id,
|
|
|
|
__( 'Search' )
|
|
|
|
);
|
2019-03-07 04:12:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$input_markup = sprintf(
|
2020-02-06 16:42:06 -05:00
|
|
|
'<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />',
|
2019-03-07 04:12:51 -05:00
|
|
|
$input_id,
|
|
|
|
esc_attr( get_search_query() ),
|
|
|
|
esc_attr( $attributes['placeholder'] )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! empty( $attributes['buttonText'] ) ) {
|
|
|
|
$button_markup = sprintf(
|
|
|
|
'<button type="submit" class="wp-block-search__button">%s</button>',
|
|
|
|
$attributes['buttonText']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = 'wp-block-search';
|
|
|
|
if ( isset( $attributes['className'] ) ) {
|
|
|
|
$class .= ' ' . $attributes['className'];
|
|
|
|
}
|
2019-09-19 11:47:58 -04:00
|
|
|
if ( isset( $attributes['align'] ) ) {
|
|
|
|
$class .= ' align' . $attributes['align'];
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:12:51 -05:00
|
|
|
return sprintf(
|
|
|
|
'<form class="%s" role="search" method="get" action="%s">%s</form>',
|
2020-05-05 19:56:33 -04:00
|
|
|
esc_attr( $class ),
|
2019-03-07 04:12:51 -05:00
|
|
|
esc_url( home_url( '/' ) ),
|
|
|
|
$label_markup . $input_markup . $button_markup
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `core/search` block on the server.
|
|
|
|
*/
|
|
|
|
function register_block_core_search() {
|
2020-06-26 09:33:47 -04:00
|
|
|
register_block_type_from_metadata(
|
|
|
|
__DIR__ . '/search',
|
2019-03-07 04:12:51 -05:00
|
|
|
array(
|
|
|
|
'render_callback' => 'render_block_core_search',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
add_action( 'init', 'register_block_core_search' );
|