2024-07-18 00:27:35 +02:00
< ? php
/**
2025-03-10 23:57:08 +01:00
* Fastlane compatibility checker .
* Detects compatibility issues and generates relevant notices .
2024-07-18 00:27:35 +02:00
*
* @ package WooCommerce\PayPalCommerce\Axo\Helper
*/
2025-03-14 11:43:33 +01:00
declare ( strict_types = 1 );
2024-07-18 00:27:35 +02:00
namespace WooCommerce\PayPalCommerce\Axo\Helper ;
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector ;
2024-07-31 14:04:48 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings ;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException ;
2025-03-14 16:15:23 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Helper\CardPaymentsConfiguration ;
2024-07-18 00:27:35 +02:00
/**
2025-03-11 08:58:13 +01:00
* Class CompatibilityChecker
2025-03-14 10:57:40 +01:00
*
* DI service : 'axo.helpers.compatibility-checker'
2024-07-18 00:27:35 +02:00
*/
2025-03-10 23:57:08 +01:00
class CompatibilityChecker {
2024-09-03 15:05:23 +04:00
/**
* The list of Fastlane incompatible plugin names .
*
* @ var string []
*/
2025-02-25 16:27:33 +01:00
protected array $incompatible_plugin_names ;
2024-09-03 15:05:23 +04:00
/**
2025-03-10 23:57:08 +01:00
* Stores the result of checkout compatibility checks .
*
* @ var array
*/
protected array $checkout_compatibility ;
/**
2025-03-14 11:13:44 +01:00
* Provides details about the DCC configuration .
2025-03-10 23:57:08 +01:00
*
2025-03-14 16:15:23 +01:00
* @ var CardPaymentsConfiguration
2025-03-10 23:57:08 +01:00
*/
2025-03-14 16:15:23 +01:00
private CardPaymentsConfiguration $dcc_configuration ;
2025-03-10 23:57:08 +01:00
/**
2025-03-11 08:57:26 +01:00
* CompatibilityChecker constructor .
2024-09-03 15:05:23 +04:00
*
2025-03-14 16:15:23 +01:00
* @ param string [] $incompatible_plugin_names The list of Fastlane incompatible
* plugin names .
* @ param CardPaymentsConfiguration $dcc_configuration DCC gateway configuration .
2024-09-03 15:05:23 +04:00
*/
2025-03-14 16:15:23 +01:00
public function __construct ( array $incompatible_plugin_names , CardPaymentsConfiguration $dcc_configuration ) {
2024-09-03 15:05:23 +04:00
$this -> incompatible_plugin_names = $incompatible_plugin_names ;
2025-03-14 11:13:44 +01:00
$this -> dcc_configuration = $dcc_configuration ;
$this -> checkout_compatibility = array (
2025-03-10 23:57:08 +01:00
'has_elementor_checkout' => null ,
'has_classic_checkout' => null ,
'has_block_checkout' => null ,
);
}
/**
* Checks if the checkout uses Elementor .
*
* @ return bool Whether the checkout uses Elementor .
*/
2025-08-22 17:00:32 +02:00
protected function has_elementor_checkout () : bool {
2025-03-10 23:57:08 +01:00
if ( $this -> checkout_compatibility [ 'has_elementor_checkout' ] === null ) {
$this -> checkout_compatibility [ 'has_elementor_checkout' ] = CartCheckoutDetector :: has_elementor_checkout ();
}
return $this -> checkout_compatibility [ 'has_elementor_checkout' ];
}
/**
* Checks if the checkout uses classic checkout .
*
* @ return bool Whether the checkout uses classic checkout .
*/
2025-08-22 17:00:32 +02:00
protected function has_classic_checkout () : bool {
2025-03-10 23:57:08 +01:00
if ( $this -> checkout_compatibility [ 'has_classic_checkout' ] === null ) {
$this -> checkout_compatibility [ 'has_classic_checkout' ] = CartCheckoutDetector :: has_classic_checkout ();
}
return $this -> checkout_compatibility [ 'has_classic_checkout' ];
}
/**
* Checks if the checkout uses block checkout .
*
* @ return bool Whether the checkout uses block checkout .
*/
2025-08-22 17:00:32 +02:00
protected function has_block_checkout () : bool {
2025-03-10 23:57:08 +01:00
if ( $this -> checkout_compatibility [ 'has_block_checkout' ] === null ) {
$this -> checkout_compatibility [ 'has_block_checkout' ] = CartCheckoutDetector :: has_block_checkout ();
}
return $this -> checkout_compatibility [ 'has_block_checkout' ];
}
2024-07-31 14:04:48 +02:00
/**
* Generates the full HTML of the notification .
*
2025-03-14 11:43:33 +01:00
* @ param string $message HTML of the inner message contents .
* @ param bool $is_error Whether the provided message is an error . Affects the notice
* color .
2025-02-25 16:27:33 +01:00
* @ param bool $raw_message Whether to return raw message without HTML wrappers .
2024-07-31 14:04:48 +02:00
*
2025-02-25 16:27:33 +01:00
* @ return string The full HTML code of the notification , or an empty string , or raw message .
2024-07-31 14:04:48 +02:00
*/
2025-08-22 17:00:32 +02:00
private function render_notice ( string $message , bool $is_error = false , bool $raw_message = false ) : string {
2024-07-31 14:04:48 +02:00
if ( ! $message ) {
return '' ;
}
2025-02-25 16:27:33 +01:00
if ( $raw_message ) {
return $message ;
}
2024-07-31 14:04:48 +02:00
return sprintf (
'<div class="ppcp-notice %1$s"><p>%2$s</p></div>' ,
$is_error ? 'ppcp-notice-error' : '' ,
$message
);
}
2024-07-18 00:27:35 +02:00
2025-03-10 23:57:08 +01:00
/**
2025-03-14 11:43:33 +01:00
* Check if there aren ' t any incompatibilities that would prevent Fastlane from working
* properly .
2025-03-10 23:57:08 +01:00
*
* @ return bool Whether the setup is compatible .
*/
2025-08-22 17:00:32 +02:00
public function is_fastlane_compatible () : bool {
2025-03-10 23:57:08 +01:00
// Check for incompatible plugins.
if ( ! empty ( $this -> incompatible_plugin_names ) ) {
return false ;
}
// Check for checkout page incompatibilities.
if ( $this -> has_elementor_checkout () ) {
return false ;
}
if ( ! $this -> has_classic_checkout () && ! $this -> has_block_checkout () ) {
return false ;
}
// No incompatibilities found.
return true ;
}
2024-07-18 00:27:35 +02:00
/**
* Generates the checkout notice .
*
2025-02-25 16:27:33 +01:00
* @ param bool $raw_message Whether to return raw message without HTML wrappers .
2024-07-18 00:27:35 +02:00
* @ return string
*/
2025-08-22 17:00:32 +02:00
public function generate_checkout_notice ( bool $raw_message = false ) : string {
2025-03-10 23:57:08 +01:00
$notice_content = '' ;
// Check for checkout incompatibilities.
$has_checkout_incompatibility = $this -> has_elementor_checkout () ||
( ! $this -> has_classic_checkout () && ! $this -> has_block_checkout () );
if ( ! $has_checkout_incompatibility ) {
return '' ;
}
2024-07-18 00:27:35 +02:00
$checkout_page_link = esc_url ( get_edit_post_link ( wc_get_page_id ( 'checkout' ) ) ? ? '' );
$block_checkout_docs_link = __ (
2024-09-17 14:38:25 +02:00
'https://woocommerce.com/document/woocommerce-store-editing/customizing-cart-and-checkout/#using-the-cart-and-checkout-blocks' ,
2024-07-18 00:27:35 +02:00
'woocommerce-paypal-payments'
);
2025-03-10 23:57:08 +01:00
if ( $this -> has_elementor_checkout () ) {
2024-07-18 00:27:35 +02:00
$notice_content = sprintf (
/* translators: %1$s: URL to the Checkout edit page. %2$s: URL to the block checkout docs. */
__ (
2024-09-17 14:38:25 +02:00
'<span class="highlight">Warning:</span> The <a href="%1$s">Checkout page</a> of your store currently uses the <code>Elementor Checkout widget</code>. To enable Fastlane and accelerate payments, the page must include either the <code>Checkout</code> block, <code>Classic Checkout</code>, or the <code>[woocommerce_checkout]</code> shortcode. See <a href="%2$s">this page</a> for instructions on how to switch to the Checkout block.' ,
2024-07-18 00:27:35 +02:00
'woocommerce-paypal-payments'
),
esc_url ( $checkout_page_link ),
esc_url ( $block_checkout_docs_link )
);
2025-03-10 23:57:08 +01:00
} elseif ( ! $this -> has_classic_checkout () && ! $this -> has_block_checkout () ) {
2024-07-18 00:27:35 +02:00
$notice_content = sprintf (
/* translators: %1$s: URL to the Checkout edit page. %2$s: URL to the block checkout docs. */
__ (
2024-09-17 14:38:25 +02:00
'<span class="highlight">Warning:</span> The <a href="%1$s">Checkout page</a> of your store does not seem to be properly configured or uses an incompatible <code>third-party Checkout</code> solution. To enable Fastlane and accelerate payments, the page must include either the <code>Checkout</code> block, <code>Classic Checkout</code>, or the <code>[woocommerce_checkout]</code> shortcode. See <a href="%2$s">this page</a> for instructions on how to switch to the Checkout block.' ,
2024-07-18 00:27:35 +02:00
'woocommerce-paypal-payments'
),
esc_url ( $checkout_page_link ),
esc_url ( $block_checkout_docs_link )
);
}
2025-02-25 16:27:33 +01:00
return $this -> render_notice ( $notice_content , true , $raw_message );
2024-07-18 00:27:35 +02:00
}
2024-07-25 00:18:15 +02:00
/**
* Generates the incompatible plugins notice .
*
2025-02-25 16:27:33 +01:00
* @ param bool $raw_message Whether to return raw message without HTML wrappers .
2024-07-25 00:18:15 +02:00
* @ return string
*/
2025-08-22 17:00:32 +02:00
public function generate_incompatible_plugins_notice ( bool $raw_message = false ) : string {
2024-09-03 15:05:23 +04:00
if ( empty ( $this -> incompatible_plugin_names ) ) {
2024-07-25 00:18:15 +02:00
return '' ;
}
$plugins_settings_link = esc_url ( admin_url ( 'plugins.php' ) );
$notice_content = sprintf (
/* translators: %1$s: URL to the plugins settings page. %2$s: List of incompatible plugins. */
__ (
'<span class="highlight">Note:</span> The accelerated guest buyer experience provided by Fastlane may not be fully compatible with some of the following <a href="%1$s">active plugins</a>: <ul class="ppcp-notice-list">%2$s</ul>' ,
'woocommerce-paypal-payments'
),
$plugins_settings_link ,
2024-09-03 15:05:23 +04:00
implode ( '' , $this -> incompatible_plugin_names )
2024-07-25 00:18:15 +02:00
);
2025-02-25 16:27:33 +01:00
return $this -> render_notice ( $notice_content , false , $raw_message );
2024-07-25 00:18:15 +02:00
}
2024-07-31 14:04:48 +02:00
/**
* Generates a warning notice with instructions on conflicting plugin - internal settings .
*
2025-03-14 11:43:33 +01:00
* @ param bool $raw_message Whether to return raw message without HTML wrappers .
2024-07-31 14:04:48 +02:00
* @ return string
*/
2025-08-22 17:00:32 +02:00
public function generate_settings_conflict_notice ( bool $raw_message = false ) : string {
2025-03-14 11:13:44 +01:00
if ( $this -> dcc_configuration -> is_enabled () ) {
2025-03-10 23:57:08 +01:00
return '' ;
2024-07-31 14:04:48 +02:00
}
2025-03-10 23:57:08 +01:00
$notice_content = __ (
'<span class="highlight">Warning:</span> To enable Fastlane and accelerate payments, the <strong>Advanced Card Processing</strong> payment method must also be enabled.' ,
'woocommerce-paypal-payments'
);
2024-07-31 14:04:48 +02:00
2025-02-25 16:27:33 +01:00
return $this -> render_notice ( $notice_content , true , $raw_message );
2024-07-31 14:04:48 +02:00
}
2024-07-18 00:27:35 +02:00
}