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 / helpers.php

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

362 lines 9.3 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 ),
55 'path' => array(
56 'fill' => true,
57 'fill-rule' => true,
58 'd' => true,
59 'transform' => true,
60 'stroke' => true,
61 'stroke-width' => true,
62 'stroke-linejoin' => true,
63 'clip-rule' => true,
64 ),
65 'polyline' => array(
66 'points' => true,
67 'fill' => true,
68 'fill-rule' => true,
69 'd' => true,
70 'transform' => true,
71 'stroke' => true,
72 'stroke-width' => true,
73 'stroke-linejoin' => true,
74 ),
75 'polygon' => array(
76 'fill' => true,
77 'fill-rule' => true,
78 'points' => true,
79 'transform' => true,
80 'focusable' => true,
81 'stroke' => true,
82 'stroke-width' => true,
83 ),
84 'rect' => array(
85 'x' => true,
86 'y' => true,
87 'rx' => true,
88 'width' => true,
89 'height' => true,
90 'transform' => true,
91 'fill' => true,
92 'stroke' => true,
93 'stroke-width' => true,
94 ),
95 'circle' => array(
96 'cx' => true,
97 'cy' => true,
98 'r' => true,
99 'width' => true,
100 'height' => true,
101 'transform' => true,
102 'fill' => true,
103 'stroke' => true,
104 'stroke-width' => true,
105 ),
106 'clipPath' => array(
107 'id' => true,
108 'class' => true,
109 'style' => true,
110 ),
111 'defs' => array(
112 'id' => true,
113 ),
114 ),
115 $include_post_tags ? wp_kses_allowed_html( 'post' ) : array()
116 );
117
118 // Include schema markup tags
119 if ( in_array( 'schema_markup', $extra, true ) || in_array( 'all', $extra, true ) ) {
120 $tags = array( 'meta', 'nav', 'ul', 'li', 'a', 'span' );
121
122 foreach ( $tags as $tag ) {
123 if ( isset( $allowed_tags[ $tag ] ) ) {
124 $allowed_tags[ $tag ]['itemprop'] = true;
125 $allowed_tags[ $tag ]['itemscope'] = true;
126 $allowed_tags[ $tag ]['itemtype'] = true;
127 } else {
128 $allowed_tags[ $tag ] = array(
129 'itemprop' => true,
130 'itemscope' => true,
131 'itemtype' => true,
132 );
133 }
134 }
135 }
136
137 // Include iframe tags
138 if ( in_array( 'iframe', $extra, true ) || in_array( 'all', $extra, true ) ) {
139 $allowed_tags['iframe'] = array(
140 'src' => true,
141 'height' => true,
142 'width' => true,
143 'frameborder' => true,
144 'allowfullscreen' => true,
145 );
146 }
147
148 // Include nonce tags
149 if ( in_array( 'nonce', $extra, true ) || in_array( 'all', $extra, true ) ) {
150 $allowed_tags['input'] = array(
151 'type' => true,
152 'id' => true,
153 'name' => true,
154 'value' => true,
155 'data-name' => true,
156 );
157 }
158
159 // Include bdi tag
160 if ( in_array( 'bdi', $extra, true ) || in_array( 'all', $extra, true ) ) {
161 $allowed_tags['bdi'] = array(
162 'class' => true,
163 'id' => true,
164 'style' => true,
165 );
166 }
167
168 // Include select2
169 if ( in_array( 'select2', $extra, true ) || in_array( 'all', $extra, true ) ) {
170 $allowed_tags['select'] = array(
171 'name' => true,
172 'class' => true,
173 'id' => true,
174 'style' => true,
175 'data-name' => true,
176 'data-source' => true,
177 'multiple' => true,
178 );
179
180 $allowed_tags['option'] = array(
181 'value' => true,
182 'selected' => true,
183 );
184
185 $allowed_tags['optgroup'] = array(
186 'label' => true,
187 );
188 }
189
190 // Include dd, dt tags
191 if ( in_array( 'dd', $extra, true ) || in_array( 'all', $extra, true ) ) {
192 $allowed_tags['dd'] = array(
193 'class' => true,
194 'id' => true,
195 'style' => true,
196 );
197
198 $allowed_tags['dt'] = array(
199 'class' => true,
200 'id' => true,
201 'style' => true,
202 );
203 }
204
205 // Include forms tags.
206 if ( in_array( 'forms', $extra, true ) || in_array( 'all', $extra, true ) ) {
207 $tags = array( 'form', 'input', 'select', 'option', 'textarea' );
208
209 foreach ( $tags as $tag ) {
210 $allowed_tags[ $tag ] = array(
211 'id' => true,
212 'class' => true,
213 'style' => true,
214 'name' => true,
215 'value' => true,
216 'type' => true,
217 'placeholder' => true,
218 'data' => '*',
219 'data-source' => true,
220 'data-product_id' => true,
221 'data-product_variations' => true,
222 'data-attribute_name' => true,
223 'data-show_option_none' => true,
224 'data-name' => true,
225 'step' => true,
226 'min' => true,
227 'max' => true,
228 'selected' => true,
229 'checked' => true,
230 'onchange' => true,
231 'autocomplete' => true,
232 'required' => true,
233 'action' => true,
234 'method' => true,
235 'enctype' => true,
236 'size' => true,
237 'role' => true,
238 'inputmode' => true,
239 'aria-label' => true,
240 'multiple' => true,
241 );
242 }
243 }
244
245 /**
246 * Filters the allowed tags.
247 *
248 * @since 1.2.5
249 */
250 return apply_filters( 'merchant_kses_allowed_tags', $allowed_tags );
251 }
252 }
253
254 /**
255 * Allowed tags for scripts.
256 *
257 * @return array $allowed_tags
258 */
259 if ( ! function_exists( 'merchant_kses_allowed_tags_for_code_snippets' ) ) {
260 function merchant_kses_allowed_tags_for_code_snippets() {
261 return array(
262 'script' => array(
263 'type' => true,
264 ),
265 );
266 }
267 }
268
269 /**
270 * Check if WooCommerce checkout page is being rendered by block.
271 * Since WooCommerce 8.3.0 the checkout page is rendered by block.
272 *
273 * @return bool
274 */
275 if ( ! function_exists( 'merchant_is_checkout_block_layout' ) ) {
276 function merchant_is_checkout_block_layout() {
277 $checkout_page = wc_get_page_id( 'checkout' );
278
279 if ( empty( $checkout_page ) ) {
280 return false;
281 }
282
283 if ( function_exists( 'has_blocks' ) && has_blocks( $checkout_page ) ) {
284 $post = get_post( $checkout_page );
285 $blocks = parse_blocks( $post->post_content );
286
287 foreach ( $blocks as $block ) {
288 if ( 'woocommerce/checkout' === $block['blockName'] ) {
289 return true;
290 }
291 }
292 }
293
294 return false;
295 }
296 }
297
298 /**
299 * Check if WooCommerce cart page is being rendered by block.
300 * Since WooCommerce 8.3.0 the cart page is rendered by block.
301 *
302 * @return bool
303 */
304 if ( ! function_exists( 'merchant_is_cart_block_layout' ) ) {
305 function merchant_is_cart_block_layout() {
306 $cart_page = wc_get_page_id( 'cart' );
307
308 if ( empty( $cart_page ) ) {
309 return false;
310 }
311
312 if ( function_exists( 'has_blocks' ) && has_blocks( $cart_page ) ) {
313 $post = get_post( $cart_page );
314 $blocks = parse_blocks( $post->post_content );
315
316 foreach ( $blocks as $block ) {
317 if ( 'woocommerce/cart' === $block['blockName'] ) {
318 return true;
319 }
320 }
321 }
322
323 return false;
324 }
325 }
326
327 /**
328 * Get the product categories.
329 */
330 if ( ! function_exists( 'merchant_get_product_categories' ) ) {
331 function merchant_get_product_categories() {
332 $product_categories = get_terms( array(
333 'taxonomy' => 'product_cat',
334 'hide_empty' => false,
335 ) );
336 $category_names = array();
337 if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
338 foreach ( $product_categories as $category ) {
339 $category_names[ $category->slug ] = $category->name;
340 }
341 }
342
343 return $category_names;
344 }
345 }
346
347 /**
348 * Convert array to css.
349 */
350 if ( ! function_exists( 'merchant_array_to_css' ) ) {
351 function merchant_array_to_css( $css_array ) {
352 $css = '';
353 if ( empty( $css_array ) ) {
354 return $css;
355 }
356 foreach ( $css_array as $key => $value ) {
357 $css .= $key . ':' . $value . ';';
358 }
359
360 return $css;
361 }
362 }