diff --git a/lib/template-loader.php b/lib/template-loader.php index b534db1637b2e4..6eaf71fc7d9842 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -149,8 +149,13 @@ function create_auto_draft_for_template_part_block( $block ) { 'post_type' => 'wp_template_part', 'post_status' => array( 'publish', 'auto-draft' ), 'title' => $block['attrs']['slug'], - 'meta_key' => 'theme', - 'meta_value' => $block['attrs']['theme'], + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $block['attrs']['theme'], + ), + ), 'posts_per_page' => 1, 'no_found_rows' => true, ) @@ -180,6 +185,11 @@ function create_auto_draft_for_template_part_block( $block ) { if ( $template_part_post && $template_part_post->post_content === $file_contents ) { $template_part_id = $template_part_post->ID; } else { + + // Get the term used for this theme. + $wp_theme_term = get_term_by( 'slug', $block['attrs']['theme'], 'wp_theme' ); + + // Insert the post. $template_part_id = wp_insert_post( array( 'post_content' => $file_contents, @@ -187,11 +197,16 @@ function create_auto_draft_for_template_part_block( $block ) { 'post_status' => 'auto-draft', 'post_type' => 'wp_template_part', 'post_name' => $block['attrs']['slug'], - 'meta_input' => array( - 'theme' => $block['attrs']['theme'], - ), ) ); + + if ( $wp_theme_term ) { + // If the term was found, assign it to this post. + wp_set_object_terms( $template_part_id, $wp_theme_term->term_id, 'wp_theme' ); + } else { + // Using a string instead of integer will create the term and assign it to the post. + wp_set_object_terms( $template_part_id, $block['attrs']['theme'], 'wp_theme' ); + } } } } diff --git a/lib/template-parts.php b/lib/template-parts.php index a6acb9baaa1fc8..c53088b5c5c327 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -51,16 +51,7 @@ function gutenberg_register_template_part_post_type() { ), ); - $meta_args = array( - 'object_subtype' => 'wp_template_part', - 'type' => 'string', - 'description' => 'The theme that provided the template part, if any.', - 'single' => true, - 'show_in_rest' => true, - ); - register_post_type( 'wp_template_part', $args ); - register_meta( 'post', 'theme', $meta_args ); } add_action( 'init', 'gutenberg_register_template_part_post_type' ); @@ -203,10 +194,11 @@ function filter_rest_wp_template_part_query( $args, $request ) { } if ( $request['theme'] ) { - $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); - $meta_query[] = array( - 'key' => 'theme', - 'value' => $request['theme'], + $tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array(); + $tax_query[] = array( + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $request['theme'], ); // Ensure auto-drafts of all theme supplied template parts are created. @@ -252,9 +244,33 @@ function get_template_part_paths( $base_directory ) { } }; - $args['meta_query'] = $meta_query; + $args['tax_query'] = $tax_query; } return $args; } add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 ); + + +/** + * Filters the post data for a response. + * + * @param WP_REST_Response $response The response object. + * @return WP_REST_Response + */ +function filter_rest_prepare_wp_template_part( $response ) { + if ( isset( $response->data ) && is_array( $response->data ) && isset( $response->data['id'] ) ) { + $response->data['wp_theme_slug'] = 'false'; + + // Get the wp_theme terms. + $wp_themes = wp_get_post_terms( $response->data['id'], 'wp_theme' ); + + // If a theme is assigned, add it to the REST response. + if ( $wp_themes && is_array( $wp_themes ) && isset( $wp_themes[0] ) ) { + $response->data['wp_theme_slug'] = $wp_themes[0]->slug; + } + } + + return $response; +} +add_filter( 'rest_prepare_wp_template_part', 'filter_rest_prepare_wp_template_part' ); diff --git a/lib/templates.php b/lib/templates.php index d0c9075b8db848..c9be943fdb5385 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -80,6 +80,23 @@ function gutenberg_register_template_post_type() { } add_action( 'init', 'gutenberg_register_template_post_type' ); +/** + * Registers block editor 'wp_theme' taxonomy. + */ +function gutenberg_register_wp_theme_taxonomy() { + + // Register wp_theme taxonomy. + register_taxonomy( + 'wp_theme', + array( 'wp_template', 'wp_template_part' ), + array( + 'public' => false, + 'show_in_rest' => true, + ) + ); +} +add_action( 'init', 'gutenberg_register_wp_theme_taxonomy' ); + /** * Filters the capabilities of a user to conditionally grant them capabilities for managing 'wp_template' posts. * diff --git a/packages/block-library/src/template-part/edit/placeholder/index.js b/packages/block-library/src/template-part/edit/placeholder/index.js index 7359538cd00f54..1ec7a722060ddc 100644 --- a/packages/block-library/src/template-part/edit/placeholder/index.js +++ b/packages/block-library/src/template-part/edit/placeholder/index.js @@ -31,7 +31,7 @@ export default function TemplatePartPlaceholder( { setAttributes } ) { setAttributes( { postId: templatePart.id, slug: templatePart.slug, - theme: templatePart.meta.theme, + theme: templatePart.wp_theme_slug, } ); }, [ setAttributes ] ); diff --git a/packages/block-library/src/template-part/edit/selection/template-part-previews.js b/packages/block-library/src/template-part/edit/selection/template-part-previews.js index 1ef852a99acd23..a171260e1f7659 100644 --- a/packages/block-library/src/template-part/edit/selection/template-part-previews.js +++ b/packages/block-library/src/template-part/edit/selection/template-part-previews.js @@ -102,14 +102,14 @@ function TemplatePartsByTheme( { composite, } ) { const templatePartsByTheme = useMemo( () => { - return Object.values( groupBy( templateParts, 'meta.theme' ) ); + return Object.values( groupBy( templateParts, 'wp_theme_slug' ) ); }, [ templateParts ] ); const currentShownTPs = useAsyncList( templateParts ); return templatePartsByTheme.map( ( templatePartList ) => ( { templatePartList.map( ( templatePart ) => { return currentShownTPs.includes( templatePart ) ? ( @@ -164,10 +164,10 @@ function TemplatePartSearchResults( { // Second prioritize index found in theme. // Since diacritics can be used in theme names, remove them for the comparison. return ( - deburr( a.meta.theme ) + deburr( a.wp_theme_slug ) .toLowerCase() .indexOf( normalizedFilterValue ) - - deburr( b.meta.theme ) + deburr( b.wp_theme_slug ) .toLowerCase() .indexOf( normalizedFilterValue ) ); @@ -178,7 +178,10 @@ function TemplatePartSearchResults( { const currentShownTPs = useAsyncList( filteredTPs ); return filteredTPs.map( ( templatePart ) => ( - + { currentShownTPs.includes( templatePart ) ? ( post.slug === cleanedSlug && - post.meta && - post.meta.theme === theme + post.wp_theme_slug === theme ); // A published post might already exist if this template part was customized elsewhere // or if it's part of a customized template. diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 29e3668170f2f4..1d877bf8b5f043 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -23,10 +23,15 @@ function render_block_core_template_part( $attributes ) { $template_part_query = new WP_Query( array( 'post_type' => 'wp_template_part', - 'post_status' => 'publish', + 'post_status' => array( 'publish', 'auto-draft' ), 'name' => $attributes['slug'], - 'meta_key' => 'theme', - 'meta_value' => $attributes['theme'], + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $attributes['theme'], + ), + ), 'posts_per_page' => 1, 'no_found_rows' => true, )