PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.0
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / modules / cart-reserved-timer / class-cart-reserved-timer.php

class-cart-reserved-timer.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.0, at inc/modules/cart-reserved-timer/class-cart-reserved-timer.php

269 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Initialize the module's option group definitions.
36 *
37 * @return array<int, array<string, mixed>> Array of option group arrays.
38 */
39 protected function init_option_groups() {
40 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
41 }
42
43 /**
44 * Constructor.
45 *
46 */
47 public function __construct() {
48 // Module id.
49 $this->module_id = self::MODULE_ID;
50
51 // WooCommerce only.
52 $this->wc_only = true;
53
54 // Parent construct.
55 parent::__construct();
56
57 // Module section.
58 $this->module_section = 'reduce-abandonment';
59
60 // Module default settings.
61 $this->module_default_settings = array(
62 'time_expires' => 'clear-cart',
63 'duration' => 10,
64 'reserved_message' => __( 'An item in your cart is in high demand.', 'merchant' ),
65 'timer_message_minutes' => __( 'Your cart is saved for {timer} minutes!', 'merchant' ),
66 'timer_message_seconds' => __( 'Your cart is saved for {timer} seconds!', 'merchant' ),
67 'icon' => 'fire',
68 );
69
70 // Module data.
71 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
72
73 // AI prompt examples.
74 $this->ai_examples = array(
75 'blurb' => esc_html__( 'See what AI can do for your cart reservation timer, from setting how long items stay reserved to choosing whether the cart clears when time runs out.', 'merchant' ),
76 'prompts' => array(
77 esc_html__( 'Explore the abilities WPVibe gives you access to, then set the cart reservation timer to reserve items for 15 minutes', 'merchant' ),
78 esc_html__( 'Check what abilities you have available through WPVibe, then have the cart reservation timer clear the cart when it expires instead of just hiding it', 'merchant' ),
79 esc_html__( "Look at your available WPVibe abilities, then change the reserved-cart message to 'Only a few left — your items are held for a limited time'", 'merchant' ),
80 esc_html__( 'See what abilities WPVibe exposes, then switch the cart reservation timer\'s icon to the hourglass and set its background color to a soft red', 'merchant' ),
81 ),
82 );
83
84 // Module options path.
85 $this->module_options_path = self::MODULE_DIR . "/admin/options.php";
86
87 // Enqueue admin styles.
88 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
89
90 // Add preview box
91 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
92
93 // Only applies if module is active or module is not active but admin only
94 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() || Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
95 // Custom CSS.
96 add_filter( 'merchant_custom_css', array( $this, 'custom_css' ), 10, 2 );
97 }
98
99 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
100 // Init translations.
101 $this->init_translations();
102 }
103 }
104
105 /**
106 * Init translations.
107 *
108 * @return void
109 */
110 public function init_translations() {
111 $settings = $this->get_module_settings();
112 if ( ! empty( $settings['reserved_message'] ) ) {
113 Merchant_Translator::register_string( $settings['reserved_message'], esc_html__( 'Cart Reserved Timer: Cart reserved message', 'merchant' ) );
114 }
115 if ( ! empty( $settings['timer_message_minutes'] ) ) {
116 Merchant_Translator::register_string( $settings['timer_message_minutes'], esc_html__( 'Cart Reserved Timer: Timer message for > 1 min', 'merchant' ) );
117 }
118 if ( ! empty( $settings['timer_message_seconds'] ) ) {
119 Merchant_Translator::register_string( $settings['timer_message_seconds'], esc_html__( 'Cart Reserved Timer: Timer message for < 1 min', 'merchant' ) );
120 }
121 }
122
123 /**
124 * Enqueue admin styles
125 *
126 * @return void
127 */
128 public function enqueue_admin_styles() {
129 if ( $this->is_module_settings_page() ) {
130 // Module styling.
131 wp_enqueue_style(
132 'merchant-' . self::MODULE_ID,
133 MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/' . self::MODULE_ID . '.min.css',
134 array(),
135 MERCHANT_VERSION
136 );
137
138 // Preview-specific styling.
139 wp_enqueue_style(
140 'merchant-preview-' . self::MODULE_ID,
141 MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css',
142 array(),
143 MERCHANT_VERSION
144 );
145 }
146 }
147
148 /**
149 * Get the icon.
150 *
151 * @param $icon
152 *
153 * @return string
154 */
155 public function get_icon( $icon ) {
156 $path = MERCHANT_URI . 'assets/images/icons/' . Merchant_Cart_Reserved_timer::MODULE_ID;
157
158 return array(
159 'none' => $path . '/cancel.svg',
160 'fire' => $path . '/fire.svg',
161 'clock' => $path . '/clock.svg',
162 'hour-glass' => $path . '/hour-glass.svg',
163 )[ $icon ];
164 }
165
166 /**
167 * Filtered module settings.
168 *
169 * @return array
170 */
171 public function get_settings() {
172 $settings = $this->get_module_settings();
173
174 // Replace {timer} with duration in these settings.
175 foreach ( array( 'timer_message_minutes', 'timer_message_seconds' ) as $setting ) {
176 $settings[ $setting ] = str_replace(
177 '{timer}',
178 '<span>' . $settings['duration'] . ':00</span>',
179 $settings[ $setting ]
180 );
181 }
182
183 // Get the icon attributes.
184 $settings['icon'] = array(
185 'src' => $this->get_icon( $settings['icon'] ),
186 'alt' => __( 'Cart Reserved Timer Icon', 'merchant' ),
187 );
188
189 return $settings;
190 }
191
192 /**
193 * Render admin preview
194 *
195 * @param Merchant_Admin_Preview $preview
196 * @param string $module
197 *
198 * @return Merchant_Admin_Preview
199 */
200 public function render_admin_preview( $preview, $module ) {
201 if ( $module === self::MODULE_ID ) {
202 // HTML.
203 $preview->set_html( $this->admin_preview_content() );
204
205 // Background Color.
206 $preview->set_css( 'background_color', '.merchant-cart-reserved-timer', '--merchant-bg-color' );
207
208 // Reserved Message Text.
209 $preview->set_text( 'reserved_message', '.merchant-cart-reserved-timer-content-title' );
210
211 // Timer Message Text.
212 $preview->set_text( 'timer_message_minutes', '.merchant-cart-reserved-timer-content-desc', array(
213 array(
214 '{timer}',
215 ),
216 array(
217 array(
218 'setting' => 'duration',
219 'format' => '<span>{string}:00</span>',
220 ),
221 ),
222 ) );
223
224 // Select Icon.
225 $preview->set_icon( 'icon', '.merchant-cart-reserved-timer-icon img' );
226
227 // Trigger Update When Duration Is Changed.
228 $preview->trigger_update( 'duration' );
229 }
230
231 return $preview;
232 }
233
234 /**
235 * Admin preview content.
236 *
237 * @return string
238 */
239 public function admin_preview_content() {
240 // Template arguments
241 $args = array_merge( $this->get_settings(), array(
242 'css' => '',
243 ) );
244
245 // Preview template.
246 return merchant_get_template_part( self::MODULE_TEMPLATES, 'cart', $args, true );
247 }
248
249 /**
250 * Custom CSS.
251 *
252 * @param string $css
253 * @param Merchant_Custom_CSS $custom_css
254 *
255 * @return string
256 */
257 public function custom_css( $css, $custom_css ) {
258 // Background Color.
259 $css .= $custom_css->get_variable_css( $this->module_id, 'background_color', '#f4f6f8', '.merchant-cart-reserved-timer', '--merchant-bg-color' );
260
261 return $css;
262 }
263 }
264
265 // Initialize the module.
266 add_action( 'init', function () {
267 Merchant_Modules::create_module( new Merchant_Cart_Reserved_Timer() );
268 } );
269