template-part.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/template-part` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/template-part` block on the server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string The render.
  13. */
  14. function render_block_core_template_part( $attributes ) {
  15. static $seen_ids = array();
  16. $template_part_id = null;
  17. $content = null;
  18. $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
  19. if (
  20. isset( $attributes['slug'] ) &&
  21. isset( $attributes['theme'] ) &&
  22. wp_get_theme()->get_stylesheet() === $attributes['theme']
  23. ) {
  24. $template_part_id = $attributes['theme'] . '//' . $attributes['slug'];
  25. $template_part_query = new WP_Query(
  26. array(
  27. 'post_type' => 'wp_template_part',
  28. 'post_status' => 'publish',
  29. 'post_name__in' => array( $attributes['slug'] ),
  30. 'tax_query' => array(
  31. array(
  32. 'taxonomy' => 'wp_theme',
  33. 'field' => 'name',
  34. 'terms' => $attributes['theme'],
  35. ),
  36. ),
  37. 'posts_per_page' => 1,
  38. 'no_found_rows' => true,
  39. )
  40. );
  41. $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
  42. if ( $template_part_post ) {
  43. // A published post might already exist if this template part was customized elsewhere
  44. // or if it's part of a customized template.
  45. $content = $template_part_post->post_content;
  46. $area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' );
  47. if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) {
  48. $area = $area_terms[0]->name;
  49. }
  50. /**
  51. * Fires when a block template part is loaded from a template post stored in the database.
  52. *
  53. * @since 5.9.0
  54. *
  55. * @param string $template_part_id The requested template part namespaced to the theme.
  56. * @param array $attributes The block attributes.
  57. * @param WP_Post $template_part_post The template part post object.
  58. * @param string $content The template part content.
  59. */
  60. do_action( 'render_block_core_template_part_post', $template_part_id, $attributes, $template_part_post, $content );
  61. } else {
  62. // Else, if the template part was provided by the active theme,
  63. // render the corresponding file content.
  64. $parent_theme_folders = get_block_theme_folders( get_template() );
  65. $child_theme_folders = get_block_theme_folders( get_stylesheet() );
  66. $child_theme_part_file_path = get_theme_file_path( '/' . $child_theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' );
  67. $parent_theme_part_file_path = get_theme_file_path( '/' . $parent_theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' );
  68. $template_part_file_path = 0 === validate_file( $attributes['slug'] ) && file_exists( $child_theme_part_file_path ) ? $child_theme_part_file_path : $parent_theme_part_file_path;
  69. if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) {
  70. $content = file_get_contents( $template_part_file_path );
  71. $content = is_string( $content ) && '' !== $content
  72. ? _inject_theme_attribute_in_block_template_content( $content )
  73. : '';
  74. }
  75. if ( '' !== $content && null !== $content ) {
  76. /**
  77. * Fires when a block template part is loaded from a template part in the theme.
  78. *
  79. * @since 5.9.0
  80. *
  81. * @param string $template_part_id The requested template part namespaced to the theme.
  82. * @param array $attributes The block attributes.
  83. * @param string $template_part_file_path Absolute path to the template path.
  84. * @param string $content The template part content.
  85. */
  86. do_action( 'render_block_core_template_part_file', $template_part_id, $attributes, $template_part_file_path, $content );
  87. } else {
  88. /**
  89. * Fires when a requested block template part does not exist in the database nor in the theme.
  90. *
  91. * @since 5.9.0
  92. *
  93. * @param string $template_part_id The requested template part namespaced to the theme.
  94. * @param array $attributes The block attributes.
  95. * @param string $template_part_file_path Absolute path to the not found template path.
  96. */
  97. do_action( 'render_block_core_template_part_none', $template_part_id, $attributes, $template_part_file_path );
  98. }
  99. }
  100. }
  101. // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
  102. // is set in `wp_debug_mode()`.
  103. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
  104. defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
  105. if ( is_null( $content ) && $is_debug ) {
  106. if ( ! isset( $attributes['slug'] ) ) {
  107. // If there is no slug this is a placeholder and we dont want to return any message.
  108. return;
  109. }
  110. return sprintf(
  111. /* translators: %s: Template part slug. */
  112. __( 'Template part has been deleted or is unavailable: %s' ),
  113. $attributes['slug']
  114. );
  115. }
  116. if ( isset( $seen_ids[ $template_part_id ] ) ) {
  117. return $is_debug ?
  118. // translators: Visible only in the front end, this warning takes the place of a faulty block.
  119. __( '[block rendering halted]' ) :
  120. '';
  121. }
  122. // Run through the actions that are typically taken on the_content.
  123. $seen_ids[ $template_part_id ] = true;
  124. $content = do_blocks( $content );
  125. unset( $seen_ids[ $template_part_id ] );
  126. $content = wptexturize( $content );
  127. $content = convert_smilies( $content );
  128. $content = shortcode_unautop( $content );
  129. $content = wp_filter_content_tags( $content );
  130. $content = do_shortcode( $content );
  131. // Handle embeds for block template parts.
  132. global $wp_embed;
  133. $content = $wp_embed->autoembed( $content );
  134. if ( empty( $attributes['tagName'] ) ) {
  135. $defined_areas = get_allowed_block_template_part_areas();
  136. $area_tag = 'div';
  137. foreach ( $defined_areas as $defined_area ) {
  138. if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) {
  139. $area_tag = $defined_area['area_tag'];
  140. }
  141. }
  142. $html_tag = $area_tag;
  143. } else {
  144. $html_tag = esc_attr( $attributes['tagName'] );
  145. }
  146. $wrapper_attributes = get_block_wrapper_attributes();
  147. return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
  148. }
  149. /**
  150. * Returns an array of area variation objects for the template part block.
  151. *
  152. * @return array Array containing the block variation objects.
  153. */
  154. function build_template_part_block_area_variations() {
  155. $variations = array();
  156. $defined_areas = get_allowed_block_template_part_areas();
  157. foreach ( $defined_areas as $area ) {
  158. if ( 'uncategorized' !== $area['area'] ) {
  159. $variations[] = array(
  160. 'name' => $area['area'],
  161. 'title' => $area['label'],
  162. 'description' => $area['description'],
  163. 'attributes' => array(
  164. 'area' => $area['area'],
  165. ),
  166. 'scope' => array( 'inserter' ),
  167. 'icon' => $area['icon'],
  168. );
  169. }
  170. }
  171. return $variations;
  172. }
  173. /**
  174. * Returns an array of instance variation objects for the template part block
  175. *
  176. * @return array Array containing the block variation objects.
  177. */
  178. function build_template_part_block_instance_variations() {
  179. // Block themes are unavailable during installation.
  180. if ( wp_installing() ) {
  181. return array();
  182. }
  183. if ( ! current_theme_supports( 'block-templates' ) && ! current_theme_supports( 'block-template-parts' ) ) {
  184. return array();
  185. }
  186. $variations = array();
  187. $template_parts = get_block_templates(
  188. array(
  189. 'post_type' => 'wp_template_part',
  190. ),
  191. 'wp_template_part'
  192. );
  193. $defined_areas = get_allowed_block_template_part_areas();
  194. $icon_by_area = array_combine( array_column( $defined_areas, 'area' ), array_column( $defined_areas, 'icon' ) );
  195. foreach ( $template_parts as $template_part ) {
  196. $variations[] = array(
  197. 'name' => sanitize_title( $template_part->slug ),
  198. 'title' => $template_part->title,
  199. // If there's no description for the template part don't show the
  200. // block description. This is a bit hacky, but prevent the fallback
  201. // by using a non-breaking space so that the value of description
  202. // isn't falsey.
  203. 'description' => $template_part->description || '&nbsp;',
  204. 'attributes' => array(
  205. 'slug' => $template_part->slug,
  206. 'theme' => $template_part->theme,
  207. 'area' => $template_part->area,
  208. ),
  209. 'scope' => array( 'inserter' ),
  210. 'icon' => $icon_by_area[ $template_part->area ],
  211. 'example' => array(
  212. 'attributes' => array(
  213. 'slug' => $template_part->slug,
  214. 'theme' => $template_part->theme,
  215. 'area' => $template_part->area,
  216. ),
  217. ),
  218. );
  219. }
  220. return $variations;
  221. }
  222. /**
  223. * Returns an array of all template part block variations.
  224. *
  225. * @return array Array containing the block variation objects.
  226. */
  227. function build_template_part_block_variations() {
  228. return array_merge( build_template_part_block_area_variations(), build_template_part_block_instance_variations() );
  229. }
  230. /**
  231. * Registers the `core/template-part` block on the server.
  232. */
  233. function register_block_core_template_part() {
  234. register_block_type_from_metadata(
  235. __DIR__ . '/template-part',
  236. array(
  237. 'render_callback' => 'render_block_core_template_part',
  238. 'variations' => build_template_part_block_variations(),
  239. )
  240. );
  241. }
  242. add_action( 'init', 'register_block_core_template_part' );