wp/wp-includes/blocks/query-title.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     8 /**
     8 /**
     9  * Renders the `core/query-title` block on the server.
     9  * Renders the `core/query-title` block on the server.
    10  * For now it only supports Archive title,
    10  * For now it only supports Archive title,
    11  * using queried object information
    11  * using queried object information
    12  *
    12  *
       
    13  * @since 5.8.0
       
    14  *
    13  * @param array $attributes Block attributes.
    15  * @param array $attributes Block attributes.
    14  *
    16  *
    15  * @return string Returns the query title based on the queried object.
    17  * @return string Returns the query title based on the queried object.
    16  */
    18  */
    17 function render_block_core_query_title( $attributes ) {
    19 function render_block_core_query_title( $attributes ) {
    18 	$type       = isset( $attributes['type'] ) ? $attributes['type'] : null;
    20 	$type       = isset( $attributes['type'] ) ? $attributes['type'] : null;
    19 	$is_archive = is_archive();
    21 	$is_archive = is_archive();
    20 	if ( ! $type || ( 'archive' === $type && ! $is_archive ) ) {
    22 	$is_search  = is_search();
       
    23 	if ( ! $type ||
       
    24 		( 'archive' === $type && ! $is_archive ) ||
       
    25 		( 'search' === $type && ! $is_search )
       
    26 		) {
    21 		return '';
    27 		return '';
    22 	}
    28 	}
    23 	$title = '';
    29 	$title = '';
    24 	if ( $is_archive ) {
    30 	if ( $is_archive ) {
    25 		$title = get_the_archive_title();
    31 		$show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
       
    32 		if ( ! $show_prefix ) {
       
    33 			add_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
       
    34 			$title = get_the_archive_title();
       
    35 			remove_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
       
    36 		} else {
       
    37 			$title = get_the_archive_title();
       
    38 		}
    26 	}
    39 	}
       
    40 	if ( $is_search ) {
       
    41 		$title = __( 'Search results' );
       
    42 
       
    43 		if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
       
    44 			$title = sprintf(
       
    45 				/* translators: %s is the search term. */
       
    46 				__( 'Search results for: "%s"' ),
       
    47 				get_search_query()
       
    48 			);
       
    49 		}
       
    50 	}
       
    51 
    27 	$tag_name           = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
    52 	$tag_name           = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
    28 	$align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
    53 	$align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
    29 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
    54 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
    30 	return sprintf(
    55 	return sprintf(
    31 		'<%1$s %2$s>%3$s</%1$s>',
    56 		'<%1$s %2$s>%3$s</%1$s>',
    35 	);
    60 	);
    36 }
    61 }
    37 
    62 
    38 /**
    63 /**
    39  * Registers the `core/query-title` block on the server.
    64  * Registers the `core/query-title` block on the server.
       
    65  *
       
    66  * @since 5.8.0
    40  */
    67  */
    41 function register_block_core_query_title() {
    68 function register_block_core_query_title() {
    42 	register_block_type_from_metadata(
    69 	register_block_type_from_metadata(
    43 		__DIR__ . '/query-title',
    70 		__DIR__ . '/query-title',
    44 		array(
    71 		array(