wp/wp-includes/block-template-utils.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     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 wp_template.
       
   108  *
       
   109  * @return WP_Block_Template|null Template.
       
   110  */
       
   111 function get_block_template( $id, $template_type = 'wp_template' ) {
       
   112 	$parts = explode( '//', $id, 2 );
       
   113 	if ( count( $parts ) < 2 ) {
       
   114 		return null;
       
   115 	}
       
   116 	list( $theme, $slug ) = $parts;
       
   117 	$wp_query_args        = array(
       
   118 		'post_name__in'  => array( $slug ),
       
   119 		'post_type'      => $template_type,
       
   120 		'post_status'    => array( 'auto-draft', 'draft', 'publish', 'trash' ),
       
   121 		'posts_per_page' => 1,
       
   122 		'no_found_rows'  => true,
       
   123 		'tax_query'      => array(
       
   124 			array(
       
   125 				'taxonomy' => 'wp_theme',
       
   126 				'field'    => 'name',
       
   127 				'terms'    => $theme,
       
   128 			),
       
   129 		),
       
   130 	);
       
   131 	$template_query       = new WP_Query( $wp_query_args );
       
   132 	$posts                = $template_query->posts;
       
   133 
       
   134 	if ( count( $posts ) > 0 ) {
       
   135 		$template = _build_template_result_from_post( $posts[0] );
       
   136 
       
   137 		if ( ! is_wp_error( $template ) ) {
       
   138 			return $template;
       
   139 		}
       
   140 	}
       
   141 
       
   142 	return null;
       
   143 }