2024-05-09 13:06:20 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Helper to detect what cart and checkout configuration is being used.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\PayPalCommerce\WcGateway\Helper
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
|
|
|
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CartCheckoutDetector
|
|
|
|
*/
|
|
|
|
class CartCheckoutDetector {
|
|
|
|
|
|
|
|
/**
|
2024-05-10 02:07:51 +02:00
|
|
|
* Returns a list of Elementor widgets if they exist for a specific page.
|
|
|
|
*
|
|
|
|
* @param int $page_id The ID of the page.
|
|
|
|
*
|
|
|
|
* @return array List of widget types if any exist, otherwise an empty array.
|
2024-05-09 13:06:20 +02:00
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
private static function get_elementor_widgets( $page_id ): array {
|
2024-05-09 13:06:20 +02:00
|
|
|
$elementor_data = get_post_meta( $page_id, '_elementor_data' );
|
|
|
|
|
2024-05-10 02:07:51 +02:00
|
|
|
if ( isset( $elementor_data[0] ) ) {
|
|
|
|
// Parse the Elementor json and find all widgets for a specific page.
|
|
|
|
$reg_exp = '/"widgetType":"([^"]*)/i';
|
2024-05-09 13:06:20 +02:00
|
|
|
$output_array = array();
|
|
|
|
|
|
|
|
if ( is_array( $elementor_data[0] ) ) {
|
2024-05-10 02:07:51 +02:00
|
|
|
$elementor_data[0] = wp_json_encode( $elementor_data[0] );
|
2024-05-09 13:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
preg_match_all( $reg_exp, $elementor_data[0], $output_array, PREG_SET_ORDER );
|
|
|
|
|
|
|
|
$widgets_list = array();
|
|
|
|
|
2024-05-10 02:07:51 +02:00
|
|
|
foreach ( $output_array as $found ) {
|
|
|
|
if ( ! isset( $found[1] ) ) {
|
2024-05-09 13:06:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$widget_key = $found[1];
|
|
|
|
|
|
|
|
$widgets_list[] = $widget_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $widgets_list;
|
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Checkout page is using Elementor.
|
|
|
|
*
|
2024-05-29 15:59:56 +02:00
|
|
|
* @param int $page_id The ID of the page.
|
|
|
|
*
|
2024-05-09 13:06:20 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-29 15:59:56 +02:00
|
|
|
public static function has_elementor_checkout( int $page_id = 0 ): bool {
|
2024-05-13 18:07:13 +02:00
|
|
|
// Check if Elementor is installed and activated.
|
2024-05-13 17:22:33 +02:00
|
|
|
if ( did_action( 'elementor/loaded' ) ) {
|
2024-05-29 15:59:56 +02:00
|
|
|
if ( $page_id ) {
|
|
|
|
$elementor_widgets = self::get_elementor_widgets( $page_id );
|
|
|
|
} else {
|
|
|
|
// Check the WooCommerce checkout page.
|
|
|
|
$elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'checkout' ) );
|
|
|
|
}
|
2024-05-09 13:06:20 +02:00
|
|
|
|
2024-05-13 17:22:33 +02:00
|
|
|
if ( $elementor_widgets ) {
|
|
|
|
return in_array( 'woocommerce-checkout-page', $elementor_widgets, true );
|
|
|
|
}
|
2024-05-09 13:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Cart page is using Elementor.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
public static function has_elementor_cart(): bool {
|
|
|
|
$elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'cart' ) );
|
2024-05-09 13:06:20 +02:00
|
|
|
|
|
|
|
if ( $elementor_widgets ) {
|
2024-05-10 02:07:51 +02:00
|
|
|
return in_array( 'woocommerce-cart-page', $elementor_widgets, true );
|
2024-05-09 13:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Checkout page is using the block checkout.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
public static function has_block_checkout(): bool {
|
2024-05-09 13:06:20 +02:00
|
|
|
$checkout_page_id = wc_get_page_id( 'checkout' );
|
|
|
|
return $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Cart page is using the block cart.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
public static function has_block_cart(): bool {
|
2024-05-09 13:06:20 +02:00
|
|
|
$cart_page_id = wc_get_page_id( 'cart' );
|
|
|
|
return $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Checkout page is using the classic checkout.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
public static function has_classic_checkout(): bool {
|
2024-05-09 13:06:20 +02:00
|
|
|
$checkout_page_id = wc_get_page_id( 'checkout' );
|
2024-05-16 19:51:47 +02:00
|
|
|
return $checkout_page_id && ( has_block( 'woocommerce/classic-shortcode', $checkout_page_id ) || self::has_classic_shortcode( $checkout_page_id, 'woocommerce_checkout' ) );
|
2024-05-09 13:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Cart page is using the classic cart.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-10 02:07:51 +02:00
|
|
|
public static function has_classic_cart(): bool {
|
2024-05-09 13:06:20 +02:00
|
|
|
$cart_page_id = wc_get_page_id( 'cart' );
|
2024-05-16 19:51:47 +02:00
|
|
|
return $cart_page_id && ( has_block( 'woocommerce/classic-shortcode', $cart_page_id ) || self::has_classic_shortcode( $cart_page_id, 'woocommerce_cart' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a page has a specific shortcode.
|
|
|
|
*
|
|
|
|
* @param int $page_id The ID of the page.
|
|
|
|
* @param string $shortcode The shortcode to check for.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function has_classic_shortcode( int $page_id, string $shortcode ): bool {
|
|
|
|
if ( ! $page_id ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = get_post( $page_id );
|
2024-05-16 22:29:26 +02:00
|
|
|
$page_content = is_object( $page ) ? $page->post_content : '';
|
2024-05-16 19:51:47 +02:00
|
|
|
|
|
|
|
return str_contains( $page_content, $shortcode );
|
2024-05-09 13:06:20 +02:00
|
|
|
}
|
|
|
|
}
|