Merge branch 'trunk' into PCP-2722-add-block-checkout-compatibility-to-advanced-card-processing

This commit is contained in:
Emili Castells Guasch 2024-05-08 14:27:40 +02:00
commit 3d924e0a04
108 changed files with 10904 additions and 369 deletions

View file

@ -34,6 +34,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\PayLaterBlock\PayLaterBlockModule;
use WooCommerce\PayPalCommerce\PayLaterWCBlocks\PayLaterWCBlocksModule;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
@ -438,7 +439,20 @@ class SmartButton implements SmartButtonInterface {
wp_is_block_theme()
);
$get_hook = function ( string $location ) use ( $default_pay_order_hook, $is_block_theme ): ?array {
$has_paylater_block =
(
PayLaterBlockModule::is_block_enabled( $this->settings_status ) &&
has_block( 'woocommerce-paypal-payments/paylater-messages' )
) ||
(
PayLaterWCBlocksModule::is_block_enabled( $this->settings_status, $location ) &&
(
has_block( 'woocommerce-paypal-payments/checkout-paylater-messages' ) ||
has_block( 'woocommerce-paypal-payments/cart-paylater-messages' )
)
);
$get_hook = function ( string $location ) use ( $default_pay_order_hook, $is_block_theme, $has_paylater_block ): ?array {
switch ( $location ) {
case 'checkout':
return $this->messages_renderer_hook( $location, 'woocommerce_review_order_before_payment', 10 );
@ -457,11 +471,14 @@ class SmartButton implements SmartButtonInterface {
? $this->messages_renderer_block( $location, 'core/navigation', 10 )
: $this->messages_renderer_hook( $location, 'loop_start', 20 );
default:
return null;
return $has_paylater_block
? $this->messages_renderer_hook( $location, 'ppcp_paylater_message_block', 10 )
: null;
}
};
$hook = $get_hook( $location );
if ( ! $hook ) {
return false;
}
@ -492,7 +509,7 @@ class SmartButton implements SmartButtonInterface {
function () {
echo '
<script>
document.querySelector("#payment").before(document.querySelector("#ppcp-messages"))
document.querySelector("#payment").before(document.querySelector(".ppcp-messages"))
</script>';
}
);
@ -601,10 +618,6 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
return false;
}
if ( in_array( $this->context(), array( 'checkout-block', 'cart-block' ), true ) ) {
return false;
}
return $this->should_load_buttons() || $this->should_load_messages() || $this->can_render_dcc();
}
@ -655,7 +668,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 = has_block( 'woocommerce-paypal-payments/paylater-messages' ) && PayLaterBlockModule::is_block_enabled( $this->settings_status );
$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':
@ -667,9 +684,6 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
return $messaging_enabled_for_current_location;
case 'block-editor':
return true;
case 'checkout-block':
case 'cart-block':
return $has_paylater_block || $this->is_block_editor();
default:
return $has_paylater_block;
}
@ -789,7 +803,7 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
*/
do_action( "ppcp_before_{$location_hook}_message_wrapper" );
$messages_placeholder = '<div id="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>';
$messages_placeholder = '<div class="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>';
if ( is_array( $block_params ) && ( $block_params['blockName'] ?? false ) ) {
$this->render_after_block(
@ -888,7 +902,20 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
$styling_per_location = $this->settings->has( 'pay_later_enable_styling_per_messaging_location' ) && $this->settings->get( 'pay_later_enable_styling_per_messaging_location' );
$location = $styling_per_location ? $location : 'general';
$setting_name_prefix = "pay_later_{$location}_message";
// Map checkout-block and cart-block message options to checkout and cart options.
switch ( $location ) {
case 'checkout-block':
$location = 'checkout';
break;
case 'cart-block':
$location = 'cart';
break;
default:
break;
}
$setting_name_prefix = "pay_later_{$location}_message";
$layout = $this->settings->has( "{$setting_name_prefix}_layout" ) ? $this->settings->get( "{$setting_name_prefix}_layout" ) : 'text';
$logo_type = $this->settings->has( "{$setting_name_prefix}_logo" ) ? $this->settings->get( "{$setting_name_prefix}_logo" ) : 'primary';
@ -899,7 +926,7 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
$text_size = $this->settings->has( "{$setting_name_prefix}_text_size" ) ? $this->settings->get( "{$setting_name_prefix}_text_size" ) : '12';
return array(
'wrapper' => '#ppcp-messages',
'wrapper' => '.ppcp-messages',
'is_hidden' => ! $this->is_pay_later_filter_enabled_for_location( $this->context() ),
'block' => array(
'enabled' => PayLaterBlockModule::is_block_enabled( $this->settings_status ),
@ -1111,6 +1138,7 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
'subscription_plan_id' => $this->subscription_helper->paypal_subscription_id(),
'vault_v3_enabled' => $this->vault_v3_enabled,
'variable_paypal_subscription_variations' => $this->subscription_helper->variable_paypal_subscription_variations(),
'variable_paypal_subscription_variation_from_cart' => $this->subscription_helper->paypal_subscription_variation_from_cart(),
'subscription_product_allowed' => $this->subscription_helper->checkout_subscription_product_allowed(),
'locations_with_subscription_product' => $this->subscription_helper->locations_with_subscription_product(),
'enforce_vault' => $this->has_subscriptions(),
@ -1428,9 +1456,45 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
* @return array
*/
private function attributes(): array {
return array(
$attributes = array(
'data-partner-attribution-id' => $this->bn_code_for_context( $this->context() ),
);
$page_type_attribute = $this->page_type_attribute();
if ( $page_type_attribute ) {
$attributes['data-page-type'] = $page_type_attribute;
}
return $attributes;
}
/**
* Retrieves the value for page type attribute(data-page-type) used for the JS SDK.
*
* @return string
*/
protected function page_type_attribute(): string {
if ( is_search() ) {
return 'search-results';
}
switch ( $this->location() ) {
case 'product':
return 'product-details';
case 'shop':
return 'product-listing';
case 'home':
return 'mini-cart';
case 'cart-block':
case 'cart':
return 'cart';
case 'checkout-block':
case 'checkout':
case 'pay-now':
return 'checkout';
default:
return '';
}
}
/**