| 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 |
static $seen_ids = array(); |
| 17 |
|
| 18 |
$template_part_id = null; |
| 19 |
$content = null; |
| 20 |
$area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; |
| 21 |
|
| 22 |
if ( |
| 23 |
isset( $attributes['slug'] ) && |
| 24 |
isset( $attributes['theme'] ) && |
| 25 |
wp_get_theme()->get_stylesheet() === $attributes['theme'] |
| 26 |
) { |
| 27 |
$template_part_id = $attributes['theme'] . '//' . $attributes['slug']; |
| 28 |
$template_part_query = new WP_Query( |
| 29 |
array( |
| 30 |
'post_type' => 'wp_template_part', |
| 31 |
'post_status' => 'publish', |
| 32 |
'post_name__in' => array( $attributes['slug'] ), |
| 33 |
'tax_query' => array( |
| 34 |
array( |
| 35 |
'taxonomy' => 'wp_theme', |
| 36 |
'field' => 'slug', |
| 37 |
'terms' => $attributes['theme'], |
| 38 |
), |
| 39 |
), |
| 40 |
'posts_per_page' => 1, |
| 41 |
'no_found_rows' => true, |
| 42 |
) |
| 43 |
); |
| 44 |
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; |
| 45 |
if ( $template_part_post ) { |
| 46 |
// A published post might already exist if this template part was customized elsewhere |
| 47 |
// or if it's part of a customized template. |
| 48 |
$content = $template_part_post->post_content; |
| 49 |
$area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' ); |
| 50 |
if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) { |
| 51 |
$area = $area_terms[0]->name; |
| 52 |
} |
| 53 |
} else { |
| 54 |
// Else, if the template part was provided by the active theme, |
| 55 |
// render the corresponding file content. |
| 56 |
$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html'; |
| 57 |
if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) { |
| 58 |
$content = _inject_theme_attribute_in_content( file_get_contents( $template_part_file_path ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( is_null( $content ) && is_user_logged_in() ) { |
| 64 |
if ( ! isset( $attributes['slug'] ) ) { |
| 65 |
// If there is no slug this is a placeholder and we dont want to return any message. |
| 66 |
return; |
| 67 |
} |
| 68 |
return sprintf( |
| 69 |
/* translators: %s: Template part slug. */ |
| 70 |
__( 'Template part has been deleted or is unavailable: %s' ), |
| 71 |
$attributes['slug'] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
if ( isset( $seen_ids[ $template_part_id ] ) ) { |
| 76 |
if ( ! is_admin() ) { |
| 77 |
trigger_error( |
| 78 |
sprintf( |
| 79 |
// translators: %s are the block attributes. |
| 80 |
__( 'Could not render Template Part block with the attributes: <code>%s</code>. Block cannot be rendered inside itself.' ), |
| 81 |
wp_json_encode( $attributes ) |
| 82 |
), |
| 83 |
E_USER_WARNING |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 88 |
// is set in `wp_debug_mode()`. |
| 89 |
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && |
| 90 |
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; |
| 91 |
return $is_debug ? |
| 92 |
// translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 93 |
__( '[block rendering halted]' ) : |
| 94 |
''; |
| 95 |
} |
| 96 |
|
| 97 |
// Run through the actions that are typically taken on the_content. |
| 98 |
$seen_ids[ $template_part_id ] = true; |
| 99 |
$content = do_blocks( $content ); |
| 100 |
unset( $seen_ids[ $template_part_id ] ); |
| 101 |
$content = wptexturize( $content ); |
| 102 |
$content = convert_smilies( $content ); |
| 103 |
$content = shortcode_unautop( $content ); |
| 104 |
if ( function_exists( 'wp_filter_content_tags' ) ) { |
| 105 |
$content = wp_filter_content_tags( $content ); |
| 106 |
} else { |
| 107 |
$content = wp_make_content_images_responsive( $content ); |
| 108 |
} |
| 109 |
$content = do_shortcode( $content ); |
| 110 |
|
| 111 |
if ( empty( $attributes['tagName'] ) ) { |
| 112 |
$area_tags = array( |
| 113 |
WP_TEMPLATE_PART_AREA_HEADER => 'header', |
| 114 |
WP_TEMPLATE_PART_AREA_FOOTER => 'footer', |
| 115 |
WP_TEMPLATE_PART_AREA_UNCATEGORIZED => 'div', |
| 116 |
); |
| 117 |
$html_tag = null !== $area && isset( $area_tags[ $area ] ) ? $area_tags[ $area ] : $area_tags[ WP_TEMPLATE_PART_AREA_UNCATEGORIZED ]; |
| 118 |
} else { |
| 119 |
$html_tag = esc_attr( $attributes['tagName'] ); |
| 120 |
} |
| 121 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 122 |
|
| 123 |
return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>"; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Registers the `core/template-part` block on the server. |
| 128 |
*/ |
| 129 |
function gutenberg_register_block_core_template_part() { |
| 130 |
register_block_type_from_metadata( |
| 131 |
__DIR__ . '/template-part', |
| 132 |
array( |
| 133 |
'render_callback' => 'gutenberg_render_block_core_template_part', |
| 134 |
) |
| 135 |
); |
| 136 |
} |
| 137 |
add_action( 'init', 'gutenberg_register_block_core_template_part', 20 ); |
| 138 |
|