Remove all unnecessary registerCheckoutBlock code in favor of the correct simplified solution

This commit is contained in:
Daniel Dudzic 2024-04-17 02:06:41 +02:00
parent ad2c8e9aed
commit 43869993c6
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
19 changed files with 157 additions and 440 deletions

2
composer.lock generated
View file

@ -5002,5 +5002,5 @@
"ext-json": "*"
},
"platform-dev": [],
"plugin-api-version": "2.6.0"
"plugin-api-version": "2.3.0"
}

View file

@ -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':

View file

@ -9,6 +9,10 @@
"parent": [ "woocommerce/cart-totals-block" ],
"attributes": {
"id": {
"type": "string",
"default": "woocommerce-paypal-payments/cart-paylater-messages"
},
"ppcpId": {
"type": "string"
},
"lock": {

View file

@ -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 (

View file

@ -1,14 +0,0 @@
/**
* External dependencies
*/
import { registerCheckoutBlock } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import metadata from './block.json';
registerCheckoutBlock({
metadata,
component: () => false,
});

View file

@ -1,5 +0,0 @@
import { useBlockProps } from '@wordpress/block-editor';
export default function save() {
return <div { ...useBlockProps.save() } />;
}

View file

@ -9,6 +9,10 @@
"parent": [ "woocommerce/checkout-totals-block" ],
"attributes": {
"id": {
"type": "string",
"default": "woocommerce-paypal-payments/checkout-paylater-messages"
},
"ppcpId": {
"type": "string"
},
"lock": {

View file

@ -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 (

View file

@ -1,14 +0,0 @@
/**
* External dependencies
*/
import { registerCheckoutBlock } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import metadata from './block.json';
registerCheckoutBlock({
metadata,
component: () => false,
});

View file

@ -1,5 +0,0 @@
import { useBlockProps } from '@wordpress/block-editor';
export default function save() {
return <div { ...useBlockProps.save() } />;
}

View file

@ -1,11 +0,0 @@
/**
* External dependencies
*/
import { registerPlugin } from '@wordpress/plugins';
const render = () => {};
registerPlugin('ppcp-cart-paylater-messages-block', {
render,
scope: 'woocommerce-checkout',
});

View file

@ -1,11 +0,0 @@
/**
* External dependencies
*/
import { registerPlugin } from '@wordpress/plugins';
const render = () => {};
registerPlugin('ppcp-checkout-paylater-messages-block', {
render,
scope: 'woocommerce-checkout',
});

View file

@ -1,291 +0,0 @@
<?php
/**
* The Pay Later WooCommerce Blocks integration.
*
* @package WooCommerce\PayPalCommerce\PayLaterWCBlocks
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\PayLaterWCBlocks;
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
/**
* Class for integrating with WooCommerce Blocks
*/
class PayLaterWCBlocksIntegration implements IntegrationInterface {
/**
* The URL of the Pay Later WooCommerce Blocks plugin.
*
* @var string
*/
protected $paylater_wc_blocks_url;
/**
* The version of the Pay Later WooCommerce Blocks plugin.
*
* @var string
*/
protected $ppcp_asset_version;
/**
* Constructor
*
* @param string $paylater_wc_blocks_url The URL of the Pay Later WooCommerce Blocks plugin.
* @param string $ppcp_asset_version The version of the Pay Later WooCommerce Blocks plugin.
*/
public function __construct( string $paylater_wc_blocks_url, string $ppcp_asset_version ) {
$this->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();
}
}

View file

@ -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
);
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
);
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
);
}
/**

View file

@ -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 '<div id="' . esc_attr( $attributes['id'] ?? '' ) . '" class="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>';
return '<div id="' . esc_attr( $attributes['ppcpId'] ?? '' ) . '" data-block-name="' . esc_attr( $attributes['id'] ?? '' ) . '" class="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>';
}
}
}

View file

@ -0,0 +1,75 @@
<?php
/**
* The Pay Later WooCommerce Blocks Utils.
*
* @package WooCommerce\PayPalCommerce\PayLaterWCBlocks
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\PayLaterWCBlocks;
/**
* Class PayLaterWCBlocksUtils
*/
class PayLaterWCBlocksUtils {
/**
* Inserts content before the last div in a block.
*
* @param string $block_content The block content.
* @param string $content_to_insert The content to insert.
* @return string The block content with the content inserted.
*/
public static function insert_before_last_div( string $block_content, string $content_to_insert ): string {
$lastIndex = strrpos( $block_content, '</div>' );
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();
}
}

View file

@ -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: {