| 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 |
|