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

243 lines 7.1 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 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['sale_ending_text'] ) ) {
113 Merchant_Translator::register_string( $settings['sale_ending_text'], esc_html__( 'Countdown Timer: Sale ending message', 'merchant' ) );
114 }
115 }
116
117 /**
118 * Admin enqueue CSS.
119 *
120 * @return void
121 */
122 public function admin_enqueue_css() {
123 if ( $this->is_module_settings_page() ) {
124 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/countdown-timer.min.css', array(), MERCHANT_VERSION );
125 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
126 }
127 }
128
129 /**
130 * Admin Enqueue scripts.
131 *
132 * @return void
133 */
134 public function admin_enqueue_scripts() {
135 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/countdown-timer.min.js', array(), MERCHANT_VERSION, true );
136 wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array(), MERCHANT_VERSION, true );
137 }
138
139 /**
140 * Render admin preview
141 *
142 * @param Merchant_Admin_Preview $preview
143 * @param string $module
144 *
145 * @return Merchant_Admin_Preview
146 */
147 public function render_admin_preview( $preview, $module ) {
148 if ( self::MODULE_ID === $module ) {
149 ob_start();
150 self::admin_preview_content();
151 $content = ob_get_clean();
152
153 // HTML.
154 $preview->set_html( $content );
155
156 // Sale Ending Color
157 $preview->set_css( 'sale_ending_color', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
158
159 // Digits Color
160 $preview->set_css( 'digits_color', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
161
162 // Icon Color.
163 $preview->set_css( 'icon_color', '.merchant-countdown-timer svg', '--merchant-icon-color' );
164
165 // Sale Ending Text.
166 $preview->set_text( 'sale_ending_text', '.merchant-countdown-timer-text' );
167 }
168
169 return $preview;
170 }
171
172 /**
173 * Admin preview content.
174 *
175 * @return void
176 */
177 public function admin_preview_content() {
178 $settings = $this->get_module_settings();
179 ?>
180
181 <div class="mrc-preview-single-product-elements">
182 <div class="mrc-preview-left-column">
183 <div class="mrc-preview-product-image-wrapper">
184 <div class="mrc-preview-product-image"></div>
185 <div class="mrc-preview-product-image-thumbs">
186 <div class="mrc-preview-product-image-thumb"></div>
187 <div class="mrc-preview-product-image-thumb"></div>
188 <div class="mrc-preview-product-image-thumb"></div>
189 </div>
190 </div>
191 </div>
192 <div class="mrc-preview-right-column">
193 <div class="mrc-preview-text-placeholder"></div>
194 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
195 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
196 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
197 <?php echo wp_kses( merchant_get_template_part( self::MODULE_TEMPLATES, 'single-product', $settings, true ), merchant_kses_allowed_tags() ); ?>
198 <div class="mrc-preview-addtocart-placeholder"></div>
199 </div>
200 </div>
201
202 <?php
203 }
204
205 /**
206 * Custom CSS.
207 *
208 * @return string
209 */
210 public function get_module_custom_css() {
211 $css = '';
212
213 // Sale Ending Color.
214 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'sale_ending_color', '#626262', '.merchant-countdown-timer-text', '--merchant-sale-ending-color' );
215
216 // Digits Color.
217 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'digits_color', '#444444', '.merchant-countdown-timer-countdown', '--merchant-digits-color' );
218
219 // Icon Color.
220 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'icon_color', '#626262', '.merchant-countdown-timer svg', '--merchant-icon-color' );
221
222 return $css;
223 }
224
225 /**
226 * Admin custom CSS.
227 *
228 * @param string $css The custom CSS.
229 *
230 * @return string $css The custom CSS.
231 */
232 public function admin_custom_css( $css ) {
233 $css .= $this->get_module_custom_css();
234
235 return $css;
236 }
237 }
238
239 // Initialize the module.
240 add_action( 'init', function () {
241 Merchant_Modules::create_module( new Merchant_Countdown_Timer() );
242 } );
243