From 5e13b92b31676b662917fd8b2a107c748c3544c1 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 21 Aug 2020 14:19:35 +0300 Subject: [PATCH 1/9] Started working on #24377 --- lib/template-loader.php | 39 +++++++++++++++++-- lib/template-parts.php | 22 ++++------- lib/templates.php | 16 ++++++++ .../block-library/src/template-part/index.php | 9 ++++- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/lib/template-loader.php b/lib/template-loader.php index e68f1740c711bd..6ae3667716ea6a 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' ), 'name' => $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,32 @@ 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. + $theme_tax_id = get_term_by( 'slug', $block['attrs']['theme'], 'wp_theme' ); + + // If a term for this theme doesn't exist, create it. + if ( ! $theme_tax_id ) { + + // Acts as a fallback. + $theme_tax_id = 0; + + // Insert the term. + $wp_theme_term = wp_insert_term( + $block['attrs']['theme'], + 'wp_theme', + array( + 'slug' => $block['attrs']['theme'], + ) + ); + + // If all went well, retrieve the term-ID. + if ( ! is_wp_error( $wp_theme_term ) ) { + $theme_tax_id = $wp_theme_term['term_id']; + } + } + + // Insert the post. $template_part_id = wp_insert_post( array( 'post_content' => $file_contents, @@ -187,8 +218,8 @@ 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'], + 'tax_input' => array( + 'wp_theme' => array( $theme_tax_id ), ), ) ); diff --git a/lib/template-parts.php b/lib/template-parts.php index dde7189621d9f3..1e9f17d945c829 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,13 @@ 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( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $request['theme'], + ), ); // Ensure auto-drafts of all theme supplied template parts are created. @@ -237,7 +231,7 @@ function filter_rest_wp_template_part_query( $args, $request ) { } }; - $args['meta_query'] = $meta_query; + $args['tax_query'] = $tax_query; } return $args; diff --git a/lib/templates.php b/lib/templates.php index 971398dbd4ff8c..86c1c16b82035b 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -55,6 +55,22 @@ 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' => 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/index.php b/packages/block-library/src/template-part/index.php index 27a08594eaca41..54ca62957d9261 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -25,8 +25,13 @@ function render_block_core_template_part( $attributes ) { 'post_type' => 'wp_template_part', 'post_status' => 'publish', '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, ) From bc228e3333c476b9fb76cb1ad17cd104c281bb85 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 25 Aug 2020 11:44:48 +0300 Subject: [PATCH 2/9] simplify & fix bugs --- lib/template-loader.php | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/lib/template-loader.php b/lib/template-loader.php index 6ae3667716ea6a..445ba977707073 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -187,28 +187,7 @@ function create_auto_draft_for_template_part_block( $block ) { } else { // Get the term used for this theme. - $theme_tax_id = get_term_by( 'slug', $block['attrs']['theme'], 'wp_theme' ); - - // If a term for this theme doesn't exist, create it. - if ( ! $theme_tax_id ) { - - // Acts as a fallback. - $theme_tax_id = 0; - - // Insert the term. - $wp_theme_term = wp_insert_term( - $block['attrs']['theme'], - 'wp_theme', - array( - 'slug' => $block['attrs']['theme'], - ) - ); - - // If all went well, retrieve the term-ID. - if ( ! is_wp_error( $wp_theme_term ) ) { - $theme_tax_id = $wp_theme_term['term_id']; - } - } + $wp_theme_term = get_term_by( 'slug', $block['attrs']['theme'], 'wp_theme' ); // Insert the post. $template_part_id = wp_insert_post( @@ -218,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'], - 'tax_input' => array( - 'wp_theme' => array( $theme_tax_id ), - ), ) ); + + 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' ); + } } } } From d78641ba371e0d33c1e7f2067127b8cf0aa9cc71 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 25 Aug 2020 11:46:39 +0300 Subject: [PATCH 3/9] Hide taxonomy --- lib/templates.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/templates.php b/lib/templates.php index 86c1c16b82035b..e18d6bc569b4d9 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -65,7 +65,7 @@ function gutenberg_register_wp_theme_taxonomy() { 'wp_theme', array( 'wp_template', 'wp_template_part' ), array( - 'public' => true, + 'public' => false, ) ); } From 414596c240001a3938e9896ff75711a1d426e2e0 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 22 Sep 2020 12:46:09 +0300 Subject: [PATCH 4/9] fix tax_query --- lib/template-parts.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 8af2934842faa2..5a0b5d511e2962 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -196,11 +196,9 @@ function filter_rest_wp_template_part_query( $args, $request ) { if ( $request['theme'] ) { $tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array(); $tax_query[] = array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'slug', - 'terms' => $request['theme'], - ), + 'taxonomy' => 'wp_theme', + 'field' => 'slug', + 'terms' => $request['theme'], ); // Ensure auto-drafts of all theme supplied template parts are created. From 960fa1a98f1683fbb05bbb068b383183932f4005 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 22 Sep 2020 14:52:16 +0300 Subject: [PATCH 5/9] Allow auto-draft in the block query --- packages/block-library/src/template-part/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index b1b6cc4b058975..1d877bf8b5f043 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -23,7 +23,7 @@ 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'], 'tax_query' => array( array( From 41c80459c4996ca0bc6f36c3e83a952f168179fd Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 23 Sep 2020 15:30:45 +0300 Subject: [PATCH 6/9] WIP: show the template-parts --- .../src/template-part/edit/use-template-part-post.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/block-library/src/template-part/edit/use-template-part-post.js b/packages/block-library/src/template-part/edit/use-template-part-post.js index 73e027f27eb364..3c9dfb8210f849 100644 --- a/packages/block-library/src/template-part/edit/use-template-part-post.js +++ b/packages/block-library/src/template-part/edit/use-template-part-post.js @@ -34,10 +34,7 @@ export default function useTemplatePartPost( postId, slug, theme ) { } ); const foundPosts = posts?.filter( - ( post ) => - post.slug === cleanedSlug && - post.meta && - post.meta.theme === theme + ( post ) => post.slug === cleanedSlug ); // A published post might already exist if this template part was customized elsewhere // or if it's part of a customized template. From 002f79b8d85c528839535eca856a0bd8733f2b5a Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 28 Sep 2020 12:24:41 +0300 Subject: [PATCH 7/9] Fix REST API responses to include the tax instead of meta --- lib/template-parts.php | 24 +++++++++++++++++++ .../template-part/edit/placeholder/index.js | 2 +- .../edit/selection/template-part-previews.js | 12 +++++----- .../edit/use-template-part-post.js | 3 ++- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 5a0b5d511e2962..a56ae55f6530d7 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -250,3 +250,27 @@ function get_template_part_paths( $base_directory ) { 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'] = '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'] = $wp_themes[0]->slug; + } + } + + return $response; +} +add_filter( 'rest_prepare_wp_template_part', 'filter_rest_prepare_wp_template_part' ); 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..b1d352fa1a1571 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, } ); }, [ 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 540e93c88daa0c..c7a88f1200f79b 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 @@ -87,14 +87,14 @@ function PanelGroup( { title, icon, children } ) { function TemplatePartsByTheme( { templateParts, setAttributes, onClose } ) { const templatePartsByTheme = useMemo( () => { - return Object.values( groupBy( templateParts, 'meta.theme' ) ); + return Object.values( groupBy( templateParts, 'wp_theme' ) ); }, [ templateParts ] ); const currentShownTPs = useAsyncList( templateParts ); return templatePartsByTheme.map( ( templatePartList ) => ( { templatePartList.map( ( templatePart ) => { return currentShownTPs.includes( templatePart ) ? ( @@ -147,10 +147,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 ) .toLowerCase() .indexOf( normalizedFilterValue ) - - deburr( b.meta.theme ) + deburr( b.wp_theme ) .toLowerCase() .indexOf( normalizedFilterValue ) ); @@ -161,7 +161,7 @@ function TemplatePartSearchResults( { const currentShownTPs = useAsyncList( filteredTPs ); return filteredTPs.map( ( templatePart ) => ( - + { currentShownTPs.includes( templatePart ) ? ( post.slug === cleanedSlug + ( post ) => + post.slug === cleanedSlug && post.wp_theme === theme ); // A published post might already exist if this template part was customized elsewhere // or if it's part of a customized template. From 745d8457775aa31023e1d329ec94688756112426 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 1 Oct 2020 08:58:10 +0300 Subject: [PATCH 8/9] Show taxonomy in REST --- lib/templates.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/templates.php b/lib/templates.php index e18d6bc569b4d9..64556b217ec564 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -65,7 +65,8 @@ function gutenberg_register_wp_theme_taxonomy() { 'wp_theme', array( 'wp_template', 'wp_template_part' ), array( - 'public' => false, + 'public' => false, + 'show_in_rest' => true, ) ); } From 752f6ef80a005e53609b47f6f56714da23d0ecf0 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 2 Oct 2020 14:11:08 +0300 Subject: [PATCH 9/9] REST: rename wp_theme to wp_theme_slug - conflict with default response --- lib/template-parts.php | 4 ++-- .../src/template-part/edit/placeholder/index.js | 2 +- .../edit/selection/template-part-previews.js | 15 +++++++++------ .../template-part/edit/use-template-part-post.js | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index a56ae55f6530d7..c53088b5c5c327 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -260,14 +260,14 @@ function get_template_part_paths( $base_directory ) { */ function filter_rest_prepare_wp_template_part( $response ) { if ( isset( $response->data ) && is_array( $response->data ) && isset( $response->data['id'] ) ) { - $response->data['wp_theme'] = 'false'; + $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'] = $wp_themes[0]->slug; + $response->data['wp_theme_slug'] = $wp_themes[0]->slug; } } 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 b1d352fa1a1571..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.wp_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 c7a88f1200f79b..50730856356b6e 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 @@ -87,14 +87,14 @@ function PanelGroup( { title, icon, children } ) { function TemplatePartsByTheme( { templateParts, setAttributes, onClose } ) { const templatePartsByTheme = useMemo( () => { - return Object.values( groupBy( templateParts, 'wp_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 ) ? ( @@ -147,10 +147,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.wp_theme ) + deburr( a.wp_theme_slug ) .toLowerCase() .indexOf( normalizedFilterValue ) - - deburr( b.wp_theme ) + deburr( b.wp_theme_slug ) .toLowerCase() .indexOf( normalizedFilterValue ) ); @@ -161,7 +161,10 @@ function TemplatePartSearchResults( { const currentShownTPs = useAsyncList( filteredTPs ); return filteredTPs.map( ( templatePart ) => ( - + { currentShownTPs.includes( templatePart ) ? ( - post.slug === cleanedSlug && post.wp_theme === theme + post.slug === cleanedSlug && + 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.