PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.2.8
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.2.8
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.2.8, at inc/modules/cart-reserved-timer/class-cart-reserved-timer.php

249 lines 6.2 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 * 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 // Module data.
62 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
63
64 // Module options path.
65 $this->module_options_path = self::MODULE_DIR . "/admin/options.php";
66
67 // Enqueue admin styles.
68 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
69
70 // Add preview box
71 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
72
73 // Only applies if module is active or module is not active but admin only
74 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() || Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
75 // Custom CSS.
76 add_filter( 'merchant_custom_css', array( $this, 'custom_css' ), 10, 2 );
77 }
78
79 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
80 // Init translations.
81 $this->init_translations();
82 }
83 }
84
85 /**
86 * Init translations.
87 *
88 * @return void
89 */
90 public function init_translations() {
91 $settings = $this->get_module_settings();
92 if ( ! empty( $settings['reserved_message'] ) ) {
93 Merchant_Translator::register_string( $settings['reserved_message'], esc_html__( 'Cart Reserved Timer: Cart reserved message', 'merchant' ) );
94 }
95 if ( ! empty( $settings['timer_message_minutes'] ) ) {
96 Merchant_Translator::register_string( $settings['timer_message_minutes'], esc_html__( 'Cart Reserved Timer: Timer message for > 1 min', 'merchant' ) );
97 }
98 if ( ! empty( $settings['timer_message_seconds'] ) ) {
99 Merchant_Translator::register_string( $settings['timer_message_seconds'], esc_html__( 'Cart Reserved Timer: Timer message for < 1 min', 'merchant' ) );
100 }
101 }
102
103 /**
104 * Enqueue admin styles
105 *
106 * @return void
107 */
108 public function enqueue_admin_styles() {
109 if ( $this->is_module_settings_page() ) {
110 // Module styling.
111 wp_enqueue_style(
112 'merchant-' . self::MODULE_ID,
113 MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/' . self::MODULE_ID . '.min.css',
114 array(),
115 MERCHANT_VERSION
116 );
117
118 // Preview-specific styling.
119 wp_enqueue_style(
120 'merchant-preview-' . self::MODULE_ID,
121 MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css',
122 array(),
123 MERCHANT_VERSION
124 );
125 }
126 }
127
128 /**
129 * Get the icon.
130 *
131 * @param $icon
132 *
133 * @return string
134 */
135 public function get_icon( $icon ) {
136 $path = MERCHANT_URI . 'assets/images/icons/' . Merchant_Cart_Reserved_timer::MODULE_ID;
137
138 return array(
139 'none' => $path . '/cancel.svg',
140 'fire' => $path . '/fire.svg',
141 'clock' => $path . '/clock.svg',
142 'hour-glass' => $path . '/hour-glass.svg',
143 )[ $icon ];
144 }
145
146 /**
147 * Filtered module settings.
148 *
149 * @return array
150 */
151 public function get_settings() {
152 $settings = $this->get_module_settings();
153
154 // Replace {timer} with duration in these settings.
155 foreach ( array( 'timer_message_minutes', 'timer_message_seconds' ) as $setting ) {
156 $settings[ $setting ] = str_replace(
157 '{timer}',
158 '<span>' . $settings['duration'] . ':00</span>',
159 $settings[ $setting ]
160 );
161 }
162
163 // Get the icon attributes.
164 $settings['icon'] = array(
165 'src' => $this->get_icon( $settings['icon'] ),
166 'alt' => __( 'Cart Reserved Timer Icon', 'merchant' ),
167 );
168
169 return $settings;
170 }
171
172 /**
173 * Render admin preview
174 *
175 * @param Merchant_Admin_Preview $preview
176 * @param string $module
177 *
178 * @return Merchant_Admin_Preview
179 */
180 public function render_admin_preview( $preview, $module ) {
181 if ( $module === self::MODULE_ID ) {
182 // HTML.
183 $preview->set_html( $this->admin_preview_content() );
184
185 // Background Color.
186 $preview->set_css( 'background_color', '.merchant-cart-reserved-timer', '--merchant-bg-color' );
187
188 // Reserved Message Text.
189 $preview->set_text( 'reserved_message', '.merchant-cart-reserved-timer-content-title' );
190
191 // Timer Message Text.
192 $preview->set_text( 'timer_message_minutes', '.merchant-cart-reserved-timer-content-desc', array(
193 array(
194 '{timer}',
195 ),
196 array(
197 array(
198 'setting' => 'duration',
199 'format' => '<span>{string}:00</span>',
200 ),
201 ),
202 ) );
203
204 // Select Icon.
205 $preview->set_icon( 'icon', '.merchant-cart-reserved-timer-icon img' );
206
207 // Trigger Update When Duration Is Changed.
208 $preview->trigger_update( 'duration' );
209 }
210
211 return $preview;
212 }
213
214 /**
215 * Admin preview content.
216 *
217 * @return string
218 */
219 public function admin_preview_content() {
220 // Template arguments
221 $args = array_merge( $this->get_settings(), array(
222 'css' => '',
223 ) );
224
225 // Preview template.
226 return merchant_get_template_part( self::MODULE_TEMPLATES, 'cart', $args, true );
227 }
228
229 /**
230 * Custom CSS.
231 *
232 * @param string $css
233 * @param Merchant_Custom_CSS $custom_css
234 *
235 * @return string
236 */
237 public function custom_css( $css, $custom_css ) {
238 // Background Color.
239 $css .= $custom_css->get_variable_css( $this->module_id, 'background_color', '#f4f6f8', '.merchant-cart-reserved-timer', '--merchant-bg-color' );
240
241 return $css;
242 }
243 }
244
245 // Initialize the module.
246 add_action( 'init', function () {
247 Merchant_Modules::create_module( new Merchant_Cart_Reserved_Timer() );
248 } );
249