| 1 |
<?php |
| 2 |
/** |
| 3 |
* Payment Logos - WooCommerce Blocks Integration. |
| 4 |
* |
| 5 |
* Handles enqueuing and data localisation for block-based cart & checkout pages. |
| 6 |
* Mirrors the architecture used by Merchant Pro modules such as |
| 7 |
* free-shipping-progress-bar and recently-viewed-products. |
| 8 |
* |
| 9 |
* @package Merchant |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Merchant_Payment_Logos_Blocks_Integration |
| 18 |
* |
| 19 |
* @since 2.2.8 |
| 20 |
*/ |
| 21 |
class Merchant_Payment_Logos_Blocks_Integration { |
| 22 |
|
| 23 |
/** |
| 24 |
* Module ID. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
const MODULE_ID = 'payment-logos'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Singleton instance. |
| 32 |
* |
| 33 |
* @var Merchant_Payment_Logos_Blocks_Integration|null |
| 34 |
*/ |
| 35 |
private static $instance = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Private constructor — use get_instance(). |
| 39 |
*/ |
| 40 |
private function __construct() {} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get singleton instance. |
| 44 |
* |
| 45 |
* @since 2.2.8 |
| 46 |
* |
| 47 |
* @return Merchant_Payment_Logos_Blocks_Integration |
| 48 |
*/ |
| 49 |
public static function get_instance() { |
| 50 |
if ( null === self::$instance ) { |
| 51 |
self::$instance = new self(); |
| 52 |
} |
| 53 |
|
| 54 |
return self::$instance; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register hooks. |
| 59 |
* |
| 60 |
* Called from the main module class after the module-active guard. |
| 61 |
* |
| 62 |
* @since 2.2.8 |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function load_hooks() { |
| 67 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_block_assets' ) ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Enqueue the blocks JS and localise data when on a block-layout cart or checkout page. |
| 72 |
* |
| 73 |
* @since 2.2.8 |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function enqueue_block_assets() { |
| 78 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $this->should_enqueue_on_current_page() ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$is_cart = merchant_is_cart_block_layout() && is_cart(); |
| 87 |
$is_checkout = merchant_is_checkout_block_layout() && is_checkout(); |
| 88 |
|
| 89 |
// Read settings the same way the main class does. |
| 90 |
$show_on_cart = (bool) Merchant_Admin_Options::get( self::MODULE_ID, 'show_on_cart_page', 0 ); |
| 91 |
$cart_position = Merchant_Admin_Options::get( self::MODULE_ID, 'cart_position', 'after_table' ); |
| 92 |
$show_on_checkout = (bool) Merchant_Admin_Options::get( self::MODULE_ID, 'show_on_checkout_page', 0 ); |
| 93 |
$checkout_position = Merchant_Admin_Options::get( self::MODULE_ID, 'checkout_position', 'before_checkout_form' ); |
| 94 |
|
| 95 |
$cart_enabled = $is_cart && $show_on_cart; |
| 96 |
$checkout_enabled = $is_checkout && $show_on_checkout; |
| 97 |
|
| 98 |
if ( ! $cart_enabled && ! $checkout_enabled ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
wp_enqueue_script( |
| 103 |
'merchant-payment-logos-blocks', |
| 104 |
MERCHANT_URI . 'assets/js/modules/payment-logos/payment-logos-blocks.min.js', |
| 105 |
array( 'jquery' ), |
| 106 |
MERCHANT_VERSION, |
| 107 |
true |
| 108 |
); |
| 109 |
|
| 110 |
// Also load the module's frontend CSS so logos are styled in block layout. |
| 111 |
wp_enqueue_style( |
| 112 |
'merchant-payment-logos', |
| 113 |
MERCHANT_URI . 'assets/css/modules/payment-logos/payment-logos.min.css', |
| 114 |
array(), |
| 115 |
MERCHANT_VERSION |
| 116 |
); |
| 117 |
|
| 118 |
wp_localize_script( |
| 119 |
'merchant-payment-logos-blocks', |
| 120 |
'merchant_payment_logos_blocks_data', |
| 121 |
array( |
| 122 |
'cart_enabled' => $cart_enabled, |
| 123 |
'cart_position' => $cart_position, |
| 124 |
'cart_html' => $cart_enabled ? $this->get_block_html( 'merchant-payment-logos--cart' ) : '', |
| 125 |
'checkout_enabled' => $checkout_enabled, |
| 126 |
'checkout_position' => $checkout_position, |
| 127 |
'checkout_html' => $checkout_enabled ? $this->get_block_html( 'merchant-payment-logos--checkout' ) : '', |
| 128 |
) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Determine whether to enqueue block assets on the current page. |
| 134 |
* |
| 135 |
* @since 2.2.8 |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
protected function should_enqueue_on_current_page() { |
| 140 |
return ( merchant_is_cart_block_layout() && is_cart() ) |
| 141 |
|| ( merchant_is_checkout_block_layout() && is_checkout() ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Capture the rendered HTML for a given location class. |
| 146 |
* |
| 147 |
* Delegates to the main module's render method via output buffering. |
| 148 |
* |
| 149 |
* @since 2.2.8 |
| 150 |
* |
| 151 |
* @param string $location_class CSS modifier class for the logos wrapper. |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
protected function get_block_html( $location_class ) { |
| 156 |
$module = Merchant_Modules::get_module( self::MODULE_ID ); |
| 157 |
if ( ! ( $module instanceof Merchant_Payment_Logos ) ) { |
| 158 |
return ''; |
| 159 |
} |
| 160 |
|
| 161 |
return $module->render_logos_to_string( $location_class ); |
| 162 |
} |
| 163 |
} |
| 164 |
|