From 43869993c64788de09e2ead613c8b000cfe6911b Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 17 Apr 2024 02:06:41 +0200 Subject: [PATCH] Remove all unnecessary registerCheckoutBlock code in favor of the correct simplified solution --- composer.lock | 2 +- .../ppcp-button/src/Assets/SmartButton.php | 12 +- .../js/CartPayLaterMessagesBlock/block.json | 6 +- .../cart-paylater-block.js} | 1 + .../js/CartPayLaterMessagesBlock/edit.js | 8 +- .../js/CartPayLaterMessagesBlock/frontend.js | 14 - .../js/CartPayLaterMessagesBlock/save.js | 5 - .../CheckoutPayLaterMessagesBlock/block.json | 6 +- .../checkout-paylater-block.js} | 1 + .../js/CheckoutPayLaterMessagesBlock/edit.js | 8 +- .../CheckoutPayLaterMessagesBlock/frontend.js | 14 - .../js/CheckoutPayLaterMessagesBlock/save.js | 5 - .../js/ppcp-cart-paylater-messages-block.js | 11 - .../ppcp-checkout-paylater-messages-block.js | 11 - .../src/PayLaterWCBlocksIntegration.php | 291 ------------------ .../src/PayLaterWCBlocksModule.php | 91 +++--- .../src/PayLaterWCBlocksRenderer.php | 2 +- .../src/PayLaterWCBlocksUtils.php | 75 +++++ .../ppcp-paylater-wc-blocks/webpack.config.js | 34 +- 19 files changed, 157 insertions(+), 440 deletions(-) rename modules/ppcp-paylater-wc-blocks/resources/js/{CheckoutPayLaterMessagesBlock/index.js => CartPayLaterMessagesBlock/cart-paylater-block.js} (99%) delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/frontend.js delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/save.js rename modules/ppcp-paylater-wc-blocks/resources/js/{CartPayLaterMessagesBlock/index.js => CheckoutPayLaterMessagesBlock/checkout-paylater-block.js} (99%) delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/frontend.js delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/save.js delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/ppcp-cart-paylater-messages-block.js delete mode 100644 modules/ppcp-paylater-wc-blocks/resources/js/ppcp-checkout-paylater-messages-block.js delete mode 100644 modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksIntegration.php create mode 100644 modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksUtils.php diff --git a/composer.lock b/composer.lock index 9bd08ba42..0643929d4 100644 --- a/composer.lock +++ b/composer.lock @@ -5002,5 +5002,5 @@ "ext-json": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 29cb4d03d..83092d9f5 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -428,8 +428,6 @@ class SmartButton implements SmartButtonInterface { ) ); - die(var_dump($has_paylater_block)); - $get_hook = function ( string $location ) use ( $default_pay_order_hook, $is_block_theme, $has_paylater_block ): ?array { switch ( $location ) { case 'checkout': @@ -646,11 +644,11 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages $messaging_enabled_for_current_location = $this->settings_status->is_pay_later_messaging_enabled_for_location( $location ); - $has_paylater_block = - PayLaterBlockModule::is_block_enabled( $this->settings_status ) && - has_block( 'woocommerce-paypal-payments/paylater-messages' ) || - has_block( 'woocommerce-paypal-payments/checkout-paylater-messages' ) || - has_block( 'woocommerce-paypal-payments/cart-paylater-messages' ); + $has_paylater_block = PayLaterBlockModule::is_block_enabled( $this->settings_status ) && has_block( 'woocommerce-paypal-payments/paylater-messages' ); + + if ( 'cart-block' === $location || 'checkout-block' === $location ) { + return true; + } switch ( $location ) { case 'checkout': diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/block.json b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/block.json index 01d57f905..77fefadbd 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/block.json +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/block.json @@ -8,7 +8,11 @@ "example": {}, "parent": [ "woocommerce/cart-totals-block" ], "attributes": { - "id": { + "id": { + "type": "string", + "default": "woocommerce-paypal-payments/cart-paylater-messages" + }, + "ppcpId": { "type": "string" }, "lock": { diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/index.js b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/cart-paylater-block.js similarity index 99% rename from modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/index.js rename to modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/cart-paylater-block.js index d2fb7ccf7..cd77866ed 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/index.js +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/cart-paylater-block.js @@ -38,3 +38,4 @@ registerBlockType(metadata, { return null; }, }); + diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/edit.js b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/edit.js index 809be74e5..4746aab23 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/edit.js +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/edit.js @@ -6,7 +6,7 @@ import { PayPalScriptProvider, PayPalMessages } from '@paypal/react-paypal-js'; import { useScriptParams } from '../../../../ppcp-paylater-block/resources/js/hooks/script-params'; export default function Edit({ attributes, clientId, setAttributes }) { - const { id } = attributes; + const { id, ppcpId } = attributes; const [loaded, setLoaded] = useState(false); @@ -47,10 +47,10 @@ export default function Edit({ attributes, clientId, setAttributes }) { const props = useBlockProps({ className: classes.join(' ') }); useEffect(() => { - if (!id) { - setAttributes({ id: `ppcp-${clientId}` }); + if (!ppcpId) { + setAttributes({ ppcpId: `ppcp-${clientId}` }); } - }, [id, clientId]); + }, [ppcpId, clientId]); if (PcpCartPayLaterBlock.vaultingEnabled) { return ( diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/frontend.js b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/frontend.js deleted file mode 100644 index ea90744bc..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/frontend.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * External dependencies - */ -import { registerCheckoutBlock } from '@woocommerce/blocks-checkout'; - -/** - * Internal dependencies - */ -import metadata from './block.json'; - -registerCheckoutBlock({ - metadata, - component: () => false, -}); diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/save.js b/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/save.js deleted file mode 100644 index b83cb5c92..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/save.js +++ /dev/null @@ -1,5 +0,0 @@ -import { useBlockProps } from '@wordpress/block-editor'; - -export default function save() { - return
; -} diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/block.json b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/block.json index 7c4e71fba..9cb53913e 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/block.json +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/block.json @@ -9,8 +9,12 @@ "parent": [ "woocommerce/checkout-totals-block" ], "attributes": { "id": { - "type": "string" + "type": "string", + "default": "woocommerce-paypal-payments/checkout-paylater-messages" }, + "ppcpId": { + "type": "string" + }, "lock": { "type": "object", "default": { diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/index.js b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/checkout-paylater-block.js similarity index 99% rename from modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/index.js rename to modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/checkout-paylater-block.js index d2fb7ccf7..cd77866ed 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CartPayLaterMessagesBlock/index.js +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/checkout-paylater-block.js @@ -38,3 +38,4 @@ registerBlockType(metadata, { return null; }, }); + diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/edit.js b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/edit.js index c82af0440..0774baa12 100644 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/edit.js +++ b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/edit.js @@ -6,7 +6,7 @@ import { PayPalScriptProvider, PayPalMessages } from '@paypal/react-paypal-js'; import { useScriptParams } from '../../../../ppcp-paylater-block/resources/js/hooks/script-params'; export default function Edit({ attributes, clientId, setAttributes }) { - const { id } = attributes; + const { id, ppcpId } = attributes; const [loaded, setLoaded] = useState(false); @@ -47,10 +47,10 @@ export default function Edit({ attributes, clientId, setAttributes }) { const props = useBlockProps({ className: classes }); useEffect(() => { - if (!id) { - setAttributes({ id: 'ppcp-' + clientId }); + if (!ppcpId) { + setAttributes({ ppcpId: 'ppcp-' + clientId }); } - }, [id, clientId]); + }, [ppcpId, clientId]); if (PcpCheckoutPayLaterBlock.vaultingEnabled) { return ( diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/frontend.js b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/frontend.js deleted file mode 100644 index ea90744bc..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/frontend.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * External dependencies - */ -import { registerCheckoutBlock } from '@woocommerce/blocks-checkout'; - -/** - * Internal dependencies - */ -import metadata from './block.json'; - -registerCheckoutBlock({ - metadata, - component: () => false, -}); diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/save.js b/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/save.js deleted file mode 100644 index b83cb5c92..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/CheckoutPayLaterMessagesBlock/save.js +++ /dev/null @@ -1,5 +0,0 @@ -import { useBlockProps } from '@wordpress/block-editor'; - -export default function save() { - return
; -} diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-cart-paylater-messages-block.js b/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-cart-paylater-messages-block.js deleted file mode 100644 index 8e4bd3c87..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-cart-paylater-messages-block.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * External dependencies - */ -import { registerPlugin } from '@wordpress/plugins'; - -const render = () => {}; - -registerPlugin('ppcp-cart-paylater-messages-block', { - render, - scope: 'woocommerce-checkout', -}); diff --git a/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-checkout-paylater-messages-block.js b/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-checkout-paylater-messages-block.js deleted file mode 100644 index 64b07fea8..000000000 --- a/modules/ppcp-paylater-wc-blocks/resources/js/ppcp-checkout-paylater-messages-block.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * External dependencies - */ -import { registerPlugin } from '@wordpress/plugins'; - -const render = () => {}; - -registerPlugin('ppcp-checkout-paylater-messages-block', { - render, - scope: 'woocommerce-checkout', -}); diff --git a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksIntegration.php b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksIntegration.php deleted file mode 100644 index 4e7dc09d2..000000000 --- a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksIntegration.php +++ /dev/null @@ -1,291 +0,0 @@ -paylater_wc_blocks_url = $paylater_wc_blocks_url; - $this->ppcp_asset_version = $ppcp_asset_version; - } - - /** - * The name of the integration. - * - * @return string - */ - public function get_name(): string { - return 'ppcp-paylater-wc-blocks'; - } - - /** - * The version of the integration. - * - * @return string - */ - public function get_version(): string { - return $this->ppcp_asset_version; - } - - /** - * When called invokes any initialization/setup for the integration. - * - * @return void - */ - public function initialize(): void { - $this->register_paylater_wc_blocks_frontend_scripts(); - $this->register_paylater_wc_blocks_editor_scripts(); - $this->register_main_integration(); - } - - /** - * Registers the main JS files. - * - * @return void - */ - private function register_main_integration() : void { - $cart_block_script_path = 'assets/js/ppcp-cart-paylater-messages-block.js'; - $checkout_block_script_path = 'assets/js/ppcp-checkout-paylater-messages-block.js'; - $style_path = 'build/style-index.css'; - - $cart_block_script_url = $this->paylater_wc_blocks_url . $cart_block_script_path; - $checkout_block_script_url = $this->paylater_wc_blocks_url . $checkout_block_script_path; - - $style_url = $this->paylater_wc_blocks_url . $style_path; - - $cart_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/ppcp-cart-paylater-messages-block.asset.php'; - $checkout_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/ppcp-checkout-paylater-messages-block.asset.php'; - - $cart_block_script_asset = file_exists( $cart_block_script_asset_path ) - ? require $cart_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $cart_block_script_asset_path ), - ); - - $checkout_block_script_asset = file_exists( $checkout_block_script_asset_path ) - ? require $checkout_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $checkout_block_script_asset_path ), - ); - - wp_register_script( - 'ppcp-cart-paylater-messages-block', - $cart_block_script_url, - $cart_block_script_asset['dependencies'], - $cart_block_script_asset['version'], - true - ); - - wp_register_script( - 'ppcp-checkout-paylater-messages-block', - $checkout_block_script_url, - $checkout_block_script_asset['dependencies'], - $checkout_block_script_asset['version'], - true - ); - - wp_set_script_translations( - 'ppcp-cart-paylater-messages-block', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . 'languages' - ); - - wp_set_script_translations( - 'ppcp-checkout-paylater-messages-block', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . 'languages' - ); - } - - /** - * Returns an array of script handles to enqueue in the frontend context. - * - * @return string[] - */ - public function get_script_handles(): array { - return array( 'ppcp-checkout-paylater-messages-block', 'ppcp-cart-paylater-messages-block', 'ppcp-cart-paylater-messages-block-frontend', 'ppcp-checkout-paylater-messages-block-frontend' ); - } - - /** - * Returns an array of script handles to enqueue in the editor context. - * - * @return string[] - */ - public function get_editor_script_handles(): array { - return array( 'ppcp-cart-paylater-wc-blocks-editor', 'ppcp-checkout-paylater-wc-blocks-editor', 'ppcp-checkout-paylater-messages-block', 'ppcp-cart-paylater-messages-block' ); - } - - /** - * An array of key, value pairs of data made available to the block on the client side. - * - * @return array - */ - public function get_script_data(): array { - - return array( - 'ppcp-paylater-wc-blocks-active' => true, - ); - } - - /** - * Registers the editor scripts. - * - * @return void - */ - public function register_paylater_wc_blocks_editor_scripts(): void { - $cart_block_script_path = 'assets/js/cart-paylater-messages-block.js'; - $checkout_block_script_path = 'assets/js/checkout-paylater-messages-block.js'; - $cart_block_script_url = $this->paylater_wc_blocks_url . $cart_block_script_path; - $checkout_block_script_url = $this->paylater_wc_blocks_url . $checkout_block_script_path; - $cart_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/cart-paylater-messages-block.asset.php'; - $checkout_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/checkout-paylater-messages-block.asset.php'; - - $cart_block_script_asset = file_exists( $cart_block_script_asset_path ) - ? require $cart_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $cart_block_script_asset_path ), - ); - - $checkout_block_script_asset = file_exists( $checkout_block_script_asset_path ) - ? require $checkout_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $checkout_block_script_asset_path ), - ); - - wp_register_script( - 'ppcp-cart-paylater-wc-blocks-editor', - $cart_block_script_url, - $cart_block_script_asset['dependencies'], - $cart_block_script_asset['version'], - true - ); - - wp_register_script( - 'ppcp-checkout-paylater-wc-blocks-editor', - $checkout_block_script_url, - $checkout_block_script_asset['dependencies'], - $checkout_block_script_asset['version'], - true - ); - - wp_set_script_translations( - 'ppcp-cart-paylater-wc-blocks-editor', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . '/languages' - ); - - wp_set_script_translations( - 'ppcp-checkout-paylater-wc-blocks-editor', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . '/languages' - ); - } - - /** - * Registers the frontend scripts. - * - * @return void - */ - public function register_paylater_wc_blocks_frontend_scripts(): void { - $cart_block_script_path = 'assets/js/cart-paylater-messages-block-frontend.js'; - $checkout_block_script_path = 'assets/js/checkout-paylater-messages-block-frontend.js'; - $cart_block_script_url = $this->paylater_wc_blocks_url . $cart_block_script_path; - $checkout_block_script_url = $this->paylater_wc_blocks_url . $checkout_block_script_path; - $cart_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/cart-paylater-messages-block-frontend.asset.php'; - $checkout_block_script_asset_path = $this->paylater_wc_blocks_url . 'assets/checkout-paylater-messages-block-frontend.asset.php'; - - $cart_block_script_asset = file_exists( $cart_block_script_asset_path ) - ? require $cart_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $cart_block_script_asset_path ), - ); - - $checkout_block_script_asset = file_exists( $checkout_block_script_asset_path ) - ? require $checkout_block_script_asset_path - : array( - 'dependencies' => array(), - 'version' => $this->get_file_version( $checkout_block_script_asset_path ), - ); - - wp_register_script( - 'ppcp-cart-paylater-messages-block-frontend', - $cart_block_script_url, - $cart_block_script_asset['dependencies'], - $cart_block_script_asset['version'], - true - ); - - wp_register_script( - 'ppcp-checkout-paylater-messages-block-frontend', - $checkout_block_script_url, - $checkout_block_script_asset['dependencies'], - $checkout_block_script_asset['version'], - true - ); - - wp_set_script_translations( - 'ppcp-cart-paylater-messages-block-frontend', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . '/languages' - ); - - wp_set_script_translations( - 'ppcp-checkout-paylater-messages-block-frontend', - 'woocommerce-paypal-payments', - $this->paylater_wc_blocks_url . '/languages' - ); - } - - /** - * Get the file modified time as a cache buster if we're in dev mode. - * - * @param string $file Local path to the file. - * @return string The cache buster value to use for the given file. - */ - protected function get_file_version( string $file ): string { - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) { - $filemtime = filemtime( $file ); - if ( $filemtime ) { - return (string) $filemtime; - } - } - return $this->get_version(); - } -} diff --git a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksModule.php b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksModule.php index e2c1804e5..969ebac7f 100644 --- a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksModule.php +++ b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksModule.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\PayLaterWCBlocks; -use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry; use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint; use WooCommerce\PayPalCommerce\PayLaterConfigurator\Factory\ConfigFactory; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; @@ -19,6 +18,7 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; +use WooCommerce\PayPalCommerce\PayLaterWCBlocks\PayLaterWCBlocksUtils; /** * Class PayLaterWCBlocksModule @@ -83,34 +83,17 @@ class PayLaterWCBlocksModule implements ModuleInterface { $settings = $c->get( 'wcgateway.settings' ); assert( $settings instanceof Settings ); - add_action( - 'woocommerce_blocks_loaded', - function () use ( $c ): void { - add_action( - 'woocommerce_blocks_checkout_block_registration', - function ( IntegrationRegistry $integration_registry ) use ( $c ): void { - $integration_registry->register( - new PayLaterWCBlocksIntegration( - $c->get( 'paylater-wc-blocks.url' ), - $c->get( 'ppcp.asset-version' ) - ) - ); - } - ); - } - ); - add_action( 'init', function () use ( $c, $settings ): void { $config_factory = $c->get( 'paylater-configurator.factory.config' ); assert( $config_factory instanceof ConfigFactory ); - $script_handle = 'ppcp-cart-paylater-messages-block'; + $script_handle = 'ppcp-cart-paylater-block'; wp_register_script( $script_handle, - $c->get( 'paylater-wc-blocks.url' ) . '/assets/js/paylater-block.js', + $c->get( 'paylater-wc-blocks.url' ) . 'assets/js/cart-paylater-block.js', array(), $c->get( 'ppcp.asset-version' ), true @@ -133,11 +116,11 @@ class PayLaterWCBlocksModule implements ModuleInterface { ) ); - $script_handle = 'ppcp-checkout-paylater-messages-block'; + $script_handle = 'ppcp-checkout-paylater-block'; wp_register_script( $script_handle, - $c->get( 'paylater-wc-blocks.url' ) . '/assets/js/paylater-block.js', + $c->get( 'paylater-wc-blocks.url' ) . 'assets/js/checkout-paylater-block.js', array(), $c->get( 'ppcp.asset-version' ), true @@ -197,17 +180,12 @@ class PayLaterWCBlocksModule implements ModuleInterface { dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CartPayLaterMessagesBlock', array( 'render_callback' => function ( array $attributes ) use ( $c ) { - $renderer = $c->get( 'paylater-wc-blocks.renderer' ); - ob_start(); - // phpcs:ignore -- No need to escape it, the PayLaterWCBlocksRenderer class is responsible for escaping. - echo $renderer->render( - // phpcs:ignore - $attributes, + return PayLaterWCBlocksUtils::render_paylater_block( + $attributes['id'] ?? 'woocommerce-paypal-payments/cart-paylater-messages', + $attributes['ppcpId'] ?? 'ppcp-cart-paylater-messages', 'cart', - // phpcs:ignore - $c + $c ); - return ob_get_clean(); }, ) ); @@ -223,21 +201,54 @@ class PayLaterWCBlocksModule implements ModuleInterface { dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CheckoutPayLaterMessagesBlock', array( 'render_callback' => function ( array $attributes ) use ( $c ) { - $renderer = $c->get( 'paylater-wc-blocks.renderer' ); - ob_start(); - // phpcs:ignore -- No need to escape it, the PayLaterWCBlocksRenderer class is responsible for escaping. - echo $renderer->render( - // phpcs:ignore - $attributes, + return PayLaterWCBlocksUtils::render_paylater_block( + $attributes['id'] ?? 'woocommerce-paypal-payments/checkout-paylater-messages', + $attributes['ppcpId'] ?? 'ppcp-checkout-paylater-messages', 'checkout', - // phpcs:ignore - $c + $c ); - return ob_get_clean(); }, ) ); } + + // This is a fallback for the default Cart block that haven't been saved with the inserted Pay Later messaging block. + add_filter( + 'render_block_woocommerce/cart-totals-block', + function ( string $block_content ) use ( $c ) { + if ( false === strpos( $block_content, 'woocommerce-paypal-payments/cart-paylater-messages' ) ) { + return PayLaterWCBlocksUtils::render_and_insert_paylater_block( + $block_content, + 'woocommerce-paypal-payments/cart-paylater-messages', + 'ppcp-cart-paylater-messages', + 'cart', + $c + ); + } + return $block_content; + }, + 10, + 1 + ); + + // This is a fallback for the default Checkout block that haven't been saved with the inserted Checkout - Pay Later messaging block. + add_filter( + 'render_block_woocommerce/checkout-totals-block', + function ( string $block_content ) use ( $c ) { + if ( false === strpos( $block_content, 'woocommerce-paypal-payments/checkout-paylater-messages' ) ) { + return PayLaterWCBlocksUtils::render_and_insert_paylater_block( + $block_content, + 'woocommerce-paypal-payments/checkout-paylater-messages', + 'ppcp-checkout-paylater-messages', + 'checkout', + $c + ); + } + return $block_content; + }, + 10, + 1 + ); } /** diff --git a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksRenderer.php b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksRenderer.php index 76387ecde..45795974c 100644 --- a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksRenderer.php +++ b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksRenderer.php @@ -26,7 +26,7 @@ class PayLaterWCBlocksRenderer { */ public function render( array $attributes, string $location, ContainerInterface $c ) { if ( PayLaterWCBlocksModule::is_placement_enabled( $c->get( 'wcgateway.settings.status' ), $location ) ) { - return '
'; + return '
'; } } } diff --git a/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksUtils.php b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksUtils.php new file mode 100644 index 000000000..9988a90b4 --- /dev/null +++ b/modules/ppcp-paylater-wc-blocks/src/PayLaterWCBlocksUtils.php @@ -0,0 +1,75 @@ +' ); + + if ( $lastIndex !== false ) { + $block_content = substr_replace( $block_content, $content_to_insert, $lastIndex, 0 ); + } + + return $block_content; + } + + /** + * Renders a PayLater message block and inserts it before the last closing div tag if the block id is not already present. + * + * @param string $block_content Current content of the block. + * @param string $block_id ID of the block to render. + * @param string $ppcp_id ID for the PPCP component. + * @param string $context Rendering context (cart or checkout). + * @param mixed $container Dependency injection container. + * @return string Updated block content. + */ + public static function render_and_insert_paylater_block( string $block_content, string $block_id, string $ppcp_id, string $context, $container ): string { + $paylater_message_block = self::render_paylater_block( $block_id, $ppcp_id, $context, $container ); + if ( false !== $paylater_message_block ) { + return self::insert_before_last_div($block_content, $paylater_message_block); + } + return $block_content; + } + + /** + * Renders the PayLater block based on the provided parameters. + * + * @param string $block_id ID of the block to render. + * @param string $ppcp_id ID for the PPCP component. + * @param string $context Rendering context (cart or checkout). + * @param mixed $container Dependency injection container. + * @return false|string Rendered content. + */ + public static function render_paylater_block( string $block_id, string $ppcp_id, string $context, $container ) { + $renderer = $container->get( 'paylater-wc-blocks.renderer' ); + ob_start(); + // phpcs:ignore -- No need to escape it, the PayLaterWCBlocksRenderer class is responsible for escaping. + echo $renderer->render( + array( + 'id' => $block_id, + 'ppcpId' => $ppcp_id, + ), + $context, + $container + ); + return ob_get_clean(); + } +} diff --git a/modules/ppcp-paylater-wc-blocks/webpack.config.js b/modules/ppcp-paylater-wc-blocks/webpack.config.js index 4ca9f6e2f..c8326c939 100644 --- a/modules/ppcp-paylater-wc-blocks/webpack.config.js +++ b/modules/ppcp-paylater-wc-blocks/webpack.config.js @@ -9,45 +9,19 @@ module.exports = { target: "web", plugins: [new DependencyExtractionWebpackPlugin()], entry: { - "ppcp-cart-paylater-messages-block": path.resolve( - process.cwd(), - "resources", - "js", - "ppcp-cart-paylater-messages-block.js" - ), - "ppcp-checkout-paylater-messages-block": path.resolve( - process.cwd(), - "resources", - "js", - "ppcp-checkout-paylater-messages-block.js" - ), - "cart-paylater-messages-block": path.resolve( + "cart-paylater-block": path.resolve( process.cwd(), "resources", "js", "CartPayLaterMessagesBlock", - "index.js" + "cart-paylater-block.js" ), - "checkout-paylater-messages-block": path.resolve( + "checkout-paylater-block": path.resolve( process.cwd(), "resources", "js", "CheckoutPayLaterMessagesBlock", - "index.js" - ), - "cart-paylater-messages-block-frontend": path.resolve( - process.cwd(), - "resources", - "js", - "CartPayLaterMessagesBlock", - "frontend.js" - ), - "checkout-paylater-messages-block-frontend": path.resolve( - process.cwd(), - "resources", - "js", - "CheckoutPayLaterMessagesBlock", - "frontend.js" + "checkout-paylater-block.js" ), }, output: {