|
| 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 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_theme_file_path( '/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_block_template_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 | + // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 77 | + // is set in `wp_debug_mode()`. |
| 78 | + $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && |
| 79 | + defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; |
| 80 | + |
| 81 | + return $is_debug ? |
| 82 | + // translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 83 | + __( '[block rendering halted]' ) : |
| 84 | + ''; |
| 85 | + } |
| 86 | + |
| 87 | + // Run through the actions that are typically taken on the_content. |
| 88 | + $seen_ids[ $template_part_id ] = true; |
| 89 | + $content = do_blocks( $content ); |
| 90 | + unset( $seen_ids[ $template_part_id ] ); |
| 91 | + $content = wptexturize( $content ); |
| 92 | + $content = convert_smilies( $content ); |
| 93 | + $content = shortcode_unautop( $content ); |
| 94 | + $content = wp_filter_content_tags( $content ); |
| 95 | + $content = do_shortcode( $content ); |
| 96 | + |
| 97 | + // Handle embeds for block template parts. |
| 98 | + global $wp_embed; |
| 99 | + $content = $wp_embed->autoembed( $content ); |
| 100 | + |
| 101 | + if ( empty( $attributes['tagName'] ) ) { |
| 102 | + $defined_areas = get_allowed_block_template_part_areas(); |
| 103 | + $area_tag = 'div'; |
| 104 | + foreach ( $defined_areas as $defined_area ) { |
| 105 | + if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) { |
| 106 | + $area_tag = $defined_area['area_tag']; |
| 107 | + } |
| 108 | + } |
| 109 | + $html_tag = $area_tag; |
| 110 | + } else { |
| 111 | + $html_tag = esc_attr( $attributes['tagName'] ); |
| 112 | + } |
| 113 | + $wrapper_attributes = get_block_wrapper_attributes(); |
| 114 | + |
| 115 | + return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>"; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Returns an array of variation objects for the template part block. |
| 120 | + * |
| 121 | + * @return array Array containing the block variation objects. |
| 122 | + */ |
| 123 | +function build_template_part_block_variations() { |
| 124 | + $variations = array(); |
| 125 | + $defined_areas = get_allowed_block_template_part_areas(); |
| 126 | + foreach ( $defined_areas as $area ) { |
| 127 | + if ( 'uncategorized' !== $area['area'] ) { |
| 128 | + $variations[] = array( |
| 129 | + 'name' => $area['area'], |
| 130 | + 'title' => $area['label'], |
| 131 | + 'description' => $area['description'], |
| 132 | + 'attributes' => array( |
| 133 | + 'area' => $area['area'], |
| 134 | + ), |
| 135 | + 'scope' => array( 'inserter' ), |
| 136 | + 'icon' => $area['icon'], |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + return $variations; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Registers the `core/template-part` block on the server. |
| 145 | + */ |
| 146 | +function register_block_core_template_part() { |
| 147 | + register_block_type_from_metadata( |
| 148 | + __DIR__ . '/template-part', |
| 149 | + array( |
| 150 | + 'render_callback' => 'render_block_core_template_part', |
| 151 | + 'variations' => build_template_part_block_variations(), |
| 152 | + ) |
| 153 | + ); |
| 154 | +} |
| 155 | +add_action( 'init', 'register_block_core_template_part' ); |
0 commit comments