Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 4b48aca

Browse files
committed
Merge branch 'upkeep/9524' of github.com:Sidsector9/woocommerce-blocks into upkeep/9524
2 parents ed48fc3 + 0c99974 commit 4b48aca

18 files changed

+145
-58
lines changed

.github/workflows/unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ jobs:
118118

119119
- name: Docker container debug information
120120
run: |
121-
npm run wp-env run tests-mysql "mysql --version"
121+
npm run wp-env run tests-mysql mysql -- --version
122122
npm run wp-env run tests-wordpress "php --version"
123123
npm run wp-env run tests-wordpress "php -m"
124124
npm run wp-env run tests-wordpress "php -i"

assets/js/blocks/classic-template/index.tsx

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getBlockType,
88
registerBlockType,
99
unregisterBlockType,
10+
parse,
1011
} from '@wordpress/blocks';
1112
import type { BlockEditProps } from '@wordpress/blocks';
1213
import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings';
@@ -18,10 +19,17 @@ import {
1819
import { Button, Placeholder, Popover } from '@wordpress/components';
1920
import { __ } from '@wordpress/i18n';
2021
import { box, Icon } from '@wordpress/icons';
21-
import { useDispatch, subscribe, useSelect, select } from '@wordpress/data';
22+
import {
23+
useDispatch,
24+
subscribe,
25+
useSelect,
26+
select,
27+
dispatch,
28+
} from '@wordpress/data';
2229
import { useEffect, useState } from '@wordpress/element';
2330
import { store as noticesStore } from '@wordpress/notices';
2431
import { useEntityRecord } from '@wordpress/core-data';
32+
import { debounce } from '@woocommerce/base-utils';
2533

2634
/**
2735
* Internal dependencies
@@ -333,6 +341,44 @@ const registerClassicTemplateBlock = ( {
333341
} );
334342
};
335343

344+
/**
345+
* Attempts to recover the Classic Template block if it fails to render on the Single Product template
346+
* due to the user resetting customizations without refreshing the page.
347+
*
348+
* When the Classic Template block fails to render, it is replaced by the 'core/missing' block, which
349+
* displays an error message stating that the WooCommerce Classic template block is unsupported.
350+
*
351+
* This function replaces the 'core/missing' block with the original Classic Template block that failed
352+
* to render, allowing the block to be displayed correctly.
353+
*
354+
* @see {@link https://github.com/woocommerce/woocommerce-blocks/issues/9637|Issue: Block error is displayed on clearing customizations for Woo Templates}
355+
*
356+
*/
357+
const tryToRecoverClassicTemplateBlockWhenItFailsToRender = debounce( () => {
358+
const blocks = select( 'core/block-editor' ).getBlocks();
359+
const blocksIncludingInnerBlocks = blocks.flatMap( ( block ) => [
360+
block,
361+
...block.innerBlocks,
362+
] );
363+
const classicTemplateThatFailedToRender = blocksIncludingInnerBlocks.find(
364+
( block ) =>
365+
block.name === 'core/missing' &&
366+
block.attributes.originalName === BLOCK_SLUG
367+
);
368+
369+
if ( classicTemplateThatFailedToRender ) {
370+
const blockToReplaceClassicTemplateBlockThatFailedToRender = parse(
371+
classicTemplateThatFailedToRender.attributes.originalContent
372+
);
373+
if ( blockToReplaceClassicTemplateBlockThatFailedToRender ) {
374+
dispatch( 'core/block-editor' ).replaceBlock(
375+
classicTemplateThatFailedToRender.clientId,
376+
blockToReplaceClassicTemplateBlockThatFailedToRender
377+
);
378+
}
379+
}
380+
}, 100 );
381+
336382
// @todo Refactor when there will be possible to show a block according on a template/post with a Gutenberg API. https://github.com/WordPress/gutenberg/pull/41718
337383

338384
let currentTemplateId: string | undefined;
@@ -341,21 +387,28 @@ subscribe( () => {
341387
const previousTemplateId = currentTemplateId;
342388
const store = select( 'core/edit-site' );
343389
currentTemplateId = store?.getEditedPostId() as string | undefined;
344-
345-
if ( previousTemplateId === currentTemplateId ) {
346-
return;
347-
}
348-
349390
const parsedTemplate = currentTemplateId?.split( '//' )[ 1 ];
350391

351392
if ( parsedTemplate === null || parsedTemplate === undefined ) {
352393
return;
353394
}
354395

355396
const block = getBlockType( BLOCK_SLUG );
397+
const isBlockRegistered = Boolean( block );
398+
399+
if (
400+
isBlockRegistered &&
401+
hasTemplateSupportForClassicTemplateBlock( parsedTemplate, TEMPLATES )
402+
) {
403+
tryToRecoverClassicTemplateBlockWhenItFailsToRender();
404+
}
405+
406+
if ( previousTemplateId === currentTemplateId ) {
407+
return;
408+
}
356409

357410
if (
358-
block !== undefined &&
411+
isBlockRegistered &&
359412
( ! hasTemplateSupportForClassicTemplateBlock(
360413
parsedTemplate,
361414
TEMPLATES
@@ -371,12 +424,12 @@ subscribe( () => {
371424
}
372425

373426
if (
374-
block === undefined &&
427+
! isBlockRegistered &&
375428
hasTemplateSupportForClassicTemplateBlock( parsedTemplate, TEMPLATES )
376429
) {
377430
registerClassicTemplateBlock( {
378431
template: parsedTemplate,
379432
inserter: true,
380433
} );
381434
}
382-
} );
435+
}, 'core/blocks-editor' );

assets/js/blocks/product-categories/block.js renamed to assets/js/blocks/product-categories/block.tsx

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import { __ } from '@wordpress/i18n';
55
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
66
import ServerSideRender from '@wordpress/server-side-render';
7-
import PropTypes from 'prop-types';
87
import { Icon, listView } from '@wordpress/icons';
98
import { isSiteEditorPage, isWidgetEditorPage } from '@woocommerce/utils';
109
import { useSelect } from '@wordpress/data';
@@ -18,6 +17,10 @@ import {
1817
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
1918
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
2019
} from '@wordpress/components';
20+
/**
21+
* Internal dependencies
22+
*/
23+
import type { ProductCategoriesBlockProps } from './types';
2124

2225
const EmptyPlaceholder = () => (
2326
<Placeholder
@@ -43,9 +46,15 @@ const EmptyPlaceholder = () => (
4346
* @param {function(any):any} props.setAttributes Setter for block attributes.
4447
* @param {string} props.name Name for block.
4548
*/
46-
const ProductCategoriesBlock = ( { attributes, setAttributes, name } ) => {
47-
const editSiteStore = useSelect( 'core/edit-site' );
48-
const editWidgetStore = useSelect( 'core/edit-widgets' );
49+
const ProductCategoriesBlock = ( {
50+
attributes,
51+
setAttributes,
52+
name,
53+
}: ProductCategoriesBlockProps ) => {
54+
const editSiteStore = useSelect( ( select ) => select( 'core/edit-site' ) );
55+
const editWidgetStore = useSelect( ( select ) =>
56+
select( 'core/edit-widgets' )
57+
);
4958
const isSiteEditor = isSiteEditorPage( editSiteStore );
5059
const isWidgetEditor = isWidgetEditorPage( editWidgetStore );
5160
const getInspectorControls = () => {
@@ -73,7 +82,7 @@ const ProductCategoriesBlock = ( { attributes, setAttributes, name } ) => {
7382
'woo-gutenberg-products-block'
7483
) }
7584
value={ isDropdown ? 'dropdown' : 'list' }
76-
onChange={ ( value ) =>
85+
onChange={ ( value: string ) =>
7786
setAttributes( {
7887
isDropdown: value === 'dropdown',
7988
} )
@@ -195,19 +204,4 @@ const ProductCategoriesBlock = ( { attributes, setAttributes, name } ) => {
195204
);
196205
};
197206

198-
ProductCategoriesBlock.propTypes = {
199-
/**
200-
* The attributes for this block
201-
*/
202-
attributes: PropTypes.object.isRequired,
203-
/**
204-
* The register block name.
205-
*/
206-
name: PropTypes.string.isRequired,
207-
/**
208-
* A callback to update attributes
209-
*/
210-
setAttributes: PropTypes.func.isRequired,
211-
};
212-
213207
export default ProductCategoriesBlock;

assets/js/blocks/product-categories/edit.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { useBlockProps } from '@wordpress/block-editor';
88
*/
99
import Block from './block';
1010
import './editor.scss';
11+
import type { ProductCategoriesBlockProps } from './types';
1112

12-
export const Edit = ( props: unknown ): JSX.Element => {
13+
export const Edit = ( props: ProductCategoriesBlockProps ): JSX.Element => {
1314
const blockProps = useBlockProps();
1415

1516
return (

assets/js/blocks/product-categories/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import './editor.scss';
1111
import metadata from './block.json';
1212
import './style.scss';
1313
import { Edit } from './edit';
14+
import type { ProductCategoriesIndexProps } from './types';
1415

1516
registerBlockType( metadata, {
1617
icon: {
@@ -27,7 +28,10 @@ registerBlockType( metadata, {
2728
type: 'block',
2829
blocks: [ 'core/legacy-widget' ],
2930
// We can't transform if raw instance isn't shown in the REST API.
30-
isMatch: ( { idBase, instance } ) =>
31+
isMatch: ( {
32+
idBase,
33+
instance,
34+
}: ProductCategoriesIndexProps ) =>
3135
idBase === 'woocommerce_product_categories' &&
3236
!! instance?.raw,
3337
transform: ( { instance } ) =>
@@ -77,10 +81,10 @@ registerBlockType( metadata, {
7781
migrate( attributes ) {
7882
return attributes;
7983
},
80-
save( props ) {
84+
save( props: ProductCategoriesIndexProps ) {
8185
const { hasCount, hasEmpty, isDropdown, isHierarchical } =
82-
props.attributes;
83-
const data = {};
86+
props;
87+
const data: { [ key: string ]: boolean } = {};
8488
if ( hasCount ) {
8589
data[ 'data-has-count' ] = true;
8690
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export interface ProductCategoriesBlockProps {
2+
attributes: {
3+
hasCount: boolean;
4+
hasImage: boolean;
5+
hasEmpty: boolean;
6+
isDropdown: boolean;
7+
isHierarchical: boolean;
8+
showChildrenOnly: boolean;
9+
};
10+
name: string;
11+
setAttributes: ( attributes: {
12+
hasCount?: boolean;
13+
hasImage?: boolean;
14+
hasEmpty?: boolean;
15+
isDropdown?: boolean;
16+
isHierarchical?: boolean;
17+
showChildrenOnly?: boolean;
18+
} ) => void;
19+
}
20+
21+
export interface ProductCategoriesIndexProps {
22+
idBase?: string;
23+
instance: {
24+
raw: {
25+
dropdown: boolean;
26+
count: boolean;
27+
hide_empty: boolean;
28+
hierarchical: boolean;
29+
};
30+
};
31+
hasCount: boolean;
32+
hasEmpty: boolean;
33+
isDropdown: boolean;
34+
isHierarchical: boolean;
35+
}

patterns/banner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<!-- wp:column {"verticalAlignment":"center"} -->
3333
<div class="wp-block-column is-vertically-aligned-center"><!-- wp:image {"id":1,"sizeSlug":"full","linkDestination":"none"} -->
34-
<figure class="wp-block-image size-full"><img src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/wood-home-wall-decoration-shelf-living-room.png', dirname( __FILE__ ) ) ); ?>" alt="" class="wp-image-1"/></figure>
34+
<figure class="wp-block-image size-full"><img src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/wood-home-wall-decoration-shelf-living-room.png', dirname( __FILE__ ) ) ); ?>" alt="<?php esc_attr_e( 'Placeholder image used to represent products being showcased in a banner.', 'woo-gutenberg-products-block' ); ?>" class="wp-image-1"/></figure>
3535
<!-- /wp:image --></div>
3636
<!-- /wp:column --></div>
3737
<!-- /wp:columns --></div>

patterns/featured-category-cover-image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- wp:cover {"url":"<?php echo esc_url( plugins_url( 'images/pattern-placeholders/wood-leather-fur-shop-jeans-shelf.png', dirname( __FILE__ ) ) ); ?>","id":1,"dimRatio":0,"focalPoint":{"x":0,"y":0},"contentPosition":"top left","align":"wide","style":{"spacing":{"padding":{"top":"2em","right":"2.25em","bottom":"2.25em","left":"2.25em"}}},"layout":{"type":"constrained"}} -->
99
<div class="wp-block-cover alignwide has-custom-content-position is-position-top-left" style="padding-top:2em;padding-right:2.25em;padding-bottom:2.25em;padding-left:2.25em">
1010
<span aria-hidden="true" class="wp-block-cover__background has-background-dim-0 has-background-dim"></span>
11-
<img class="wp-block-cover__image-background wp-image-1" alt="" src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/wood-leather-fur-shop-jeans-shelf.png', dirname( __FILE__ ) ) ); ?>" style="object-position:0% 0%" data-object-fit="cover" data-object-position="0% 0%"/>
11+
<img class="wp-block-cover__image-background wp-image-1" alt="<?php esc_attr_e( 'Placeholder image used to represent products being showcased in a featured category section.', 'woo-gutenberg-products-block' ); ?>" src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/wood-leather-fur-shop-jeans-shelf.png', dirname( __FILE__ ) ) ); ?>" style="object-position:0% 0%" data-object-fit="cover" data-object-position="0% 0%"/>
1212

1313
<div class="wp-block-cover__inner-container">
1414
<!-- wp:paragraph {"align":"left","placeholder":"Write title…","style":{"typography":{"lineHeight":"1.5","fontSize":"2.2em","textColor":"background"},"color":{"text":"#ffffff"},"spacing":{"margin":{"bottom":"0px","top":"0px"}}}} -->

patterns/featured-category-focus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- wp:group {"align":"full","style":{"color":{"background":"#84bfe1"},"spacing":{"padding":{"top":"var:preset|spacing|70","right":"var:preset|spacing|70","bottom":"var:preset|spacing|70","left":"var:preset|spacing|70"}}},"layout":{"type":"flex","orientation":"vertical","justifyContent":"center","flexWrap":"wrap"}} -->
99
<div class="wp-block-group alignfull has-background" style="background-color:#84bfe1;padding-top:var(--wp--preset--spacing--70);padding-right:var(--wp--preset--spacing--70);padding-bottom:var(--wp--preset--spacing--70);padding-left:var(--wp--preset--spacing--70)">
1010
<!-- wp:image {"id":1,"width":502,"height":335,"sizeSlug":"full","linkDestination":"none"} -->
11-
<figure class="wp-block-image size-full is-resized"><img src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/floor-home-decoration-fireplace-property-living-room.png', dirname( __FILE__ ) ) ); ?>" alt="" class="wp-image-1" width="502" height="335"/></figure>
11+
<figure class="wp-block-image size-full is-resized"><img src="<?php echo esc_url( plugins_url( 'images/pattern-placeholders/floor-home-decoration-fireplace-property-living-room.png', dirname( __FILE__ ) ) ); ?>" alt="<?php esc_attr_e( 'Placeholder image used to represent products being showcased in a featured category section.', 'woo-gutenberg-products-block' ); ?>" class="wp-image-1" width="502" height="335"/></figure>
1212
<!-- /wp:image -->
1313

1414
<!-- wp:paragraph {"align":"center","style":{"color":{"text":"#000000"}},"fontSize":"large"} -->

0 commit comments

Comments
 (0)