WordPress/wp-includes/blocks/search.php
whyisjake 0861afcb29 Block Editor: Update WordPress Packages for WordPress 5.4.1
The following packages were updated:

@wordpress/block-directory 1.5.7 ➡️ 1.5.8
@wordpress/block-editor 3.7.7 ➡️ 3.7.8
@wordpress/block-library 2.14.7 ➡️ 2.14.8
@wordpress/edit-post 3.13.9 ➡️ 3.13.10
@wordpress/editor 9.12.7 ➡️ 9.12.8
@wordpress/format-library 1.14.7 ➡️ 1.14.8

Props whyisjake, youknowriad, toro_unit, ellatrix, roo2, noahtallen, arnaudbroes, chrisvanpatten, apeatling, mcsf, jorgefilipecosta, talldanwp, aduth, noisysocks.
Fixes #49974.

Built from https://develop.svn.wordpress.org/branches/5.4@47619


git-svn-id: http://core.svn.wordpress.org/branches/5.4@47394 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-04-24 15:32:14 +00:00

99 lines
2.3 KiB
PHP

<?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;
$input_id = 'wp-block-search__input-' . ++$instance_id;
$label_markup = '';
$button_markup = '';
if ( ! empty( $attributes['label'] ) ) {
$label_markup = sprintf(
'<label for="%s" class="wp-block-search__label">%s</label>',
$input_id,
$attributes['label']
);
} else {
$label_markup = sprintf(
'<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>',
$input_id,
__( 'Search' )
);
}
$input_markup = sprintf(
'<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />',
$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'];
}
if ( isset( $attributes['align'] ) ) {
$class .= ' align' . $attributes['align'];
}
return sprintf(
'<form class="%s" role="search" method="get" action="%s">%s</form>',
esc_attr( $class ),
esc_url( home_url( '/' ) ),
$label_markup . $input_markup . $button_markup
);
}
/**
* Registers the `core/search` block on the server.
*/
function register_block_core_search() {
register_block_type(
'core/search',
array(
'attributes' => array(
'align' => array(
'type' => 'string',
'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
),
'className' => array(
'type' => 'string',
),
'label' => array(
'type' => 'string',
'default' => __( 'Search' ),
),
'placeholder' => array(
'type' => 'string',
'default' => '',
),
'buttonText' => array(
'type' => 'string',
'default' => __( 'Search' ),
),
),
'render_callback' => 'render_block_core_search',
)
);
}
add_action( 'init', 'register_block_core_search' );