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

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