PluginProbe
Gutenberg / 23.2.0
Gutenberg v23.2.0
24.0.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 All 403 releases
gutenberg / build / scripts / block-library / template-part.php
build/scripts/block-library
accordion 6 months ago accordion-heading 9 months ago accordion-item 4 months ago accordion-panel 7 months ago archives 7 months ago audio 10 months ago avatar 7 months ago block 7 months ago breadcrumbs 7 months ago button 6 months ago buttons 7 months ago calendar 7 months ago categories 7 months ago code 10 months ago column 10 months ago columns 10 months ago comment-author-avatar 10 months ago comment-author-name 7 months ago comment-content 7 months ago comment-date 7 months ago comment-edit-link 7 months ago comment-reply-link 7 months ago comment-template 7 months ago comments 7 months ago comments-pagination 7 months ago comments-pagination-next 7 months ago comments-pagination-numbers 7 months ago comments-pagination-previous 7 months ago comments-title 7 months ago cover 10 months ago details 10 months ago embed 7 months ago file 10 months ago footnotes 7 months ago form 4 months ago form-input 4 months ago form-submission-notification 4 months ago form-submit-button 4 months ago freeform 7 months ago gallery 6 months ago group 4 months ago heading 7 months ago home-link 7 months ago html 5 months ago icon 5 months ago image 6 months ago latest-comments 7 months ago latest-posts 7 months ago list 7 months ago list-item 5 months ago loginout 7 months ago math 7 months ago media-text 10 months ago missing 7 months ago more 7 months ago navigation 6 months ago navigation-link 5 months ago navigation-overlay-close 6 months ago navigation-submenu 6 months ago nextpage 5 months ago page-list 6 months ago page-list-item 6 months ago paragraph 6 months ago pattern 9 months ago playlist 6 months ago playlist-track 6 months ago post-author 6 months ago post-author-biography 6 months ago post-author-name 6 months ago post-comment 10 months ago post-comments-count 6 months ago post-comments-form 6 months ago post-comments-link 6 months ago post-content 7 months ago post-date 5 months ago post-excerpt 5 months ago post-featured-image 7 months ago post-navigation-link 6 months ago post-template 7 months ago post-terms 6 months ago post-time-to-read 6 months ago post-title 6 months ago preformatted 10 months ago pullquote 4 months ago query 6 months ago query-no-results 7 months ago query-pagination 7 months ago query-pagination-next 7 months ago query-pagination-numbers 7 months ago query-pagination-previous 7 months ago query-title 6 months ago query-total 7 months ago quote 10 months ago read-more 7 months ago rss 7 months ago search 4 months ago separator 10 months ago shortcode 5 months ago site-logo 7 months ago site-tagline 6 months ago site-title 6 months ago social-link 7 months ago social-links 7 months ago spacer 10 months ago tab 4 months ago tab-list 4 months ago tab-panel 4 months ago tab-panels 4 months ago table 10 months ago table-of-contents 7 months ago tabs 4 months ago tag-cloud 7 months ago template-part 10 months ago term-count 7 months ago term-description 6 months ago term-name 7 months ago term-template 7 months ago terms-query 7 months ago text-columns 10 months ago verse 6 months ago video 10 months ago accordion-item.php 2.9 KB 5 months ago accordion.php 1.2 KB 10 months ago archives.php 4.4 KB 7 months ago avatar.php 5.6 KB 7 months ago block.php 4.1 KB 10 months ago blocks-manifest.php 213.3 KB 4 months ago breadcrumbs.php 19.0 KB 7 months ago button.php 4.2 KB 6 months ago calendar.php 6.1 KB 10 months ago categories.php 4.9 KB 7 months ago comment-author-avatar.php 2.5 KB 7 months ago comment-author-name.php 2.1 KB 10 months ago comment-content.php 2.4 KB 10 months ago comment-date.php 1.9 KB 10 months ago comment-edit-link.php 1.7 KB 10 months ago comment-reply-link.php 2.1 KB 10 months ago comment-template.php 4.5 KB 10 months ago comments-pagination-next.php 1.9 KB 10 months ago comments-pagination-numbers.php 1.6 KB 10 months ago comments-pagination-previous.php 1.8 KB 10 months ago comments-pagination.php 1.3 KB 10 months ago comments-title.php 2.8 KB 7 months ago comments.php 6.7 KB 10 months ago cover.php 7.3 KB 5 months ago details.php 1.5 KB 5 months ago file.php 1.9 KB 10 months ago footnotes.php 3.8 KB 7 months ago form-input.php 1.2 KB 10 months ago form-submission-notification.php 1.5 KB 10 months ago form.php 5.8 KB 10 months ago gallery.php 8.9 KB 6 months ago heading.php 1.3 KB 10 months ago home-link.php 4.9 KB 5 months ago icon.php 4.0 KB 6 months ago image.php 17.4 KB 4 months ago index.js 2.6 MB 3 months ago index.js.map 3.9 MB 3 months ago index.min.asset.php 696 B 3 months ago index.min.js 1.1 MB 3 months ago index.min.js.map 4.0 MB 3 months ago latest-comments.php 5.4 KB 7 months ago latest-posts.php 8.7 KB 4 months ago list.php 1.3 KB 10 months ago loginout.php 2.1 KB 5 months ago media-text.php 4.2 KB 6 months ago navigation-link.php 15.3 KB 5 months ago navigation-overlay-close.php 1.8 KB 6 months ago navigation-submenu.php 11.0 KB 5 months ago navigation.php 63.6 KB 3 months ago page-list-item.php 385 B 10 months ago page-list.php 14.3 KB 5 months ago paragraph.php 1.2 KB 7 months ago pattern.php 1.8 KB 10 months ago playlist-track.php 2.1 KB 6 months ago playlist.php 4.6 KB 6 months ago post-author-biography.php 1.5 KB 10 months ago post-author-name.php 1.9 KB 10 months ago post-author.php 2.7 KB 10 months ago post-comment.php 429 B 10 months ago post-comments-count.php 1.3 KB 10 months ago post-comments-form.php 2.8 KB 10 months ago post-comments-link.php 2.2 KB 10 months ago post-content.php 2.3 KB 10 months ago post-date.php 3.6 KB 7 months ago post-excerpt.php 4.3 KB 7 months ago post-featured-image.php 9.1 KB 6 months ago post-navigation-link.php 4.7 KB 7 months ago post-template.php 5.9 KB 4 months ago post-terms.php 3.6 KB 10 months ago post-time-to-read.php 6.3 KB 7 months ago post-title.php 2.1 KB 7 months ago query-no-results.php 1.8 KB 10 months ago query-pagination-next.php 3.6 KB 6 months ago query-pagination-numbers.php 4.6 KB 6 months ago query-pagination-previous.php 3.4 KB 6 months ago query-pagination.php 1.2 KB 10 months ago query-title.php 2.7 KB 6 months ago query-total.php 2.5 KB 6 months ago query.php 5.6 KB 10 months ago read-more.php 1.8 KB 10 months ago rss.php 4.5 KB 10 months ago search.php 24.1 KB 4 months ago shortcode.php 779 B 10 months ago site-logo.php 6.4 KB 10 months ago site-tagline.php 1.2 KB 10 months ago site-title.php 1.9 KB 10 months ago social-link.php 66.2 KB 7 months ago tab-list.php 2.1 KB 4 months ago tab-panel.php 2.1 KB 4 months ago tab.php 2.1 KB 4 months ago table-of-contents.php 1.2 KB 10 months ago tabs.php 4.6 KB 4 months ago tag-cloud.php 1.6 KB 10 months ago template-part.php 10.3 KB 6 months ago term-count.php 1.8 KB 10 months ago term-description.php 1.8 KB 10 months ago term-name.php 2.0 KB 7 months ago term-template.php 4.5 KB 7 months ago video.php 2.7 KB 10 months ago

template-part.php in Gutenberg 23.2.0, at build/scripts/block-library/template-part.php

315 lines 10.3 KB Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/template-part` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/template-part` block on the server.
10 *
11 * @since 5.9.0
12 *
13 * @global WP_Embed $wp_embed WordPress Embed object.
14 *
15 * @param array $attributes The block attributes.
16 *
17 * @return string The render.
18 */
19 function gutenberg_render_block_core_template_part( $attributes ) {
20 static $seen_ids = array();
21
22 $template_part_id = null;
23 $content = null;
24 $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
25 $theme = $attributes['theme'] ?? get_stylesheet();
26
27 if ( isset( $attributes['slug'] ) && get_stylesheet() === $theme ) {
28 $template_part_id = $theme . '//' . $attributes['slug'];
29 $template_part_query = new WP_Query(
30 array(
31 'post_type' => 'wp_template_part',
32 'post_status' => 'publish',
33 'post_name__in' => array( $attributes['slug'] ),
34 'tax_query' => array(
35 array(
36 'taxonomy' => 'wp_theme',
37 'field' => 'name',
38 'terms' => $theme,
39 ),
40 ),
41 'posts_per_page' => 1,
42 'no_found_rows' => true,
43 'lazy_load_term_meta' => false, // Do not lazy load term meta, as template parts only have one term.
44 )
45 );
46 $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
47 if ( $template_part_post ) {
48 // A published post might already exist if this template part was customized elsewhere
49 // or if it's part of a customized template.
50 $block_template = _build_block_template_result_from_post( $template_part_post );
51 $content = $block_template->content;
52 if ( isset( $block_template->area ) ) {
53 $area = $block_template->area;
54 }
55 /**
56 * Fires when a block template part is loaded from a template post stored in the database.
57 *
58 * @since 5.9.0
59 *
60 * @param string $template_part_id The requested template part namespaced to the theme.
61 * @param array $attributes The block attributes.
62 * @param WP_Post $template_part_post The template part post object.
63 * @param string $content The template part content.
64 */
65 do_action( 'render_block_core_template_part_post', $template_part_id, $attributes, $template_part_post, $content );
66 } else {
67 $template_part_file_path = '';
68 // Else, if the template part was provided by the active theme,
69 // render the corresponding file content.
70 if ( 0 === validate_file( $attributes['slug'] ) ) {
71 $block_template = get_block_file_template( $template_part_id, 'wp_template_part' );
72
73 if ( isset( $block_template->content ) ) {
74 $content = $block_template->content;
75 }
76 if ( isset( $block_template->area ) ) {
77 $area = $block_template->area;
78 }
79
80 // Needed for the `render_block_core_template_part_file` and `render_block_core_template_part_none` actions below.
81 $block_template_file = _get_block_template_file( 'wp_template_part', $attributes['slug'] );
82 if ( $block_template_file ) {
83 $template_part_file_path = $block_template_file['path'];
84 }
85 }
86
87 if ( '' !== $content && null !== $content ) {
88 /**
89 * Fires when a block template part is loaded from a template part in the theme.
90 *
91 * @since 5.9.0
92 *
93 * @param string $template_part_id The requested template part namespaced to the theme.
94 * @param array $attributes The block attributes.
95 * @param string $template_part_file_path Absolute path to the template path.
96 * @param string $content The template part content.
97 */
98 do_action( 'render_block_core_template_part_file', $template_part_id, $attributes, $template_part_file_path, $content );
99 } else {
100 /**
101 * Fires when a requested block template part does not exist in the database nor in the theme.
102 *
103 * @since 5.9.0
104 *
105 * @param string $template_part_id The requested template part namespaced to the theme.
106 * @param array $attributes The block attributes.
107 * @param string $template_part_file_path Absolute path to the not found template path.
108 */
109 do_action( 'render_block_core_template_part_none', $template_part_id, $attributes, $template_part_file_path );
110 }
111 }
112 }
113
114 // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
115 // is set in `wp_debug_mode()`.
116 $is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
117
118 if ( is_null( $content ) ) {
119 if ( $is_debug && isset( $attributes['slug'] ) ) {
120 return sprintf(
121 /* translators: %s: Template part slug. */
122 __( 'Template part has been deleted or is unavailable: %s' ),
123 $attributes['slug']
124 );
125 }
126
127 return '';
128 }
129
130 if ( isset( $seen_ids[ $template_part_id ] ) ) {
131 return $is_debug ?
132 // translators: Visible only in the front end, this warning takes the place of a faulty block.
133 __( '[block rendering halted]' ) :
134 '';
135 }
136
137 // Look up area definition.
138 $area_definition = null;
139 $defined_areas = get_allowed_block_template_part_areas();
140 foreach ( $defined_areas as $defined_area ) {
141 if ( $defined_area['area'] === $area ) {
142 $area_definition = $defined_area;
143 break;
144 }
145 }
146
147 // If $area is not allowed, set it back to the uncategorized default.
148 if ( ! $area_definition ) {
149 $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
150 }
151
152 // Run through the actions that are typically taken on the_content.
153 $content = shortcode_unautop( $content );
154 $content = do_shortcode( $content );
155 $seen_ids[ $template_part_id ] = true;
156 $content = do_blocks( $content );
157 unset( $seen_ids[ $template_part_id ] );
158 $content = wptexturize( $content );
159 $content = convert_smilies( $content );
160 $content = wp_filter_content_tags( $content, "template_part_{$area}" );
161
162 /**
163 * Handle embeds for block template parts.
164 *
165 * @global WP_Embed $wp_embed WordPress Embed object.
166 */
167 global $wp_embed;
168 $content = $wp_embed->autoembed( $content );
169
170 if ( empty( $attributes['tagName'] ) || tag_escape( $attributes['tagName'] ) !== $attributes['tagName'] ) {
171 $area_tag = 'div';
172 if ( $area_definition && isset( $area_definition['area_tag'] ) ) {
173 $area_tag = $area_definition['area_tag'];
174 }
175 $html_tag = $area_tag;
176 } else {
177 $html_tag = esc_attr( $attributes['tagName'] );
178 }
179 $wrapper_attributes = get_block_wrapper_attributes();
180
181 return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
182 }
183
184 /**
185 * Returns an array of area variation objects for the template part block.
186 *
187 * @since 6.1.0
188 *
189 * @param array $instance_variations The variations for instances.
190 *
191 * @return array Array containing the block variation objects.
192 */
193 function gutenberg_build_template_part_block_area_variations( $instance_variations ) {
194 $variations = array();
195 $defined_areas = get_allowed_block_template_part_areas();
196
197 foreach ( $defined_areas as $area ) {
198 if ( 'uncategorized' !== $area['area'] && 'navigation-overlay' !== $area['area'] ) {
199 $has_instance_for_area = false;
200 foreach ( $instance_variations as $variation ) {
201 if ( $variation['attributes']['area'] === $area['area'] ) {
202 $has_instance_for_area = true;
203 break;
204 }
205 }
206
207 $scope = $has_instance_for_area ? array() : array( 'inserter' );
208
209 $variations[] = array(
210 'name' => 'area_' . $area['area'],
211 'title' => $area['label'],
212 'description' => $area['description'],
213 'attributes' => array(
214 'area' => $area['area'],
215 ),
216 'scope' => $scope,
217 'icon' => $area['icon'],
218 );
219 }
220 }
221 return $variations;
222 }
223
224 /**
225 * Returns an array of instance variation objects for the template part block
226 *
227 * @since 6.1.0
228 *
229 * @return array Array containing the block variation objects.
230 */
231 function gutenberg_build_template_part_block_instance_variations() {
232 // Block themes are unavailable during installation.
233 if ( wp_installing() ) {
234 return array();
235 }
236
237 if ( ! current_theme_supports( 'block-templates' ) && ! current_theme_supports( 'block-template-parts' ) ) {
238 return array();
239 }
240
241 $variations = array();
242 $template_parts = get_block_templates(
243 array(
244 'post_type' => 'wp_template_part',
245 ),
246 'wp_template_part'
247 );
248
249 $defined_areas = get_allowed_block_template_part_areas();
250 $icon_by_area = array_combine( array_column( $defined_areas, 'area' ), array_column( $defined_areas, 'icon' ) );
251
252 foreach ( $template_parts as $template_part ) {
253 // Navigation overlay template parts should not appear in the
254 // general inserter. They are managed through the Navigation
255 // block's overlay template part selector.
256 $scope = ( 'navigation-overlay' === $template_part->area )
257 ? array()
258 : array( 'inserter' );
259
260 $variations[] = array(
261 'name' => 'instance_' . sanitize_title( $template_part->slug ),
262 'title' => $template_part->title,
263 // If there's no description for the template part don't show the
264 // block description. This is a bit hacky, but prevent the fallback
265 // by using a non-breaking space so that the value of description
266 // isn't falsey.
267 'description' => $template_part->description || '&nbsp;',
268 'attributes' => array(
269 'slug' => $template_part->slug,
270 'theme' => $template_part->theme,
271 'area' => $template_part->area,
272 ),
273 'scope' => $scope,
274 'icon' => $icon_by_area[ $template_part->area ] ?? null,
275 'example' => array(
276 'attributes' => array(
277 'slug' => $template_part->slug,
278 'theme' => $template_part->theme,
279 'area' => $template_part->area,
280 ),
281 ),
282 );
283 }
284 return $variations;
285 }
286
287 /**
288 * Returns an array of all template part block variations.
289 *
290 * @since 5.9.0
291 *
292 * @return array Array containing the block variation objects.
293 */
294 function gutenberg_build_template_part_block_variations() {
295 $instance_variations = gutenberg_build_template_part_block_instance_variations();
296 $area_variations = gutenberg_build_template_part_block_area_variations( $instance_variations );
297 return array_merge( $area_variations, $instance_variations );
298 }
299
300 /**
301 * Registers the `core/template-part` block on the server.
302 *
303 * @since 5.9.0
304 */
305 function gutenberg_register_block_core_template_part() {
306 register_block_type_from_metadata(
307 __DIR__ . '/template-part',
308 array(
309 'render_callback' => 'gutenberg_render_block_core_template_part',
310 'variation_callback' => 'gutenberg_build_template_part_block_variations',
311 )
312 );
313 }
314 add_action( 'init', 'gutenberg_register_block_core_template_part', 20 );
315