PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 / block-library / blocks / template-part.php

template-part.php in Gutenberg 7.4.0, at build/block-library/blocks/template-part.php

61 lines 1.6 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/template-part` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/template-part` block on the server.
10 *
11 * @param array $attributes The block attributes.
12 *
13 * @return string The render.
14 */
15 function gutenberg_render_block_core_template_part( $attributes ) {
16 $content = null;
17
18 if ( ! empty( $attributes['postId'] ) ) {
19 // If we have a post ID, which means this template part
20 // is user-customized, render the corresponding post content.
21 $content = get_post( $attributes['postId'] )->post_content;
22 } elseif ( wp_get_theme()->get( 'TextDomain' ) === $attributes['theme'] ) {
23 // Else, if the template part was provided by the active theme,
24 // render the corresponding file content.
25 $template_part_file_path =
26 get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
27 if ( file_exists( $template_part_file_path ) ) {
28 $content = file_get_contents( $template_part_file_path );
29 }
30 }
31
32 if ( is_null( $content ) ) {
33 return 'Template Part Not Found';
34 }
35 return apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );
36 }
37
38 /**
39 * Registers the `core/template-part` block on the server.
40 */
41 function gutenberg_register_block_core_template_part() {
42 register_block_type(
43 'core/template-part',
44 array(
45 'attributes' => array(
46 'postId' => array(
47 'type' => 'number',
48 ),
49 'slug' => array(
50 'type' => 'string',
51 ),
52 'theme' => array(
53 'type' => 'string',
54 ),
55 ),
56 'render_callback' => 'gutenberg_render_block_core_template_part',
57 )
58 );
59 }
60 add_action( 'init', 'gutenberg_register_block_core_template_part', 20 );
61