From 51f72829502569d3b1a8eb7ddf6aa11b630067ec Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 17 Jun 2020 17:06:00 -0400 Subject: [PATCH 1/7] add auto-drafting for theme files --- lib/template-parts.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/template-parts.php b/lib/template-parts.php index 0edc337c1e968..5fbe70ae8ef2e 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -213,6 +213,17 @@ function filter_rest_wp_template_part_query( $args, $request ) { 'value' => $request['theme'], ); + // Ensure auto-drafts of all theme supplied template parts are created. + if( $request['theme'] === get_current_theme() ) { + foreach ( get_template_types() as $template_type ) { + // Skip 'embed' for now because it is not a regular template type. + if ( in_array( $template_type, array( 'embed' ), true ) ) { + continue; + } + gutenberg_find_template_post_and_parts( $template_type ); + } + }; + $args['meta_query'] = $meta_query; } From 52733d2535549d032b0ef9965b29b1c47082833c Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 17 Jun 2020 20:55:56 -0400 Subject: [PATCH 2/7] try getting them from the template part folder --- lib/template-parts.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 5fbe70ae8ef2e..0a3416e09f2a6 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -215,12 +215,15 @@ function filter_rest_wp_template_part_query( $args, $request ) { // Ensure auto-drafts of all theme supplied template parts are created. if( $request['theme'] === get_current_theme() ) { - foreach ( get_template_types() as $template_type ) { - // Skip 'embed' for now because it is not a regular template type. - if ( in_array( $template_type, array( 'embed' ), true ) ) { - continue; - } - gutenberg_find_template_post_and_parts( $template_type ); + $template_part_files = glob( get_stylesheet_directory() . '/block-template-parts/*.html' ); + $template_part_files = is_array( $template_part_files ) ? $template_part_files : array(); + if ( is_child_theme() ) { + $child_template_part_files = glob( get_template_directory() . '/block-template-parts/*.html' ); + $child_template_part_files = is_array( $child_template_part_files ) ? $child_template_part_files : array(); + $template_part_files = array_merge( $template_part_files, $child_template_part_files ); + } + foreach ( $template_part_files as $template_part ) { + create_auto_draft_for_template_part_block( $template_part ); } }; From 820c0b344c919c01192fad94f4a21a099adbb3ba Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 18 Jun 2020 17:28:20 -0400 Subject: [PATCH 3/7] yoda and linter things --- lib/template-parts.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 0a3416e09f2a6..1bb2f8902f1ff 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -214,7 +214,7 @@ function filter_rest_wp_template_part_query( $args, $request ) { ); // Ensure auto-drafts of all theme supplied template parts are created. - if( $request['theme'] === get_current_theme() ) { + if ( wp_get_theme() === $request['theme'] ) { $template_part_files = glob( get_stylesheet_directory() . '/block-template-parts/*.html' ); $template_part_files = is_array( $template_part_files ) ? $template_part_files : array(); if ( is_child_theme() ) { @@ -222,7 +222,8 @@ function filter_rest_wp_template_part_query( $args, $request ) { $child_template_part_files = is_array( $child_template_part_files ) ? $child_template_part_files : array(); $template_part_files = array_merge( $template_part_files, $child_template_part_files ); } - foreach ( $template_part_files as $template_part ) { + foreach ( $template_part_files as $template_part_file ) { + $template_part = parse_block( $template_part_file )[0]; create_auto_draft_for_template_part_block( $template_part ); } }; From 09289307a68584ee5a3fdaca756501ab1d0d13fe Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 18 Jun 2020 18:03:51 -0400 Subject: [PATCH 4/7] build template part to parse --- lib/template-parts.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 1bb2f8902f1ff..311dd243212a3 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -223,7 +223,17 @@ function filter_rest_wp_template_part_query( $args, $request ) { $template_part_files = array_merge( $template_part_files, $child_template_part_files ); } foreach ( $template_part_files as $template_part_file ) { - $template_part = parse_block( $template_part_file )[0]; + $content = file_get_contents( $template_part_file ); + // Infer slug from filepath. + $slug = substr( + $template_part_file, + // Starting position of slug. + strpos( $template_part_file, 'block-template-parts/' ) + 21, + // Subtract ending '.html'. + -5 + ); + $template_part_string = '' . $content . ''; + $template_part = parse_blocks( $template_part_string )[0]; create_auto_draft_for_template_part_block( $template_part ); } }; From 75c2e90953f82686c49ca03854247dbaa30205e1 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 18 Jun 2020 18:36:20 -0400 Subject: [PATCH 5/7] build template part block --- lib/template-parts.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/template-parts.php b/lib/template-parts.php index 311dd243212a3..3b5bea77cd325 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -214,7 +214,8 @@ function filter_rest_wp_template_part_query( $args, $request ) { ); // Ensure auto-drafts of all theme supplied template parts are created. - if ( wp_get_theme() === $request['theme'] ) { + if ( wp_get_theme()->get( 'TextDomain' ) === $request['theme'] ) { + // Get file paths for all theme supplied template parts. $template_part_files = glob( get_stylesheet_directory() . '/block-template-parts/*.html' ); $template_part_files = is_array( $template_part_files ) ? $template_part_files : array(); if ( is_child_theme() ) { @@ -222,6 +223,7 @@ function filter_rest_wp_template_part_query( $args, $request ) { $child_template_part_files = is_array( $child_template_part_files ) ? $child_template_part_files : array(); $template_part_files = array_merge( $template_part_files, $child_template_part_files ); } + // Build and save each template part. foreach ( $template_part_files as $template_part_file ) { $content = file_get_contents( $template_part_file ); // Infer slug from filepath. @@ -232,9 +234,10 @@ function filter_rest_wp_template_part_query( $args, $request ) { // Subtract ending '.html'. -5 ); - $template_part_string = '' . $content . ''; - $template_part = parse_blocks( $template_part_string )[0]; - create_auto_draft_for_template_part_block( $template_part ); + // Wrap content with the template part block, parse, and create auto-draft. + $template_part_string = '' . $content . ''; + $template_part_block = parse_blocks( $template_part_string )[0]; + create_auto_draft_for_template_part_block( $template_part_block ); } }; From 58933c47f1ebcf985152c284e29fbc21d8441040 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 18 Jun 2020 19:57:58 -0400 Subject: [PATCH 6/7] update template part selection query --- .../edit/placeholder/template-part-previews.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js b/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js index 2d4d87801c0cd..fe3cb3d6a8df1 100644 --- a/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js +++ b/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js @@ -178,11 +178,13 @@ export default function TemplateParts( { setAttributes, filterValue } ) { per_page: -1, } ); - const resolvedTemplateParts = select( 'core' ).getEntityRecords( + const currentTheme = select( 'core' ).getCurrentTheme().textdomain; + const themeTemplateParts = select( 'core' ).getEntityRecords( 'postType', 'wp_template_part', { - resolved: true, + theme: currentTheme, + status: [ 'publish', 'auto-draft' ], per_page: -1, } ); @@ -190,8 +192,8 @@ export default function TemplateParts( { setAttributes, filterValue } ) { if ( publishedTemplateParts ) { combinedTemplateParts.push( ...publishedTemplateParts ); } - if ( resolvedTemplateParts ) { - combinedTemplateParts.push( ...resolvedTemplateParts ); + if ( themeTemplateParts ) { + combinedTemplateParts.push( ...themeTemplateParts ); } return uniq( combinedTemplateParts ); }, [] ); From 0ce857d08448f995154079800ce14a99664687e8 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 19 Jun 2020 13:39:56 -0400 Subject: [PATCH 7/7] check currentTheme exists before getting textdomain --- .../template-part/edit/placeholder/template-part-previews.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js b/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js index fe3cb3d6a8df1..7cbe4312cf9a2 100644 --- a/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js +++ b/packages/block-library/src/template-part/edit/placeholder/template-part-previews.js @@ -178,7 +178,7 @@ export default function TemplateParts( { setAttributes, filterValue } ) { per_page: -1, } ); - const currentTheme = select( 'core' ).getCurrentTheme().textdomain; + const currentTheme = select( 'core' ).getCurrentTheme()?.textdomain; const themeTemplateParts = select( 'core' ).getEntityRecords( 'postType', 'wp_template_part',