Skip to content

Commit

Permalink
Store Editing Templates v1: add basic taxonomy block templates (wooco…
Browse files Browse the repository at this point in the history
…mmerce#5063)

* Add taxonomy block templates

* Change Category template display name

* Check for product category/tag taxonomy explicitely

* Group archive templates in LegacyTemplate
  • Loading branch information
frontdevde authored and jonny-bull committed Dec 16, 2021
1 parent 7e90bc5 commit 2f7658a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 16 deletions.
38 changes: 29 additions & 9 deletions assets/js/blocks/legacy-template/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@
*/
import { __ } from '@wordpress/i18n';

export const TEMPLATE_TITLES: Record< string, string > = {
'single-product': __(
'WooCommerce Single Product Template',
'woo-gutenberg-products-block'
),
'archive-product': __(
'WooCommerce Product Archive Template',
'woo-gutenberg-products-block'
),
export const TEMPLATES: Record< string, Record< string, string > > = {
'single-product': {
title: __(
'WooCommerce Single Product Template',
'woo-gutenberg-products-block'
),
placeholder: 'single-product',
},
'archive-product': {
title: __(
'WooCommerce Product Archive Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
'taxonomy-product_cat': {
title: __(
'WooCommerce Product Taxonomy Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
'taxonomy-product_tag': {
title: __(
'WooCommerce Product Tag Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
};
2 changes: 1 addition & 1 deletion assets/js/blocks/legacy-template/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
}

.editor-styles-wrapper .wp-block-woocommerce-legacy-template__placeholder-image {
.wp-block-woocommerce-legacy-template__placeholder .wp-block-woocommerce-legacy-template__placeholder-image {
display: none;
width: 100%;
height: auto;
Expand Down
8 changes: 5 additions & 3 deletions assets/js/blocks/legacy-template/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { page } from '@wordpress/icons';
* Internal dependencies
*/
import './editor.scss';
import { TEMPLATE_TITLES } from './constants';
import { TEMPLATES } from './constants';

interface Props {
attributes: {
Expand All @@ -25,7 +25,9 @@ interface Props {
const Edit = ( { attributes }: Props ) => {
const blockProps = useBlockProps();
const templateTitle =
TEMPLATE_TITLES[ attributes.template ] ?? attributes.template;
TEMPLATES[ attributes.template ]?.title ?? attributes.template;
const templatePlaceholder =
TEMPLATES[ attributes.template ]?.placeholder ?? 'fallback';
return (
<div { ...blockProps }>
<Placeholder
Expand All @@ -46,7 +48,7 @@ const Edit = ( { attributes }: Props ) => {
<div className="wp-block-woocommerce-legacy-template__placeholder-wireframe">
<img
className="wp-block-woocommerce-legacy-template__placeholder-image"
src={ `${ WC_BLOCKS_IMAGE_URL }template-placeholders/${ attributes.template }.svg` }
src={ `${ WC_BLOCKS_IMAGE_URL }template-placeholders/${ templatePlaceholder }.svg` }
alt={ templateTitle }
/>
</div>
Expand Down
3 changes: 3 additions & 0 deletions images/template-placeholders/fallback.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ public function render_block_template() {
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
is_tax() &&
( is_product_taxonomy() && is_tax( 'product_cat' ) ) &&
! $this->theme_has_template( 'taxonomy-product_cat' ) &&
$this->default_block_template_is_available( 'taxonomy-product_cat' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_product_taxonomy() && is_tax( 'product_tag' ) ) &&
! $this->theme_has_template( 'taxonomy-product_tag' ) &&
$this->default_block_template_is_available( 'taxonomy-product_tag' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
! $this->theme_has_template( 'archive-product' ) &&
Expand Down
4 changes: 3 additions & 1 deletion src/BlockTypes/LegacyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ protected function render( $attributes, $content ) {
return;
}

$archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag' );

if ( 'single-product' === $attributes['template'] ) {
return $this->render_single_product();
} elseif ( 'archive-product' === $attributes['template'] ) {
} elseif ( in_array( $attributes['template'], $archive_templates, true ) ) {
return $this->render_archive_product();
} else {
ob_start();
Expand Down
4 changes: 3 additions & 1 deletion src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public static function convert_slug_to_title( $template_slug ) {
case 'archive-product':
return __( 'Product Archive Page', 'woo-gutenberg-products-block' );
case 'taxonomy-product_cat':
return __( 'Product Taxonomy Page', 'woo-gutenberg-products-block' );
return __( 'Product Category Page', 'woo-gutenberg-products-block' );
case 'taxonomy-product_tag':
return __( 'Product Tag Page', 'woo-gutenberg-products-block' );
default:
// Replace all hyphens and underscores with spaces.
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
Expand Down
3 changes: 3 additions & 0 deletions templates/block-templates/taxonomy-product_cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_cat"} /-->
<!-- wp:template-part {"slug":"footer"} /-->
3 changes: 3 additions & 0 deletions templates/block-templates/taxonomy-product_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_tag"} /-->
<!-- wp:template-part {"slug":"footer"} /-->

0 comments on commit 2f7658a

Please sign in to comment.