| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Cart reserved timer. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Countdown timer class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Cart_Reserved_Timer extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
*/ |
| 22 |
const MODULE_ID = 'cart-reserved-timer'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Module path. |
| 26 |
*/ |
| 27 |
const MODULE_DIR = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID; |
| 28 |
|
| 29 |
/** |
| 30 |
* Module template path. |
| 31 |
*/ |
| 32 |
const MODULE_TEMPLATES = 'modules/' . self::MODULE_ID; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
// Module id. |
| 40 |
$this->module_id = self::MODULE_ID; |
| 41 |
|
| 42 |
// WooCommerce only. |
| 43 |
$this->wc_only = true; |
| 44 |
|
| 45 |
// Parent construct. |
| 46 |
parent::__construct(); |
| 47 |
|
| 48 |
// Module section. |
| 49 |
$this->module_section = 'reduce-abandonment'; |
| 50 |
|
| 51 |
// Module default settings. |
| 52 |
$this->module_default_settings = array( |
| 53 |
'time_expires' => 'clear-cart', |
| 54 |
'duration' => 10, |
| 55 |
'reserved_message' => __( 'An item in your cart is in high demand.', 'merchant' ), |
| 56 |
'timer_message_minutes' => __( 'Your cart is saved for {timer} minutes!', 'merchant' ), |
| 57 |
'timer_message_seconds' => __( 'Your cart is saved for {timer} seconds!', 'merchant' ), |
| 58 |
'icon' => 'fire', |
| 59 |
); |
| 60 |
|
| 61 |
// Mount preview url. |
| 62 |
$preview_url = site_url( '/' ); |
| 63 |
|
| 64 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 65 |
$preview_url = get_permalink( wc_get_page_id( 'cart' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
// Module data. |
| 69 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 70 |
$this->module_data['preview_url'] = $preview_url; |
| 71 |
|
| 72 |
// Module options path. |
| 73 |
$this->module_options_path = self::MODULE_DIR . "/admin/options.php"; |
| 74 |
|
| 75 |
// Enqueue admin styles. |
| 76 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 77 |
|
| 78 |
// Add preview box |
| 79 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 80 |
|
| 81 |
// Only applies if module is active or module is not active but admin only |
| 82 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() || Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 83 |
// Custom CSS. |
| 84 |
add_filter( 'merchant_custom_css', array( $this, 'custom_css' ), 10, 2 ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Enqueue admin styles |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function enqueue_admin_styles() { |
| 94 |
if ( $this->is_module_settings_page() ) { |
| 95 |
// Module styling. |
| 96 |
wp_enqueue_style( |
| 97 |
'merchant-' . self::MODULE_ID, |
| 98 |
MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/' . self::MODULE_ID . '.min.css', |
| 99 |
array(), |
| 100 |
MERCHANT_VERSION |
| 101 |
); |
| 102 |
|
| 103 |
// Preview-specific styling. |
| 104 |
wp_enqueue_style( |
| 105 |
'merchant-preview-' . self::MODULE_ID, |
| 106 |
MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', |
| 107 |
array(), |
| 108 |
MERCHANT_VERSION |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the icon. |
| 115 |
* |
| 116 |
* @param $icon |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function get_icon( $icon ) { |
| 121 |
$path = MERCHANT_URI . 'assets/images/icons/' . Merchant_Cart_Reserved_timer::MODULE_ID; |
| 122 |
|
| 123 |
return array( |
| 124 |
'none' => $path . '/cancel.svg', |
| 125 |
'fire' => $path . '/fire.svg', |
| 126 |
'clock' => $path . '/clock.svg', |
| 127 |
'hour-glass' => $path . '/hour-glass.svg', |
| 128 |
)[ $icon ]; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Filtered module settings. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function get_settings() { |
| 137 |
$settings = $this->get_module_settings(); |
| 138 |
|
| 139 |
// Replace {timer} with duration in these settings. |
| 140 |
foreach ( array( 'timer_message_minutes', 'timer_message_seconds' ) as $setting ) { |
| 141 |
$settings[ $setting ] = str_replace( |
| 142 |
'{timer}', |
| 143 |
'<span>' . $settings['duration'] . ':00</span>', |
| 144 |
$settings[ $setting ] |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
// Get the icon attributes. |
| 149 |
$settings['icon'] = array( |
| 150 |
'src' => $this->get_icon( $settings['icon'] ), |
| 151 |
'alt' => __( 'Cart Reserved Timer Icon', 'merchant' ) |
| 152 |
); |
| 153 |
|
| 154 |
return $settings; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Render admin preview |
| 159 |
* |
| 160 |
* @param Merchant_Admin_Preview $preview |
| 161 |
* @param string $module |
| 162 |
* |
| 163 |
* @return Merchant_Admin_Preview |
| 164 |
*/ |
| 165 |
public function render_admin_preview( $preview, $module ) { |
| 166 |
if ( $module === self::MODULE_ID ) { |
| 167 |
// HTML. |
| 168 |
$preview->set_html( $this->admin_preview_content() ); |
| 169 |
|
| 170 |
// Background Color. |
| 171 |
$preview->set_css( 'background_color', '.merchant-cart-reserved-timer', '--merchant-bg-color' ); |
| 172 |
|
| 173 |
// Reserved Message Text. |
| 174 |
$preview->set_text( 'reserved_message', '.merchant-cart-reserved-timer-content-title' ); |
| 175 |
|
| 176 |
// Timer Message Text. |
| 177 |
$preview->set_text( 'timer_message_minutes', '.merchant-cart-reserved-timer-content-desc', array( |
| 178 |
array( |
| 179 |
'{timer}', |
| 180 |
), |
| 181 |
array( |
| 182 |
array( |
| 183 |
'setting' => 'duration', |
| 184 |
'format' => '<span>{string}:00</span>', |
| 185 |
), |
| 186 |
), |
| 187 |
) ); |
| 188 |
|
| 189 |
// Select Icon. |
| 190 |
$preview->set_icon( 'icon', '.merchant-cart-reserved-timer-icon img' ); |
| 191 |
|
| 192 |
// Trigger Update When Duration Is Changed. |
| 193 |
$preview->trigger_update( 'duration' ); |
| 194 |
} |
| 195 |
|
| 196 |
return $preview; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Admin preview content. |
| 201 |
* |
| 202 |
* @return string |
| 203 |
*/ |
| 204 |
public function admin_preview_content() { |
| 205 |
// Template arguments |
| 206 |
$args = array_merge( $this->get_settings(), array( |
| 207 |
'css' => '', |
| 208 |
) ); |
| 209 |
|
| 210 |
// Preview template. |
| 211 |
return merchant_get_template_part( self::MODULE_TEMPLATES, 'cart', $args, true ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Custom CSS. |
| 216 |
* |
| 217 |
* @param string $css |
| 218 |
* @param Merchant_Custom_CSS $custom_css |
| 219 |
* |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
public function custom_css( $css, $custom_css ) { |
| 223 |
// Background Color. |
| 224 |
$css .= $custom_css->get_variable_css( $this->module_id, 'background_color', '#f4f6f8', '.merchant-cart-reserved-timer', '--merchant-bg-color' ); |
| 225 |
|
| 226 |
return $css; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// Initialize the module. |
| 231 |
add_action( 'init', function () { |
| 232 |
Merchant_Modules::create_module( new Merchant_Cart_Reserved_Timer() ); |
| 233 |
} ); |
| 234 |
|