2022-08-12 14:23:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
|
|
|
import {
|
|
|
|
InspectorControls,
|
|
|
|
store as blockEditorStore,
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { addFilter, hasFilter } from '@wordpress/hooks';
|
|
|
|
import type { StoreDescriptor } from '@wordpress/data';
|
|
|
|
import { CartCheckoutSidebarCompatibilityNotice } from '@woocommerce/editor-components/sidebar-compatibility-notice';
|
2023-04-06 11:16:42 +00:00
|
|
|
import { NoPaymentMethodsNotice } from '@woocommerce/editor-components/no-payment-methods-notice';
|
|
|
|
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';
|
|
|
|
import { DefaultNotice } from '@woocommerce/editor-components/default-notice';
|
2023-09-29 10:39:57 +00:00
|
|
|
import { IncompatibleExtensionsNotice } from '@woocommerce/editor-components/incompatible-extension-notice';
|
2022-08-12 14:23:08 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
2022-12-28 16:30:46 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
2023-06-29 13:41:22 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
declare module '@wordpress/editor' {
|
|
|
|
let store: StoreDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare module '@wordpress/core-data' {
|
|
|
|
let store: StoreDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare module '@wordpress/block-editor' {
|
|
|
|
let store: StoreDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
const withSidebarNotices = createHigherOrderComponent(
|
|
|
|
( BlockEdit ) => ( props ) => {
|
2023-04-06 11:16:42 +00:00
|
|
|
const {
|
|
|
|
clientId,
|
|
|
|
name: blockName,
|
|
|
|
isSelected: isBlockSelected,
|
|
|
|
} = props;
|
2022-10-20 19:17:26 +00:00
|
|
|
|
2022-12-28 16:30:46 +00:00
|
|
|
const [
|
2023-09-29 10:39:57 +00:00
|
|
|
isIncompatibleExtensionsNoticeDismissed,
|
|
|
|
setIsIncompatibleExtensionsNoticeDismissed,
|
2022-12-28 16:30:46 +00:00
|
|
|
] = useState( true );
|
|
|
|
|
2023-09-29 10:39:57 +00:00
|
|
|
const toggleIncompatibleExtensionsNoticeDismissedStatus = (
|
2022-12-28 16:30:46 +00:00
|
|
|
isDismissed: boolean
|
|
|
|
) => {
|
2023-09-29 10:39:57 +00:00
|
|
|
setIsIncompatibleExtensionsNoticeDismissed( isDismissed );
|
2022-12-28 16:30:46 +00:00
|
|
|
};
|
|
|
|
|
2023-04-06 11:16:42 +00:00
|
|
|
const { isCart, isCheckout, isPaymentMethodsBlock, hasPaymentMethods } =
|
|
|
|
useSelect( ( select ) => {
|
2022-08-12 14:23:08 +00:00
|
|
|
const { getBlockParentsByBlockName, getBlockName } =
|
|
|
|
select( blockEditorStore );
|
|
|
|
const parent = getBlockParentsByBlockName( clientId, [
|
|
|
|
'woocommerce/cart',
|
|
|
|
'woocommerce/checkout',
|
|
|
|
] ).map( getBlockName );
|
|
|
|
const currentBlockName = getBlockName( clientId );
|
|
|
|
return {
|
|
|
|
isCart:
|
|
|
|
parent.includes( 'woocommerce/cart' ) ||
|
|
|
|
currentBlockName === 'woocommerce/cart',
|
|
|
|
isCheckout:
|
|
|
|
parent.includes( 'woocommerce/checkout' ) ||
|
|
|
|
currentBlockName === 'woocommerce/checkout',
|
2023-04-06 11:16:42 +00:00
|
|
|
isPaymentMethodsBlock:
|
|
|
|
currentBlockName ===
|
|
|
|
'woocommerce/checkout-payment-block',
|
|
|
|
hasPaymentMethods:
|
|
|
|
select(
|
|
|
|
PAYMENT_STORE_KEY
|
|
|
|
).paymentMethodsInitialized() &&
|
|
|
|
Object.keys(
|
|
|
|
select(
|
|
|
|
PAYMENT_STORE_KEY
|
|
|
|
).getAvailablePaymentMethods()
|
|
|
|
).length > 0,
|
2022-08-12 14:23:08 +00:00
|
|
|
};
|
2023-04-06 11:16:42 +00:00
|
|
|
} );
|
2022-12-28 16:30:46 +00:00
|
|
|
|
2023-03-23 10:24:04 +00:00
|
|
|
// Show sidebar notices only when a WooCommerce block is selected.
|
2023-04-06 11:16:42 +00:00
|
|
|
if (
|
|
|
|
! blockName.startsWith( 'woocommerce/' ) ||
|
|
|
|
! isBlockSelected ||
|
|
|
|
! ( isCart || isCheckout )
|
|
|
|
) {
|
2023-03-23 10:24:04 +00:00
|
|
|
return <BlockEdit key="edit" { ...props } />;
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-04-06 11:16:42 +00:00
|
|
|
<InspectorControls>
|
2023-09-29 10:39:57 +00:00
|
|
|
<IncompatibleExtensionsNotice
|
2023-04-06 11:16:42 +00:00
|
|
|
toggleDismissedStatus={
|
2023-09-29 10:39:57 +00:00
|
|
|
toggleIncompatibleExtensionsNoticeDismissedStatus
|
2023-04-06 11:16:42 +00:00
|
|
|
}
|
|
|
|
block={
|
|
|
|
isCheckout
|
|
|
|
? 'woocommerce/checkout'
|
|
|
|
: 'woocommerce/cart'
|
|
|
|
}
|
|
|
|
/>
|
2022-12-28 16:30:46 +00:00
|
|
|
|
2023-09-19 09:58:18 +00:00
|
|
|
<DefaultNotice block={ isCheckout ? 'checkout' : 'cart' } />
|
2023-06-29 13:41:22 +00:00
|
|
|
|
2023-09-29 10:39:57 +00:00
|
|
|
{ isIncompatibleExtensionsNoticeDismissed ? (
|
2023-06-29 13:41:22 +00:00
|
|
|
<CartCheckoutSidebarCompatibilityNotice
|
|
|
|
block={ isCheckout ? 'checkout' : 'cart' }
|
|
|
|
/>
|
2023-04-06 11:16:42 +00:00
|
|
|
) : null }
|
2022-12-28 16:30:46 +00:00
|
|
|
|
2023-04-06 11:16:42 +00:00
|
|
|
{ isPaymentMethodsBlock && ! hasPaymentMethods && (
|
|
|
|
<NoPaymentMethodsNotice />
|
|
|
|
) }
|
2022-08-12 14:23:08 +00:00
|
|
|
|
2023-04-06 11:16:42 +00:00
|
|
|
<CartCheckoutFeedbackPrompt />
|
|
|
|
</InspectorControls>
|
2023-01-10 10:14:43 +00:00
|
|
|
<BlockEdit key="edit" { ...props } />
|
2022-08-12 14:23:08 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
'withSidebarNotices'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
! hasFilter(
|
|
|
|
'editor.BlockEdit',
|
|
|
|
'woocommerce/add/sidebar-compatibility-notice'
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
addFilter(
|
|
|
|
'editor.BlockEdit',
|
|
|
|
'woocommerce/add/sidebar-compatibility-notice',
|
|
|
|
withSidebarNotices,
|
|
|
|
11
|
|
|
|
);
|
|
|
|
}
|