woocommerce/plugins/woocommerce-blocks/assets/js/atomic/utils/register-block-single-produ...

105 lines
3.0 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
import {
BlockAttributes,
BlockConfiguration,
BlockVariation,
getBlockType,
registerBlockType,
registerBlockVariation,
unregisterBlockType,
unregisterBlockVariation,
} from '@wordpress/blocks';
import { subscribe, select } from '@wordpress/data';
export const registerBlockSingleProductTemplate = ( {
blockName,
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
blockMetadata,
blockSettings,
isVariationBlock = false,
variationName,
}: {
blockName: string;
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
blockMetadata: Partial< BlockConfiguration >;
blockSettings: Partial< BlockConfiguration >;
isVariationBlock?: boolean;
variationName?: string;
} ) => {
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
let currentTemplateId: string | undefined = '';
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
currentTemplateId = store?.getEditedPostContext< {
templateSlug?: string;
} >()?.templateSlug;
const hasChangedTemplate = previousTemplateId !== currentTemplateId;
const hasTemplateId = Boolean( currentTemplateId );
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
if ( ! hasChangedTemplate || ! hasTemplateId || ! blockName ) {
return;
}
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
let isBlockRegistered = Boolean( getBlockType( blockName ) );
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
/**
* We need to unregister the block each time the user visits or leaves the Single Product template.
*
* The Single Product template is the only template where the `ancestor` property is not needed because it provides the context
* for the product blocks. We need to unregister and re-register the block to remove or add the `ancestor` property depending on which
* location (template, post, page, etc.) the user is in.
*
*/
if (
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
isBlockRegistered &&
( currentTemplateId?.includes( 'single-product' ) ||
previousTemplateId?.includes( 'single-product' ) )
) {
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
if ( isVariationBlock && variationName ) {
unregisterBlockVariation( blockName, variationName );
} else {
unregisterBlockType( blockName );
}
isBlockRegistered = false;
}
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
if ( ! isBlockRegistered ) {
if ( isVariationBlock ) {
registerBlockVariation( blockName, {
...blockSettings,
// @ts-expect-error: `ancestor` key is typed in WordPress core
ancestor: ! currentTemplateId?.includes( 'single-product' )
? blockSettings?.ancestor
: undefined,
} );
} else {
// @ts-expect-error: `registerBlockType` is typed in WordPress core
registerBlockType( blockMetadata, {
...blockSettings,
ancestor: ! currentTemplateId?.includes( 'single-product' )
? blockSettings?.ancestor
: undefined,
} );
}
}
}, 'core/edit-site' );
Add single product block (https://github.com/woocommerce/woocommerce-blocks/pull/8610) * Add block minimal structure * Add Single Product block icon * Add usesContext to block.json * Add Save function to Single Product Block registration settings * Add InnerBlocks template to Editor for Single Product Block * Add Product selector to Single Product block * Add withProduct HOC to the block editor * Add save function to the Single Product block * Add renderOnServerSide attribute to Product image This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block. * Add renderOnServerSide attribute to Product Category List block * Add renderOnServerSide attribute to Product Price block * Add renderOnServerSide attribute to Product Rating block * Add renderOnServerSide attribute to Product Stock Indicator block * Add renderOnServerSide attribute to Product Summary block * Add renderOnServerSide attribute to Product Tag List block * Add renderOnServerSide attribute to Product Title block * Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block * Reuse editor code for Single Product block * Add Props interface to Single Product Server Side Editor * Add render_block_context hook to SingleProductServerSide block * Fix Single Product Server Side block that was not appearing in the block inserter * Fix bug where the correct product was not being displayed on the frontend * Remove Single Product Block * Add Product title and product summary variations * Add Add To Cart Form and Product Meta blocks to the Single Product Block * remove more files that belong the Single Product block * fix php linter error * wrap up Single Product Block * Fix issue preventing the block from working on the Single Product Template * Fix update context function for Single Product block * Replace Single Product block title * Replace global $post only for Single Product inner blocks * Remove unnecessary var_dump statement * Improve documentation for the Single Product block * fix registration: add-to-cart-form and product-meta blocks * Improve documentation for Single Product block * Fix lint error related to imports * Remove query monitor debug code * Remove unnecessary `renderOnServerSide` attribute from product elements * Fix return type for the Save function of the Product Elements blocks * Fix return type for the Save function of the Product Title block * Add scss file for Single Product block * Fix PHP coding standards * Fix php coding standards * Remove internal dependencies section duplication * Replace .wc-block with .wc-block-editor- styles According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor. * Solve style for the Edit card component in the Single Product block * Fix error preventing Block Control from being displayed * Make the Product title to be a link by default * Fix PHP Coding Standards * Fix PHP Code Standards * Fix php cs issues * Fix issue with php cs * Remove unnecessary comment * Remove unused Icon import * Remove unnecessary dot from the className attributes in the Single Product Block * Replace metadata.name with metadata.title in the Editor file * Ignore phpcs rule * Add new attributes to the Attributes interface of the Price block * Fix typescript errors on the Product Meta block * Fix ts errors on Single Product editor * Fix ts errors on layout-editor.tsx * Fix ts errors in Single Product index file * Fix ts errors on add to cart form index file * Fix unsupported block error when using inner blocks outside the Single Product template * Fix unsupported block error for the Single Product block when editing Posts/Pages * Remove unnecessary JSDoc types * Remove unnecessary APIError component * Replace global $post only when rendering a `core/post-excerpt` block * Fix php cs errors * Put the Single Product block behind an experimental flag * Fix error occurring when trying to access ancestor of an undefined block settings * Remove JS Doc from shared-product-control.tsx * Fix single-product-frontend.js 404 error * Fix error 'Block names must be string' when adding Single Product block * Add todo comment to state the temporary solution used in the Single Product block * Move the wp_reset_postdata outside the block name check condition * Add comments about why we need to unregister the block on Single Product template --------- Co-authored-by: Luigi <gigitux@gmail.com>
2023-04-06 18:40:43 +00:00
subscribe( () => {
const isBlockRegistered = Boolean( getBlockType( blockName ) );
const editPostStoreExists = Boolean( select( 'core/edit-post' ) );
if ( ! isBlockRegistered && editPostStoreExists ) {
if ( isVariationBlock ) {
registerBlockVariation(
blockName,
blockSettings as BlockVariation< BlockAttributes >
);
} else {
// @ts-expect-error: `registerBlockType` is typed in WordPress core
registerBlockType( blockMetadata, blockSettings );
}
}
}, 'core/edit-post' );
};