wp/wp-includes/blocks/search.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/blocks/search.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/blocks/search.php	Tue Dec 15 13:49:49 2020 +0100
@@ -15,7 +15,21 @@
 function render_block_core_search( $attributes ) {
 	static $instance_id = 0;
 
-	$input_id = 'wp-block-search__input-' . ++$instance_id;
+	// 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' ),
+		)
+	);
+
+	$input_id      = 'wp-block-search__input-' . ++$instance_id;
+	$label_markup  = '';
+	$button_markup = '';
 
 	if ( ! empty( $attributes['label'] ) ) {
 		$label_markup = sprintf(
@@ -23,10 +37,16 @@
 			$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" />',
+		'<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'] )
@@ -43,10 +63,13 @@
 	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>',
-		$class,
+		esc_attr( $class ),
 		esc_url( home_url( '/' ) ),
 		$label_markup . $input_markup . $button_markup
 	);
@@ -56,27 +79,11 @@
  * Registers the `core/search` block on the server.
  */
 function register_block_core_search() {
-	register_block_type(
-		'core/search',
+	register_block_type_from_metadata(
+		__DIR__ . '/search',
 		array(
-			'attributes'      => array(
-				'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' );