[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> block-template-utils.php (source)

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


Generated: Fri Sep 24 01:00:04 2021 Cross-referenced by PHPXref 0.7.1