| 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 |
$theme_folders = get_block_theme_folders(); |
| 57 |
$template_part_file_path = get_theme_file_path( '/' . $theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' ); |
| 58 |
if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) { |
| 59 |
$content = file_get_contents( $template_part_file_path ); |
| 60 |
$content = is_string( $content ) && '' !== $content |
| 61 |
? _inject_theme_attribute_in_block_template_content( $content ) |
| 62 |
: ''; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if ( is_null( $content ) && is_user_logged_in() ) { |
| 68 |
if ( ! isset( $attributes['slug'] ) ) { |
| 69 |
// If there is no slug this is a placeholder and we dont want to return any message. |
| 70 |
return; |
| 71 |
} |
| 72 |
return sprintf( |
| 73 |
/* translators: %s: Template part slug. */ |
| 74 |
__( 'Template part has been deleted or is unavailable: %s' ), |
| 75 |
$attributes['slug'] |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
if ( isset( $seen_ids[ $template_part_id ] ) ) { |
| 80 |
// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 81 |
// is set in `wp_debug_mode()`. |
| 82 |
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && |
| 83 |
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; |
| 84 |
|
| 85 |
return $is_debug ? |
| 86 |
// translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 87 |
__( '[block rendering halted]' ) : |
| 88 |
''; |
| 89 |
} |
| 90 |
|
| 91 |
// Run through the actions that are typically taken on the_content. |
| 92 |
$seen_ids[ $template_part_id ] = true; |
| 93 |
$content = do_blocks( $content ); |
| 94 |
unset( $seen_ids[ $template_part_id ] ); |
| 95 |
$content = wptexturize( $content ); |
| 96 |
$content = convert_smilies( $content ); |
| 97 |
$content = shortcode_unautop( $content ); |
| 98 |
$content = wp_filter_content_tags( $content ); |
| 99 |
$content = do_shortcode( $content ); |
| 100 |
|
| 101 |
// Handle embeds for block template parts. |
| 102 |
global $wp_embed; |
| 103 |
$content = $wp_embed->autoembed( $content ); |
| 104 |
|
| 105 |
if ( empty( $attributes['tagName'] ) ) { |
| 106 |
$defined_areas = get_allowed_block_template_part_areas(); |
| 107 |
$area_tag = 'div'; |
| 108 |
foreach ( $defined_areas as $defined_area ) { |
| 109 |
if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) { |
| 110 |
$area_tag = $defined_area['area_tag']; |
| 111 |
} |
| 112 |
} |
| 113 |
$html_tag = $area_tag; |
| 114 |
} else { |
| 115 |
$html_tag = esc_attr( $attributes['tagName'] ); |
| 116 |
} |
| 117 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 118 |
|
| 119 |
return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>"; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns an array of variation objects for the template part block. |
| 124 |
* |
| 125 |
* @return array Array containing the block variation objects. |
| 126 |
*/ |
| 127 |
function gutenberg_build_template_part_block_variations() { |
| 128 |
$variations = array(); |
| 129 |
$defined_areas = get_allowed_block_template_part_areas(); |
| 130 |
foreach ( $defined_areas as $area ) { |
| 131 |
if ( 'uncategorized' !== $area['area'] ) { |
| 132 |
$variations[] = array( |
| 133 |
'name' => $area['area'], |
| 134 |
'title' => $area['label'], |
| 135 |
'description' => $area['description'], |
| 136 |
'attributes' => array( |
| 137 |
'area' => $area['area'], |
| 138 |
), |
| 139 |
'scope' => array( 'inserter' ), |
| 140 |
'icon' => $area['icon'], |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
return $variations; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Registers the `core/template-part` block on the server. |
| 149 |
*/ |
| 150 |
function gutenberg_register_block_core_template_part() { |
| 151 |
register_block_type_from_metadata( |
| 152 |
__DIR__ . '/template-part', |
| 153 |
array( |
| 154 |
'render_callback' => 'gutenberg_render_block_core_template_part', |
| 155 |
'variations' => gutenberg_build_template_part_block_variations(), |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
add_action( 'init', 'gutenberg_register_block_core_template_part', 20 ); |
| 160 |
|