block-templates.php
1 year ago
class-templates-list-table.php
2 years ago
default-template-types.php
7 months ago
full-site-editing.php
1 year ago
get-block-templates.php
1 year ago
template-loader.php
1 year ago
template-parts.php
1 month ago
templates.php
1 month ago
get-block-templates.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Retrieves a list of unified template objects based on a query. |
| 5 | * |
| 6 | * @since 5.8.0 |
| 7 | * |
| 8 | * @param array $query { |
| 9 | * Optional. Arguments to retrieve templates. |
| 10 | * |
| 11 | * @type array $slug__in List of slugs to include. |
| 12 | * @type int $wp_id Post ID of customized template. |
| 13 | * @type string $theme Theme slug |
| 14 | * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). |
| 15 | * @type string $post_type Post type to get the templates for. |
| 16 | * } |
| 17 | * @param string $template_type 'wp_template' or 'wp_template_part'. |
| 18 | * |
| 19 | * @return array Templates. |
| 20 | */ |
| 21 | |
| 22 | function kubio_get_block_templates( $query = array(), $template_type = 'wp_template' ) { |
| 23 | $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; |
| 24 | $wp_query_args = array( |
| 25 | 'post_status' => array( 'auto-draft', 'draft', 'publish' ), |
| 26 | 'post_type' => $template_type, |
| 27 | 'posts_per_page' => -1, |
| 28 | 'no_found_rows' => true, |
| 29 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 30 | 'tax_query' => array( |
| 31 | array( |
| 32 | 'taxonomy' => 'wp_theme', |
| 33 | 'field' => 'name', |
| 34 | 'terms' => isset( $query['theme'] ) ? $query['theme'] : wp_get_theme()->get_stylesheet(), |
| 35 | ), |
| 36 | ), |
| 37 | ); |
| 38 | |
| 39 | if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { |
| 40 | $wp_query_args['tax_query'][] = array( |
| 41 | 'taxonomy' => 'wp_template_part_area', |
| 42 | 'field' => 'name', |
| 43 | 'terms' => $query['area'], |
| 44 | ); |
| 45 | $wp_query_args['tax_query']['relation'] = 'AND'; |
| 46 | } |
| 47 | |
| 48 | if ( isset( $query['slug__in'] ) ) { |
| 49 | $wp_query_args['post_name__in'] = $query['slug__in']; |
| 50 | } |
| 51 | |
| 52 | // This is only needed for the regular templates/template parts post type listing and editor. |
| 53 | if ( isset( $query['wp_id'] ) ) { |
| 54 | $wp_query_args['p'] = $query['wp_id']; |
| 55 | } else { |
| 56 | $wp_query_args['post_status'] = 'publish'; |
| 57 | } |
| 58 | |
| 59 | $template_query = new WP_Query( $wp_query_args ); |
| 60 | $query_result = array(); |
| 61 | foreach ( $template_query->posts as $post ) { |
| 62 | $template = _build_block_template_result_from_post( $post ); |
| 63 | |
| 64 | if ( is_wp_error( $template ) ) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if ( $post_type && ! $template->is_custom ) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | $query_result[] = $template; |
| 73 | } |
| 74 | |
| 75 | if ( ! isset( $query['wp_id'] ) ) { |
| 76 | $template_files = _get_block_templates_files( $template_type ); |
| 77 | foreach ( $template_files as $template_file ) { |
| 78 | $template = _build_block_template_result_from_file( $template_file, $template_type ); |
| 79 | |
| 80 | if ( $post_type && ! $template->is_custom ) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if ( $post_type && |
| 85 | isset( $template->post_types ) && |
| 86 | ! in_array( $post_type, $template->post_types, true ) |
| 87 | ) { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | $is_not_custom = false === array_search( |
| 92 | wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], |
| 93 | array_column( $query_result, 'id' ), |
| 94 | true |
| 95 | ); |
| 96 | $fits_slug_query = |
| 97 | ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); |
| 98 | $fits_area_query = |
| 99 | ! isset( $query['area'] ) || $template_file['area'] === $query['area']; |
| 100 | $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; |
| 101 | if ( $should_include ) { |
| 102 | $query_result[] = $template; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Filters the array of queried block templates array after they've been fetched. |
| 109 | * |
| 110 | * @since 5.9.0 |
| 111 | * |
| 112 | * @param WP_Block_Template[] $query_result Array of found block templates. |
| 113 | * @param array $query { |
| 114 | * Optional. Arguments to retrieve templates. |
| 115 | * |
| 116 | * @type array $slug__in List of slugs to include. |
| 117 | * @type int $wp_id Post ID of customized template. |
| 118 | * } |
| 119 | * @param string $template_type wp_template or wp_template_part. |
| 120 | */ |
| 121 | return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); |
| 122 | } |
| 123 |