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';
|
2022-08-29 08:35:05 +00:00
|
|
|
import {
|
|
|
|
DefaultNotice,
|
|
|
|
LegacyNotice,
|
|
|
|
} from '@woocommerce/editor-components/default-notice';
|
2022-12-28 16:30:46 +00:00
|
|
|
import { IncompatiblePaymentGatewaysNotice } from '@woocommerce/editor-components/incompatible-payment-gateways-notice';
|
2022-08-12 14:23:08 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
2022-08-29 08:35:05 +00:00
|
|
|
import { isWcVersion } from '@woocommerce/settings';
|
2022-12-28 16:30:46 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
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 ) => {
|
2022-10-20 19:17:26 +00:00
|
|
|
const { name: blockName, isSelected: isBlockSelected } = props;
|
|
|
|
|
|
|
|
// Show sidebar notices only when a WooCommerce block is selected.
|
|
|
|
if ( ! blockName.startsWith( 'woocommerce/' ) || ! isBlockSelected ) {
|
2023-01-10 10:14:43 +00:00
|
|
|
return <BlockEdit key="edit" { ...props } />;
|
2022-10-20 19:17:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 16:30:46 +00:00
|
|
|
const [
|
|
|
|
isIncompatiblePaymentGatewaysNoticeDismissed,
|
|
|
|
setIsIncompatiblePaymentGatewaysNoticeDismissed,
|
|
|
|
] = useState( true );
|
|
|
|
|
|
|
|
const toggleIncompatiblePaymentGatewaysNoticeDismissedStatus = (
|
|
|
|
isDismissed: boolean
|
|
|
|
) => {
|
|
|
|
setIsIncompatiblePaymentGatewaysNoticeDismissed( isDismissed );
|
|
|
|
};
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
const addressFieldOrAccountBlocks = [
|
|
|
|
'woocommerce/checkout-shipping-address-block',
|
|
|
|
'woocommerce/checkout-billing-address-block',
|
|
|
|
'woocommerce/checkout-contact-information-block',
|
|
|
|
'woocommerce/checkout-fields-block',
|
|
|
|
];
|
|
|
|
const { clientId } = props;
|
|
|
|
const { isCart, isCheckout, isAddressFieldBlock } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
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',
|
|
|
|
isAddressFieldBlock:
|
|
|
|
addressFieldOrAccountBlocks.includes(
|
|
|
|
currentBlockName
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2022-12-28 16:30:46 +00:00
|
|
|
const MakeAsDefaultNotice = () => (
|
|
|
|
<>
|
|
|
|
{ isWcVersion( '6.9.0', '>=' ) ? (
|
|
|
|
<DefaultNotice block={ isCheckout ? 'checkout' : 'cart' } />
|
|
|
|
) : (
|
|
|
|
<LegacyNotice block={ isCheckout ? 'checkout' : 'cart' } />
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ ( isCart || isCheckout ) && (
|
|
|
|
<InspectorControls>
|
2022-12-28 16:30:46 +00:00
|
|
|
<IncompatiblePaymentGatewaysNotice
|
|
|
|
toggleDismissedStatus={
|
|
|
|
toggleIncompatiblePaymentGatewaysNoticeDismissedStatus
|
|
|
|
}
|
2023-02-24 10:57:24 +00:00
|
|
|
block={
|
|
|
|
isCheckout
|
|
|
|
? 'woocommerce/checkout'
|
|
|
|
: 'woocommerce/cart'
|
|
|
|
}
|
2022-08-12 14:23:08 +00:00
|
|
|
/>
|
2022-12-28 16:30:46 +00:00
|
|
|
|
|
|
|
{ isIncompatiblePaymentGatewaysNoticeDismissed ? (
|
|
|
|
<>
|
|
|
|
<MakeAsDefaultNotice />
|
|
|
|
<CartCheckoutSidebarCompatibilityNotice
|
|
|
|
block={ isCheckout ? 'checkout' : 'cart' }
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null }
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
{ isAddressFieldBlock ? null : (
|
|
|
|
<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
|
|
|
|
);
|
|
|
|
}
|