PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.9.11
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.9.11
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 / helpers.php

helpers.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.9.11, at inc/helpers.php

604 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Helper functions.
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Allowed tags general.
15 * Should be used to escape complex outputs like entire features html.
16 *
17 * @param array $extra
18 * @param bool $include_post_tags Whether to include post kses allowed tags or not. Default true.
19 *
20 * @return array $allowed_tags
21 */
22 if ( ! function_exists( 'merchant_kses_allowed_tags' ) ) {
23 function merchant_kses_allowed_tags( $extra = array(), $include_post_tags = true ) {
24 // Default
25 $allowed_tags = array_merge(
26 array(
27 // Meta
28 'meta' => array(
29 'name' => true,
30 'content' => true,
31 ),
32
33 // SVG Support
34 'svg' => array(
35 'class' => true,
36 'xmlns' => true,
37 'width' => true,
38 'height' => true,
39 'viewbox' => true,
40 'aria-hidden' => true,
41 'role' => true,
42 'focusable' => true,
43 'fill' => true,
44 'stroke' => true,
45 'stroke-linecap' => true,
46 'stroke-linejoin' => true,
47 'stroke-width' => true,
48 ),
49 'g' => array(
50 'id' => true,
51 'class' => true,
52 'clip-path' => true,
53 'style' => true,
54 'transform' => true,
55 ),
56 'path' => array(
57 'fill' => true,
58 'fill-rule' => true,
59 'd' => true,
60 'transform' => true,
61 'stroke' => true,
62 'stroke-width' => true,
63 'stroke-linejoin' => true,
64 'clip-rule' => true,
65 ),
66 'polyline' => array(
67 'points' => true,
68 'fill' => true,
69 'fill-rule' => true,
70 'd' => true,
71 'transform' => true,
72 'stroke' => true,
73 'stroke-width' => true,
74 'stroke-linejoin' => true,
75 ),
76 'polygon' => array(
77 'fill' => true,
78 'fill-rule' => true,
79 'points' => true,
80 'transform' => true,
81 'focusable' => true,
82 'stroke' => true,
83 'stroke-width' => true,
84 ),
85 'rect' => array(
86 'x' => true,
87 'y' => true,
88 'rx' => true,
89 'width' => true,
90 'height' => true,
91 'transform' => true,
92 'fill' => true,
93 'stroke' => true,
94 'stroke-width' => true,
95 ),
96 'circle' => array(
97 'cx' => true,
98 'cy' => true,
99 'r' => true,
100 'width' => true,
101 'height' => true,
102 'transform' => true,
103 'fill' => true,
104 'stroke' => true,
105 'stroke-width' => true,
106 'stroke-linecap' => true,
107 'stroke-linejoin' => true,
108 ),
109 'line' => array(
110 'x1' => true,
111 'y1' => true,
112 'x2' => true,
113 'y2' => true,
114 'width' => true,
115 'height' => true,
116 'transform' => true,
117 'fill' => true,
118 'stroke' => true,
119 'stroke-width' => true,
120 'stroke-linecap' => true,
121 'stroke-linejoin' => true,
122 ),
123 'clipPath' => array(
124 'id' => true,
125 'class' => true,
126 'style' => true,
127 ),
128 'defs' => array(
129 'id' => true,
130 ),
131 'progress' => array(
132 'id' => true,
133 'class' => true,
134 'value' => true,
135 'max' => true,
136 ),
137 ),
138 $include_post_tags ? wp_kses_allowed_html( 'post' ) : array()
139 );
140
141 // Include schema markup tags
142 if ( in_array( 'schema_markup', $extra, true ) || in_array( 'all', $extra, true ) ) {
143 $tags = array( 'meta', 'nav', 'ul', 'li', 'a', 'span' );
144
145 foreach ( $tags as $tag ) {
146 if ( isset( $allowed_tags[ $tag ] ) ) {
147 $allowed_tags[ $tag ]['itemprop'] = true;
148 $allowed_tags[ $tag ]['itemscope'] = true;
149 $allowed_tags[ $tag ]['itemtype'] = true;
150 } else {
151 $allowed_tags[ $tag ] = array(
152 'itemprop' => true,
153 'itemscope' => true,
154 'itemtype' => true,
155 );
156 }
157 }
158 }
159
160 // Include iframe tags
161 if ( in_array( 'iframe', $extra, true ) || in_array( 'all', $extra, true ) ) {
162 $allowed_tags['iframe'] = array(
163 'src' => true,
164 'height' => true,
165 'width' => true,
166 'frameborder' => true,
167 'allowfullscreen' => true,
168 );
169 }
170
171 if ( in_array( 'wc_email', $extra, true ) || in_array( 'all', $extra, true ) ) {
172 $allowed_tags['body'] = array(
173 'class' => true,
174 'style' => true,
175 'leftmargin' => true,
176 'marginwidth' => true,
177 'topmargin' => true,
178 'marginheight' => true,
179 'offset' => true,
180 );
181
182 $allowed_tags['html'] = array(
183 'lang' => true,
184 );
185
186 $allowed_tags['style'] = array(
187 'type' => true,
188 );
189 $allowed_tags['head'] = array();
190 $allowed_tags['meta']['http-equiv'] = true;
191 }
192
193 // Include nonce tags
194 if ( in_array( 'nonce', $extra, true ) || in_array( 'all', $extra, true ) ) {
195 $allowed_tags['input'] = array(
196 'type' => true,
197 'id' => true,
198 'name' => true,
199 'value' => true,
200 'data-name' => true,
201 );
202 }
203
204 // Include bdi tag
205 if ( in_array( 'bdi', $extra, true ) || in_array( 'all', $extra, true ) ) {
206 $allowed_tags['bdi'] = array(
207 'class' => true,
208 'id' => true,
209 'style' => true,
210 );
211 }
212
213 // Include select2
214 if ( in_array( 'select2', $extra, true ) || in_array( 'all', $extra, true ) ) {
215 $allowed_tags['select'] = array(
216 'name' => true,
217 'class' => true,
218 'id' => true,
219 'style' => true,
220 'data-name' => true,
221 'data-source' => true,
222 'multiple' => true,
223 );
224
225 $allowed_tags['option'] = array(
226 'value' => true,
227 'selected' => true,
228 );
229
230 $allowed_tags['optgroup'] = array(
231 'label' => true,
232 );
233 }
234
235 // Include dd, dt tags
236 if ( in_array( 'dd', $extra, true ) || in_array( 'all', $extra, true ) ) {
237 $allowed_tags['dd'] = array(
238 'class' => true,
239 'id' => true,
240 'style' => true,
241 );
242
243 $allowed_tags['dt'] = array(
244 'class' => true,
245 'id' => true,
246 'style' => true,
247 );
248 }
249
250 // Include forms tags.
251 if ( in_array( 'forms', $extra, true ) || in_array( 'all', $extra, true ) ) {
252 $tags = array( 'form', 'input', 'select', 'option', 'textarea' );
253
254 foreach ( $tags as $tag ) {
255 $allowed_tags[ $tag ] = array(
256 'id' => true,
257 'class' => true,
258 'style' => true,
259 'name' => true,
260 'value' => true,
261 'type' => true,
262 'placeholder' => true,
263 'data' => '*',
264 'data-source' => true,
265 'data-product_id' => true,
266 'data-product_variations' => true,
267 'data-attribute_name' => true,
268 'data-show_option_none' => true,
269 'data-name' => true,
270 'data-allowed-types' => true,
271 'step' => true,
272 'min' => true,
273 'max' => true,
274 'selected' => true,
275 'checked' => true,
276 'onchange' => true,
277 'autocomplete' => true,
278 'required' => true,
279 'action' => true,
280 'method' => true,
281 'enctype' => true,
282 'size' => true,
283 'role' => true,
284 'inputmode' => true,
285 'aria-label' => true,
286 'multiple' => true,
287 );
288 }
289 }
290
291 /**
292 * Filters the allowed tags.
293 *
294 * @since 1.2.5
295 */
296 return apply_filters( 'merchant_kses_allowed_tags', $allowed_tags );
297 }
298 }
299
300 /**
301 * Allowed tags for scripts.
302 *
303 * @return array $allowed_tags
304 */
305 if ( ! function_exists( 'merchant_kses_allowed_tags_for_code_snippets' ) ) {
306 function merchant_kses_allowed_tags_for_code_snippets() {
307 return array(
308 'script' => array(
309 'type' => true,
310 ),
311 );
312 }
313 }
314
315 /**
316 * Check if WooCommerce checkout page is being rendered by block.
317 * Since WooCommerce 8.3.0 the checkout page is rendered by block.
318 *
319 * @return bool
320 */
321 if ( ! function_exists( 'merchant_is_checkout_block_layout' ) ) {
322 function merchant_is_checkout_block_layout() {
323 $checkout_page = wc_get_page_id( 'checkout' );
324
325 if ( empty( $checkout_page ) ) {
326 return false;
327 }
328
329 if ( function_exists( 'has_blocks' ) && has_blocks( $checkout_page ) ) {
330 $post = get_post( $checkout_page );
331 $blocks = parse_blocks( $post->post_content );
332
333 foreach ( $blocks as $block ) {
334 if ( 'woocommerce/checkout' === $block['blockName'] ) {
335 return true;
336 }
337 }
338 }
339
340 return false;
341 }
342 }
343
344 /**
345 * Check if WooCommerce cart page is being rendered by block.
346 * Since WooCommerce 8.3.0 the cart page is rendered by block.
347 *
348 * @return bool
349 */
350 if ( ! function_exists( 'merchant_is_cart_block_layout' ) ) {
351 function merchant_is_cart_block_layout() {
352 $cart_page = wc_get_page_id( 'cart' );
353
354 if ( empty( $cart_page ) ) {
355 return false;
356 }
357
358 if ( function_exists( 'has_blocks' ) && has_blocks( $cart_page ) ) {
359 $post = get_post( $cart_page );
360 $blocks = parse_blocks( $post->post_content );
361
362 foreach ( $blocks as $block ) {
363 if ( 'woocommerce/cart' === $block['blockName'] ) {
364 return true;
365 }
366 }
367 }
368
369 return false;
370 }
371 }
372
373 /**
374 * Get the product categories.
375 */
376 if ( ! function_exists( 'merchant_get_product_categories' ) ) {
377 function merchant_get_product_categories() {
378 $product_categories = get_terms( array(
379 'taxonomy' => 'product_cat',
380 'hide_empty' => false,
381 ) );
382 $category_names = array();
383 if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
384 foreach ( $product_categories as $category ) {
385 $category_names[ $category->slug ] = $category->name;
386 }
387 }
388
389 return $category_names;
390 }
391 }
392
393 /**
394 * Get the product tags.
395 */
396 if ( ! function_exists( 'merchant_get_product_tags' ) ) {
397 function merchant_get_product_tags() {
398 $product_tags = get_terms( array(
399 'taxonomy' => 'product_tag',
400 'hide_empty' => false,
401 ) );
402
403 $tag_names = array();
404
405 if ( ! empty( $product_tags ) && ! is_wp_error( $product_tags ) ) {
406 foreach ( $product_tags as $tag ) {
407 $tag_names[ $tag->slug ] = $tag->name;
408 }
409 }
410
411 return $tag_names;
412 }
413 }
414
415 /**
416 * Convert array to css.
417 */
418 if ( ! function_exists( 'merchant_array_to_css' ) ) {
419 function merchant_array_to_css( $css_array ) {
420 $css = '';
421 if ( empty( $css_array ) ) {
422 return $css;
423 }
424 foreach ( $css_array as $key => $value ) {
425 $css .= $key . ':' . $value . ';';
426 }
427
428 return $css;
429 }
430 }
431
432 /**
433 * Get the share link data structure.
434 *
435 * @return array
436 */
437 if ( ! function_exists( 'merchant_get_share_link_data' ) ) {
438 function merchant_get_share_link_data() {
439 return array(
440 'facebook' => array(
441 'url' => 'https://www.facebook.com/sharer.php?u={{url}}',
442 'title' => __( 'Facebook', 'merchant' ),
443 ),
444 'twitter' => array(
445 'url' => 'https://twitter.com/intent/tweet?url={{url}}&text={{title}}',
446 'title' => __( 'X', 'merchant' ),
447 ),
448 'linkedin' => array(
449 'url' => 'https://www.linkedin.com/sharing/share-offsite/?url={{url}}',
450 'title' => __( 'LinkedIn', 'merchant' ),
451 ),
452 'reddit' => array(
453 'url' => 'https://reddit.com/submit?url={{url}}&title={{title}}',
454 'title' => __( 'Reddit', 'merchant' ),
455 ),
456 'whatsapp' => array(
457 'url' => 'https://api.whatsapp.com/send/?text={{url}}',
458 'title' => __( 'WhatsApp', 'merchant' ),
459 ),
460 'pinterest' => array(
461 'url' => 'http://pinterest.com/pin/create/link/?url={{url}}',
462 'title' => __( 'Pinterest', 'merchant' ),
463 ),
464 'telegram' => array(
465 'url' => 'https://t.me/share/url?url={{url}}&text={{title}}',
466 'title' => __( 'Telegram', 'merchant' ),
467 ),
468 'weibo' => array(
469 'url' => 'http://service.weibo.com/share/share.php?url={{url}}&appkey=&title={{title}}&pic=&ralateUid=',
470 'title' => __( 'Weibo', 'merchant' ),
471 ),
472 'vk' => array(
473 'url' => 'http://vk.com/share.php?url={{url}}&title={{title}}&comment={text}',
474 'title' => __( 'VK', 'merchant' ),
475 ),
476 'ok' => array(
477 'url' => 'https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl={{url}}',
478 'title' => __( 'OK', 'merchant' ),
479 ),
480 'xing' => array(
481 'url' => 'https://www.xing.com/spi/shares/new?url={{url}}',
482 'title' => __( 'Xing', 'merchant' ),
483 ),
484 'mail' => array(
485 'url' => 'mailto:?subject={{title}}&body={{url}}',
486 'title' => __( 'Mail', 'merchant' ),
487 ),
488 );
489 }
490 }
491
492 /**
493 * Get the share link url.
494 *
495 * @param string $social_network
496 *
497 * @return string
498 */
499 if ( ! function_exists( 'merchant_get_share_link_url' ) ) {
500 function merchant_get_share_link_url( $social_network, $url_to_share, $title_to_share = '' ) {
501 $share_link_data = merchant_get_share_link_data();
502
503 if ( ! isset( $share_link_data[ $social_network ] ) ) {
504 return '';
505 }
506
507 $share_link_url = str_replace(
508 array( '{{url}}', '{{title}}' ),
509 array( $url_to_share, $title_to_share ),
510 $share_link_data[ $social_network ]['url']
511 );
512
513 return $share_link_url;
514 }
515 }
516
517 /**
518 * Get the share link title.
519 *
520 * @param string $social_network
521 *
522 * @return string
523 */
524 if ( ! function_exists( 'merchant_get_share_link_title' ) ) {
525 function merchant_get_share_link_title( $social_network ) {
526 $share_link_data = merchant_get_share_link_data();
527
528 return isset( $share_link_data[ $social_network ] ) ? $share_link_data[ $social_network ]['title'] : '';
529 }
530 }
531
532 if ( ! function_exists( 'merchant_timezone' ) ) {
533 /**
534 * Get the WP timezone.
535 *
536 * @return string
537 */
538 function merchant_timezone() {
539 /**
540 * Filter the storewide sale timezone.
541 *
542 * @param string $timezone
543 *
544 * @since 1.9.9
545 */
546 return apply_filters(
547 'merchant_storewide_sale_timezone',
548 wp_timezone_string()
549 );
550 }
551 }
552
553 if ( ! function_exists( 'merchant_get_current_timestamp' ) ) {
554 /**
555 * Get the current timestamp.
556 *
557 * @return int|string
558 */
559 function merchant_get_current_timestamp() {
560 $timezone = new DateTimeZone( merchant_timezone() );
561
562 // Get the timestamp
563 return ( new DateTime( 'now', $timezone ) )->getTimestamp();
564 }
565 }
566
567 if ( ! function_exists( 'merchant_convert_date_to_timestamp' ) ) {
568 /**
569 * Convert date to timestamp.
570 *
571 * @param string $date The date to convert
572 * @param string $format The format of the date
573 *
574 * @return int The timestamp
575 */
576 function merchant_convert_date_to_timestamp( $date, $format = 'm-d-Y h:i A' ) {
577 $timezone = new DateTimeZone( merchant_timezone() );
578 $date_object = DateTime::createFromFormat( $format, $date, $timezone ); // Create DateTime object with specified format and timezone
579 if ( false === $date_object ) {
580 return 0;
581 }
582
583 return $date_object->getTimestamp(); // Output the timestamp
584 }
585 }
586
587 /**
588 * Parses a list of product IDs
589 * @return array
590 */
591 if ( ! function_exists( 'merchant_parse_product_ids' ) ) {
592 function merchant_parse_product_ids( $product_ids ) {
593 $parsed_ids = $product_ids ?? array();
594
595 // Convert to array if it's not already an array
596 $parsed_ids = ! is_array( $parsed_ids ) ? explode( ',', $parsed_ids ) : $parsed_ids;
597
598 // Convert all elements to integers
599 $parsed_ids = array_map( 'intval', $parsed_ids );
600
601 return $parsed_ids;
602 }
603 }
604