| 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 |
* @return array $allowed_tags |
| 20 |
*/ |
| 21 |
if ( ! function_exists( 'merchant_kses_allowed_tags' ) ) { |
| 22 |
function merchant_kses_allowed_tags( $extra = array(), $include_post_tags = true ) { |
| 23 |
|
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 ) || in_array( 'all', $extra ) ) { |
| 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 |
'data-source' => true, |
| 226 |
'step' => true, |
| 227 |
'min' => true, |
| 228 |
'max' => true, |
| 229 |
'selected' => true, |
| 230 |
'checked' => true, |
| 231 |
'onchange' => true, |
| 232 |
'autocomplete' => true, |
| 233 |
'required' => true, |
| 234 |
'action' => true, |
| 235 |
'method' => true, |
| 236 |
'data-name' => true, |
| 237 |
'enctype' => true, |
| 238 |
'size' => true, |
| 239 |
'role' => true, |
| 240 |
'inputmode' => true, |
| 241 |
'aria-label' => true |
| 242 |
); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Filters the allowed tags. |
| 248 |
* |
| 249 |
* @since 1.2.5 |
| 250 |
*/ |
| 251 |
return apply_filters( 'merchant_kses_allowed_tags', $allowed_tags ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Allowed tags for scripts. |
| 257 |
* |
| 258 |
* @return array $allowed_tags |
| 259 |
*/ |
| 260 |
if ( ! function_exists( 'merchant_kses_allowed_tags_for_code_snippets' ) ) { |
| 261 |
function merchant_kses_allowed_tags_for_code_snippets() { |
| 262 |
return array( |
| 263 |
'script' => array( |
| 264 |
'type' => true |
| 265 |
) |
| 266 |
); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Check if WooCommerce checkout page is being rendered by block. |
| 272 |
* Since WooCommerce 8.3.0 the checkout page is rendered by block. |
| 273 |
* |
| 274 |
* @return bool |
| 275 |
*/ |
| 276 |
if ( ! function_exists( 'merchant_is_checkout_block_layout' ) ) { |
| 277 |
function merchant_is_checkout_block_layout() { |
| 278 |
$checkout_page = wc_get_page_id( 'checkout' ); |
| 279 |
|
| 280 |
if ( empty( $checkout_page ) ) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
if ( function_exists( 'has_blocks' ) && has_blocks( $checkout_page ) ) { |
| 285 |
$post = get_post( $checkout_page ); |
| 286 |
$blocks = parse_blocks( $post->post_content ); |
| 287 |
|
| 288 |
foreach ( $blocks as $block ) { |
| 289 |
if ( 'woocommerce/checkout' === $block['blockName'] ) { |
| 290 |
return true; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
return false; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Check if WooCommerce cart page is being rendered by block. |
| 301 |
* Since WooCommerce 8.3.0 the cart page is rendered by block. |
| 302 |
* |
| 303 |
* @return bool |
| 304 |
*/ |
| 305 |
if ( ! function_exists( 'merchant_is_cart_block_layout' ) ) { |
| 306 |
function merchant_is_cart_block_layout() { |
| 307 |
$cart_page = wc_get_page_id( 'cart' ); |
| 308 |
|
| 309 |
if ( empty( $cart_page ) ) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
if ( function_exists( 'has_blocks' ) && has_blocks( $cart_page ) ) { |
| 314 |
$post = get_post( $cart_page ); |
| 315 |
$blocks = parse_blocks( $post->post_content ); |
| 316 |
|
| 317 |
foreach ( $blocks as $block ) { |
| 318 |
if ( 'woocommerce/cart' === $block['blockName'] ) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return false; |
| 325 |
} |
| 326 |
} |