PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.11.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.11.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 1.9.12 All 59 releases
merchant / inc / modules / countdown-timer / class-countdown-timer.php

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

302 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Countdown 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_Countdown_Timer extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'countdown-timer';
24
25 /**
26 * Module template path.
27 */
28 const MODULE_TEMPLATES = 'modules/' . self::MODULE_ID;
29
30 /**
31 * Is module preview.
32 *
33 */
34 public static $is_module_preview = false;
35
36 /**
37 * Constructor.
38 */
39 public function __construct() {
40 // Module id.
41 $this->module_id = self::MODULE_ID;
42
43 // WooCommerce only.
44 $this->wc_only = true;
45
46 // Parent construct.
47 parent::__construct();
48
49 // Module section.
50 $this->module_section = 'convert-more';
51
52 // Module default settings.
53 $this->module_default_settings = array(
54 'discount_products_only' => true,
55 'sale_ending_text' => esc_html__( 'Sale ends in', 'merchant' ),
56 'end_date' => 'evergreen',
57 'cool_off_period' => 15,
58 'min_expiration_deadline' => 2,
59 'max_expiration_deadline' => 26,
60 'sale_ending_alignment' => 'left',
61 );
62
63 // Mount preview url.
64 $preview_url = site_url( '/' );
65
66 if ( function_exists( 'wc_get_products' ) ) {
67 $products = wc_get_products( array( 'limit' => 1 ) );
68
69 if ( ! empty( $products ) && ! empty( $products[0] ) ) {
70 $preview_url = get_permalink( $products[0]->get_id() );
71 }
72 }
73
74 // Module data.
75 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
76 $this->module_data['preview_url'] = $preview_url;
77
78 // Module options path.
79 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
80
81 // Is module preview page.
82 if ( is_admin() && parent::is_module_settings_page() ) {
83 self::$is_module_preview = true;
84
85 // Enqueue admin styles.
86 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
87
88 // Enqueue admin scripts.
89 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
90
91 // Localize Script.
92 add_filter( 'merchant_admin_localize_script', array( $this, 'localize_script' ) );
93
94 // Admin preview box.
95 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
96
97 // Custom CSS.
98 // The custom CSS should be added here as well due to ensure preview box works properly.
99 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
100 }
101
102 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
103 // Init translations.
104 $this->init_translations();
105 }
106 }
107
108 /**
109 * Init translations.
110 *
111 * @return void
112 */
113 public function init_translations() {
114 $settings = $this->get_module_settings();
115 if ( ! empty( $settings['sale_ending_text'] ) ) {
116 Merchant_Translator::register_string( $settings['sale_ending_text'], esc_html__( 'Countdown Timer: Sale ending message', 'merchant' ) );
117 }
118 }
119
120 /**
121 * Admin enqueue CSS.
122 *
123 * @return void
124 */
125 public function admin_enqueue_css() {
126 if ( $this->is_module_settings_page() ) {
127 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/countdown-timer.min.css', array(), MERCHANT_VERSION );
128 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
129 }
130 }
131
132 /**
133 * Admin Enqueue scripts.
134 *
135 * @return void
136 */
137 public function admin_enqueue_scripts() {
138 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/countdown-timer.min.js', array(), MERCHANT_VERSION, true );
139 wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array(), MERCHANT_VERSION, true );
140 }
141
142 /**
143 * Localize Script.
144 */
145 public function localize_script( $data ) {
146 $data['is_admin'] = is_admin();
147
148 return $data;
149 }
150
151 /**
152 * Render admin preview
153 *
154 * @param Merchant_Admin_Preview $preview
155 * @param string $module
156 *
157 * @return Merchant_Admin_Preview
158 */
159 public function render_admin_preview( $preview, $module ) {
160 if ( self::MODULE_ID === $module ) {
161 ob_start();
162 self::admin_preview_content();
163 $content = ob_get_clean();
164
165 // HTML.
166 $preview->set_html( $content );
167
168 // Font size
169 $preview->set_css( 'digits_font_size', '.merchant-countdown-timer-countdown', '--merchant-digits-font-size', 'px' );
170 $preview->set_css( 'labels_font_size', '.merchant-countdown-timer-countdown', '--merchant-labels-font-size', 'px' );
171
172 // Sale Ending Color
173 $preview->set_css( 'sale_ending_color', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
174
175 // Digits Color
176 $preview->set_css( 'digits_color', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
177
178 // Digits Background Color
179 $preview->set_css( 'digits_background', '.merchant-countdown-timer-countdown', '--merchant-digits-background' );
180
181 // Progress Color
182 $preview->set_css( 'progress_color', '.merchant-countdown-timer-countdown', '--merchant-progress-color' );
183
184 // Labels Color
185 $preview->set_css( 'labels_color', '.merchant-countdown-timer-countdown', '--merchant-labels-color' );
186
187 // Border Color
188 $preview->set_css( 'digits_border', '.merchant-countdown-timer-countdown', '--merchant-digits-border' );
189
190 // Digits width & height
191 $preview->set_css( 'digits_width', '.merchant-countdown-timer-countdown', '--merchant-digits-width', 'px' );
192 $preview->set_css( 'digits_height', '.merchant-countdown-timer-countdown', '--merchant-digits-height', 'px' );
193
194 // Icon Color.
195 $preview->set_css( 'icon_color', '.merchant-countdown-timer svg', '--merchant-icon-color' );
196
197 // Sale Ending Text.
198 $preview->set_text( 'sale_ending_text', '.merchant-countdown-timer-text' );
199 }
200
201 return $preview;
202 }
203
204 /**
205 * Admin preview content.
206 *
207 * @return void
208 */
209 public function admin_preview_content() {
210 $settings = $this->get_module_settings();
211 ?>
212
213 <div class="mrc-preview-single-product-elements">
214 <div class="mrc-preview-left-column">
215 <div class="mrc-preview-product-image-wrapper">
216 <div class="mrc-preview-product-image"></div>
217 </div>
218 </div>
219 <div class="mrc-preview-right-column">
220 <h3 style="margin-top: 0;"><?php echo esc_html__( 'Your Product Name', 'merchant' ); ?></h3>
221 <div class="mrc-preview-rating">
222 <div class="star-rating merchant-star-rating-style2" role="img" aria-label="Rated 3.00 out of 5">
223 <span style="width: 80%"></span>
224 </div>
225 <span style="color: #969696;"><?php echo esc_html__( 'reviews', 'merchant' ); ?></span>
226 </div>
227 <h3><?php echo esc_html__( '$49', 'merchant' ); ?></h3>
228 <p><?php echo esc_html__( "An amazing product people can't refuse. What’s the next moment of value-realization when using your product? Tell the biggest use case. Briefly expand your product benefits on how this will help customers.", 'merchant' ); ?></p>
229 <?php echo wp_kses( merchant_get_template_part( self::MODULE_TEMPLATES, 'single-product', $settings, true ), merchant_kses_allowed_tags() ); ?>
230
231 <div class="merchant-preview-add-to-cart-inner">
232 <div class="merchant-preview-qty">
233 <button><?php echo esc_html( '+' ); ?></button>
234 <input type="text" value="<?php echo esc_attr( '1' ); ?>">
235 <button><?php echo esc_html( '-' ); ?></button>
236 </div>
237 <div class="merchant-preview-add-to-cart"><?php echo esc_html__( 'Add to cart', 'merchant' ); ?></div>
238 </div>
239 </div>
240 </div>
241
242 <?php
243 }
244
245 /**
246 * Custom CSS.
247 *
248 * @return string
249 */
250 public function get_module_custom_css() {
251 $css = '';
252
253 // Font sizes
254 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_font_size', 16, '.merchant-countdown-timer-countdown', '--merchant-digits-font-size', 'px' );
255 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'labels_font_size', 16, '.merchant-countdown-timer-countdown', '--merchant-labels-font-size', 'px' );
256
257 // Sale Ending Color.
258 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'sale_ending_color', '#626262', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
259
260 // Digits Color.
261 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_color', '#444444', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
262
263 // Digits Background Color.
264 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_background', '#fff', '.merchant-countdown-timer-countdown', '--merchant-digits-background' );
265
266 // Progress Color.
267 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'progress_color', '#3858E9', 'body', '--merchant-progress-color' );
268
269 // Labels Color.
270 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'labels_color', '#444444', '.merchant-countdown-timer-countdown', '--merchant-labels-color' );
271
272 // Border Color.
273 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_border', '#444444', '.merchant-countdown-timer-countdown', '--merchant-digits-border' );
274
275 // Digits width & height
276 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_width', 80, '.merchant-countdown-timer-countdown', '--merchant-digits-width', 'px' );
277 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_height', 80, '.merchant-countdown-timer-countdown', '--merchant-digits-height', 'px' );
278
279 // Icon Color.
280 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'icon_color', '#626262', '.merchant-countdown-timer svg', '--merchant-icon-color' );
281
282 return $css;
283 }
284
285 /**
286 * Admin custom CSS.
287 *
288 * @param string $css The custom CSS.
289 *
290 * @return string $css The custom CSS.
291 */
292 public function admin_custom_css( $css ) {
293 $css .= $this->get_module_custom_css();
294
295 return $css;
296 }
297 }
298
299 // Initialize the module.
300 add_action( 'init', function () {
301 Merchant_Modules::create_module( new Merchant_Countdown_Timer() );
302 } );