| 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 ( isset( $attributes['theme'] ) && wp_get_theme()->get( 'TextDomain' ) === $attributes['theme'] ) { |
| 23 |
$template_part_query = new WP_Query( |
| 24 |
array( |
| 25 |
'post_type' => 'wp_template_part', |
| 26 |
'post_status' => 'publish', |
| 27 |
'name' => $attributes['slug'], |
| 28 |
'meta_key' => 'theme', |
| 29 |
'meta_value' => $attributes['theme'], |
| 30 |
'posts_per_page' => 1, |
| 31 |
'no_found_rows' => true, |
| 32 |
) |
| 33 |
); |
| 34 |
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; |
| 35 |
if ( $template_part_post ) { |
| 36 |
// A published post might already exist if this template part was customized elsewhere |
| 37 |
// or if it's part of a customized template. |
| 38 |
$content = $template_part_post->post_content; |
| 39 |
} else { |
| 40 |
// Else, if the template part was provided by the active theme, |
| 41 |
// render the corresponding file content. |
| 42 |
$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html'; |
| 43 |
if ( 0 === validate_file( $template_part_file_path ) && file_exists( $template_part_file_path ) ) { |
| 44 |
$content = file_get_contents( $template_part_file_path ); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if ( is_null( $content ) ) { |
| 50 |
return 'Template Part Not Found'; |
| 51 |
} |
| 52 |
|
| 53 |
// Run through the actions that are typically taken on the_content. |
| 54 |
$content = do_blocks( $content ); |
| 55 |
$content = wptexturize( $content ); |
| 56 |
$content = convert_smilies( $content ); |
| 57 |
$content = wpautop( $content ); |
| 58 |
$content = shortcode_unautop( $content ); |
| 59 |
if ( function_exists( 'wp_filter_content_tags' ) ) { |
| 60 |
$content = wp_filter_content_tags( $content ); |
| 61 |
} else { |
| 62 |
$content = wp_make_content_images_responsive( $content ); |
| 63 |
} |
| 64 |
$content = do_shortcode( $content ); |
| 65 |
|
| 66 |
return str_replace( ']]>', ']]>', $content ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Registers the `core/template-part` block on the server. |
| 71 |
*/ |
| 72 |
function gutenberg_register_block_core_template_part() { |
| 73 |
register_block_type_from_metadata( |
| 74 |
__DIR__ . '/template-part', |
| 75 |
array( |
| 76 |
'render_callback' => 'gutenberg_render_block_core_template_part', |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
add_action( 'init', 'gutenberg_register_block_core_template_part', 20 ); |
| 81 |
|