PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / query-title.php

query-title.php in Gutenberg 22.9.0, at build/scripts/block-library/query-title.php

104 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/query-title` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/query-title` block on the server.
10 * For now it supports Archive title, Search title, and Post Type Label,
11 * using queried object information
12 *
13 * @since 5.8.0
14 *
15 * @param array $attributes Block attributes.
16 * @param array $content Block content.
17 * @param object $block Block instance.
18 *
19 * @return string Returns the query title based on the queried object.
20 */
21 function gutenberg_render_block_core_query_title( $attributes, $content, $block ) {
22 $type = $attributes['type'] ?? null;
23 $is_archive = is_archive();
24 $is_search = is_search();
25 $post_type = $block->context['query']['postType'] ?? get_post_type();
26
27 if ( ! $type ||
28 ( 'archive' === $type && ! $is_archive ) ||
29 ( 'search' === $type && ! $is_search ) ||
30 ( 'post-type' === $type && ! $post_type )
31 ) {
32 return '';
33 }
34 $title = '';
35 if ( $is_archive ) {
36 $show_prefix = $attributes['showPrefix'] ?? true;
37 if ( ! $show_prefix ) {
38 add_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
39 $title = get_the_archive_title();
40 remove_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
41 } else {
42 $title = get_the_archive_title();
43 }
44 }
45 if ( $is_search ) {
46 $title = __( 'Search results' );
47
48 if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
49 $title = sprintf(
50 /* translators: %s is the search term. */
51 __( 'Search results for: &#8220;%s&#8221;' ),
52 get_search_query()
53 );
54 }
55 }
56 if ( 'post-type' === $type ) {
57 $post_type_object = get_post_type_object( $post_type );
58
59 if ( ! $post_type_object ) {
60 return '';
61 }
62
63 $post_type_name = $post_type_object->labels->singular_name;
64 $show_prefix = $attributes['showPrefix'] ?? true;
65
66 if ( $show_prefix ) {
67 $title = sprintf(
68 /* translators: %s is the post type name. */
69 __( 'Post Type: &#8220;%s&#8221;' ),
70 $post_type_name
71 );
72 } else {
73 $title = $post_type_name;
74 }
75 }
76
77 $level = (int) ( $attributes['level'] ?? 1 );
78 $tag_name = 0 === $level ? 'p' : 'h' . $level;
79
80 $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
81 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
82 return sprintf(
83 '<%1$s %2$s>%3$s</%1$s>',
84 $tag_name,
85 $wrapper_attributes,
86 $title
87 );
88 }
89
90 /**
91 * Registers the `core/query-title` block on the server.
92 *
93 * @since 5.8.0
94 */
95 function gutenberg_register_block_core_query_title() {
96 register_block_type_from_metadata(
97 __DIR__ . '/query-title',
98 array(
99 'render_callback' => 'gutenberg_render_block_core_query_title',
100 )
101 );
102 }
103 add_action( 'init', 'gutenberg_register_block_core_query_title', 20 );
104