| 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 |
), |
| 132 |
$include_post_tags ? wp_kses_allowed_html( 'post' ) : array() |
| 133 |
); |
| 134 |
|
| 135 |
// Include schema markup tags |
| 136 |
if ( in_array( 'schema_markup', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 137 |
$tags = array( 'meta', 'nav', 'ul', 'li', 'a', 'span' ); |
| 138 |
|
| 139 |
foreach ( $tags as $tag ) { |
| 140 |
if ( isset( $allowed_tags[ $tag ] ) ) { |
| 141 |
$allowed_tags[ $tag ]['itemprop'] = true; |
| 142 |
$allowed_tags[ $tag ]['itemscope'] = true; |
| 143 |
$allowed_tags[ $tag ]['itemtype'] = true; |
| 144 |
} else { |
| 145 |
$allowed_tags[ $tag ] = array( |
| 146 |
'itemprop' => true, |
| 147 |
'itemscope' => true, |
| 148 |
'itemtype' => true, |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Include iframe tags |
| 155 |
if ( in_array( 'iframe', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 156 |
$allowed_tags['iframe'] = array( |
| 157 |
'src' => true, |
| 158 |
'height' => true, |
| 159 |
'width' => true, |
| 160 |
'frameborder' => true, |
| 161 |
'allowfullscreen' => true, |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
if ( in_array( 'wc_email', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 166 |
$allowed_tags['body'] = array( |
| 167 |
'class' => true, |
| 168 |
'style' => true, |
| 169 |
'leftmargin' => true, |
| 170 |
'marginwidth' => true, |
| 171 |
'topmargin' => true, |
| 172 |
'marginheight' => true, |
| 173 |
'offset' => true, |
| 174 |
); |
| 175 |
|
| 176 |
$allowed_tags['html'] = array( |
| 177 |
'lang' => true, |
| 178 |
); |
| 179 |
|
| 180 |
$allowed_tags['style'] = array( |
| 181 |
'type' => true, |
| 182 |
); |
| 183 |
$allowed_tags['head'] = array(); |
| 184 |
$allowed_tags['meta']['http-equiv'] = true; |
| 185 |
} |
| 186 |
|
| 187 |
// Include nonce tags |
| 188 |
if ( in_array( 'nonce', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 189 |
$allowed_tags['input'] = array( |
| 190 |
'type' => true, |
| 191 |
'id' => true, |
| 192 |
'name' => true, |
| 193 |
'value' => true, |
| 194 |
'data-name' => true, |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
// Include bdi tag |
| 199 |
if ( in_array( 'bdi', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 200 |
$allowed_tags['bdi'] = array( |
| 201 |
'class' => true, |
| 202 |
'id' => true, |
| 203 |
'style' => true, |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
// Include select2 |
| 208 |
if ( in_array( 'select2', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 209 |
$allowed_tags['select'] = array( |
| 210 |
'name' => true, |
| 211 |
'class' => true, |
| 212 |
'id' => true, |
| 213 |
'style' => true, |
| 214 |
'data-name' => true, |
| 215 |
'data-source' => true, |
| 216 |
'multiple' => true, |
| 217 |
); |
| 218 |
|
| 219 |
$allowed_tags['option'] = array( |
| 220 |
'value' => true, |
| 221 |
'selected' => true, |
| 222 |
); |
| 223 |
|
| 224 |
$allowed_tags['optgroup'] = array( |
| 225 |
'label' => true, |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
// Include dd, dt tags |
| 230 |
if ( in_array( 'dd', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 231 |
$allowed_tags['dd'] = array( |
| 232 |
'class' => true, |
| 233 |
'id' => true, |
| 234 |
'style' => true, |
| 235 |
); |
| 236 |
|
| 237 |
$allowed_tags['dt'] = array( |
| 238 |
'class' => true, |
| 239 |
'id' => true, |
| 240 |
'style' => true, |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
// Include forms tags. |
| 245 |
if ( in_array( 'forms', $extra, true ) || in_array( 'all', $extra, true ) ) { |
| 246 |
$tags = array( 'form', 'input', 'select', 'option', 'textarea' ); |
| 247 |
|
| 248 |
foreach ( $tags as $tag ) { |
| 249 |
$allowed_tags[ $tag ] = array( |
| 250 |
'id' => true, |
| 251 |
'class' => true, |
| 252 |
'style' => true, |
| 253 |
'name' => true, |
| 254 |
'value' => true, |
| 255 |
'type' => true, |
| 256 |
'placeholder' => true, |
| 257 |
'data' => '*', |
| 258 |
'data-source' => true, |
| 259 |
'data-product_id' => true, |
| 260 |
'data-product_variations' => true, |
| 261 |
'data-attribute_name' => true, |
| 262 |
'data-show_option_none' => true, |
| 263 |
'data-name' => true, |
| 264 |
'step' => true, |
| 265 |
'min' => true, |
| 266 |
'max' => true, |
| 267 |
'selected' => true, |
| 268 |
'checked' => true, |
| 269 |
'onchange' => true, |
| 270 |
'autocomplete' => true, |
| 271 |
'required' => true, |
| 272 |
'action' => true, |
| 273 |
'method' => true, |
| 274 |
'enctype' => true, |
| 275 |
'size' => true, |
| 276 |
'role' => true, |
| 277 |
'inputmode' => true, |
| 278 |
'aria-label' => true, |
| 279 |
'multiple' => true, |
| 280 |
); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Filters the allowed tags. |
| 286 |
* |
| 287 |
* @since 1.2.5 |
| 288 |
*/ |
| 289 |
return apply_filters( 'merchant_kses_allowed_tags', $allowed_tags ); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Allowed tags for scripts. |
| 295 |
* |
| 296 |
* @return array $allowed_tags |
| 297 |
*/ |
| 298 |
if ( ! function_exists( 'merchant_kses_allowed_tags_for_code_snippets' ) ) { |
| 299 |
function merchant_kses_allowed_tags_for_code_snippets() { |
| 300 |
return array( |
| 301 |
'script' => array( |
| 302 |
'type' => true, |
| 303 |
), |
| 304 |
); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Check if WooCommerce checkout page is being rendered by block. |
| 310 |
* Since WooCommerce 8.3.0 the checkout page is rendered by block. |
| 311 |
* |
| 312 |
* @return bool |
| 313 |
*/ |
| 314 |
if ( ! function_exists( 'merchant_is_checkout_block_layout' ) ) { |
| 315 |
function merchant_is_checkout_block_layout() { |
| 316 |
$checkout_page = wc_get_page_id( 'checkout' ); |
| 317 |
|
| 318 |
if ( empty( $checkout_page ) ) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
if ( function_exists( 'has_blocks' ) && has_blocks( $checkout_page ) ) { |
| 323 |
$post = get_post( $checkout_page ); |
| 324 |
$blocks = parse_blocks( $post->post_content ); |
| 325 |
|
| 326 |
foreach ( $blocks as $block ) { |
| 327 |
if ( 'woocommerce/checkout' === $block['blockName'] ) { |
| 328 |
return true; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
return false; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Check if WooCommerce cart page is being rendered by block. |
| 339 |
* Since WooCommerce 8.3.0 the cart page is rendered by block. |
| 340 |
* |
| 341 |
* @return bool |
| 342 |
*/ |
| 343 |
if ( ! function_exists( 'merchant_is_cart_block_layout' ) ) { |
| 344 |
function merchant_is_cart_block_layout() { |
| 345 |
$cart_page = wc_get_page_id( 'cart' ); |
| 346 |
|
| 347 |
if ( empty( $cart_page ) ) { |
| 348 |
return false; |
| 349 |
} |
| 350 |
|
| 351 |
if ( function_exists( 'has_blocks' ) && has_blocks( $cart_page ) ) { |
| 352 |
$post = get_post( $cart_page ); |
| 353 |
$blocks = parse_blocks( $post->post_content ); |
| 354 |
|
| 355 |
foreach ( $blocks as $block ) { |
| 356 |
if ( 'woocommerce/cart' === $block['blockName'] ) { |
| 357 |
return true; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
return false; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get the product categories. |
| 368 |
*/ |
| 369 |
if ( ! function_exists( 'merchant_get_product_categories' ) ) { |
| 370 |
function merchant_get_product_categories() { |
| 371 |
$product_categories = get_terms( array( |
| 372 |
'taxonomy' => 'product_cat', |
| 373 |
'hide_empty' => false, |
| 374 |
) ); |
| 375 |
$category_names = array(); |
| 376 |
if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) { |
| 377 |
foreach ( $product_categories as $category ) { |
| 378 |
$category_names[ $category->slug ] = $category->name; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return $category_names; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Convert array to css. |
| 388 |
*/ |
| 389 |
if ( ! function_exists( 'merchant_array_to_css' ) ) { |
| 390 |
function merchant_array_to_css( $css_array ) { |
| 391 |
$css = ''; |
| 392 |
if ( empty( $css_array ) ) { |
| 393 |
return $css; |
| 394 |
} |
| 395 |
foreach ( $css_array as $key => $value ) { |
| 396 |
$css .= $key . ':' . $value . ';'; |
| 397 |
} |
| 398 |
|
| 399 |
return $css; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Get the share link data structure. |
| 405 |
* |
| 406 |
* @return array |
| 407 |
*/ |
| 408 |
if ( ! function_exists( 'merchant_get_share_link_data' ) ) { |
| 409 |
function merchant_get_share_link_data() { |
| 410 |
return array( |
| 411 |
'facebook' => array( |
| 412 |
'url' => 'https://www.facebook.com/sharer.php?u={{url}}', |
| 413 |
'title' => __( 'Facebook', 'merchant' ), |
| 414 |
), |
| 415 |
'twitter' => array( |
| 416 |
'url' => 'https://twitter.com/intent/tweet?url={{url}}&text={{title}}', |
| 417 |
'title' => __( 'X', 'merchant' ), |
| 418 |
), |
| 419 |
'linkedin' => array( |
| 420 |
'url' => 'https://www.linkedin.com/sharing/share-offsite/?url={{url}}', |
| 421 |
'title' => __( 'LinkedIn', 'merchant' ), |
| 422 |
), |
| 423 |
'reddit' => array( |
| 424 |
'url' => 'https://reddit.com/submit?url={{url}}&title={{title}}', |
| 425 |
'title' => __( 'Reddit', 'merchant' ), |
| 426 |
), |
| 427 |
'whatsapp' => array( |
| 428 |
'url' => 'https://api.whatsapp.com/send/?text={{url}}', |
| 429 |
'title' => __( 'WhatsApp', 'merchant' ), |
| 430 |
), |
| 431 |
'pinterest' => array( |
| 432 |
'url' => 'http://pinterest.com/pin/create/link/?url={{url}}', |
| 433 |
'title' => __( 'Pinterest', 'merchant' ), |
| 434 |
), |
| 435 |
'telegram' => array( |
| 436 |
'url' => 'https://t.me/share/url?url={{url}}&text={{title}}', |
| 437 |
'title' => __( 'Telegram', 'merchant' ), |
| 438 |
), |
| 439 |
'weibo' => array( |
| 440 |
'url' => 'http://service.weibo.com/share/share.php?url={{url}}&appkey=&title={{title}}&pic=&ralateUid=', |
| 441 |
'title' => __( 'Weibo', 'merchant' ), |
| 442 |
), |
| 443 |
'vk' => array( |
| 444 |
'url' => 'http://vk.com/share.php?url={{url}}&title={{title}}&comment={text}', |
| 445 |
'title' => __( 'VK', 'merchant' ), |
| 446 |
), |
| 447 |
'ok' => array( |
| 448 |
'url' => 'https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl={{url}}', |
| 449 |
'title' => __( 'OK', 'merchant' ), |
| 450 |
), |
| 451 |
'xing' => array( |
| 452 |
'url' => 'https://www.xing.com/spi/shares/new?url={{url}}', |
| 453 |
'title' => __( 'Xing', 'merchant' ), |
| 454 |
), |
| 455 |
'mail' => array( |
| 456 |
'url' => 'mailto:?subject={{title}}&body={{url}}', |
| 457 |
'title' => __( 'Mail', 'merchant' ), |
| 458 |
), |
| 459 |
); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Get the share link url. |
| 465 |
* |
| 466 |
* @param string $social_network |
| 467 |
* |
| 468 |
* @return string |
| 469 |
*/ |
| 470 |
if ( ! function_exists( 'merchant_get_share_link_url' ) ) { |
| 471 |
function merchant_get_share_link_url( $social_network, $url_to_share, $title_to_share = '' ) { |
| 472 |
$share_link_data = merchant_get_share_link_data(); |
| 473 |
|
| 474 |
if ( ! isset( $share_link_data[ $social_network ] ) ) { |
| 475 |
return ''; |
| 476 |
} |
| 477 |
|
| 478 |
$share_link_url = str_replace( |
| 479 |
array( '{{url}}', '{{title}}' ), |
| 480 |
array( $url_to_share, $title_to_share ), |
| 481 |
$share_link_data[ $social_network ]['url'] |
| 482 |
); |
| 483 |
|
| 484 |
return $share_link_url; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Get the share link title. |
| 490 |
* |
| 491 |
* @param string $social_network |
| 492 |
* |
| 493 |
* @return string |
| 494 |
*/ |
| 495 |
if ( ! function_exists( 'merchant_get_share_link_title' ) ) { |
| 496 |
function merchant_get_share_link_title( $social_network ) { |
| 497 |
$share_link_data = merchant_get_share_link_data(); |
| 498 |
|
| 499 |
return isset( $share_link_data[ $social_network ] ) ? $share_link_data[ $social_network ]['title'] : ''; |
| 500 |
} |
| 501 |
} |
| 502 |
|