Aucune description

block-template.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Block template loader functions.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Find a block template with equal or higher specificity than a given PHP template file.
  9. *
  10. * Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
  11. *
  12. * @since 5.8.0
  13. *
  14. * @global string $_wp_current_template_content
  15. *
  16. * @param string $template Path to the template. See locate_template().
  17. * @param string $type Sanitized filename without extension.
  18. * @param array $templates A list of template candidates, in descending order of priority.
  19. * @return string The path to the Full Site Editing template canvas file, or the fallback PHP template.
  20. */
  21. function locate_block_template( $template, $type, array $templates ) {
  22. global $_wp_current_template_content;
  23. if ( $template ) {
  24. /*
  25. * locate_template() has found a PHP template at the path specified by $template.
  26. * That means that we have a fallback candidate if we cannot find a block template
  27. * with higher specificity.
  28. *
  29. * Thus, before looking for matching block themes, we shorten our list of candidate
  30. * templates accordingly.
  31. */
  32. // Locate the index of $template (without the theme directory path) in $templates.
  33. $relative_template_path = str_replace(
  34. array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),
  35. '',
  36. $template
  37. );
  38. $index = array_search( $relative_template_path, $templates, true );
  39. // If the template hiearchy algorithm has successfully located a PHP template file,
  40. // we will only consider block templates with higher or equal specificity.
  41. $templates = array_slice( $templates, 0, $index + 1 );
  42. }
  43. $block_template = resolve_block_template( $type, $templates );
  44. if ( $block_template ) {
  45. if ( empty( $block_template->content ) && is_user_logged_in() ) {
  46. $_wp_current_template_content =
  47. sprintf(
  48. /* translators: %s: Template title */
  49. __( 'Empty template: %s' ),
  50. $block_template->title
  51. );
  52. } elseif ( ! empty( $block_template->content ) ) {
  53. $_wp_current_template_content = $block_template->content;
  54. }
  55. if ( isset( $_GET['_wp-find-template'] ) ) {
  56. wp_send_json_success( $block_template );
  57. }
  58. } else {
  59. if ( $template ) {
  60. return $template;
  61. }
  62. if ( 'index' === $type ) {
  63. if ( isset( $_GET['_wp-find-template'] ) ) {
  64. wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );
  65. }
  66. } else {
  67. return ''; // So that the template loader keeps looking for templates.
  68. }
  69. }
  70. // Add hooks for template canvas.
  71. // Add viewport meta tag.
  72. add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 );
  73. // Render title tag with content, regardless of whether theme has title-tag support.
  74. remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
  75. add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional.
  76. // This file will be included instead of the theme's template file.
  77. return ABSPATH . WPINC . '/template-canvas.php';
  78. }
  79. /**
  80. * Return the correct 'wp_template' to render for the request template type.
  81. *
  82. * @access private
  83. * @since 5.8.0
  84. *
  85. * @param string $template_type The current template type.
  86. * @param string[] $template_hierarchy The current template hierarchy, ordered by priority.
  87. * @return WP_Block_Template|null template A template object, or null if none could be found.
  88. */
  89. function resolve_block_template( $template_type, $template_hierarchy ) {
  90. if ( ! $template_type ) {
  91. return null;
  92. }
  93. if ( empty( $template_hierarchy ) ) {
  94. $template_hierarchy = array( $template_type );
  95. }
  96. $slugs = array_map(
  97. '_strip_template_file_suffix',
  98. $template_hierarchy
  99. );
  100. // Find all potential templates 'wp_template' post matching the hierarchy.
  101. $query = array(
  102. 'theme' => wp_get_theme()->get_stylesheet(),
  103. 'slug__in' => $slugs,
  104. );
  105. $templates = get_block_templates( $query );
  106. // Order these templates per slug priority.
  107. // Build map of template slugs to their priority in the current hierarchy.
  108. $slug_priorities = array_flip( $slugs );
  109. usort(
  110. $templates,
  111. function ( $template_a, $template_b ) use ( $slug_priorities ) {
  112. return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
  113. }
  114. );
  115. return count( $templates ) ? $templates[0] : null;
  116. }
  117. /**
  118. * Displays title tag with content, regardless of whether theme has title-tag support.
  119. *
  120. * @access private
  121. * @since 5.8.0
  122. *
  123. * @see _wp_render_title_tag()
  124. */
  125. function _block_template_render_title_tag() {
  126. echo '<title>' . wp_get_document_title() . '</title>' . "\n";
  127. }
  128. /**
  129. * Returns the markup for the current template.
  130. *
  131. * @access private
  132. * @since 5.8.0
  133. *
  134. * @global string $_wp_current_template_content
  135. * @global WP_Embed $wp_embed
  136. *
  137. * @return string Block template markup.
  138. */
  139. function get_the_block_template_html() {
  140. global $_wp_current_template_content;
  141. global $wp_embed;
  142. if ( ! $_wp_current_template_content ) {
  143. if ( is_user_logged_in() ) {
  144. return '<h1>' . esc_html__( 'No matching template found' ) . '</h1>';
  145. }
  146. return;
  147. }
  148. $content = $wp_embed->run_shortcode( $_wp_current_template_content );
  149. $content = $wp_embed->autoembed( $content );
  150. $content = do_blocks( $content );
  151. $content = wptexturize( $content );
  152. $content = wp_filter_content_tags( $content );
  153. $content = str_replace( ']]>', ']]&gt;', $content );
  154. // Wrap block template in .wp-site-blocks to allow for specific descendant styles
  155. // (e.g. `.wp-site-blocks > *`).
  156. return '<div class="wp-site-blocks">' . $content . '</div>';
  157. }
  158. /**
  159. * Renders a 'viewport' meta tag.
  160. *
  161. * This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
  162. *
  163. * @access private
  164. * @since 5.8.0
  165. */
  166. function _block_template_viewport_meta_tag() {
  167. echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
  168. }
  169. /**
  170. * Strips .php or .html suffix from template file names.
  171. *
  172. * @access private
  173. * @since 5.8.0
  174. *
  175. * @param string $template_file Template file name.
  176. * @return string Template file name without extension.
  177. */
  178. function _strip_template_file_suffix( $template_file ) {
  179. return preg_replace( '/\.(php|html)$/', '', $template_file );
  180. }
  181. /**
  182. * Removes post details from block context when rendering a block template.
  183. *
  184. * @access private
  185. * @since 5.8.0
  186. *
  187. * @param array $context Default context.
  188. *
  189. * @return array Filtered context.
  190. */
  191. function _block_template_render_without_post_block_context( $context ) {
  192. /*
  193. * When loading a template directly and not through a page that resolves it,
  194. * the top-level post ID and type context get set to that of the template.
  195. * Templates are just the structure of a site, and they should not be available
  196. * as post context because blocks like Post Content would recurse infinitely.
  197. */
  198. if ( isset( $context['postType'] ) && 'wp_template' === $context['postType'] ) {
  199. unset( $context['postId'] );
  200. unset( $context['postType'] );
  201. }
  202. return $context;
  203. }