From fa05b02f1fae94d4b092a8983b9ce72525112ead Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 21 Jan 2021 16:32:55 -0500 Subject: [PATCH 01/36] register term --- lib/full-site-editing/template-parts.php | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 172ca6a5ed2f9..9761707a8ece1 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -60,6 +60,35 @@ function gutenberg_register_template_part_post_type() { } add_action( 'init', 'gutenberg_register_template_part_post_type' ); +/** + * Registers the 'template_part_type' taxonomy. + */ +function gutenberg_register_template_part_type_taxonomy(){ + if ( ! gutenberg_is_fse_theme() ) { + return; + } + + register_taxonomy( + 'template_part_type', + array( 'wp_template_part' ), + array( + 'public' => false, + 'hierarchical' => false, + 'labels' => array( + 'name' => __( 'Types', 'gutenberg' ), + 'singular_name' => __( 'Type', 'gutenberg' ), + ), + 'query_var' => false, + 'rewrite' => false, + 'show_ui' => false, + '_builtin' => true, + 'show_in_nav_menus' => false, + 'show_in_rest' => false, + ) + ); +} +add_action( 'init', 'gutenberg_register_template_part_type_taxonomy' ); + /** * Fixes the label of the 'wp_template_part' admin menu entry. */ From 068293792ece7615c361b505519ee420d67a90bf Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 21 Jan 2021 18:47:13 -0500 Subject: [PATCH 02/36] update rest controller and build result functions --- lib/full-site-editing/block-templates.php | 42 ++++++++++++------- .../class-wp-rest-templates-controller.php | 25 ++++++++++- lib/full-site-editing/template-parts.php | 2 +- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 5f706c2233680..97a97cc4a038c 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -171,6 +171,17 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template->title = $default_template_types[ $template_file['slug'] ]['title']; } + if ( 'wp_template_part' === $template_type && is_readable( locate_template( 'experimental-theme.json' ) ) ) { + $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); + $data = json_decode( + $theme_json, + true + ); + if ( isset( $data['template-parts'][ $template_file['slug'] ]['type'] ) ) { + $template->template_part_type = $data['template-parts'][ $template_file['slug'] ]['type']; + } + } + return $template; } @@ -182,7 +193,8 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t * @return WP_Block_Template|WP_Error Template. */ function _gutenberg_build_template_result_from_post( $post ) { - $terms = get_the_terms( $post, 'wp_theme' ); + $terms = get_the_terms( $post, 'wp_theme' ); + $type_terms = get_the_terms( $post, 'template_part_type' ); if ( is_wp_error( $terms ) ) { return $terms; @@ -192,19 +204,21 @@ function _gutenberg_build_template_result_from_post( $post ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); } - $theme = $terms[0]->name; - - $template = new WP_Block_Template(); - $template->wp_id = $post->ID; - $template->id = $theme . '//' . $post->post_name; - $template->theme = $theme; - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->is_custom = true; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; + $theme = $terms[0]->name; + $template_part_type = $type_terms[0]->name; + + $template = new WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->is_custom = true; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + $template->template_part_type = $template_part_type; return $template; } diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 79eb4dbba56d7..30c19b787a122 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -336,7 +336,8 @@ protected function prepare_item_for_database( $request ) { $changes->post_type = $this->post_type; $changes->post_status = 'publish'; $changes->tax_input = array( - 'wp_theme' => $template->theme, + 'wp_theme' => $template->theme, + 'template_part_type' => $template->template_part_type, ); } else { $changes->ID = $template->wp_id; @@ -358,6 +359,16 @@ protected function prepare_item_for_database( $request ) { $changes->post_excerpt = $template->description; } + if ( isset( $request['template_part_type'] ) ) { + if ( is_array( $changes->tax_input ) ) { + $changes->tax_input['template_part_type'] = $request['template_part_type']; + } else { + $changes->tax_input = array( + 'template_part_type' => $request['template_part_type'], + ); + } + } + return $changes; } @@ -386,6 +397,10 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno 'wp_id' => $template->wp_id, ); + if ( 'wp_template_part' === $template->type ) { + $result['template_part_type'] = $template->template_part_type; + } + $result = $this->add_additional_fields_to_object( $result, $request ); $response = rest_ensure_response( $result ); @@ -536,6 +551,14 @@ public function get_item_schema() { ), ); + if ( 'wp_template_part' === $this->post_type ) { + $schema['properties']['template_part_type'] = array( + 'description' => __( 'Type of template part (header, footer, etc.)', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + ); + } + $this->schema = $schema; return $this->add_additional_fields_schema( $this->schema ); diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 9761707a8ece1..f55b6daf3ef31 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -63,7 +63,7 @@ function gutenberg_register_template_part_post_type() { /** * Registers the 'template_part_type' taxonomy. */ -function gutenberg_register_template_part_type_taxonomy(){ +function gutenberg_register_template_part_type_taxonomy() { if ( ! gutenberg_is_fse_theme() ) { return; } From 299124b69ab45fc4d6371dea4b7a1fa57e51709a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 21 Jan 2021 19:24:19 -0500 Subject: [PATCH 03/36] dont add term to regular template posts --- lib/full-site-editing/block-templates.php | 37 ++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 97a97cc4a038c..c205951287bbf 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -193,8 +193,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t * @return WP_Block_Template|WP_Error Template. */ function _gutenberg_build_template_result_from_post( $post ) { - $terms = get_the_terms( $post, 'wp_theme' ); - $type_terms = get_the_terms( $post, 'template_part_type' ); + $terms = get_the_terms( $post, 'wp_theme' ); if ( is_wp_error( $terms ) ) { return $terms; @@ -204,21 +203,25 @@ function _gutenberg_build_template_result_from_post( $post ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) ); } - $theme = $terms[0]->name; - $template_part_type = $type_terms[0]->name; - - $template = new WP_Block_Template(); - $template->wp_id = $post->ID; - $template->id = $theme . '//' . $post->post_name; - $template->theme = $theme; - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->is_custom = true; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; - $template->template_part_type = $template_part_type; + $theme = $terms[0]->name; + + $template = new WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->is_custom = true; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + + if ( 'wp_template_part' === $post->post_type ) { + $type_terms = get_the_terms( $post, 'template_part_type' ); + $template_part_type = $type_terms[0]->name; + $template->template_part_type = $template_part_type; + } return $template; } From 45451b3d0c4208b4c72425040f24fd371b64c322 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 21 Jan 2021 19:43:35 -0500 Subject: [PATCH 04/36] only prepare term for database if template part --- .../class-wp-rest-templates-controller.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 30c19b787a122..ce3c4ce7aefa1 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -336,8 +336,7 @@ protected function prepare_item_for_database( $request ) { $changes->post_type = $this->post_type; $changes->post_status = 'publish'; $changes->tax_input = array( - 'wp_theme' => $template->theme, - 'template_part_type' => $template->template_part_type, + 'wp_theme' => $template->theme, ); } else { $changes->ID = $template->wp_id; @@ -359,13 +358,23 @@ protected function prepare_item_for_database( $request ) { $changes->post_excerpt = $template->description; } - if ( isset( $request['template_part_type'] ) ) { - if ( is_array( $changes->tax_input ) ) { - $changes->tax_input['template_part_type'] = $request['template_part_type']; - } else { - $changes->tax_input = array( - 'template_part_type' => $request['template_part_type'], - ); + if ( 'wp_template_part' === $this->post_type ) { + $template_part_type = null; + + if ( isset( $request['template_part_type'] ) ) { + $template_part_type = $request['template_part_type']; + } elseif ( null !== $template && ! $template->is_custom ) { + $template_part_type = $template->template_part_type; + } + + if ( $template_part_type ) { + if ( is_array( $changes->tax_input ) ) { + $changes->tax_input['template_part_type'] = $template_part_type; + } else { + $changes->tax_input = array( + 'template_part_type' => $template_part_type, + ); + } } } From 264279a6a8dceb674be0fb4bf950b2a9c1128b20 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 22 Jan 2021 16:14:50 -0500 Subject: [PATCH 05/36] rename 'section' --- lib/full-site-editing/block-templates.php | 10 ++++---- .../class-wp-rest-templates-controller.php | 24 +++++-------------- lib/full-site-editing/template-parts.php | 10 ++++---- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index c205951287bbf..c16b10f11ed63 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -177,8 +177,8 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $theme_json, true ); - if ( isset( $data['template-parts'][ $template_file['slug'] ]['type'] ) ) { - $template->template_part_type = $data['template-parts'][ $template_file['slug'] ]['type']; + if ( isset( $data['template-parts'][ $template_file['slug'] ]['section'] ) ) { + $template->section = $data['template-parts'][ $template_file['slug'] ]['section']; } } @@ -218,9 +218,9 @@ function _gutenberg_build_template_result_from_post( $post ) { $template->status = $post->post_status; if ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'template_part_type' ); - $template_part_type = $type_terms[0]->name; - $template->template_part_type = $template_part_type; + $type_terms = get_the_terms( $post, 'section' ); + $section = $type_terms[0]->name; + $template->section = $section; } return $template; diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index ce3c4ce7aefa1..d1f4a4ea4f77a 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -359,22 +359,10 @@ protected function prepare_item_for_database( $request ) { } if ( 'wp_template_part' === $this->post_type ) { - $template_part_type = null; - - if ( isset( $request['template_part_type'] ) ) { - $template_part_type = $request['template_part_type']; + if ( isset( $request['section'] ) ) { + $changes->tax_input['section'] = $request['section']; } elseif ( null !== $template && ! $template->is_custom ) { - $template_part_type = $template->template_part_type; - } - - if ( $template_part_type ) { - if ( is_array( $changes->tax_input ) ) { - $changes->tax_input['template_part_type'] = $template_part_type; - } else { - $changes->tax_input = array( - 'template_part_type' => $template_part_type, - ); - } + $changes->tax_input['section'] = $template->section; } } @@ -407,7 +395,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno ); if ( 'wp_template_part' === $template->type ) { - $result['template_part_type'] = $template->template_part_type; + $result['section'] = $template->section; } $result = $this->add_additional_fields_to_object( $result, $request ); @@ -561,8 +549,8 @@ public function get_item_schema() { ); if ( 'wp_template_part' === $this->post_type ) { - $schema['properties']['template_part_type'] = array( - 'description' => __( 'Type of template part (header, footer, etc.)', 'gutenberg' ), + $schema['properties']['section'] = array( + 'description' => __( 'Where the template part is intended for use (header, footer, etc.)', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), ); diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index f55b6daf3ef31..ade5e7d9dd125 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -63,20 +63,20 @@ function gutenberg_register_template_part_post_type() { /** * Registers the 'template_part_type' taxonomy. */ -function gutenberg_register_template_part_type_taxonomy() { +function gutenberg_register_template_part_section_taxonomy() { if ( ! gutenberg_is_fse_theme() ) { return; } register_taxonomy( - 'template_part_type', + 'section', array( 'wp_template_part' ), array( 'public' => false, 'hierarchical' => false, 'labels' => array( - 'name' => __( 'Types', 'gutenberg' ), - 'singular_name' => __( 'Type', 'gutenberg' ), + 'name' => __( 'Sections', 'gutenberg' ), + 'singular_name' => __( 'Section', 'gutenberg' ), ), 'query_var' => false, 'rewrite' => false, @@ -87,7 +87,7 @@ function gutenberg_register_template_part_type_taxonomy() { ) ); } -add_action( 'init', 'gutenberg_register_template_part_type_taxonomy' ); +add_action( 'init', 'gutenberg_register_template_part_section_taxonomy' ); /** * Fixes the label of the 'wp_template_part' admin menu entry. From 90a26413c25e0324e2e2a87761db0373966aeca1 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 22 Jan 2021 17:35:43 -0500 Subject: [PATCH 06/36] add query support --- lib/full-site-editing/block-templates.php | 51 +++++++++++++------ .../class-wp-rest-templates-controller.php | 3 ++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index c16b10f11ed63..13ad232411abe 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -85,6 +85,7 @@ function _gutenberg_get_template_files( $template_type ) { foreach ( $themes as $theme_slug => $theme_dir ) { $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { + $template_info = array(); $template_base_path = $template_base_paths[ $template_type ]; $template_slug = substr( $template_file, @@ -93,11 +94,26 @@ function _gutenberg_get_template_files( $template_type ) { // Subtract ending '.html'. -5 ); - $template_files[] = array( - 'slug' => $template_slug, - 'path' => $template_file, - 'theme' => $theme_slug, - 'type' => $template_type, + + if ( 'wp_template_part' === $template_type && is_readable( locate_template( 'experimental-theme.json' ) ) ) { + $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); + $data = json_decode( + $theme_json, + true + ); + if ( isset( $data['template-parts'][ $template_slug ]['section'] ) ) { + $template_info['section'] = $data['template-parts'][ $template_slug ]['section']; + } + } + + $template_files[] = array_merge( + $template_info, + array( + 'slug' => $template_slug, + 'path' => $template_file, + 'theme' => $theme_slug, + 'type' => $template_type, + ) ); } } @@ -171,15 +187,8 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template->title = $default_template_types[ $template_file['slug'] ]['title']; } - if ( 'wp_template_part' === $template_type && is_readable( locate_template( 'experimental-theme.json' ) ) ) { - $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); - $data = json_decode( - $theme_json, - true - ); - if ( isset( $data['template-parts'][ $template_file['slug'] ]['section'] ) ) { - $template->section = $data['template-parts'][ $template_file['slug'] ]['section']; - } + if ( 'wp_template_part' === $template_type && $template_file['section'] ) { + $template->section = $template_file['section']; } return $template; @@ -254,6 +263,15 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t ), ); + if ( 'wp_template_part' === $template_type && isset( $query['section'] ) ) { + $wp_query_args['tax_query'][] = array( + 'taxonomy' => 'section', + 'field' => 'name', + 'terms' => $query['section'], + ); + $wp_query_args['tax_query']['relation'] = 'AND'; + } + if ( isset( $query['slug__in'] ) ) { $wp_query_args['post_name__in'] = $query['slug__in']; } @@ -284,8 +302,9 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t true ); $should_include = false === $is_custom && ( - ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ) - ); + ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ) ) && ( + ! isset( $query['section'] ) || $template_file['section'] === $query['section'] + ); if ( $should_include ) { $query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); } diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index d1f4a4ea4f77a..5cffeae1758fc 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -138,6 +138,9 @@ public function get_items( $request ) { if ( isset( $request['wp_id'] ) ) { $query['wp_id'] = $request['wp_id']; } + if ( isset( $request['section'] ) ) { + $query['section'] = $request['section']; + } $templates = array(); foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { $data = $this->prepare_item_for_response( $template, $request ); From 3a5534622b071bb3daf7ffac20d99a3f9dfd3cf0 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 22 Jan 2021 18:32:54 -0500 Subject: [PATCH 07/36] refactor json attribute injection, use for both plural and singular file getters --- lib/full-site-editing/block-templates.php | 49 ++++++++++++++--------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 13ad232411abe..d682e71ed005e 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -49,11 +49,13 @@ function _gutenberg_get_template_file( $template_type, $slug ) { foreach ( $themes as $theme_slug => $theme_dir ) { $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { - return array( - 'slug' => $slug, - 'path' => $file_path, - 'theme' => $theme_slug, - 'type' => $template_type, + return _gutenberg_add_template_part_section_info( + array( + 'slug' => $slug, + 'path' => $file_path, + 'theme' => $theme_slug, + 'type' => $template_type, + ) ); } } @@ -85,7 +87,6 @@ function _gutenberg_get_template_files( $template_type ) { foreach ( $themes as $theme_slug => $theme_dir ) { $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { - $template_info = array(); $template_base_path = $template_base_paths[ $template_type ]; $template_slug = substr( $template_file, @@ -95,19 +96,7 @@ function _gutenberg_get_template_files( $template_type ) { -5 ); - if ( 'wp_template_part' === $template_type && is_readable( locate_template( 'experimental-theme.json' ) ) ) { - $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); - $data = json_decode( - $theme_json, - true - ); - if ( isset( $data['template-parts'][ $template_slug ]['section'] ) ) { - $template_info['section'] = $data['template-parts'][ $template_slug ]['section']; - } - } - - $template_files[] = array_merge( - $template_info, + $template_files[] = _gutenberg_add_template_part_section_info( array( 'slug' => $template_slug, 'path' => $template_file, @@ -121,6 +110,28 @@ function _gutenberg_get_template_files( $template_type ) { return $template_files; } +/** + * Attempts to add the template part's section information from the + * experimental-theme.json file to the input array. + * + * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). + * + * @return array Template. + */ +function _gutenberg_add_template_part_section_info( $template_info ) { + if ( 'wp_template_part' === $template_info['type'] && is_readable( locate_template( 'experimental-theme.json' ) ) ) { + $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); + $data = json_decode( + $theme_json, + true + ); + if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) ) { + $template_info['section'] = $data['template-parts'][ $template_info['slug'] ]['section']; + } + } + return $template_info; +} + /** * Parses wp_template content and injects the current theme's * stylesheet as a theme attribute into each wp_template_part From 34f7bd9021c555abb68514875ac12e9cb90a1b3d Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 22 Jan 2021 19:29:18 -0500 Subject: [PATCH 08/36] only allow for supported section types --- lib/full-site-editing/block-templates.php | 3 ++- .../class-wp-rest-templates-controller.php | 2 +- lib/full-site-editing/template-parts.php | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index d682e71ed005e..74c5792f5087b 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -125,7 +125,8 @@ function _gutenberg_add_template_part_section_info( $template_info ) { $theme_json, true ); - if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) ) { + if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) && in_array( $data['template-parts'][ $template_info['slug'] ]['section'], gutenberg_get_allowed_template_part_section_types(), true ) ) { + $template_info['section'] = $data['template-parts'][ $template_info['slug'] ]['section']; } } diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 5cffeae1758fc..a808aed8d8a28 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -362,7 +362,7 @@ protected function prepare_item_for_database( $request ) { } if ( 'wp_template_part' === $this->post_type ) { - if ( isset( $request['section'] ) ) { + if ( isset( $request['section'] ) && in_array( $request['section'], gutenberg_get_allowed_template_part_section_types(), true ) ) { $changes->tax_input['section'] = $request['section']; } elseif ( null !== $template && ! $template->is_custom ) { $changes->tax_input['section'] = $template->section; diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index ade5e7d9dd125..9b68d02d468a8 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -147,3 +147,22 @@ function set_unique_slug_on_create_template_part( $post_id ) { } } add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' ); + +/** + * Returns a filtered list of allowed section types for template parts. + * + * @return array The supported template part section types. + */ +function gutenberg_get_allowed_template_part_section_types() { + $default_section_types = array( + 'header', + 'footer', + ); + + /** + * Filters the list of allowed template part section types. + * + * @param array $default_section_types An array of supported section types. + */ + return apply_filters( 'default_template_part_section_types', $default_section_types ); +} From dd2e647cdef1dc17a2b19d8785016fb4fdd67b10 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 22 Jan 2021 19:42:54 -0500 Subject: [PATCH 09/36] remove unnecessary newlines --- lib/full-site-editing/block-templates.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 74c5792f5087b..33f19ecf8c64a 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -95,7 +95,6 @@ function _gutenberg_get_template_files( $template_type ) { // Subtract ending '.html'. -5 ); - $template_files[] = _gutenberg_add_template_part_section_info( array( 'slug' => $template_slug, @@ -126,7 +125,6 @@ function _gutenberg_add_template_part_section_info( $template_info ) { true ); if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) && in_array( $data['template-parts'][ $template_info['slug'] ]['section'], gutenberg_get_allowed_template_part_section_types(), true ) ) { - $template_info['section'] = $data['template-parts'][ $template_info['slug'] ]['section']; } } From f4c25403813a39d12d17574f5906a366f5d15155 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 25 Jan 2021 16:58:15 -0500 Subject: [PATCH 10/36] help function for ensuring supported section type --- lib/full-site-editing/block-templates.php | 4 ++-- .../class-wp-rest-templates-controller.php | 8 ++++---- lib/full-site-editing/template-parts.php | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 33f19ecf8c64a..cb247776f8d24 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -124,8 +124,8 @@ function _gutenberg_add_template_part_section_info( $template_info ) { $theme_json, true ); - if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) && in_array( $data['template-parts'][ $template_info['slug'] ]['section'], gutenberg_get_allowed_template_part_section_types(), true ) ) { - $template_info['section'] = $data['template-parts'][ $template_info['slug'] ]['section']; + if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) ) { + $template_info['section'] = gutenberg_filter_template_part_section_type( $data['template-parts'][ $template_info['slug'] ]['section'] ); } } return $template_info; diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index a808aed8d8a28..2ca4b1ffc974f 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -362,10 +362,10 @@ protected function prepare_item_for_database( $request ) { } if ( 'wp_template_part' === $this->post_type ) { - if ( isset( $request['section'] ) && in_array( $request['section'], gutenberg_get_allowed_template_part_section_types(), true ) ) { - $changes->tax_input['section'] = $request['section']; - } elseif ( null !== $template && ! $template->is_custom ) { - $changes->tax_input['section'] = $template->section; + if ( isset( $request['section'] ) ) { + $changes->tax_input['section'] = gutenberg_filter_template_part_section_type( $request['section'] ); + } elseif ( null !== $template && ! $template->is_custom && $template->section ) { + $changes->tax_input['section'] = gutenberg_filter_template_part_section_type( $template->section ); } } diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 9b68d02d468a8..0c8c5eb7f4f42 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -157,6 +157,7 @@ function gutenberg_get_allowed_template_part_section_types() { $default_section_types = array( 'header', 'footer', + 'other', ); /** @@ -166,3 +167,18 @@ function gutenberg_get_allowed_template_part_section_types() { */ return apply_filters( 'default_template_part_section_types', $default_section_types ); } + +/** + * Checks whether the input 'type' is a supported section type. + * Returns the input if supported, otherwise returns the 'other' type. + * + * @param string $type Template part section name + * + * @return string Input if supported, else 'other'. + */ +function gutenberg_filter_template_part_section_type( $type ) { + if ( in_array( $type, gutenberg_get_allowed_template_part_section_types(), true ) ) { + return $type; + } + return 'other'; +} From 60bb1685c99d0e23be6dd0ac3498d351c2da0e1c Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 26 Jan 2021 14:41:44 -0500 Subject: [PATCH 11/36] fix php lint error --- lib/full-site-editing/template-parts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 0c8c5eb7f4f42..af3d7d0e6703f 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -172,7 +172,7 @@ function gutenberg_get_allowed_template_part_section_types() { * Checks whether the input 'type' is a supported section type. * Returns the input if supported, otherwise returns the 'other' type. * - * @param string $type Template part section name + * @param string $type Template part section name. * * @return string Input if supported, else 'other'. */ From d0823f356876b1fc7e99537c9f742f48b6787648 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 26 Jan 2021 18:49:02 -0500 Subject: [PATCH 12/36] rename function --- lib/full-site-editing/block-templates.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index cb247776f8d24..1db846f45a825 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -49,7 +49,7 @@ function _gutenberg_get_template_file( $template_type, $slug ) { foreach ( $themes as $theme_slug => $theme_dir ) { $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { - return _gutenberg_add_template_part_section_info( + return _gutenberg_conditionally_add_template_part_section_info( array( 'slug' => $slug, 'path' => $file_path, @@ -95,7 +95,7 @@ function _gutenberg_get_template_files( $template_type ) { // Subtract ending '.html'. -5 ); - $template_files[] = _gutenberg_add_template_part_section_info( + $template_files[] = _gutenberg_conditionally_add_template_part_section_info( array( 'slug' => $template_slug, 'path' => $template_file, @@ -117,7 +117,7 @@ function _gutenberg_get_template_files( $template_type ) { * * @return array Template. */ -function _gutenberg_add_template_part_section_info( $template_info ) { +function _gutenberg_conditionally_add_template_part_section_info( $template_info ) { if ( 'wp_template_part' === $template_info['type'] && is_readable( locate_template( 'experimental-theme.json' ) ) ) { $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); $data = json_decode( From 64e3a60bda5a04cd830b246f2b0322372985d2b7 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 17:01:24 -0500 Subject: [PATCH 13/36] only decode json once per request --- lib/full-site-editing/block-templates.php | 89 ++++++++++++++++------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 1db846f45a825..5f967e6b2e43c 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -46,17 +46,30 @@ function _gutenberg_get_template_file( $template_type, $slug ) { get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); + + if ( 'wp_template_part' === $template_type ) { + $template_part_data = _gutenberg_get_template_part_info_from_theme_json(); + } + foreach ( $themes as $theme_slug => $theme_dir ) { $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { - return _gutenberg_conditionally_add_template_part_section_info( - array( - 'slug' => $slug, - 'path' => $file_path, - 'theme' => $theme_slug, - 'type' => $template_type, - ) + + $new_template_item = array( + 'slug' => $slug, + 'path' => $file_path, + 'theme' => $theme_slug, + 'type' => $template_type, ); + + if ( 'wp_template_part' === $template_type ) { + return _gutenberg_conditionally_add_template_part_section_info( + $new_template_item, + $template_part_data + ); + } else { + return $new_template_item; + } } } @@ -84,6 +97,11 @@ function _gutenberg_get_template_files( $template_type ) { ); $template_files = array(); + + if ( 'wp_template_part' === $template_type ) { + $template_part_data = _gutenberg_get_template_part_info_from_theme_json(); + } + foreach ( $themes as $theme_slug => $theme_dir ) { $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { @@ -95,14 +113,21 @@ function _gutenberg_get_template_files( $template_type ) { // Subtract ending '.html'. -5 ); - $template_files[] = _gutenberg_conditionally_add_template_part_section_info( - array( - 'slug' => $template_slug, - 'path' => $template_file, - 'theme' => $theme_slug, - 'type' => $template_type, - ) + + $new_template_item = array( + 'slug' => $template_slug, + 'path' => $template_file, + 'theme' => $theme_slug, + 'type' => $template_type, ); + + if ( 'wp_template_part' === $template_type ) { + $template_files[] = _gutenberg_conditionally_add_template_part_section_info( + $new_template_item, $template_part_data + ); + } else { + $template_files[] = $new_template_item; + } } } @@ -110,23 +135,35 @@ function _gutenberg_get_template_files( $template_type ) { } /** - * Attempts to add the template part's section information from the - * experimental-theme.json file to the input array. + * Attempts to read the theme.json and return the 'template-parts' field. * - * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). - * - * @return array Template. + * @return array Template part data from theme.json or empty array if not found. */ -function _gutenberg_conditionally_add_template_part_section_info( $template_info ) { - if ( 'wp_template_part' === $template_info['type'] && is_readable( locate_template( 'experimental-theme.json' ) ) ) { +function _gutenberg_get_template_part_info_from_theme_json() { + if( is_readable( locate_template( 'experimental-theme.json' ) ) ){ $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); - $data = json_decode( + $data = json_decode( $theme_json, true ); - if ( isset( $data['template-parts'][ $template_info['slug'] ]['section'] ) ) { - $template_info['section'] = gutenberg_filter_template_part_section_type( $data['template-parts'][ $template_info['slug'] ]['section'] ); - } + } + if ( isset( $data['template-parts'] ) ) { + return $data['template-parts']; + } + return array(); +} + +/** + * Attempts to add the template part's section information to the input template. + * + * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). + * @param array $theme_data Template part information from theme.json. + * + * @return array Template. + */ +function _gutenberg_conditionally_add_template_part_section_info( $template_info, $theme_data ) { + if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { + $template_info['section'] = gutenberg_filter_template_part_section_type( $theme_data[ $template_info['slug'] ]['section'] ); } return $template_info; } @@ -197,7 +234,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template->title = $default_template_types[ $template_file['slug'] ]['title']; } - if ( 'wp_template_part' === $template_type && $template_file['section'] ) { + if ( 'wp_template_part' === $template_type && isset( $template_file['section'] ) ) { $template->section = $template_file['section']; } From 503382fb278ab78addb239957ccfcb72d47983ff Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 17:48:39 -0500 Subject: [PATCH 14/36] update 'from post' test --- phpunit/class-block-templates-test.php | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 40c62bf3e6f78..2ee13c17ebcda 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -11,13 +11,16 @@ */ class Block_Templates_Test extends WP_UnitTestCase { private static $post; + private static $template_part_post; public static function wpSetUpBeforeClass() { switch_theme( 'tt1-blocks' ); gutenberg_register_template_post_type(); gutenberg_register_template_part_post_type(); gutenberg_register_wp_theme_taxonomy(); + gutenberg_register_template_part_section_taxonomy(); + // Set up template post. $args = array( 'post_type' => 'wp_template', 'post_name' => 'my_template', @@ -32,6 +35,26 @@ public static function wpSetUpBeforeClass() { ); self::$post = self::factory()->post->create_and_get( $args ); wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' ); + + // Set up template part post. + $template_part_args = array( + 'post_type' => 'wp_template_part', + 'post_name' => 'my_template_part', + 'post_title' => 'My Template Part', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template part', + 'tax_input' => array( + 'wp_theme' => array( + get_stylesheet(), + ), + 'section' => array( + 'header' + ) + ), + ); + self::$template_part_post = self::factory()->post->create_and_get( $template_part_args ); + wp_set_post_terms( self::$template_part_post->ID, 'header', 'section' ); + wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' ); } public static function wpTearDownAfterClass() { @@ -55,6 +78,8 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'Single', $template->title ); $this->assertEquals( 'Used when a single entry that is not a Page is queried', $template->description ); $this->assertEquals( 'wp_template', $template->type ); + + // Test template part. } function test_gutenberg_build_template_result_from_post() { @@ -72,6 +97,22 @@ function test_gutenberg_build_template_result_from_post() { $this->assertEquals( 'My Template', $template->title ); $this->assertEquals( 'Description of my template', $template->description ); $this->assertEquals( 'wp_template', $template->type ); + + // Test template part. + $template_part = _gutenberg_build_template_result_from_post( + self::$template_part_post, + 'wp_template_part' + ); + $this->assertNotWPError( $template_part ); + $this->assertEquals( get_stylesheet() . '//my_template_part', $template_part->id ); + $this->assertEquals( get_stylesheet(), $template_part->theme ); + $this->assertEquals( 'my_template_part', $template_part->slug ); + $this->assertEquals( 'publish', $template_part->status ); + $this->assertEquals( true, $template_part->is_custom ); + $this->assertEquals( 'My Template Part', $template_part->title ); + $this->assertEquals( 'Description of my template part', $template_part->description ); + $this->assertEquals( 'wp_template_part', $template_part->type ); + $this->assertEquals( 'header', $template_part->section ); } function test_inject_theme_attribute_in_content() { From a691b4af9f4227bfb879d74592f998a0901b35eb Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 18:00:50 -0500 Subject: [PATCH 15/36] rename term 'wp_template_section' --- lib/full-site-editing/block-templates.php | 11 ++++++----- .../class-wp-rest-templates-controller.php | 4 ++-- lib/full-site-editing/template-parts.php | 2 +- phpunit/class-block-templates-test.php | 14 ++++++-------- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 5f967e6b2e43c..8752c48963a61 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -123,7 +123,8 @@ function _gutenberg_get_template_files( $template_type ) { if ( 'wp_template_part' === $template_type ) { $template_files[] = _gutenberg_conditionally_add_template_part_section_info( - $new_template_item, $template_part_data + $new_template_item, + $template_part_data ); } else { $template_files[] = $new_template_item; @@ -140,9 +141,9 @@ function _gutenberg_get_template_files( $template_type ) { * @return array Template part data from theme.json or empty array if not found. */ function _gutenberg_get_template_part_info_from_theme_json() { - if( is_readable( locate_template( 'experimental-theme.json' ) ) ){ + if ( is_readable( locate_template( 'experimental-theme.json' ) ) ) { $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); - $data = json_decode( + $data = json_decode( $theme_json, true ); @@ -274,7 +275,7 @@ function _gutenberg_build_template_result_from_post( $post ) { $template->status = $post->post_status; if ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'section' ); + $type_terms = get_the_terms( $post, 'wp_template_section' ); $section = $type_terms[0]->name; $template->section = $section; } @@ -312,7 +313,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t if ( 'wp_template_part' === $template_type && isset( $query['section'] ) ) { $wp_query_args['tax_query'][] = array( - 'taxonomy' => 'section', + 'taxonomy' => 'wp_template_section', 'field' => 'name', 'terms' => $query['section'], ); diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 2ca4b1ffc974f..5cc97574b7dfa 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -363,9 +363,9 @@ protected function prepare_item_for_database( $request ) { if ( 'wp_template_part' === $this->post_type ) { if ( isset( $request['section'] ) ) { - $changes->tax_input['section'] = gutenberg_filter_template_part_section_type( $request['section'] ); + $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $request['section'] ); } elseif ( null !== $template && ! $template->is_custom && $template->section ) { - $changes->tax_input['section'] = gutenberg_filter_template_part_section_type( $template->section ); + $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $template->section ); } } diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index af3d7d0e6703f..0551386e1b2ab 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -69,7 +69,7 @@ function gutenberg_register_template_part_section_taxonomy() { } register_taxonomy( - 'section', + 'wp_template_section', array( 'wp_template_part' ), array( 'public' => false, diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 2ee13c17ebcda..e59eee57b8380 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -37,23 +37,23 @@ public static function wpSetUpBeforeClass() { wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' ); // Set up template part post. - $template_part_args = array( + $template_part_args = array( 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', 'post_content' => 'Content', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( - 'wp_theme' => array( + 'wp_theme' => array( get_stylesheet(), ), - 'section' => array( - 'header' - ) + 'wp_template_section' => array( + 'header', + ), ), ); self::$template_part_post = self::factory()->post->create_and_get( $template_part_args ); - wp_set_post_terms( self::$template_part_post->ID, 'header', 'section' ); + wp_set_post_terms( self::$template_part_post->ID, 'header', 'wp_template_section' ); wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' ); } @@ -78,8 +78,6 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'Single', $template->title ); $this->assertEquals( 'Used when a single entry that is not a Page is queried', $template->description ); $this->assertEquals( 'wp_template', $template->type ); - - // Test template part. } function test_gutenberg_build_template_result_from_post() { From 07a862c5a95936d1603f38fa23b81f0622251c28 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 18:32:22 -0500 Subject: [PATCH 16/36] update rest tests for template parts --- ...class-wp-rest-template-controller-test.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-wp-rest-template-controller-test.php index f7d8ffd182c74..a83b000449d65 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-wp-rest-template-controller-test.php @@ -76,6 +76,30 @@ function find_and_normalize_template_by_id( $templates, $id ) { ), find_and_normalize_template_by_id( $data, 'tt1-blocks//index' ) ); + + // Test template parts. + $request = new WP_REST_Request( 'GET', '/wp/v2/template-parts' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( + array( + 'id' => 'tt1-blocks//header', + 'theme' => 'tt1-blocks', + 'slug' => 'header', + 'title' => array( + 'raw' => 'header', + 'rendered' => 'header', + ), + 'description' => '', + 'status' => 'publish', + 'is_custom' => false, + 'type' => 'wp_template_part', + 'wp_id' => null, + 'section' => null, + ), + find_and_normalize_template_by_id( $data, 'tt1-blocks//header' ) + ); } public function test_get_item() { @@ -103,6 +127,31 @@ public function test_get_item() { ), $data ); + + // Test template parts. + $request = new WP_REST_Request( 'GET', '/wp/v2/template-parts/tt1-blocks//header' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset( $data['content'] ); + unset( $data['_links'] ); + $this->assertEquals( + array( + 'id' => 'tt1-blocks//header', + 'theme' => 'tt1-blocks', + 'slug' => 'header', + 'title' => array( + 'raw' => 'header', + 'rendered' => 'header', + ), + 'description' => '', + 'status' => 'publish', + 'is_custom' => false, + 'type' => 'wp_template_part', + 'wp_id' => null, + 'section' => null, + ), + $data + ); } public function test_create_item() { @@ -140,6 +189,43 @@ public function test_create_item() { ), $data ); + + // Test template parts. + $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' ); + $request->set_body_params( + array( + 'slug' => 'my_custom_template_part', + 'title' => 'My Template Part', + 'description' => 'Just a description of a template part', + 'content' => 'Content', + 'section' => 'header', + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + unset( $data['_links'] ); + unset( $data['wp_id'] ); + + $this->assertEquals( + array( + 'id' => 'tt1-blocks//my_custom_template_part', + 'theme' => 'tt1-blocks', + 'slug' => 'my_custom_template_part', + 'title' => array( + 'raw' => 'My Template Part', + 'rendered' => 'My Template Part', + ), + 'description' => 'Just a description of a template part', + 'status' => 'publish', + 'is_custom' => true, + 'type' => 'wp_template_part', + 'content' => array( + 'raw' => 'Content', + ), + 'section' => 'header', + ), + $data + ); } public function test_update_item() { @@ -154,6 +240,18 @@ public function test_update_item() { $data = $response->get_data(); $this->assertEquals( 'My new Index Title', $data['title']['raw'] ); $this->assertEquals( true, $data['is_custom'] ); + + // Test template parts. + $request = new WP_REST_Request( 'PUT', '/wp/v2/template-parts/tt1-blocks//header' ); + $request->set_body_params( + array( + 'section' => 'something unsupported', + ) + ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 'other', $data['section'] ); + $this->assertEquals( true, $data['is_custom'] ); } public function test_delete_item() { From 744a1aa2abd4ac6246f369e904c923adac178062 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 18:48:28 -0500 Subject: [PATCH 17/36] update block templates tests --- phpunit/class-block-templates-test.php | 50 +++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index e59eee57b8380..bdb10cd1ef439 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -78,6 +78,25 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'Single', $template->title ); $this->assertEquals( 'Used when a single entry that is not a Page is queried', $template->description ); $this->assertEquals( 'wp_template', $template->type ); + + // Test template parts. + $template_part = _gutenberg_build_template_result_from_file( + array( + 'slug' => 'header', + 'path' => __DIR__ . '/fixtures/template.html', + ), + 'wp_template_part' + ); + $this->assertEquals( get_stylesheet() . '//header', $template_part->id ); + $this->assertEquals( get_stylesheet(), $template_part->theme ); + $this->assertEquals( 'header', $template_part->slug ); + $this->assertEquals( 'publish', $template_part->status ); + $this->assertEquals( false, $template_part->is_custom ); + $this->assertEquals( 'header', $template_part->title ); + $this->assertEquals( '', $template_part->description ); + $this->assertEquals( 'wp_template_part', $template_part->type ); + // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. + $this->assertEquals( null, $template_part->section ); } function test_gutenberg_build_template_result_from_post() { @@ -96,7 +115,7 @@ function test_gutenberg_build_template_result_from_post() { $this->assertEquals( 'Description of my template', $template->description ); $this->assertEquals( 'wp_template', $template->type ); - // Test template part. + // Test template parts. $template_part = _gutenberg_build_template_result_from_post( self::$template_part_post, 'wp_template_part' @@ -155,6 +174,18 @@ function test_gutenberg_get_block_template_from_file() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( false, $template->is_custom ); $this->assertEquals( 'wp_template', $template->type ); + + // Test template parts. + $id = get_stylesheet() . '//' . 'header'; + $template = gutenberg_get_block_template( $id, 'wp_template_part' ); + $this->assertEquals( $id, $template->id ); + $this->assertEquals( get_stylesheet(), $template->theme ); + $this->assertEquals( 'header', $template->slug ); + $this->assertEquals( 'publish', $template->status ); + $this->assertEquals( false, $template->is_custom ); + $this->assertEquals( 'wp_template_part', $template->type ); + // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. + $this->assertEquals( null, $template->section ); } /** @@ -169,6 +200,17 @@ function test_gutenberg_get_block_template_from_post() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( true, $template->is_custom ); $this->assertEquals( 'wp_template', $template->type ); + + // Test template parts. + $id = get_stylesheet() . '//' . 'my_template_part'; + $template = gutenberg_get_block_template( $id, 'wp_template_part' ); + $this->assertEquals( $id, $template->id ); + $this->assertEquals( get_stylesheet(), $template->theme ); + $this->assertEquals( 'my_template_part', $template->slug ); + $this->assertEquals( 'publish', $template->status ); + $this->assertEquals( true, $template->is_custom ); + $this->assertEquals( 'wp_template_part', $template->type ); + $this->assertEquals( 'header', $template->section ); } /** @@ -201,5 +243,11 @@ function( $template ) { $templates = gutenberg_get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' ); $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); + + // Filter template part by section. + $templates = gutenberg_get_block_templates( array( 'section' => 'header' ), 'wp_template_part' ); + $template_ids = get_template_ids( $templates ); + // TODO - update following array result once tt1-blocks theme.json is updated for section info. + $this->assertEquals( array( get_stylesheet() . '//' . 'my_template_part' ), $template_ids ); } } From 524abc049e92cfd6ef680aeec9a6c158dc601c8a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 1 Feb 2021 18:50:53 -0500 Subject: [PATCH 18/36] add TODOs to follow up on after updating themes --- phpunit/class-wp-rest-template-controller-test.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-wp-rest-template-controller-test.php index a83b000449d65..81cffcab3422c 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-wp-rest-template-controller-test.php @@ -96,6 +96,7 @@ function find_and_normalize_template_by_id( $templates, $id ) { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, + // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. 'section' => null, ), find_and_normalize_template_by_id( $data, 'tt1-blocks//header' ) @@ -148,6 +149,7 @@ public function test_get_item() { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, + // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. 'section' => null, ), $data From 6d21411d8a0868cda4c6cef60a98a820b8317254 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 2 Feb 2021 14:54:37 -0500 Subject: [PATCH 19/36] rename register function --- lib/full-site-editing/template-parts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 0551386e1b2ab..f71f198d1669c 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -61,9 +61,9 @@ function gutenberg_register_template_part_post_type() { add_action( 'init', 'gutenberg_register_template_part_post_type' ); /** - * Registers the 'template_part_type' taxonomy. + * Registers the 'wp_template_section' taxonomy. */ -function gutenberg_register_template_part_section_taxonomy() { +function gutenberg_register_wp_template_section_taxonomy() { if ( ! gutenberg_is_fse_theme() ) { return; } @@ -87,7 +87,7 @@ function gutenberg_register_template_part_section_taxonomy() { ) ); } -add_action( 'init', 'gutenberg_register_template_part_section_taxonomy' ); +add_action( 'init', 'gutenberg_register_wp_template_section_taxonomy' ); /** * Fixes the label of the 'wp_template_part' admin menu entry. From 7dc515aff3def491cdd3d0397b364e2d76535f20 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 2 Feb 2021 14:56:33 -0500 Subject: [PATCH 20/36] remove unnecessary else --- lib/full-site-editing/block-templates.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 8752c48963a61..bea94b5e8ad83 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -67,9 +67,8 @@ function _gutenberg_get_template_file( $template_type, $slug ) { $new_template_item, $template_part_data ); - } else { - return $new_template_item; } + return $new_template_item; } } From bd35f4c85384cdd1b4fb0016257864a4d3735cb0 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 2 Feb 2021 15:08:08 -0500 Subject: [PATCH 21/36] fix renamed function in tests --- phpunit/class-block-templates-test.php | 2 +- phpunit/class-wp-rest-template-controller-test.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index bdb10cd1ef439..b8dff6767ae23 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -18,7 +18,7 @@ public static function wpSetUpBeforeClass() { gutenberg_register_template_post_type(); gutenberg_register_template_part_post_type(); gutenberg_register_wp_theme_taxonomy(); - gutenberg_register_template_part_section_taxonomy(); + gutenberg_register_wp_template_section_taxonomy(); // Set up template post. $args = array( diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-wp-rest-template-controller-test.php index 81cffcab3422c..ee92065d304e0 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-wp-rest-template-controller-test.php @@ -16,6 +16,7 @@ public static function wpSetupBeforeClass( $factory ) { gutenberg_register_template_post_type(); gutenberg_register_template_part_post_type(); gutenberg_register_wp_theme_taxonomy(); + gutenberg_register_wp_template_section_taxonomy(); self::$admin_id = $factory->user->create( array( 'role' => 'administrator', From 8ab0d8842b5ae8e62146c5fbfc67f81af261c407 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 2 Feb 2021 15:11:06 -0500 Subject: [PATCH 22/36] make should_include logic more readable --- lib/full-site-editing/block-templates.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index bea94b5e8ad83..44dcef88c18d4 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -343,15 +343,16 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t if ( ! isset( $query['wp_id'] ) ) { $template_files = _gutenberg_get_template_files( $template_type ); foreach ( $template_files as $template_file ) { - $is_custom = array_search( + $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], array_column( $query_result, 'id' ), true ); - $should_include = false === $is_custom && ( - ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ) ) && ( - ! isset( $query['section'] ) || $template_file['section'] === $query['section'] - ); + $fits_slug_query = + ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); + $fits_section_query = + ! isset( $query['section'] ) || $template_file['section'] === $query['section']; + $should_include = $is_not_custom && $fits_slug_query && $fits_section_query; if ( $should_include ) { $query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); } From 3125a400aaaff66b0f5a598095e4284d9f18de56 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 2 Feb 2021 19:48:05 -0500 Subject: [PATCH 23/36] update format, function name, and check terms error --- lib/full-site-editing/block-templates.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 44dcef88c18d4..db87f2f583679 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -63,7 +63,7 @@ function _gutenberg_get_template_file( $template_type, $slug ) { ); if ( 'wp_template_part' === $template_type ) { - return _gutenberg_conditionally_add_template_part_section_info( + return _gutenberg_add_template_part_section_info( $new_template_item, $template_part_data ); @@ -121,7 +121,7 @@ function _gutenberg_get_template_files( $template_type ) { ); if ( 'wp_template_part' === $template_type ) { - $template_files[] = _gutenberg_conditionally_add_template_part_section_info( + $template_files[] = _gutenberg_add_template_part_section_info( $new_template_item, $template_part_data ); @@ -150,6 +150,7 @@ function _gutenberg_get_template_part_info_from_theme_json() { if ( isset( $data['template-parts'] ) ) { return $data['template-parts']; } + return array(); } @@ -161,7 +162,7 @@ function _gutenberg_get_template_part_info_from_theme_json() { * * @return array Template. */ -function _gutenberg_conditionally_add_template_part_section_info( $template_info, $theme_data ) { +function _gutenberg_add_template_part_section_info( $template_info, $theme_data ) { if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { $template_info['section'] = gutenberg_filter_template_part_section_type( $theme_data[ $template_info['slug'] ]['section'] ); } @@ -274,9 +275,10 @@ function _gutenberg_build_template_result_from_post( $post ) { $template->status = $post->post_status; if ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'wp_template_section' ); - $section = $type_terms[0]->name; - $template->section = $section; + $type_terms = get_the_terms( $post, 'wp_template_section' ); + if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { + $template->section = $type_terms[0]->name; + } } return $template; From ff5e4caf122a3ee5f7a7482fd1aa6727a09a372f Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 3 Feb 2021 17:50:43 -0500 Subject: [PATCH 24/36] define constants --- lib/full-site-editing/template-parts.php | 25 +++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index f71f198d1669c..96afe1d6ad74f 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -89,6 +89,20 @@ function gutenberg_register_wp_template_section_taxonomy() { } add_action( 'init', 'gutenberg_register_wp_template_section_taxonomy' ); +// Definte constants for supported WP_TEMPLATE_SECTION taxonomy. +if ( ! defined( 'WP_TEMPLATE_SECTION_HEADER' ) ) { + define( 'WP_TEMPLATE_SECTION_HEADER', 'header' ); +} +if ( ! defined( 'WP_TEMPLATE_SECTION_FOOTER' ) ) { + define( 'WP_TEMPLATE_SECTION_FOOTER', 'footer' ); +} +if ( ! defined( 'WP_TEMPLATE_SECTION_SIDEBAR' ) ) { + define( 'WP_TEMPLATE_SECTION_SIDEBAR', 'sidebar' ); +} +if ( ! defined( 'WP_TEMPLATE_SECTION_OTHER' ) ) { + define( 'WP_TEMPLATE_SECTION_OTHER', 'other' ); +} + /** * Fixes the label of the 'wp_template_part' admin menu entry. */ @@ -155,9 +169,10 @@ function set_unique_slug_on_create_template_part( $post_id ) { */ function gutenberg_get_allowed_template_part_section_types() { $default_section_types = array( - 'header', - 'footer', - 'other', + WP_TEMPLATE_SECTION_HEADER, + WP_TEMPLATE_SECTION_FOOTER, + WP_TEMPLATE_SECTION_SIDEBAR, + WP_TEMPLATE_SECTION_OTHER, ); /** @@ -165,7 +180,7 @@ function gutenberg_get_allowed_template_part_section_types() { * * @param array $default_section_types An array of supported section types. */ - return apply_filters( 'default_template_part_section_types', $default_section_types ); + return apply_filters( 'default_wp_template_section_types', $default_section_types ); } /** @@ -180,5 +195,5 @@ function gutenberg_filter_template_part_section_type( $type ) { if ( in_array( $type, gutenberg_get_allowed_template_part_section_types(), true ) ) { return $type; } - return 'other'; + return WP_TEMPLATE_SECTION_OTHER; } From cf73932ea22ddda30011445607507b8f183defd0 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 3 Feb 2021 18:20:38 -0500 Subject: [PATCH 25/36] trigger warning when unsupported type is used --- lib/full-site-editing/template-parts.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 96afe1d6ad74f..016457b43f432 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -195,5 +195,9 @@ function gutenberg_filter_template_part_section_type( $type ) { if ( in_array( $type, gutenberg_get_allowed_template_part_section_types(), true ) ) { return $type; } + $warning_message = '"' . $type . '"'; + $warning_message .= __( ' is not a supported wp_template_section type and has been added as ', 'gutenberg' ); + $warning_message .= '"' . WP_TEMPLATE_SECTION_OTHER . '".'; + trigger_error( $warning_message, E_USER_NOTICE ); return WP_TEMPLATE_SECTION_OTHER; } From dfd1bfa0acbbd5c5d9c5ac07aa7fac59149a97da Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 3 Feb 2021 19:30:58 -0500 Subject: [PATCH 26/36] use WP_Theme_JSON_Resolver --- lib/class-wp-theme-json.php | 13 +++++++++++++ lib/full-site-editing/block-templates.php | 16 +++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index a3043bf78d52d..7a8527a259bb5 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -104,6 +104,7 @@ class WP_Theme_JSON { */ const SCHEMA = array( 'customTemplates' => null, + 'templateParts' => null, 'styles' => array( 'border' => array( 'radius' => null, @@ -1064,6 +1065,18 @@ public function get_custom_templates() { } } + /** + * Returns the template part data of current theme. + * + * @return array + */ + public function get_template_part_data() { + if ( ! isset( $this->theme_json['templateParts'] ) ) { + return array(); + } + return $this->theme_json['templateParts']; + } + /** * Returns the stylesheet that results of processing * the theme.json structure this object represents. diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index db87f2f583679..fc2d39cf06520 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -135,22 +135,16 @@ function _gutenberg_get_template_files( $template_type ) { } /** - * Attempts to read the theme.json and return the 'template-parts' field. + * Attempts to get template parts data from theme.json. * * @return array Template part data from theme.json or empty array if not found. */ function _gutenberg_get_template_part_info_from_theme_json() { - if ( is_readable( locate_template( 'experimental-theme.json' ) ) ) { - $theme_json = file_get_contents( locate_template( 'experimental-theme.json' ) ); - $data = json_decode( - $theme_json, - true - ); - } - if ( isset( $data['template-parts'] ) ) { - return $data['template-parts']; + if ( gutenberg_experimental_global_styles_has_theme_json_support() ) { + $resolver = new WP_Theme_JSON_Resolver(); + $template_part_data = $resolver->get_theme_origin()->get_template_part_data(); + return $template_part_data; } - return array(); } From fa668f6e298413cd1561a5fb4d2fc1c87418b9ab Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 3 Feb 2021 19:47:14 -0500 Subject: [PATCH 27/36] just some spacing --- lib/full-site-editing/block-templates.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index fc2d39cf06520..0e5fdda7fb054 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -145,6 +145,7 @@ function _gutenberg_get_template_part_info_from_theme_json() { $template_part_data = $resolver->get_theme_origin()->get_template_part_data(); return $template_part_data; } + return array(); } @@ -160,6 +161,7 @@ function _gutenberg_add_template_part_section_info( $template_info, $theme_data if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { $template_info['section'] = gutenberg_filter_template_part_section_type( $theme_data[ $template_info['slug'] ]['section'] ); } + return $template_info; } From 9e5cee00bf795f0246ead641142f41d1814882d4 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 5 Feb 2021 13:56:42 -0500 Subject: [PATCH 28/36] use new methods after rebase updates --- lib/full-site-editing/block-templates.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 0e5fdda7fb054..3f36a924b650e 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -140,9 +140,8 @@ function _gutenberg_get_template_files( $template_type ) { * @return array Template part data from theme.json or empty array if not found. */ function _gutenberg_get_template_part_info_from_theme_json() { - if ( gutenberg_experimental_global_styles_has_theme_json_support() ) { - $resolver = new WP_Theme_JSON_Resolver(); - $template_part_data = $resolver->get_theme_origin()->get_template_part_data(); + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + $template_part_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_part_data(); return $template_part_data; } From 193c66f908f6a28f14759601bacbfc2933be4efb Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 5 Feb 2021 14:40:57 -0500 Subject: [PATCH 29/36] use 'uncategorized' for both unspecified and unsupported --- lib/full-site-editing/block-templates.php | 41 ++++--------------- .../class-wp-rest-templates-controller.php | 2 + lib/full-site-editing/template-parts.php | 14 +++---- phpunit/class-block-templates-test.php | 22 +++++----- ...class-wp-rest-template-controller-test.php | 10 ++--- 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 3f36a924b650e..e58b569baaabe 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -47,10 +47,6 @@ function _gutenberg_get_template_file( $template_type, $slug ) { get_template() => get_template_directory(), ); - if ( 'wp_template_part' === $template_type ) { - $template_part_data = _gutenberg_get_template_part_info_from_theme_json(); - } - foreach ( $themes as $theme_slug => $theme_dir ) { $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { @@ -63,10 +59,7 @@ function _gutenberg_get_template_file( $template_type, $slug ) { ); if ( 'wp_template_part' === $template_type ) { - return _gutenberg_add_template_part_section_info( - $new_template_item, - $template_part_data - ); + return _gutenberg_add_template_part_section_info( $new_template_item ); } return $new_template_item; } @@ -97,10 +90,6 @@ function _gutenberg_get_template_files( $template_type ) { $template_files = array(); - if ( 'wp_template_part' === $template_type ) { - $template_part_data = _gutenberg_get_template_part_info_from_theme_json(); - } - foreach ( $themes as $theme_slug => $theme_dir ) { $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { @@ -121,10 +110,7 @@ function _gutenberg_get_template_files( $template_type ) { ); if ( 'wp_template_part' === $template_type ) { - $template_files[] = _gutenberg_add_template_part_section_info( - $new_template_item, - $template_part_data - ); + $template_files[] = _gutenberg_add_template_part_section_info( $new_template_item ); } else { $template_files[] = $new_template_item; } @@ -134,31 +120,22 @@ function _gutenberg_get_template_files( $template_type ) { return $template_files; } -/** - * Attempts to get template parts data from theme.json. - * - * @return array Template part data from theme.json or empty array if not found. - */ -function _gutenberg_get_template_part_info_from_theme_json() { - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $template_part_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_part_data(); - return $template_part_data; - } - - return array(); -} - /** * Attempts to add the template part's section information to the input template. * * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). - * @param array $theme_data Template part information from theme.json. * * @return array Template. */ -function _gutenberg_add_template_part_section_info( $template_info, $theme_data ) { +function _gutenberg_add_template_part_section_info( $template_info ) { + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_part_data(); + } + if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { $template_info['section'] = gutenberg_filter_template_part_section_type( $theme_data[ $template_info['slug'] ]['section'] ); + } else { + $template_info['section'] = WP_TEMPLATE_SECTION_UNCATEGORIZED; } return $template_info; diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 5cc97574b7dfa..b46c86b5be4a6 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -366,6 +366,8 @@ protected function prepare_item_for_database( $request ) { $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $request['section'] ); } elseif ( null !== $template && ! $template->is_custom && $template->section ) { $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $template->section ); + } elseif ( ! $template->section ) { + $changes->tax_input['wp_template_section'] = WP_TEMPLATE_SECTION_UNCATEGORIZED; } } diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 016457b43f432..39391b1818784 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -75,8 +75,8 @@ function gutenberg_register_wp_template_section_taxonomy() { 'public' => false, 'hierarchical' => false, 'labels' => array( - 'name' => __( 'Sections', 'gutenberg' ), - 'singular_name' => __( 'Section', 'gutenberg' ), + 'name' => __( 'Template Sections', 'gutenberg' ), + 'singular_name' => __( 'Template Section', 'gutenberg' ), ), 'query_var' => false, 'rewrite' => false, @@ -99,8 +99,8 @@ function gutenberg_register_wp_template_section_taxonomy() { if ( ! defined( 'WP_TEMPLATE_SECTION_SIDEBAR' ) ) { define( 'WP_TEMPLATE_SECTION_SIDEBAR', 'sidebar' ); } -if ( ! defined( 'WP_TEMPLATE_SECTION_OTHER' ) ) { - define( 'WP_TEMPLATE_SECTION_OTHER', 'other' ); +if ( ! defined( 'WP_TEMPLATE_SECTION_UNCATEGORIZED' ) ) { + define( 'WP_TEMPLATE_SECTION_UNCATEGORIZED', 'uncategorized' ); } /** @@ -172,7 +172,7 @@ function gutenberg_get_allowed_template_part_section_types() { WP_TEMPLATE_SECTION_HEADER, WP_TEMPLATE_SECTION_FOOTER, WP_TEMPLATE_SECTION_SIDEBAR, - WP_TEMPLATE_SECTION_OTHER, + WP_TEMPLATE_SECTION_UNCATEGORIZED, ); /** @@ -197,7 +197,7 @@ function gutenberg_filter_template_part_section_type( $type ) { } $warning_message = '"' . $type . '"'; $warning_message .= __( ' is not a supported wp_template_section type and has been added as ', 'gutenberg' ); - $warning_message .= '"' . WP_TEMPLATE_SECTION_OTHER . '".'; + $warning_message .= '"' . WP_TEMPLATE_SECTION_UNCATEGORIZED . '".'; trigger_error( $warning_message, E_USER_NOTICE ); - return WP_TEMPLATE_SECTION_OTHER; + return WP_TEMPLATE_SECTION_UNCATEGORIZED; } diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index b8dff6767ae23..62ba9a963674c 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -48,12 +48,12 @@ public static function wpSetUpBeforeClass() { get_stylesheet(), ), 'wp_template_section' => array( - 'header', + WP_TEMPLATE_SECTION_SIDEBAR, ), ), ); self::$template_part_post = self::factory()->post->create_and_get( $template_part_args ); - wp_set_post_terms( self::$template_part_post->ID, 'header', 'wp_template_section' ); + wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_SECTION_SIDEBAR, 'wp_template_section' ); wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' ); } @@ -82,8 +82,9 @@ function test_gutenberg_build_template_result_from_file() { // Test template parts. $template_part = _gutenberg_build_template_result_from_file( array( - 'slug' => 'header', - 'path' => __DIR__ . '/fixtures/template.html', + 'slug' => 'header', + 'path' => __DIR__ . '/fixtures/template.html', + 'section' => WP_TEMPLATE_SECTION_HEADER, ), 'wp_template_part' ); @@ -95,8 +96,7 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'header', $template_part->title ); $this->assertEquals( '', $template_part->description ); $this->assertEquals( 'wp_template_part', $template_part->type ); - // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. - $this->assertEquals( null, $template_part->section ); + $this->assertEquals( WP_TEMPLATE_SECTION_HEADER, $template_part->section ); } function test_gutenberg_build_template_result_from_post() { @@ -129,7 +129,7 @@ function test_gutenberg_build_template_result_from_post() { $this->assertEquals( 'My Template Part', $template_part->title ); $this->assertEquals( 'Description of my template part', $template_part->description ); $this->assertEquals( 'wp_template_part', $template_part->type ); - $this->assertEquals( 'header', $template_part->section ); + $this->assertEquals( WP_TEMPLATE_SECTION_SIDEBAR, $template_part->section ); } function test_inject_theme_attribute_in_content() { @@ -184,8 +184,8 @@ function test_gutenberg_get_block_template_from_file() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( false, $template->is_custom ); $this->assertEquals( 'wp_template_part', $template->type ); - // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. - $this->assertEquals( null, $template->section ); + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. + $this->assertEquals( WP_TEMPLATE_SECTION_UNCATEGORIZED, $template->section ); } /** @@ -210,7 +210,7 @@ function test_gutenberg_get_block_template_from_post() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( true, $template->is_custom ); $this->assertEquals( 'wp_template_part', $template->type ); - $this->assertEquals( 'header', $template->section ); + $this->assertEquals( WP_TEMPLATE_SECTION_SIDEBAR, $template->section ); } /** @@ -245,7 +245,7 @@ function( $template ) { $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); // Filter template part by section. - $templates = gutenberg_get_block_templates( array( 'section' => 'header' ), 'wp_template_part' ); + $templates = gutenberg_get_block_templates( array( 'section' => WP_TEMPLATE_SECTION_SIDEBAR ), 'wp_template_part' ); $template_ids = get_template_ids( $templates ); // TODO - update following array result once tt1-blocks theme.json is updated for section info. $this->assertEquals( array( get_stylesheet() . '//' . 'my_template_part' ), $template_ids ); diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-wp-rest-template-controller-test.php index ee92065d304e0..235bc2b1303a6 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-wp-rest-template-controller-test.php @@ -97,8 +97,8 @@ function find_and_normalize_template_by_id( $templates, $id ) { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, - // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. - 'section' => null, + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. + 'section' => WP_TEMPLATE_SECTION_UNCATEGORIZED, ), find_and_normalize_template_by_id( $data, 'tt1-blocks//header' ) ); @@ -150,8 +150,8 @@ public function test_get_item() { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, - // TODO - update null to 'header' once tt1-blocks theme.json updated for template part section info. - 'section' => null, + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. + 'section' => WP_TEMPLATE_SECTION_UNCATEGORIZED, ), $data ); @@ -253,7 +253,7 @@ public function test_update_item() { ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( 'other', $data['section'] ); + $this->assertEquals( WP_TEMPLATE_SECTION_UNCATEGORIZED, $data['section'] ); $this->assertEquals( true, $data['is_custom'] ); } From 89553a8b29189d3adcabd55b9356e7cea3bf6dc7 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 5 Feb 2021 14:53:27 -0500 Subject: [PATCH 30/36] remove unnecessarily added newlines --- lib/full-site-editing/block-templates.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index e58b569baaabe..0b9eb1d712393 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -46,11 +46,9 @@ function _gutenberg_get_template_file( $template_type, $slug ) { get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); - foreach ( $themes as $theme_slug => $theme_dir ) { $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { - $new_template_item = array( 'slug' => $slug, 'path' => $file_path, @@ -89,7 +87,6 @@ function _gutenberg_get_template_files( $template_type ) { ); $template_files = array(); - foreach ( $themes as $theme_slug => $theme_dir ) { $theme_template_files = _gutenberg_get_template_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { @@ -101,7 +98,6 @@ function _gutenberg_get_template_files( $template_type ) { // Subtract ending '.html'. -5 ); - $new_template_item = array( 'slug' => $template_slug, 'path' => $template_file, From be7856c8958d99a528a03f3b2907a230c521b65b Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 5 Feb 2021 15:15:09 -0500 Subject: [PATCH 31/36] add test for new theme json getter --- phpunit/class-wp-theme-json-test.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 13dd9c41930f9..c8fd31e2218c3 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -760,4 +760,27 @@ function test_get_custom_templates() { ) ); } + + function test_get_template_part_data() { + $theme_json = new WP_Theme_JSON( + array( + 'templateParts' => array( + 'header' => array( + 'section' => 'Some section', + ), + ), + ) + ); + + $template_parts = $theme_json->get_template_part_data(); + + $this->assertEqualSetsWithIndex( + $template_parts, + array( + 'header' => array( + 'section' => 'Some section', + ), + ) + ); + } } From 9691842acd5751d02e53fdcbf4d77a0f880a5323 Mon Sep 17 00:00:00 2001 From: Addison Stavlo Date: Mon, 8 Feb 2021 10:54:13 -0500 Subject: [PATCH 32/36] Update lib/class-wp-theme-json.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André --- lib/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index 7a8527a259bb5..55207a9b6496a 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -1070,7 +1070,7 @@ public function get_custom_templates() { * * @return array */ - public function get_template_part_data() { + public function get_template_parts() { if ( ! isset( $this->theme_json['templateParts'] ) ) { return array(); } From 34d47243bc8a3ac8a0deea398fa26c225078a9e8 Mon Sep 17 00:00:00 2001 From: Addison Stavlo Date: Mon, 8 Feb 2021 10:54:23 -0500 Subject: [PATCH 33/36] Update phpunit/class-wp-theme-json-test.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André --- phpunit/class-wp-theme-json-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index c8fd31e2218c3..bba709c51477d 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -761,7 +761,7 @@ function test_get_custom_templates() { ); } - function test_get_template_part_data() { + function test_get_template_parts { $theme_json = new WP_Theme_JSON( array( 'templateParts' => array( From 9db03dec803e7dd3f72ef6c45f0ab351acb5e6ac Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 8 Feb 2021 11:00:46 -0500 Subject: [PATCH 34/36] update method name calls --- lib/full-site-editing/block-templates.php | 2 +- phpunit/class-wp-theme-json-test.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 0b9eb1d712393..bff32e0e67440 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -125,7 +125,7 @@ function _gutenberg_get_template_files( $template_type ) { */ function _gutenberg_add_template_part_section_info( $template_info ) { if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_part_data(); + $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_parts(); } if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index bba709c51477d..87477458bc25e 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -761,7 +761,7 @@ function test_get_custom_templates() { ); } - function test_get_template_parts { + function test_get_template_parts() { $theme_json = new WP_Theme_JSON( array( 'templateParts' => array( @@ -772,7 +772,7 @@ function test_get_template_parts { ) ); - $template_parts = $theme_json->get_template_part_data(); + $template_parts = $theme_json->get_template_parts(); $this->assertEqualSetsWithIndex( $template_parts, From a9758109bc811af10e029830a39d8614b7597d59 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 9 Feb 2021 11:21:52 -0500 Subject: [PATCH 35/36] rename section to area --- lib/full-site-editing/block-templates.php | 38 +++++------ .../class-wp-rest-templates-controller.php | 20 +++--- lib/full-site-editing/template-parts.php | 66 +++++++++---------- phpunit/class-block-templates-test.php | 32 ++++----- ...class-wp-rest-template-controller-test.php | 18 ++--- phpunit/class-wp-theme-json-test.php | 6 +- 6 files changed, 90 insertions(+), 90 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index bff32e0e67440..62bc8f818f2b5 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -57,7 +57,7 @@ function _gutenberg_get_template_file( $template_type, $slug ) { ); if ( 'wp_template_part' === $template_type ) { - return _gutenberg_add_template_part_section_info( $new_template_item ); + return _gutenberg_add_template_part_area_info( $new_template_item ); } return $new_template_item; } @@ -106,7 +106,7 @@ function _gutenberg_get_template_files( $template_type ) { ); if ( 'wp_template_part' === $template_type ) { - $template_files[] = _gutenberg_add_template_part_section_info( $new_template_item ); + $template_files[] = _gutenberg_add_template_part_area_info( $new_template_item ); } else { $template_files[] = $new_template_item; } @@ -117,21 +117,21 @@ function _gutenberg_get_template_files( $template_type ) { } /** - * Attempts to add the template part's section information to the input template. + * Attempts to add the template part's area information to the input template. * * @param array $template_info Template to add information to (requires 'type' and 'slug' fields). * * @return array Template. */ -function _gutenberg_add_template_part_section_info( $template_info ) { +function _gutenberg_add_template_part_area_info( $template_info ) { if ( WP_Theme_JSON_Resolver::theme_has_support() ) { $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_parts(); } - if ( isset( $theme_data[ $template_info['slug'] ]['section'] ) ) { - $template_info['section'] = gutenberg_filter_template_part_section_type( $theme_data[ $template_info['slug'] ]['section'] ); + if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { + $template_info['area'] = gutenberg_filter_template_part_area_type( $theme_data[ $template_info['slug'] ]['area'] ); } else { - $template_info['section'] = WP_TEMPLATE_SECTION_UNCATEGORIZED; + $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } return $template_info; @@ -203,8 +203,8 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t $template->title = $default_template_types[ $template_file['slug'] ]['title']; } - if ( 'wp_template_part' === $template_type && isset( $template_file['section'] ) ) { - $template->section = $template_file['section']; + if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) { + $template->area = $template_file['area']; } return $template; @@ -243,9 +243,9 @@ function _gutenberg_build_template_result_from_post( $post ) { $template->status = $post->post_status; if ( 'wp_template_part' === $post->post_type ) { - $type_terms = get_the_terms( $post, 'wp_template_section' ); + $type_terms = get_the_terms( $post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $template->section = $type_terms[0]->name; + $template->area = $type_terms[0]->name; } } @@ -280,11 +280,11 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t ), ); - if ( 'wp_template_part' === $template_type && isset( $query['section'] ) ) { + if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { $wp_query_args['tax_query'][] = array( - 'taxonomy' => 'wp_template_section', + 'taxonomy' => 'wp_template_part_area', 'field' => 'name', - 'terms' => $query['section'], + 'terms' => $query['area'], ); $wp_query_args['tax_query']['relation'] = 'AND'; } @@ -313,16 +313,16 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t if ( ! isset( $query['wp_id'] ) ) { $template_files = _gutenberg_get_template_files( $template_type ); foreach ( $template_files as $template_file ) { - $is_not_custom = false === array_search( + $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], array_column( $query_result, 'id' ), true ); - $fits_slug_query = + $fits_slug_query = ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true ); - $fits_section_query = - ! isset( $query['section'] ) || $template_file['section'] === $query['section']; - $should_include = $is_not_custom && $fits_slug_query && $fits_section_query; + $fits_area_query = + ! isset( $query['area'] ) || $template_file['area'] === $query['area']; + $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; if ( $should_include ) { $query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); } diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index b46c86b5be4a6..119514dbc8c4a 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -138,8 +138,8 @@ public function get_items( $request ) { if ( isset( $request['wp_id'] ) ) { $query['wp_id'] = $request['wp_id']; } - if ( isset( $request['section'] ) ) { - $query['section'] = $request['section']; + if ( isset( $request['area'] ) ) { + $query['area'] = $request['area']; } $templates = array(); foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { @@ -362,12 +362,12 @@ protected function prepare_item_for_database( $request ) { } if ( 'wp_template_part' === $this->post_type ) { - if ( isset( $request['section'] ) ) { - $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $request['section'] ); - } elseif ( null !== $template && ! $template->is_custom && $template->section ) { - $changes->tax_input['wp_template_section'] = gutenberg_filter_template_part_section_type( $template->section ); - } elseif ( ! $template->section ) { - $changes->tax_input['wp_template_section'] = WP_TEMPLATE_SECTION_UNCATEGORIZED; + if ( isset( $request['area'] ) ) { + $changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area_type( $request['area'] ); + } elseif ( null !== $template && ! $template->is_custom && $template->area ) { + $changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area_type( $template->area ); + } elseif ( ! $template->area ) { + $changes->tax_input['wp_template_part_area'] = WP_TEMPLAT_PART_AREA_UNCATEGORIZED; } } @@ -400,7 +400,7 @@ public function prepare_item_for_response( $template, $request ) { // phpcs:igno ); if ( 'wp_template_part' === $template->type ) { - $result['section'] = $template->section; + $result['area'] = $template->area; } $result = $this->add_additional_fields_to_object( $result, $request ); @@ -554,7 +554,7 @@ public function get_item_schema() { ); if ( 'wp_template_part' === $this->post_type ) { - $schema['properties']['section'] = array( + $schema['properties']['area'] = array( 'description' => __( 'Where the template part is intended for use (header, footer, etc.)', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 39391b1818784..46552ba9d3e6c 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -61,22 +61,22 @@ function gutenberg_register_template_part_post_type() { add_action( 'init', 'gutenberg_register_template_part_post_type' ); /** - * Registers the 'wp_template_section' taxonomy. + * Registers the 'wp_template_part_area' taxonomy. */ -function gutenberg_register_wp_template_section_taxonomy() { +function gutenberg_register_wp_template_part_area_taxonomy() { if ( ! gutenberg_is_fse_theme() ) { return; } register_taxonomy( - 'wp_template_section', + 'wp_template_part_area', array( 'wp_template_part' ), array( 'public' => false, 'hierarchical' => false, 'labels' => array( - 'name' => __( 'Template Sections', 'gutenberg' ), - 'singular_name' => __( 'Template Section', 'gutenberg' ), + 'name' => __( 'Template Part Areas', 'gutenberg' ), + 'singular_name' => __( 'Template Part Area', 'gutenberg' ), ), 'query_var' => false, 'rewrite' => false, @@ -87,20 +87,20 @@ function gutenberg_register_wp_template_section_taxonomy() { ) ); } -add_action( 'init', 'gutenberg_register_wp_template_section_taxonomy' ); +add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' ); -// Definte constants for supported WP_TEMPLATE_SECTION taxonomy. -if ( ! defined( 'WP_TEMPLATE_SECTION_HEADER' ) ) { - define( 'WP_TEMPLATE_SECTION_HEADER', 'header' ); +// Definte constants for supported wp_template_part_area taxonomy. +if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) { + define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' ); } -if ( ! defined( 'WP_TEMPLATE_SECTION_FOOTER' ) ) { - define( 'WP_TEMPLATE_SECTION_FOOTER', 'footer' ); +if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) { + define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' ); } -if ( ! defined( 'WP_TEMPLATE_SECTION_SIDEBAR' ) ) { - define( 'WP_TEMPLATE_SECTION_SIDEBAR', 'sidebar' ); +if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) { + define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' ); } -if ( ! defined( 'WP_TEMPLATE_SECTION_UNCATEGORIZED' ) ) { - define( 'WP_TEMPLATE_SECTION_UNCATEGORIZED', 'uncategorized' ); +if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { + define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); } /** @@ -163,41 +163,41 @@ function set_unique_slug_on_create_template_part( $post_id ) { add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' ); /** - * Returns a filtered list of allowed section types for template parts. + * Returns a filtered list of allowed area types for template parts. * - * @return array The supported template part section types. + * @return array The supported template part area types. */ -function gutenberg_get_allowed_template_part_section_types() { - $default_section_types = array( - WP_TEMPLATE_SECTION_HEADER, - WP_TEMPLATE_SECTION_FOOTER, - WP_TEMPLATE_SECTION_SIDEBAR, - WP_TEMPLATE_SECTION_UNCATEGORIZED, +function gutenberg_get_allowed_template_part_area_types() { + $default_area_types = array( + WP_TEMPLATE_PART_AREA_HEADER, + WP_TEMPLATE_PART_AREA_FOOTER, + WP_TEMPLATE_PART_AREA_SIDEBAR, + WP_TEMPLATE_PART_AREA_UNCATEGORIZED, ); /** - * Filters the list of allowed template part section types. + * Filters the list of allowed template part area types. * - * @param array $default_section_types An array of supported section types. + * @param array $default_area_types An array of supported area types. */ - return apply_filters( 'default_wp_template_section_types', $default_section_types ); + return apply_filters( 'default_wp_template_part_area_types', $default_area_types ); } /** - * Checks whether the input 'type' is a supported section type. + * Checks whether the input 'type' is a supported area type. * Returns the input if supported, otherwise returns the 'other' type. * - * @param string $type Template part section name. + * @param string $type Template part area name. * * @return string Input if supported, else 'other'. */ -function gutenberg_filter_template_part_section_type( $type ) { - if ( in_array( $type, gutenberg_get_allowed_template_part_section_types(), true ) ) { +function gutenberg_filter_template_part_area_type( $type ) { + if ( in_array( $type, gutenberg_get_allowed_template_part_area_types(), true ) ) { return $type; } $warning_message = '"' . $type . '"'; - $warning_message .= __( ' is not a supported wp_template_section type and has been added as ', 'gutenberg' ); - $warning_message .= '"' . WP_TEMPLATE_SECTION_UNCATEGORIZED . '".'; + $warning_message .= __( ' is not a supported wp_template_part_area type and has been added as ', 'gutenberg' ); + $warning_message .= '"' . WP_TEMPLATE_PART_AREA_UNCATEGORIZED . '".'; trigger_error( $warning_message, E_USER_NOTICE ); - return WP_TEMPLATE_SECTION_UNCATEGORIZED; + return WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } diff --git a/phpunit/class-block-templates-test.php b/phpunit/class-block-templates-test.php index 62ba9a963674c..df952f0f01ec4 100644 --- a/phpunit/class-block-templates-test.php +++ b/phpunit/class-block-templates-test.php @@ -18,7 +18,7 @@ public static function wpSetUpBeforeClass() { gutenberg_register_template_post_type(); gutenberg_register_template_part_post_type(); gutenberg_register_wp_theme_taxonomy(); - gutenberg_register_wp_template_section_taxonomy(); + gutenberg_register_wp_template_part_area_taxonomy(); // Set up template post. $args = array( @@ -44,16 +44,16 @@ public static function wpSetUpBeforeClass() { 'post_content' => 'Content', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( - 'wp_theme' => array( + 'wp_theme' => array( get_stylesheet(), ), - 'wp_template_section' => array( - WP_TEMPLATE_SECTION_SIDEBAR, + 'wp_template_part_area' => array( + WP_TEMPLATE_PART_AREA_SIDEBAR, ), ), ); self::$template_part_post = self::factory()->post->create_and_get( $template_part_args ); - wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_SECTION_SIDEBAR, 'wp_template_section' ); + wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_SIDEBAR, 'wp_template_part_area' ); wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' ); } @@ -82,9 +82,9 @@ function test_gutenberg_build_template_result_from_file() { // Test template parts. $template_part = _gutenberg_build_template_result_from_file( array( - 'slug' => 'header', - 'path' => __DIR__ . '/fixtures/template.html', - 'section' => WP_TEMPLATE_SECTION_HEADER, + 'slug' => 'header', + 'path' => __DIR__ . '/fixtures/template.html', + 'area' => WP_TEMPLATE_PART_AREA_HEADER, ), 'wp_template_part' ); @@ -96,7 +96,7 @@ function test_gutenberg_build_template_result_from_file() { $this->assertEquals( 'header', $template_part->title ); $this->assertEquals( '', $template_part->description ); $this->assertEquals( 'wp_template_part', $template_part->type ); - $this->assertEquals( WP_TEMPLATE_SECTION_HEADER, $template_part->section ); + $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); } function test_gutenberg_build_template_result_from_post() { @@ -129,7 +129,7 @@ function test_gutenberg_build_template_result_from_post() { $this->assertEquals( 'My Template Part', $template_part->title ); $this->assertEquals( 'Description of my template part', $template_part->description ); $this->assertEquals( 'wp_template_part', $template_part->type ); - $this->assertEquals( WP_TEMPLATE_SECTION_SIDEBAR, $template_part->section ); + $this->assertEquals( WP_TEMPLATE_PART_AREA_SIDEBAR, $template_part->area ); } function test_inject_theme_attribute_in_content() { @@ -184,8 +184,8 @@ function test_gutenberg_get_block_template_from_file() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( false, $template->is_custom ); $this->assertEquals( 'wp_template_part', $template->type ); - // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. - $this->assertEquals( WP_TEMPLATE_SECTION_UNCATEGORIZED, $template->section ); + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part area info. + $this->assertEquals( WP_TEMPLATE_PART_AREA_UNCATEGORIZED, $template->area ); } /** @@ -210,7 +210,7 @@ function test_gutenberg_get_block_template_from_post() { $this->assertEquals( 'publish', $template->status ); $this->assertEquals( true, $template->is_custom ); $this->assertEquals( 'wp_template_part', $template->type ); - $this->assertEquals( WP_TEMPLATE_SECTION_SIDEBAR, $template->section ); + $this->assertEquals( WP_TEMPLATE_PART_AREA_SIDEBAR, $template->area ); } /** @@ -244,10 +244,10 @@ function( $template ) { $template_ids = get_template_ids( $templates ); $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids ); - // Filter template part by section. - $templates = gutenberg_get_block_templates( array( 'section' => WP_TEMPLATE_SECTION_SIDEBAR ), 'wp_template_part' ); + // Filter template part by area. + $templates = gutenberg_get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_SIDEBAR ), 'wp_template_part' ); $template_ids = get_template_ids( $templates ); - // TODO - update following array result once tt1-blocks theme.json is updated for section info. + // TODO - update following array result once tt1-blocks theme.json is updated for area info. $this->assertEquals( array( get_stylesheet() . '//' . 'my_template_part' ), $template_ids ); } } diff --git a/phpunit/class-wp-rest-template-controller-test.php b/phpunit/class-wp-rest-template-controller-test.php index 235bc2b1303a6..154a8d440990f 100644 --- a/phpunit/class-wp-rest-template-controller-test.php +++ b/phpunit/class-wp-rest-template-controller-test.php @@ -16,7 +16,7 @@ public static function wpSetupBeforeClass( $factory ) { gutenberg_register_template_post_type(); gutenberg_register_template_part_post_type(); gutenberg_register_wp_theme_taxonomy(); - gutenberg_register_wp_template_section_taxonomy(); + gutenberg_register_wp_template_part_area_taxonomy(); self::$admin_id = $factory->user->create( array( 'role' => 'administrator', @@ -97,8 +97,8 @@ function find_and_normalize_template_by_id( $templates, $id ) { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, - // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. - 'section' => WP_TEMPLATE_SECTION_UNCATEGORIZED, + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part area info. + 'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED, ), find_and_normalize_template_by_id( $data, 'tt1-blocks//header' ) ); @@ -150,8 +150,8 @@ public function test_get_item() { 'is_custom' => false, 'type' => 'wp_template_part', 'wp_id' => null, - // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part section info. - 'section' => WP_TEMPLATE_SECTION_UNCATEGORIZED, + // TODO - update 'UNCATEGORIZED' to 'HEADER' once tt1-blocks theme.json updated for template part area info. + 'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED, ), $data ); @@ -201,7 +201,7 @@ public function test_create_item() { 'title' => 'My Template Part', 'description' => 'Just a description of a template part', 'content' => 'Content', - 'section' => 'header', + 'area' => 'header', ) ); $response = rest_get_server()->dispatch( $request ); @@ -225,7 +225,7 @@ public function test_create_item() { 'content' => array( 'raw' => 'Content', ), - 'section' => 'header', + 'area' => 'header', ), $data ); @@ -248,12 +248,12 @@ public function test_update_item() { $request = new WP_REST_Request( 'PUT', '/wp/v2/template-parts/tt1-blocks//header' ); $request->set_body_params( array( - 'section' => 'something unsupported', + 'area' => 'something unsupported', ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEquals( WP_TEMPLATE_SECTION_UNCATEGORIZED, $data['section'] ); + $this->assertEquals( WP_TEMPLATE_PART_AREA_UNCATEGORIZED, $data['area'] ); $this->assertEquals( true, $data['is_custom'] ); } diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 87477458bc25e..857b46d57baf5 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -247,7 +247,7 @@ function test_get_stylesheet() { ), 'misc' => 'value', ), - 'core/group' => array( + 'core/group' => array( 'custom' => array( 'base-font' => 16, 'line-height' => array( @@ -766,7 +766,7 @@ function test_get_template_parts() { array( 'templateParts' => array( 'header' => array( - 'section' => 'Some section', + 'area' => 'Some area', ), ), ) @@ -778,7 +778,7 @@ function test_get_template_parts() { $template_parts, array( 'header' => array( - 'section' => 'Some section', + 'area' => 'Some area', ), ) ); From a41d6c8ec398408e5e23b264e47fac786822d83b Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 10 Feb 2021 11:54:33 -0500 Subject: [PATCH 36/36] fix typo --- lib/full-site-editing/class-wp-rest-templates-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/full-site-editing/class-wp-rest-templates-controller.php b/lib/full-site-editing/class-wp-rest-templates-controller.php index 119514dbc8c4a..f2baa6a5b3698 100644 --- a/lib/full-site-editing/class-wp-rest-templates-controller.php +++ b/lib/full-site-editing/class-wp-rest-templates-controller.php @@ -367,7 +367,7 @@ protected function prepare_item_for_database( $request ) { } elseif ( null !== $template && ! $template->is_custom && $template->area ) { $changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area_type( $template->area ); } elseif ( ! $template->area ) { - $changes->tax_input['wp_template_part_area'] = WP_TEMPLAT_PART_AREA_UNCATEGORIZED; + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } }