PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.10.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.10.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 / classes / class-merchant-custom-css.php

class-merchant-custom-css.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.10.1, at inc/classes/class-merchant-custom-css.php

413 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant_Custom_CSS Class.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 if ( ! class_exists( 'Merchant_Custom_CSS' ) ) {
11 class Merchant_Custom_CSS {
12
13 /**
14 * The single class instance.
15 */
16 private static $instance = null;
17
18 /**
19 * Instance.
20 */
21 public static function instance() {
22 if ( is_null( self::$instance ) ) {
23 self::$instance = new self();
24 }
25
26 return self::$instance;
27 }
28
29 /**
30 * Constructor.
31 */
32 public function __construct() {
33 add_action( 'wp_enqueue_scripts', array( $this, 'print_styles' ), 15 );
34 add_action( 'admin_enqueue_scripts', array( $this, 'print_styles_admin' ), 15 );
35 }
36
37 /**
38 * Print Styles.
39 */
40 public function print_styles() {
41 $css = $this->output_css();
42
43 wp_add_inline_style( 'merchant', $css );
44 }
45
46 /**
47 * Print Styles In Admin
48 */
49 public function print_styles_admin() {
50 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
51 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
52
53 if ( ! empty( $page ) && false !== strpos( $page, 'merchant' ) && ! empty( $module ) ) {
54 $css = $this->output_css();
55
56 wp_add_inline_style( 'merchant-admin', $css );
57 }
58 }
59
60 /**
61 * Output CSS.
62 */
63 public function output_css() {
64 $css = '';
65
66 // Scroll To Top Button
67 if ( Merchant_Modules::is_module_active( 'scroll-to-top-button' ) ) {
68 $css .= $this->get_variable_css( 'scroll-to-top-button', 'icon-color', '#ffffff', '.merchant-scroll-to-top-button', '--merchant-icon-color' );
69 $css .= $this->get_variable_css( 'scroll-to-top-button', 'icon-hover-color', '#ffffff', '.merchant-scroll-to-top-button', '--merchant-icon-hover-color' );
70 $css .= $this->get_variable_css( 'scroll-to-top-button', 'text-color', '#ffffff', '.merchant-scroll-to-top-button', '--merchant-text-color' );
71 $css .= $this->get_variable_css( 'scroll-to-top-button', 'text-hover-color', '#ffffff', '.merchant-scroll-to-top-button', '--merchant-text-hover-color' );
72 $css .= $this->get_variable_css( 'scroll-to-top-button', 'border-color', '#212121', '.merchant-scroll-to-top-button', '--merchant-border-color' );
73 $css .= $this->get_variable_css( 'scroll-to-top-button', 'border-hover-color', '#757575', '.merchant-scroll-to-top-button', '--merchant-border-hover-color' );
74 $css .= $this->get_variable_css( 'scroll-to-top-button', 'background-color', '#212121', '.merchant-scroll-to-top-button', '--merchant-background-color' );
75 $css .= $this->get_variable_css( 'scroll-to-top-button', 'background-hover-color', '#757575', '.merchant-scroll-to-top-button', '--merchant-background-hover-color' );
76 $css .= $this->get_variable_css( 'scroll-to-top-button', 'border-radius', 30, '.merchant-scroll-to-top-button', '--merchant-border-radius', 'px' );
77 $css .= $this->get_variable_css( 'scroll-to-top-button', 'padding', 15, '.merchant-scroll-to-top-button', '--merchant-padding', 'px' );
78 $css .= $this->get_variable_css( 'scroll-to-top-button', 'text-size', 18, '.merchant-scroll-to-top-button', '--merchant-text-size', 'px' );
79 $css .= $this->get_variable_css( 'scroll-to-top-button', 'icon-size', 18, '.merchant-scroll-to-top-button', '--merchant-icon-size', 'px' );
80 $css .= $this->get_variable_css( 'scroll-to-top-button', 'border-size', 2, '.merchant-scroll-to-top-button', '--merchant-border-size', 'px' );
81 $css .= $this->get_variable_css( 'scroll-to-top-button', 'side-offset', 30, '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' );
82 $css .= $this->get_variable_css( 'scroll-to-top-button', 'bottom-offset', 30, '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' );
83
84 // Mobile Styles
85 $css .= '@media (max-width: 768px) {';
86 $css .= $this->get_variable_css( 'scroll-to-top-button', 'side-offset-mobile', '30', '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' );
87 $css .= $this->get_variable_css( 'scroll-to-top-button', 'bottom-offset-mobile', '30', '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' );
88 $css .= '}';
89 }
90
91 // Cookie Banner
92 if ( Merchant_Modules::is_module_active( 'cookie-banner' ) ) {
93 $css .= $this->get_variable_css( 'cookie-banner', 'modal_width', 750, '.merchant-cookie-banner', '--merchant-modal-width', 'px' );
94 $css .= $this->get_variable_css( 'cookie-banner', 'modal_height', 50, '.merchant-cookie-banner', '--merchant-modal-height', 'px' );
95 $css .= $this->get_variable_css( 'cookie-banner', 'background_color', '#000000', '.merchant-cookie-banner', '--merchant-background' );
96 $css .= $this->get_variable_css( 'cookie-banner', 'text_color', '#ffffff', '.merchant-cookie-banner', '--merchant-text-color' );
97 $css .= $this->get_variable_css( 'cookie-banner', 'link_color', '#aeaeae', '.merchant-cookie-banner', '--merchant-link-color' );
98 $css .= $this->get_variable_css( 'cookie-banner', 'button_background_color', '#dddddd', '.merchant-cookie-banner', '--merchant-button-background' );
99 $css .= $this->get_variable_css( 'cookie-banner', 'button_text_color', '#222222', '.merchant-cookie-banner', '--merchant-button-text-color' );
100 }
101
102 // Real Time Search
103 if ( Merchant_Modules::is_module_active( 'real-time-search' ) ) {
104 $css .= $this->get_variable_css( 'real-time-search', 'results_box_width', 500, '.merchant-ajax-search-wrapper', '--merchant-results-box-width', 'px' );
105 }
106
107 // Code Snippets
108 if ( Merchant_Modules::is_module_active( 'code-snippets' ) ) {
109 $css .= Merchant_Option::get( 'code-snippets', 'custom_css', '' );
110 }
111
112 // Global Settings.
113 if ( ! is_admin() ) {
114 $css .= Merchant_Option::get( 'global-settings', 'custom_css', '' );
115 }
116
117 /**
118 * Hook: merchant_custom_css
119 *
120 * @since 1.0
121 */
122 $css .= apply_filters( 'merchant_custom_css', '', $this );
123
124 $css = $this->minify( $css );
125
126 return $css;
127 }
128
129 /**
130 * Get variable CSS
131 *
132 * @param string $module Module name.
133 * @param string $setting Setting name.
134 * @param string $default_val Default value.
135 * @param string $selector CSS selector.
136 * @param string $variable CSS variable.
137 * @param string $unit CSS unit.
138 * @param string $condition CSS condition.
139 *
140 */
141 public static function get_variable_css( $module, $setting = '', $default_val = null, $selector = '', $variable = '', $unit = '', $condition = '' ) {
142 $value = self::get_option( $module, $setting, $default_val );
143
144 if ( '' === $value || null === $value ) {
145 return '';
146 }
147
148 if ( ! empty( $condition ) ) {
149 switch ( $condition ) {
150 case 'pass_empty':
151 if ( empty( $value ) ) {
152 return;
153 }
154
155 break;
156 }
157 }
158
159 // Remove everything after ":"in case a selector like a:hover is used.
160 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
161 if ( ( $position = strpos( $selector, ':' ) ) !== false ) {
162 $selector = substr( $selector, 0, $position );
163 }
164
165 if ( class_exists( 'Merchant_Admin_Preview' ) ) {
166 Merchant_Admin_Preview::instance()->set_css( $setting, $selector, $variable, $unit );
167 }
168
169 return $selector . '{ ' . esc_attr( $variable ) . ':' . esc_attr( $value ) . esc_attr( $unit ) . '; }' . "\n";
170 }
171
172 /**
173 * CSS (can pass css prop and unit)
174 *
175 * @param string $module Module name.
176 * @param string $setting Setting name.
177 * @param string $default_val Default value.
178 * @param string $selector CSS selector.
179 * @param string $css_prop CSS prop.
180 * @param string $unit Unit value.
181 * @param bool $important Important rule.
182 * @param bool|string $variable Whether to auto-create a variable
183 *
184 * @return string
185 */
186 public static function get_css( $module, $setting = '', $default_val = '', $selector = '', $css_prop = '', $unit = 'px', $important = false, $variable = false ) {
187 $css_output = '';
188
189 if ( $variable === false ) {
190 $css_value = self::get_option( $module, $setting, $default_val ) . ( isset( $css['unit'] ) ? $css['unit'] : '' );
191 } else {
192 $css_variable = is_string( $variable ) ? $variable : '--merchant-' . str_replace( '_', '-', sanitize_title( $setting ) );
193 $css_value = "var({$css_variable})";
194 $css_output .= static::get_variable_css( $module, $setting, $default_val, $selector, $css_variable, $unit );
195 }
196
197 if( is_array( $css_prop ) ) {
198 foreach( $css_prop as $css ) {
199 $css_output .= $selector . '{ '. $css['prop'] .':' . esc_attr( $css_value ) . ( $important ? '!important' : '' ) . ';}' . "\n";
200 }
201 } else {
202 $css_output .= $selector . '{ '. $css_prop .':' . esc_attr( $css_value ) . ( $important ? '!important' : '' ) . ';}' . "\n";
203 }
204
205 return $css_output;
206 }
207
208 /**
209 * Get color CSS
210 *
211 * @param string $module Module name.
212 * @param string $setting Setting name.
213 * @param string $default_val Default value.
214 * @param string $selector CSS selector.
215 * @param bool $important Important rule.
216 * @param bool|string $variable Whether to auto-create a variable
217 *
218 * @return string
219 */
220 public static function get_color_css( $module, $setting = '', $default_val = '', $selector = '', $important = false, $variable = false ) {
221 return self::get_css( $module, $setting, $default_val, $selector, 'color', '', $important, $variable );
222 }
223
224 /**
225 * Get border color CSS
226 *
227 * @param string $module Module name.
228 * @param string $setting Setting name.
229 * @param string $default_val Default value.
230 * @param string $selector CSS selector.
231 * @param bool $important Important rule.
232 * @param bool|string $variable Whether to auto-create a variable
233 *
234 * @return string
235 */
236 public static function get_border_color_css( $module, $setting = '', $default_val = '', $selector = '', $important = false, $variable = false ) {
237 return self::get_css( $module, $setting, $default_val, $selector, 'border-color', '', $important, $variable );
238 }
239
240 /**
241 * Get background color CSS
242 *
243 * @param string $module Module name.
244 * @param string $setting Setting name.
245 * @param string $default_val Default value.
246 * @param string $selector CSS selector.
247 * @param bool $important Important rule.
248 * @param bool|string $variable Whether to auto-create a variable
249 *
250 * @return string
251 */
252 public static function get_background_color_css( $module, $setting = '', $default_val = '', $selector = '', $important = false, $variable = false ) {
253 return self::get_css( $module, $setting, $default_val, $selector, 'background-color', '', $important, $variable );
254 }
255
256 /**
257 * Responsive dimensions
258 *
259 * @param string $module Module name.
260 * @param string $setting Setting name.
261 * @param array $defaults Default values.
262 * @param string $selector CSS selector.
263 * @param string $css_prop CSS prop.
264 * @param bool $important Important rule.
265 *
266 * @return string
267 */
268 public static function get_responsive_dimensions_css( $module, $setting = '', $defaults = array(), $selector = '', $css_prop = '', $important = false ) {
269 $devices = array(
270 'desktop' => '@media (min-width: 992px)',
271 'tablet' => '@media (min-width: 576px) and (max-width: 991px)',
272 'mobile' => '@media (max-width: 575px)',
273 );
274
275 $css = '';
276
277 foreach ( $devices as $device => $media ) {
278 $value = self::get_option( $module, $setting, $defaults )[ $device ];
279
280 $value['top'] = ! isset( $value['top'] ) || $value['top'] === '' ? 0 : $value['top'];
281 $value['right'] = ! isset( $value['right'] ) || $value['right'] === '' ? 0 : $value['right'];
282 $value['bottom'] = ! isset( $value['bottom'] ) || $value['bottom'] === '' ? 0 : $value['bottom'];
283 $value['left'] = ! isset( $value['left'] ) || $value['left'] === '' ? 0 : $value['left'];
284
285 $css_prop_value = "{$value['top']}{$value['unit']} {$value['right']}{$value['unit']} {$value['bottom']}{$value['unit']} {$value['left']}{$value['unit']}";
286 $css .= $media . ' { ' . $selector . ' { ' . $css_prop . ':' . esc_attr( $css_prop_value ) . ( $important ? '!important' : '' ) . '; } }' . "\n";
287 }
288
289 return $css;
290 }
291
292 /**
293 * Dimensions
294 *
295 * @param string $module Module name.
296 * @param string $setting Setting name.
297 * @param string $default_val Default value.
298 * @param string $selector CSS selector.
299 * @param string $css_prop CSS prop.
300 * @param bool $important Important rule.
301 *
302 * @return string
303 */
304 public static function get_dimensions_css( $module, $setting = '', $default_val = '', $selector = '', $css_prop = '', $important = false ) {
305 $value = self::get_option( $module, $setting, $default_val );
306
307 $value['top'] = ! isset( $value['top'] ) || $value['top'] === '' ? 0 : $value['top'];
308 $value['right'] = ! isset( $value['right'] ) || $value['right'] === '' ? 0 : $value['right'];
309 $value['bottom'] = ! isset( $value['bottom'] ) || $value['bottom'] === '' ? 0 : $value['bottom'];
310 $value['left'] = ! isset( $value['left'] ) || $value['left'] === '' ? 0 : $value['left'];
311
312 $css_prop_value = "{$value['top']}{$value['unit']} {$value['right']}{$value['unit']} {$value['bottom']}{$value['unit']} {$value['left']}{$value['unit']}";
313
314 if ( is_array( $css_prop ) ) {
315 $css_output = '';
316
317 foreach ( $css_prop as $css ) {
318 $css_output .= $selector . '{ ' . $css['prop'] . ':' . esc_attr( $css_prop_value ) . ( $important ? '!important' : '' ) . ';}' . "\n";
319 }
320
321 return $css_output;
322 } else {
323 return $selector . '{ ' . $css_prop . ':' . esc_attr( $css_prop_value ) . ( $important ? '!important' : '' ) . ';}' . "\n";
324 }
325 }
326
327 /**
328 * Responsive CSS (can pass css prop and unit)
329 *
330 * @param string $module Module name.
331 * @param string $setting Setting name.
332 * @param array $defaults Default values.
333 * @param string $selector CSS selector.
334 * @param string $css_prop CSS prop.
335 * @param string $unit Unit value.
336 * @param bool $important Important rule.
337 *
338 * @return string
339 */
340 public static function get_responsive_css( $module, $setting = '', $defaults = array(), $selector = '', $css_prop = '', $unit = 'px', $important = false ) {
341 $devices = array(
342 'desktop' => '@media (min-width: 992px)',
343 'tablet' => '@media (min-width: 576px) and (max-width: 991px)',
344 'mobile' => '@media (max-width: 575px)',
345 );
346
347 $css = '';
348
349 foreach ( $devices as $device => $media ) {
350 $default = ( isset( $defaults[ $device ] ) ) ? $defaults[ $device ] : $defaults;
351
352 $setting = self::get_option( $module, $setting . '_' . $device, $default );
353
354 // Some properties need to be converted to be compatible with the respective css property
355 $type = '';
356 if ( strpos( $setting, '_visibility' ) !== false && $css_prop === 'display' ) {
357 $type = 'display';
358 }
359
360 // Check and convert value to be compatible with 'display' css property
361 if ( $css_prop === 'display' ) {
362 if ( $setting === 'hidden' ) {
363 $setting = 'none';
364 } else {
365 continue;
366 }
367 }
368
369 $css .= $media . ' { ' . $selector . ' { ' . $css_prop . ':' . esc_attr( $setting ) . ( $unit ? $unit : '' ) . ( $important ? '!important' : '' ) . '; } }' . "\n";
370 }
371
372 return $css;
373 }
374
375 /**
376 * Get option
377 *
378 * @param string $module Module name.
379 * @param string $setting Setting name.
380 * @param mixed $default_val Default value.
381 *
382 * @return mixed
383 */
384 public static function get_option( $module, $setting, $default_val ) {
385 $options = get_option( 'merchant', array() );
386
387 $value = $default_val;
388
389 if ( isset( $options[ $module ] ) && isset( $options[ $module ][ $setting ] ) ) {
390 $value = $options[ $module ][ $setting ];
391 }
392
393 return $value;
394 }
395
396 /**
397 * CSS code minification.
398 */
399 private function minify( $css ) {
400 $css = preg_replace( '/\s+/', ' ', $css );
401 $css = preg_replace( '/\/\*[^\!](.*?)\*\//', '', $css );
402 $css = preg_replace( '/(,|:|;|\{|}) /', '$1', $css );
403 $css = preg_replace( '/ (,|;|\{|})/', '$1', $css );
404 $css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css );
405 $css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css );
406
407 return trim( $css );
408 }
409 }
410
411 Merchant_Custom_CSS::instance();
412 }
413