| 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 |
if ( in_array( 'wc_email', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 149 |
$allowed_tags['body'] = array( |
| 150 |
'class' => true, |
| 151 |
'style' => true, |
| 152 |
'leftmargin' => true, |
| 153 |
'marginwidth' => true, |
| 154 |
'topmargin' => true, |
| 155 |
'marginheight' => true, |
| 156 |
'offset' => true, |
| 157 |
); |
| 158 |
|
| 159 |
$allowed_tags['html'] = array( |
| 160 |
'lang' => true, |
| 161 |
); |
| 162 |
|
| 163 |
$allowed_tags['style'] = array( |
| 164 |
'type' => true, |
| 165 |
); |
| 166 |
$allowed_tags['head'] = array(); |
| 167 |
$allowed_tags['meta']['http-equiv'] = true; |
| 168 |
} |
| 169 |
|
| 170 |
// Include nonce tags |
| 171 |
if ( in_array( 'nonce', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 172 |
$allowed_tags['input'] = array( |
| 173 |
'type' => true, |
| 174 |
'id' => true, |
| 175 |
'name' => true, |
| 176 |
'value' => true, |
| 177 |
'data-name' => true, |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
// Include bdi tag |
| 182 |
if ( in_array( 'bdi', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 183 |
$allowed_tags['bdi'] = array( |
| 184 |
'class' => true, |
| 185 |
'id' => true, |
| 186 |
'style' => true, |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
// Include select2 |
| 191 |
if ( in_array( 'select2', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 192 |
$allowed_tags['select'] = array( |
| 193 |
'name' => true, |
| 194 |
'class' => true, |
| 195 |
'id' => true, |
| 196 |
'style' => true, |
| 197 |
'data-name' => true, |
| 198 |
'data-source' => true, |
| 199 |
'multiple' => true, |
| 200 |
); |
| 201 |
|
| 202 |
$allowed_tags['option'] = array( |
| 203 |
'value' => true, |
| 204 |
'selected' => true, |
| 205 |
); |
| 206 |
|
| 207 |
$allowed_tags['optgroup'] = array( |
| 208 |
'label' => true, |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
// Include dd, dt tags |
| 213 |
if ( in_array( 'dd', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 214 |
$allowed_tags['dd'] = array( |
| 215 |
'class' => true, |
| 216 |
'id' => true, |
| 217 |
'style' => true, |
| 218 |
); |
| 219 |
|
| 220 |
$allowed_tags['dt'] = array( |
| 221 |
'class' => true, |
| 222 |
'id' => true, |
| 223 |
'style' => true, |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
// Include forms tags. |
| 228 |
if ( in_array( 'forms', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 229 |
$tags = array( 'form', 'input', 'select', 'option', 'textarea' ); |
| 230 |
|
| 231 |
foreach ( $tags as $tag ) { |
| 232 |
$allowed_tags[ $tag ] = array( |
| 233 |
'id' => true, |
| 234 |
'class' => true, |
| 235 |
'style' => true, |
| 236 |
'name' => true, |
| 237 |
'value' => true, |
| 238 |
'type' => true, |
| 239 |
'placeholder' => true, |
| 240 |
'data' => '*', |
| 241 |
'data-source' => true, |
| 242 |
'data-product_id' => true, |
| 243 |
'data-product_variations' => true, |
| 244 |
'data-attribute_name' => true, |
| 245 |
'data-show_option_none' => true, |
| 246 |
'data-name' => true, |
| 247 |
'step' => true, |
| 248 |
'min' => true, |
| 249 |
'max' => true, |
| 250 |
'selected' => true, |
| 251 |
'checked' => true, |
| 252 |
'onchange' => true, |
| 253 |
'autocomplete' => true, |
| 254 |
'required' => true, |
| 255 |
'action' => true, |
| 256 |
'method' => true, |
| 257 |
'enctype' => true, |
| 258 |
'size' => true, |
| 259 |
'role' => true, |
| 260 |
'inputmode' => true, |
| 261 |
'aria-label' => true, |
| 262 |
'multiple' => true, |
| 263 |
); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Filters the allowed tags. |
| 269 |
* |
| 270 |
* @since 1.2.5 |
| 271 |
*/ |
| 272 |
return apply_filters( 'merchant_kses_allowed_tags', $allowed_tags ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Allowed tags for scripts. |
| 278 |
* |
| 279 |
* @return array $allowed_tags |
| 280 |
*/ |
| 281 |
if ( ! function_exists( 'merchant_kses_allowed_tags_for_code_snippets' ) ) { |
| 282 |
function merchant_kses_allowed_tags_for_code_snippets() { |
| 283 |
return array( |
| 284 |
'script' => array( |
| 285 |
'type' => true, |
| 286 |
), |
| 287 |
); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Check if WooCommerce checkout page is being rendered by block. |
| 293 |
* Since WooCommerce 8.3.0 the checkout page is rendered by block. |
| 294 |
* |
| 295 |
* @return bool |
| 296 |
*/ |
| 297 |
if ( ! function_exists( 'merchant_is_checkout_block_layout' ) ) { |
| 298 |
function merchant_is_checkout_block_layout() { |
| 299 |
$checkout_page = wc_get_page_id( 'checkout' ); |
| 300 |
|
| 301 |
if ( empty( $checkout_page ) ) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
if ( function_exists( 'has_blocks' ) && has_blocks( $checkout_page ) ) { |
| 306 |
$post = get_post( $checkout_page ); |
| 307 |
$blocks = parse_blocks( $post->post_content ); |
| 308 |
|
| 309 |
foreach ( $blocks as $block ) { |
| 310 |
if ( 'woocommerce/checkout' === $block['blockName'] ) { |
| 311 |
return true; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return false; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Check if WooCommerce cart page is being rendered by block. |
| 322 |
* Since WooCommerce 8.3.0 the cart page is rendered by block. |
| 323 |
* |
| 324 |
* @return bool |
| 325 |
*/ |
| 326 |
if ( ! function_exists( 'merchant_is_cart_block_layout' ) ) { |
| 327 |
function merchant_is_cart_block_layout() { |
| 328 |
$cart_page = wc_get_page_id( 'cart' ); |
| 329 |
|
| 330 |
if ( empty( $cart_page ) ) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
if ( function_exists( 'has_blocks' ) && has_blocks( $cart_page ) ) { |
| 335 |
$post = get_post( $cart_page ); |
| 336 |
$blocks = parse_blocks( $post->post_content ); |
| 337 |
|
| 338 |
foreach ( $blocks as $block ) { |
| 339 |
if ( 'woocommerce/cart' === $block['blockName'] ) { |
| 340 |
return true; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
return false; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Get the product categories. |
| 351 |
*/ |
| 352 |
if ( ! function_exists( 'merchant_get_product_categories' ) ) { |
| 353 |
function merchant_get_product_categories() { |
| 354 |
$product_categories = get_terms( array( |
| 355 |
'taxonomy' => 'product_cat', |
| 356 |
'hide_empty' => false, |
| 357 |
) ); |
| 358 |
$category_names = array(); |
| 359 |
if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) { |
| 360 |
foreach ( $product_categories as $category ) { |
| 361 |
$category_names[ $category->slug ] = $category->name; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
return $category_names; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Convert array to css. |
| 371 |
*/ |
| 372 |
if ( ! function_exists( 'merchant_array_to_css' ) ) { |
| 373 |
function merchant_array_to_css( $css_array ) { |
| 374 |
$css = ''; |
| 375 |
if ( empty( $css_array ) ) { |
| 376 |
return $css; |
| 377 |
} |
| 378 |
foreach ( $css_array as $key => $value ) { |
| 379 |
$css .= $key . ':' . $value . ';'; |
| 380 |
} |
| 381 |
|
| 382 |
return $css; |
| 383 |
} |
| 384 |
} |