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

226 lines 6.6 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 // Admin preview box.
92 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
93
94 // Custom CSS.
95 // The custom CSS should be added here as well due to ensure preview box works properly.
96 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
97 }
98 }
99
100 /**
101 * Admin enqueue CSS.
102 *
103 * @return void
104 */
105 public function admin_enqueue_css() {
106 if ( $this->is_module_settings_page() ) {
107 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/countdown-timer.min.css', [], MERCHANT_VERSION );
108 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
109 }
110 }
111
112 /**
113 * Admin Enqueue scripts.
114 *
115 * @return void
116 */
117 public function admin_enqueue_scripts() {
118 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/countdown-timer.min.js', array(), MERCHANT_VERSION, true );
119 wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array(), MERCHANT_VERSION, true );
120 }
121
122 /**
123 * Render admin preview
124 *
125 * @param Merchant_Admin_Preview $preview
126 * @param string $module
127 *
128 * @return Merchant_Admin_Preview
129 */
130 public function render_admin_preview( $preview, $module ) {
131 if ( self::MODULE_ID === $module ) {
132 ob_start();
133 self::admin_preview_content();
134 $content = ob_get_clean();
135
136 // HTML.
137 $preview->set_html( $content );
138
139 // Sale Ending Color
140 $preview->set_css( 'sale_ending_color', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
141
142 // Digits Color
143 $preview->set_css( 'digits_color', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
144
145 // Icon Color.
146 $preview->set_css( 'icon_color', '.merchant-countdown-timer svg', '--merchant-icon-color' );
147
148 // Sale Ending Text.
149 $preview->set_text( 'sale_ending_text', '.merchant-countdown-timer-text' );
150 }
151
152 return $preview;
153 }
154
155 /**
156 * Admin preview content.
157 *
158 * @return void
159 */
160 public function admin_preview_content() {
161 $settings = $this->get_module_settings();
162 ?>
163
164 <div class="mrc-preview-single-product-elements">
165 <div class="mrc-preview-left-column">
166 <div class="mrc-preview-product-image-wrapper">
167 <div class="mrc-preview-product-image"></div>
168 <div class="mrc-preview-product-image-thumbs">
169 <div class="mrc-preview-product-image-thumb"></div>
170 <div class="mrc-preview-product-image-thumb"></div>
171 <div class="mrc-preview-product-image-thumb"></div>
172 </div>
173 </div>
174 </div>
175 <div class="mrc-preview-right-column">
176 <div class="mrc-preview-text-placeholder"></div>
177 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
178 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
179 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
180 <?php echo wp_kses( merchant_get_template_part( self::MODULE_TEMPLATES, 'single-product', $settings, true ), merchant_kses_allowed_tags() ); ?>
181 <div class="mrc-preview-addtocart-placeholder"></div>
182 </div>
183 </div>
184
185 <?php
186 }
187
188 /**
189 * Custom CSS.
190 *
191 * @return string
192 */
193 public function get_module_custom_css() {
194 $css = '';
195
196 // Sale Ending Color.
197 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'sale_ending_color', '#626262', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
198
199 // Digits Color.
200 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_color', '#444444', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
201
202 // Icon Color.
203 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'icon_color', '#626262', '.merchant-countdown-timer svg', '--merchant-icon-color' );
204
205 return $css;
206 }
207
208 /**
209 * Admin custom CSS.
210 *
211 * @param string $css The custom CSS.
212 *
213 * @return string $css The custom CSS.
214 */
215 public function admin_custom_css( $css ) {
216 $css .= $this->get_module_custom_css();
217
218 return $css;
219 }
220 }
221
222 // Initialize the module.
223 add_action( 'init', function () {
224 Merchant_Modules::create_module( new Merchant_Countdown_Timer() );
225 } );
226