No Description

block-template-utils.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Utilities used to fetch and create templates.
  4. *
  5. * @package WordPress
  6. * @since 5.8.0
  7. */
  8. /**
  9. * Build a unified template object based a post Object.
  10. *
  11. * @access private
  12. * @since 5.8.0
  13. *
  14. * @param WP_Post $post Template post.
  15. *
  16. * @return WP_Block_Template|WP_Error Template.
  17. */
  18. function _build_template_result_from_post( $post ) {
  19. $terms = get_the_terms( $post, 'wp_theme' );
  20. if ( is_wp_error( $terms ) ) {
  21. return $terms;
  22. }
  23. if ( ! $terms ) {
  24. return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
  25. }
  26. $theme = $terms[0]->name;
  27. $template = new WP_Block_Template();
  28. $template->wp_id = $post->ID;
  29. $template->id = $theme . '//' . $post->post_name;
  30. $template->theme = $theme;
  31. $template->content = $post->post_content;
  32. $template->slug = $post->post_name;
  33. $template->source = 'custom';
  34. $template->type = $post->post_type;
  35. $template->description = $post->post_excerpt;
  36. $template->title = $post->post_title;
  37. $template->status = $post->post_status;
  38. $template->has_theme_file = false;
  39. return $template;
  40. }
  41. /**
  42. * Retrieves a list of unified template objects based on a query.
  43. *
  44. * @since 5.8.0
  45. *
  46. * @param array $query {
  47. * Optional. Arguments to retrieve templates.
  48. *
  49. * @type array $slug__in List of slugs to include.
  50. * @type int $wp_id Post ID of customized template.
  51. * }
  52. * @param string $template_type Optional. The template type (post type). Default 'wp_template'.
  53. * @return WP_Block_Template[] Block template objects.
  54. */
  55. function get_block_templates( $query = array(), $template_type = 'wp_template' ) {
  56. $wp_query_args = array(
  57. 'post_status' => array( 'auto-draft', 'draft', 'publish' ),
  58. 'post_type' => $template_type,
  59. 'posts_per_page' => -1,
  60. 'no_found_rows' => true,
  61. 'tax_query' => array(
  62. array(
  63. 'taxonomy' => 'wp_theme',
  64. 'field' => 'name',
  65. 'terms' => wp_get_theme()->get_stylesheet(),
  66. ),
  67. ),
  68. );
  69. if ( isset( $query['slug__in'] ) ) {
  70. $wp_query_args['post_name__in'] = $query['slug__in'];
  71. }
  72. // This is only needed for the regular templates CPT listing and editor.
  73. if ( isset( $query['wp_id'] ) ) {
  74. $wp_query_args['p'] = $query['wp_id'];
  75. } else {
  76. $wp_query_args['post_status'] = 'publish';
  77. }
  78. $template_query = new WP_Query( $wp_query_args );
  79. $query_result = array();
  80. foreach ( $template_query->posts as $post ) {
  81. $template = _build_template_result_from_post( $post );
  82. if ( ! is_wp_error( $template ) ) {
  83. $query_result[] = $template;
  84. }
  85. }
  86. return $query_result;
  87. }
  88. /**
  89. * Retrieves a single unified template object using its id.
  90. *
  91. * @since 5.8.0
  92. *
  93. * @param string $id Template unique identifier (example: theme_slug//template_slug).
  94. * @param string $template_type wp_template.
  95. *
  96. * @return WP_Block_Template|null Template.
  97. */
  98. function get_block_template( $id, $template_type = 'wp_template' ) {
  99. $parts = explode( '//', $id, 2 );
  100. if ( count( $parts ) < 2 ) {
  101. return null;
  102. }
  103. list( $theme, $slug ) = $parts;
  104. $wp_query_args = array(
  105. 'post_name__in' => array( $slug ),
  106. 'post_type' => $template_type,
  107. 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
  108. 'posts_per_page' => 1,
  109. 'no_found_rows' => true,
  110. 'tax_query' => array(
  111. array(
  112. 'taxonomy' => 'wp_theme',
  113. 'field' => 'name',
  114. 'terms' => $theme,
  115. ),
  116. ),
  117. );
  118. $template_query = new WP_Query( $wp_query_args );
  119. $posts = $template_query->posts;
  120. if ( count( $posts ) > 0 ) {
  121. $template = _build_template_result_from_post( $posts[0] );
  122. if ( ! is_wp_error( $template ) ) {
  123. return $template;
  124. }
  125. }
  126. return null;
  127. }