| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilder\Helpers; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Global helper class. |
| 9 |
* |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
class Utils { |
| 14 |
|
| 15 |
/** |
| 16 |
* Returns an array of allowed HTML tags and attributes for SVG elements. |
| 17 |
* |
| 18 |
* @return array The array of allowed HTML tags and attributes. |
| 19 |
*/ |
| 20 |
public static function svg_allowed_html() { |
| 21 |
$allowed_svg_tags = [ |
| 22 |
'svg' => [ |
| 23 |
'xmlns' => true, |
| 24 |
'width' => true, |
| 25 |
'height' => true, |
| 26 |
'viewBox' => true, |
| 27 |
'viewbox' => true, |
| 28 |
'fill' => true, |
| 29 |
'class' => true, |
| 30 |
'aria-hidden' => true, |
| 31 |
'aria-labelledby' => true, |
| 32 |
'role' => true, |
| 33 |
'preserveaspectratio' => true, |
| 34 |
'version' => true, |
| 35 |
], |
| 36 |
'title' => array( 'title' => true ), |
| 37 |
'g' => [ |
| 38 |
'transform' => true, |
| 39 |
'style' => true, |
| 40 |
'id' => true, |
| 41 |
], |
| 42 |
'path' => [ |
| 43 |
'd' => true, |
| 44 |
'fill' => true, |
| 45 |
'fill-rule' => true, |
| 46 |
'transform' => true, |
| 47 |
'style' => true, |
| 48 |
'opacity' => true, |
| 49 |
'stroke' => true, |
| 50 |
'stroke-width' => true, |
| 51 |
'stroke-miterlimit' => true, |
| 52 |
'stroke-linecap' => true, |
| 53 |
'stroke-linejoin' => true, |
| 54 |
'fill-opacity' => true, |
| 55 |
], |
| 56 |
'circle' => [ |
| 57 |
'cx' => true, |
| 58 |
'cy' => true, |
| 59 |
'r' => true, |
| 60 |
'fill' => true, |
| 61 |
'stroke' => true, |
| 62 |
'stroke-width' => true, |
| 63 |
], |
| 64 |
'ellipse' => [ |
| 65 |
'cx' => true, |
| 66 |
'cy' => true, |
| 67 |
'rx' => true, |
| 68 |
'ry' => true, |
| 69 |
'fill' => true, |
| 70 |
'stroke' => true, |
| 71 |
'stroke-width' => true, |
| 72 |
], |
| 73 |
'line' => [ |
| 74 |
'x1' => true, |
| 75 |
'y1' => true, |
| 76 |
'x2' => true, |
| 77 |
'y2' => true, |
| 78 |
'stroke' => true, |
| 79 |
'stroke-width' => true, |
| 80 |
], |
| 81 |
'polygon' => [ |
| 82 |
'points' => true, |
| 83 |
'fill' => true, |
| 84 |
'stroke' => true, |
| 85 |
'stroke-width' => true, |
| 86 |
], |
| 87 |
'polyline' => [ |
| 88 |
'points' => true, |
| 89 |
'fill' => true, |
| 90 |
'stroke' => true, |
| 91 |
'stroke-width' => true, |
| 92 |
], |
| 93 |
'rect' => [ |
| 94 |
'x' => true, |
| 95 |
'y' => true, |
| 96 |
'width' => true, |
| 97 |
'height' => true, |
| 98 |
'fill' => true, |
| 99 |
'stroke' => true, |
| 100 |
'stroke-width' => true, |
| 101 |
], |
| 102 |
'text' => [ |
| 103 |
'x' => true, |
| 104 |
'y' => true, |
| 105 |
'dx' => true, |
| 106 |
'dy' => true, |
| 107 |
'text-anchor' => true, |
| 108 |
'style' => true, |
| 109 |
], |
| 110 |
'tspan' => [ |
| 111 |
'x' => true, |
| 112 |
'y' => true, |
| 113 |
'dx' => true, |
| 114 |
'dy' => true, |
| 115 |
'text-anchor' => true, |
| 116 |
'style' => true, |
| 117 |
], |
| 118 |
'defs' => [], |
| 119 |
'lineargradient' => [ |
| 120 |
'id' => true, |
| 121 |
'x1' => true, |
| 122 |
'y1' => true, |
| 123 |
'x2' => true, |
| 124 |
'y2' => true, |
| 125 |
'gradientunits' => true, |
| 126 |
], |
| 127 |
'stop' => [ |
| 128 |
'offset' => true, |
| 129 |
'style' => true, |
| 130 |
'stop-color' => true, |
| 131 |
'stop-opacity' => true, |
| 132 |
], |
| 133 |
'radialgradient' => [ |
| 134 |
'id' => true, |
| 135 |
'cx' => true, |
| 136 |
'cy' => true, |
| 137 |
'r' => true, |
| 138 |
'gradientunits' => true, |
| 139 |
'gradienttransform' => true, |
| 140 |
], |
| 141 |
]; |
| 142 |
|
| 143 |
return apply_filters( 'table_builder_allowed_svg_attrs_tags', $allowed_svg_tags ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns an array of allowed JSON attribute tags. |
| 148 |
* |
| 149 |
* This function defines an array of allowed JSON attribute tags and their corresponding properties. |
| 150 |
* The array includes tags such as 'object', 'array', 'string', 'number', 'integer', 'boolean', 'null', |
| 151 |
* 'enum', 'const', 'oneOf', 'allOf', 'anyOf', 'not', 'if', 'then', 'else', and 'format'. |
| 152 |
* |
| 153 |
* @return array The array of allowed JSON attribute tags. |
| 154 |
*/ |
| 155 |
public static function allowed_json_attrs_tags() { |
| 156 |
$allowed_json_tags = [ |
| 157 |
'object' => [ |
| 158 |
'type' => true, |
| 159 |
'properties' => true, |
| 160 |
'required' => true, |
| 161 |
'additionalProperties' => true, |
| 162 |
'propertyNames' => true, |
| 163 |
'dependencies' => true, |
| 164 |
'minProperties' => true, |
| 165 |
'maxProperties' => true, |
| 166 |
], |
| 167 |
'array' => [ |
| 168 |
'type' => true, |
| 169 |
'items' => true, |
| 170 |
'minItems' => true, |
| 171 |
'maxItems' => true, |
| 172 |
'uniqueItems' => true, |
| 173 |
], |
| 174 |
'string' => [ |
| 175 |
'type' => true, |
| 176 |
'minLength' => true, |
| 177 |
'maxLength' => true, |
| 178 |
'pattern' => true, |
| 179 |
'format' => true, |
| 180 |
'contentEncoding' => true, |
| 181 |
'contentMediaType' => true, |
| 182 |
], |
| 183 |
'number' => [ |
| 184 |
'type' => true, |
| 185 |
'minimum' => true, |
| 186 |
'maximum' => true, |
| 187 |
'exclusiveMinimum' => true, |
| 188 |
'exclusiveMaximum' => true, |
| 189 |
'multipleOf' => true, |
| 190 |
], |
| 191 |
'integer' => [ |
| 192 |
'type' => true, |
| 193 |
'minimum' => true, |
| 194 |
'maximum' => true, |
| 195 |
'exclusiveMinimum' => true, |
| 196 |
'exclusiveMaximum' => true, |
| 197 |
], |
| 198 |
'boolean' => [ |
| 199 |
'type' => true, |
| 200 |
], |
| 201 |
'null' => [ |
| 202 |
'type' => true, |
| 203 |
], |
| 204 |
'enum' => [ |
| 205 |
'type' => true, |
| 206 |
'enum' => true, |
| 207 |
], |
| 208 |
'const' => [ |
| 209 |
'type' => true, |
| 210 |
'const' => true, |
| 211 |
], |
| 212 |
'oneOf' => [ |
| 213 |
'type' => true, |
| 214 |
'oneOf' => true, |
| 215 |
], |
| 216 |
'allOf' => [ |
| 217 |
'type' => true, |
| 218 |
'allOf' => true, |
| 219 |
], |
| 220 |
'anyOf' => [ |
| 221 |
'type' => true, |
| 222 |
'anyOf' => true, |
| 223 |
], |
| 224 |
'not' => [ |
| 225 |
'type' => true, |
| 226 |
'not' => true, |
| 227 |
], |
| 228 |
'if' => [ |
| 229 |
'type' => true, |
| 230 |
'if' => true, |
| 231 |
], |
| 232 |
'then' => [ |
| 233 |
'type' => true, |
| 234 |
'then' => true, |
| 235 |
], |
| 236 |
'else' => [ |
| 237 |
'type' => true, |
| 238 |
'else' => true, |
| 239 |
], |
| 240 |
'format' => [ |
| 241 |
'type' => true, |
| 242 |
'format' => true, |
| 243 |
], |
| 244 |
'title' => true, |
| 245 |
'description' => true, |
| 246 |
'default' => true, |
| 247 |
'examples' => true, |
| 248 |
'$ref' => true, |
| 249 |
'$id' => true, |
| 250 |
'$schema' => true, |
| 251 |
// Adding Lottie-specific keys |
| 252 |
'v' => true, |
| 253 |
'meta' => true, |
| 254 |
'fr' => true, |
| 255 |
'ip' => true, |
| 256 |
'op' => true, |
| 257 |
'w' => true, |
| 258 |
'h' => true, |
| 259 |
'nm' => true, |
| 260 |
'ddd' => true, |
| 261 |
'assets' => true, |
| 262 |
'layers' => true, |
| 263 |
'markers' => true, |
| 264 |
]; |
| 265 |
|
| 266 |
return apply_filters('table_builder_allowed_json_attrs_tags', $allowed_json_tags); |
| 267 |
} |
| 268 |
|
| 269 |
public static function iframe_allowed_html() { |
| 270 |
return array( |
| 271 |
'iframe' => array( |
| 272 |
'src' => true, |
| 273 |
'name' => true, |
| 274 |
'sandbox' => true, |
| 275 |
'width' => true, |
| 276 |
'height' => true, |
| 277 |
'marginheight' => true, |
| 278 |
'marginwidth' => true, |
| 279 |
'scrolling' => true, |
| 280 |
'allowfullscreen' => true, |
| 281 |
'frameborder' => true, |
| 282 |
'title' => true, |
| 283 |
'id' => true, |
| 284 |
'class' => true, |
| 285 |
'style' => true, |
| 286 |
'tabindex' => true, |
| 287 |
'allow' => true, |
| 288 |
), |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
public static function gdc_allowed_html() |
| 293 |
{ |
| 294 |
return array( |
| 295 |
'gdc' => array( |
| 296 |
'selectedpath' => true, |
| 297 |
'class' => true, |
| 298 |
'id' => true, |
| 299 |
'fallback' => true, |
| 300 |
'postcustomfield' => true, |
| 301 |
'postcustomfieldkey' => true, |
| 302 |
'postdatetype' => true, |
| 303 |
'dateformat' => true, |
| 304 |
'customdateformat' => true, |
| 305 |
'excerptlength' => true, |
| 306 |
'tagindex' => true, |
| 307 |
'timetype' => true, |
| 308 |
'timeformat' => true, |
| 309 |
'customtimeformat' => true, |
| 310 |
'categoryindex' => true, |
| 311 |
'nocomment' => true, |
| 312 |
'singlecomment' => true, |
| 313 |
'multicomments' => true, |
| 314 |
'currentdateformat' => true, |
| 315 |
'customcurrentdateformat' => true, |
| 316 |
'currenttimeformat' => true, |
| 317 |
'customcurrenttimeformat' => true, |
| 318 |
'authorinfo' => true, |
| 319 |
'currentuserinfo' => true, |
| 320 |
'acfgroup' => true, |
| 321 |
'acffield' => true, |
| 322 |
), |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Returns the allowed HTML tags and attributes for the img element. |
| 328 |
* |
| 329 |
* @return array The allowed HTML tags and attributes. |
| 330 |
*/ |
| 331 |
public static function img_allowed_html() { |
| 332 |
return array( |
| 333 |
'img' => array( |
| 334 |
'alt' => true, |
| 335 |
'src' => true, |
| 336 |
'srcset' => true, |
| 337 |
'class' => true, |
| 338 |
'height' => true, |
| 339 |
'width' => true, |
| 340 |
) |
| 341 |
); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Returns the allowed HTML tags and attributes for the style element. |
| 346 |
* |
| 347 |
* @return array The allowed HTML tags and attributes. |
| 348 |
*/ |
| 349 |
public static function style_allowed_html() { |
| 350 |
return array( |
| 351 |
'style' => array( |
| 352 |
'class' => true, |
| 353 |
'id' => true, |
| 354 |
) |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
public static function get_device_list() |
| 359 |
{ |
| 360 |
$default_device_list = [ |
| 361 |
[ |
| 362 |
'label' => 'Desktop', |
| 363 |
'slug' => 'Desktop', |
| 364 |
'value' => 'base', |
| 365 |
'direction' => 'max', |
| 366 |
'isActive' => true, |
| 367 |
'isRequired' => true, |
| 368 |
], |
| 369 |
[ |
| 370 |
'label' => 'Tablet', |
| 371 |
'slug' => 'Tablet', |
| 372 |
'value' => '1024', |
| 373 |
'direction' => 'max', |
| 374 |
'isActive' => true, |
| 375 |
'isRequired' => true, |
| 376 |
], |
| 377 |
[ |
| 378 |
'label' => 'Mobile', |
| 379 |
'slug' => 'Mobile', |
| 380 |
'value' => '767', |
| 381 |
'direction' => 'max', |
| 382 |
'isActive' => true, |
| 383 |
'isRequired' => true, |
| 384 |
] |
| 385 |
]; |
| 386 |
|
| 387 |
return $default_device_list; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Adds class to SVG |
| 392 |
* |
| 393 |
* @since 1.0.0 |
| 394 |
* @access public |
| 395 |
*/ |
| 396 |
public static function add_class_to_svg( $svg ) { |
| 397 |
$originalString = $svg; |
| 398 |
$substringToAdd = "class='table-builder-icon' "; |
| 399 |
|
| 400 |
$position = strpos( $originalString, '<svg' ); |
| 401 |
|
| 402 |
if ( $position !== false ) { |
| 403 |
$svg = substr_replace( $originalString, $substringToAdd, $position + 5, 0 ); |
| 404 |
return $svg; |
| 405 |
} |
| 406 |
|
| 407 |
return $svg; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Retrieves the dynamic block wrapper attributes. |
| 412 |
* |
| 413 |
* This function retrieves the wrapper attributes for a dynamic block. |
| 414 |
* It checks if the function "get_block_wrapper_attributes" exists and if the block is not empty. |
| 415 |
* If both conditions are met, it retrieves the block attributes, including the block ID. |
| 416 |
* It then merges the required attributes with any extra attributes provided. |
| 417 |
* Finally, it applies the "roxslider/dynamic_block_wrapper_attributes" filter and returns the wrapper attributes. |
| 418 |
* |
| 419 |
* @param object $block The dynamic block object. |
| 420 |
* @param array $extra_attrs Additional attributes to be merged with the required attributes. |
| 421 |
* @return string The wrapper attributes for the dynamic block. |
| 422 |
*/ |
| 423 |
public static function get_dynamic_block_wrapper_attributes($block, $extra_attrs = array()) { |
| 424 |
if(function_exists("get_block_wrapper_attributes") && !empty($block)) { |
| 425 |
$block_attrs = $block->attributes; |
| 426 |
$block_id = $block_attrs['blockID']; |
| 427 |
$required_attrs = array( |
| 428 |
'id' => 'block-' . $block_id |
| 429 |
); |
| 430 |
$wrapper_attrs = apply_filters('tablebuilder/dynamic_block_wrapper_attributes', array_merge($required_attrs, $extra_attrs), $block); |
| 431 |
return get_block_wrapper_attributes($wrapper_attrs); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Extends the allowed HTML tags for post content. |
| 437 |
* |
| 438 |
* @return array The extended array of allowed HTML tags. |
| 439 |
*/ |
| 440 |
public static function post_kses_extend_allowed_html() { |
| 441 |
$default_post_allowed_html = wp_kses_allowed_html( 'post' ); |
| 442 |
|
| 443 |
$post_allowed_html = array_merge( $default_post_allowed_html, self::svg_allowed_html(), self::iframe_allowed_html(), self::gdc_allowed_html(), self::style_allowed_html() ); |
| 444 |
|
| 445 |
return $post_allowed_html; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Retrieves the settings from the specified key in the options table. |
| 450 |
* |
| 451 |
* @param string $key The key of the settings in the options table. |
| 452 |
* @param string $list Optional. The specific list within the settings to retrieve. |
| 453 |
* @param string $option Optional. The specific option within the list to retrieve. |
| 454 |
* @return mixed The retrieved settings, or false if the list or option is not found. |
| 455 |
*/ |
| 456 |
public static function get_settings($key = '', $field = 'status', $inner_field = '' ) { |
| 457 |
$settings = get_option( 'table_builder_settings_list' ); |
| 458 |
|
| 459 |
// check if $key & $field both empty |
| 460 |
if ( empty($key) && empty($field) ) { |
| 461 |
return $settings; |
| 462 |
} |
| 463 |
|
| 464 |
// check for primary key |
| 465 |
if ( !empty($key) && !empty($settings[$key]) ) { |
| 466 |
$settings = $settings[$key]; |
| 467 |
} |
| 468 |
|
| 469 |
// check for primary field |
| 470 |
if( !empty($field) ) { |
| 471 |
if( $field === 'status' && !empty($settings[$field]) ) { |
| 472 |
return ($settings[$field] === 'active') ? true : false; |
| 473 |
} else { |
| 474 |
$settings = !empty($settings[$field]) ? $settings[$field] : false; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// check for inner field |
| 479 |
if( !empty($inner_field) ) { |
| 480 |
if( isset($settings[$inner_field]['value']) ) { |
| 481 |
$settings = $settings[$inner_field]['value']; |
| 482 |
} else { |
| 483 |
$settings = !empty($settings[$inner_field]) ? $settings[$inner_field] : false; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
return $settings; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Returns an array representing the border value based on the given key. |
| 492 |
* |
| 493 |
* @param mixed $key The key used to determine the border value. |
| 494 |
* @return array An array representing the border value. The array contains the following keys: |
| 495 |
* - 'border': The border value, or null if the key is null. |
| 496 |
*/ |
| 497 |
public static function get_border_value($key) { |
| 498 |
|
| 499 |
|
| 500 |
if (!is_array($key)) { |
| 501 |
return ['border' => null]; |
| 502 |
} |
| 503 |
|
| 504 |
$keyLength = count($key); |
| 505 |
|
| 506 |
|
| 507 |
if ($keyLength < 3) { |
| 508 |
$properties = ['style', 'color', 'width']; |
| 509 |
$borderParts = []; |
| 510 |
|
| 511 |
foreach ($properties as $property) { |
| 512 |
if (isset($key[$property])) { |
| 513 |
$borderParts[] = $key[$property]; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
return ['border' => implode(' ', $borderParts)]; |
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
if ($keyLength === 3) { |
| 524 |
if (isset($key['style'])) { |
| 525 |
return ['border' => "{$key['width']} {$key['style']} {$key['color']}"]; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if ($keyLength === 4 || $keyLength === 3) { |
| 530 |
$border = []; |
| 531 |
foreach ($key as $direction => $value) { |
| 532 |
if (isset($value['style'])) { |
| 533 |
$border["border-{$direction}"] = "{$value['width']} {$value['style']} {$value['color']}"; |
| 534 |
} |
| 535 |
} |
| 536 |
return $border; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* get box value |
| 542 |
* |
| 543 |
* Similar to getBoxValue in roxslider js helper |
| 544 |
* @param array $key |
| 545 |
* @param string $property |
| 546 |
*/ |
| 547 |
public static function get_box_value($key = [], $property = "") { |
| 548 |
$top = isset($key['top']) ? $key['top'] : null; |
| 549 |
$right = isset($key['right']) ? $key['right'] : null; |
| 550 |
$bottom = isset($key['bottom']) ? $key['bottom'] : null; |
| 551 |
$left = isset($key['left']) ? $key['left'] : null; |
| 552 |
|
| 553 |
$boxObject = ['top' => $top, 'right' => $right, 'bottom' => $bottom, 'left' => $left]; |
| 554 |
$count = count(array_filter($boxObject, function ($value) { |
| 555 |
return $value !== null; |
| 556 |
})); |
| 557 |
|
| 558 |
if ($count === 0) return [$property => null]; |
| 559 |
|
| 560 |
if ($count === 4) { |
| 561 |
$boxValues = ''; |
| 562 |
|
| 563 |
if ($top === $bottom && $top === $right && $top === $left) { |
| 564 |
$boxValues = $top; |
| 565 |
} elseif ($top === $bottom && $left === $right) { |
| 566 |
$boxValues = "{$top} {$right}"; |
| 567 |
} else { |
| 568 |
$boxValues = "{$top} {$right} {$bottom} {$left}"; |
| 569 |
} |
| 570 |
|
| 571 |
return [$property => $boxValues]; |
| 572 |
} |
| 573 |
|
| 574 |
$finalBox = []; |
| 575 |
|
| 576 |
if ($property !== "border-radius") { |
| 577 |
foreach ($boxObject as $direction => $value) { |
| 578 |
$finalBox["{$property}-{$direction}"] = isset($key[$direction]) ? $key[$direction] : null; |
| 579 |
} |
| 580 |
|
| 581 |
return $finalBox; |
| 582 |
} |
| 583 |
|
| 584 |
if ($property === "border-radius") { |
| 585 |
$finalBox["border-top-left-radius"] = $top; |
| 586 |
$finalBox["border-top-right-radius"] = $right; |
| 587 |
$finalBox["border-bottom-right-radius"] = $bottom; |
| 588 |
$finalBox["border-bottom-left-radius"] = $left; |
| 589 |
|
| 590 |
return $finalBox; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Retrieves the color based on the given type and color. |
| 596 |
* |
| 597 |
* @param string $type The type of color to retrieve. Can be either 'gradient' or 'color'. |
| 598 |
* @param string $color The color value. |
| 599 |
* @return string The retrieved color. |
| 600 |
*/ |
| 601 |
public static function get_color($type, $color) { |
| 602 |
|
| 603 |
if (empty($color) || strpos($color, "linear-gradient(") === 0 || strpos($color, "radial-gradient(") === 0 || strpos($color, '#') === 0) { |
| 604 |
return $color; |
| 605 |
} |
| 606 |
|
| 607 |
$color_parts = explode(',', $color, 2); |
| 608 |
|
| 609 |
|
| 610 |
$color_parts1 = $color_parts[0]; |
| 611 |
$color_parts2 = $color_parts[1]; |
| 612 |
|
| 613 |
$bg_color = ""; |
| 614 |
|
| 615 |
if($type == 'gradient'){ |
| 616 |
$bg_color = "var(--wp--preset--gradient--".$color_parts1.",".$color_parts2.")"; |
| 617 |
|
| 618 |
} else { |
| 619 |
$bg_color = "var(--wp--preset--color--".$color_parts1.",". $color_parts2.")"; |
| 620 |
} |
| 621 |
return $bg_color; |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* fill_background_generator |
| 626 |
* |
| 627 |
* Similar to fillBackgroundGenerator in roxSlider js helper |
| 628 |
* |
| 629 |
* @param array $background |
| 630 |
* @param string $device |
| 631 |
*/ |
| 632 |
public static function fill_background_generator($background, $device = "Desktop") { |
| 633 |
|
| 634 |
$fillBackground = [ |
| 635 |
'background-image' => '', |
| 636 |
]; |
| 637 |
|
| 638 |
if (isset($background['backgroundType']) && $background['backgroundType'] === 'classic') { |
| 639 |
$fillBackground['background-color'] = isset($background['backgroundColor']) ? self::get_color('color', $background['backgroundColor']) : ''; |
| 640 |
} |
| 641 |
|
| 642 |
if (isset($background['backgroundType']) && $background['backgroundType'] === 'gradient' && !empty($background['gradient'])) { |
| 643 |
|
| 644 |
$fillBackground['background-image'] = isset($background['gradient']) ? (self::get_color('gradient',$background['gradient']) ?? '') : ''; |
| 645 |
} |
| 646 |
|
| 647 |
if (isset($background['backgroundType']) && $background['backgroundType'] === 'image' && !empty($background['backgroundImage'])) { |
| 648 |
if (!empty($background['backgroundImage']['imageUrl'])) { |
| 649 |
$fillBackground['background-image'] = "url({$background['backgroundImage']['imageUrl']})"; |
| 650 |
} |
| 651 |
|
| 652 |
if (!empty($background['backgroundAttachment'])) { |
| 653 |
$fillBackground['background-attachment'] = $background['backgroundAttachment']; |
| 654 |
} |
| 655 |
|
| 656 |
if (!empty($background['backgroundPosition'][$device]) && $background['backgroundPosition'][$device] !== "custom") { |
| 657 |
$fillBackground['background-position'] = $background['backgroundPosition'][$device]; |
| 658 |
} |
| 659 |
|
| 660 |
if ( |
| 661 |
!empty($background['backgroundPosition'][$device]) && $background['backgroundPosition'][$device] === "custom" && |
| 662 |
!empty($background['customPositionX'][$device]) && !empty($background['customPositionY'][$device]) |
| 663 |
) { |
| 664 |
$fillBackground['background-position'] = self::get_slider_value($background['customPositionX'][$device]) . ' ' . self::get_slider_value($background['customPositionY'][$device]); |
| 665 |
} |
| 666 |
|
| 667 |
if (!empty($background['backgroundSize']) && $background['backgroundSize'] !== "custom") { |
| 668 |
$fillBackground['background-size'] = $background['backgroundSize']; |
| 669 |
} |
| 670 |
|
| 671 |
if ( |
| 672 |
!empty($background['backgroundSize']) && $background['backgroundSize'] === "custom" && |
| 673 |
!empty($background['customSize'][$device]) |
| 674 |
) { |
| 675 |
$fillBackground['background-size'] = self::get_slider_value($background['customSize'][$device]) . ' auto'; |
| 676 |
} |
| 677 |
|
| 678 |
if (!empty($background['backgroundRepeat'])) { |
| 679 |
$fillBackground['background-repeat'] = $background['backgroundRepeat']; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return $fillBackground; |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* get_typography_value |
| 688 |
* |
| 689 |
* Similar to getTypographyValue in roxSlider js helper |
| 690 |
*/ |
| 691 |
public static function get_typography_value($key, $device) { |
| 692 |
if ($device === 'Desktop') { |
| 693 |
return [ |
| 694 |
'font-family' => isset($key['fontFamily']['value']) ? $key['fontFamily']['value'] : null, |
| 695 |
'font-size' => isset($key['fontSize'][$device]['size']) && isset($key['fontSize'][$device]['unit']) |
| 696 |
? $key['fontSize'][$device]['size'] . $key['fontSize'][$device]['unit'] |
| 697 |
: null, |
| 698 |
'font-style' => isset($key['fontStyle']) ? $key['fontStyle'] : null, |
| 699 |
'font-weight' => isset($key['fontWeight']['value']) ? $key['fontWeight']['value'] : null, |
| 700 |
'text-decoration' => isset($key['textDecoration']) ? $key['textDecoration'] : null, |
| 701 |
'text-transform' => isset($key['textTransform']) ? $key['textTransform'] : null, |
| 702 |
'line-height' => isset($key['lineHeight'][$device]['size']) && isset($key['lineHeight'][$device]['unit']) |
| 703 |
? $key['lineHeight'][$device]['size'] . $key['lineHeight'][$device]['unit'] |
| 704 |
: null, |
| 705 |
'letter-spacing' => isset($key['letterSpacing'][$device]['size']) && isset($key['letterSpacing'][$device]['unit']) |
| 706 |
? $key['letterSpacing'][$device]['size'] . $key['letterSpacing'][$device]['unit'] |
| 707 |
: null, |
| 708 |
'word-spacing' => isset($key['wordSpacing'][$device]['size']) && isset($key['wordSpacing'][$device]['unit']) |
| 709 |
? $key['wordSpacing'][$device]['size'] . $key['wordSpacing'][$device]['unit'] |
| 710 |
: null, |
| 711 |
]; |
| 712 |
} else { |
| 713 |
return [ |
| 714 |
'font-size' => isset($key['fontSize'][$device]['size']) && isset($key['fontSize'][$device]['unit']) |
| 715 |
? $key['fontSize'][$device]['size'] . $key['fontSize'][$device]['unit'] |
| 716 |
: null, |
| 717 |
'line-height' => isset($key['lineHeight'][$device]['size']) && isset($key['lineHeight'][$device]['unit']) |
| 718 |
? $key['lineHeight'][$device]['size'] . $key['lineHeight'][$device]['unit'] |
| 719 |
: null, |
| 720 |
'letter-spacing' => isset($key['letterSpacing'][$device]['size']) && isset($key['letterSpacing'][$device]['unit']) |
| 721 |
? $key['letterSpacing'][$device]['size'] . $key['letterSpacing'][$device]['unit'] |
| 722 |
: null, |
| 723 |
'word-spacing' => isset($key['wordSpacing'][$device]['size']) && isset($key['wordSpacing'][$device]['unit']) |
| 724 |
? $key['wordSpacing'][$device]['size'] . $key['wordSpacing'][$device]['unit'] |
| 725 |
: null, |
| 726 |
]; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* get slider value |
| 732 |
* |
| 733 |
* Similar to getSliderValue function in JS helper |
| 734 |
* @param string $value |
| 735 |
* @return string |
| 736 |
*/ |
| 737 |
public static function get_slider_value($key) { |
| 738 |
$value = ''; |
| 739 |
|
| 740 |
if (!empty($key['size']) && !empty($key['unit'])) { |
| 741 |
$value = $key['size'] . $key['unit']; |
| 742 |
} elseif (!empty($key['size']) && empty($key['unit'])) { |
| 743 |
$value = $key['size']; |
| 744 |
} else { |
| 745 |
$value = null; |
| 746 |
} |
| 747 |
|
| 748 |
return $value; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* parse css |
| 753 |
* |
| 754 |
* @param string $css |
| 755 |
* @return string |
| 756 |
*/ |
| 757 |
public static function parse_css($raw_css) { |
| 758 |
|
| 759 |
$styles = []; |
| 760 |
$device_list = ['desktop', 'tablet', 'mobile', 'tabletlandscape', 'mobilelandscape', 'laptop', 'widescreen']; |
| 761 |
|
| 762 |
foreach ($device_list as $device) { |
| 763 |
$deviceStyles = $raw_css[$device] ?? []; |
| 764 |
|
| 765 |
$styles[$device] = array_map(function ($style) { |
| 766 |
if (!is_array($style) || !isset($style['selector'])) { |
| 767 |
return ''; |
| 768 |
} |
| 769 |
|
| 770 |
$selector = $style['selector']; |
| 771 |
$cssValues = array_filter($style, function ($value, $key) { |
| 772 |
return $key !== 'selector' && $value !== null && $value !== '' && !is_numeric($value) && !in_array($value, ['px', 'em', 'rem', '%', 'vh', 'vw']) && strpos($value, 'undefined') === false; |
| 773 |
}, ARRAY_FILTER_USE_BOTH); |
| 774 |
|
| 775 |
if (empty($cssValues)) { |
| 776 |
return ''; |
| 777 |
} |
| 778 |
|
| 779 |
return "{$selector} { " . implode(' ', array_map(function ($key, $value) { |
| 780 |
return "{$key}: {$value};"; |
| 781 |
}, array_keys($cssValues), $cssValues)) . " }"; |
| 782 |
}, $deviceStyles); |
| 783 |
} |
| 784 |
|
| 785 |
$device_styles = array_map(function ($style) { |
| 786 |
return implode("\n", $style); |
| 787 |
}, $styles); |
| 788 |
|
| 789 |
return $device_styles; |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Check if the block is a roxSlider block. |
| 794 |
* |
| 795 |
* @param string $attrs |
| 796 |
* @return bool |
| 797 |
*/ |
| 798 |
public static function is_table_builder_block($block_content, $parsed_block, $attrs = '', $attrs2 = '') { |
| 799 |
// Check if $block_content is not empty |
| 800 |
$hasBlockContent = !empty($block_content); |
| 801 |
// Check if $block['blockName'] is not empty and contains 'roxSlider' |
| 802 |
$hasValidBlockName = isset($parsed_block['blockName']) && strpos($parsed_block['blockName'], 'tablebuilder') !== false; |
| 803 |
|
| 804 |
// Check if $block['attrs']['blockClass'] is not empty |
| 805 |
$hasBlockClass = !empty($attrs) && !empty($attrs2) |
| 806 |
? !empty($parsed_block['attrs'][$attrs][$attrs2]) |
| 807 |
: !empty($parsed_block['attrs'][$attrs] ?? ''); |
| 808 |
|
| 809 |
if(empty($attrs) && empty($attrs2)) { |
| 810 |
$hasBlockClass = true; |
| 811 |
} |
| 812 |
|
| 813 |
// Return true if all conditions are met |
| 814 |
return $hasBlockContent && $hasValidBlockName && $hasBlockClass; |
| 815 |
} |
| 816 |
|
| 817 |
/** |
| 818 |
* Retrieves the link attributes based on the provided attribute array. |
| 819 |
* |
| 820 |
* @param array $attribute The attribute array containing the link data. |
| 821 |
* @return string The generated link attributes as a string. |
| 822 |
*/ |
| 823 |
public static function get_link_attributes($attribute) { |
| 824 |
if (empty($attribute['url'])) return ''; |
| 825 |
|
| 826 |
$link_data = []; |
| 827 |
|
| 828 |
$link_data['href'] = esc_url($attribute['url'], wp_allowed_protocols()); |
| 829 |
|
| 830 |
(isset($attribute['newTab']) && $attribute['newTab']) ? $link_data['target'] = '_blank' : ''; |
| 831 |
|
| 832 |
(isset($attribute['noFollow']) && $attribute['noFollow']) ? $link_data['rel'] = "nofollow" : ''; |
| 833 |
|
| 834 |
if (isset($attribute['customAttributes']) && gettype($attribute['customAttributes']) == 'array') { |
| 835 |
foreach ($attribute['customAttributes'] as $key => $value) { |
| 836 |
if (!empty($value)) { |
| 837 |
$attr_key_value = explode('|', $value); |
| 838 |
|
| 839 |
$attr_key = mb_strtolower($attr_key_value[0]); |
| 840 |
|
| 841 |
// Not allowed characters are removed. |
| 842 |
preg_match('/[-_a-z0-9]+/', $attr_key, $attr_key_matches); |
| 843 |
|
| 844 |
if (empty($attr_key_matches[0])) { |
| 845 |
continue; |
| 846 |
} |
| 847 |
|
| 848 |
$attr_key = $attr_key_matches[0]; |
| 849 |
|
| 850 |
// Javascript events and unescaped href are avoided. |
| 851 |
if ('href' === $attr_key || 'on' === substr($attr_key, 0, 2)) { |
| 852 |
continue; |
| 853 |
} |
| 854 |
|
| 855 |
if (isset($attr_key_value[1])) { |
| 856 |
$attr_value = trim($attr_key_value[1]); |
| 857 |
} else { |
| 858 |
$attr_value = ''; |
| 859 |
} |
| 860 |
|
| 861 |
$link_data[$attr_key] = $attr_value; |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
$link_attributes = ''; |
| 867 |
foreach ($link_data as $key => $value) { |
| 868 |
$link_attributes .= sprintf('%s="%s" ', $key, esc_attr($value)); |
| 869 |
} |
| 870 |
|
| 871 |
return $link_attributes; |
| 872 |
} |
| 873 |
|
| 874 |
public static function status() { |
| 875 |
|
| 876 |
$cached = wp_cache_get('table_builder__license_status'); |
| 877 |
|
| 878 |
if(false !== $cached) { |
| 879 |
return $cached; |
| 880 |
} |
| 881 |
|
| 882 |
$oppai = get_option('__table_builder_oppai__', ''); |
| 883 |
$key = get_option('__table_builder_license_key__', ''); |
| 884 |
$status = 'invalid'; |
| 885 |
|
| 886 |
if($oppai != '' && $key != '') { |
| 887 |
$status = 'valid'; |
| 888 |
} |
| 889 |
|
| 890 |
wp_cache_set('table_builder__license_status', $status); |
| 891 |
|
| 892 |
return $status; |
| 893 |
} |
| 894 |
|
| 895 |
public static function is_local() { |
| 896 |
$valid_domains = [ |
| 897 |
".academy", ".accountant", ".accountants", ".actor", ".adult", ".africa", ".agency", ".airforce", |
| 898 |
".apartments", ".app", ".army", ".art", ".asia", ".associates", ".attorney", ".auction", ".audio", |
| 899 |
".auto", ".baby", ".band", ".bar", ".bargains", ".beer", ".berlin", ".best", ".bid", ".bike", |
| 900 |
".bingo", ".bio", ".biz", ".black", ".blackfriday", ".blog", ".blue", ".boston", ".boutique", |
| 901 |
".build", ".builders", ".business", ".buzz", ".cab", ".cafe", ".cam", ".camera", ".camp", |
| 902 |
".capital", ".car", ".cards", ".care", ".careers", ".cars", ".casa", ".cash", ".casino", |
| 903 |
".catering", ".center", ".ceo", ".chat", ".cheap", ".christmas", ".church", ".city", ".claims", |
| 904 |
".cleaning", ".click", ".clinic", ".clothing", ".cloud", ".club", ".coach", ".codes", ".coffee", |
| 905 |
".college", ".com", ".community", ".company", ".computer", ".condos", ".construction", |
| 906 |
".consulting", ".contact", ".contractors", ".cooking", ".cool", ".country", ".coupons", |
| 907 |
".courses", ".credit", ".creditcard", ".cricket", ".cruises", ".cymru", ".cyou", ".dance", |
| 908 |
".date", ".dating", ".day", ".deals", ".degree", ".delivery", ".democrat", ".dental", |
| 909 |
".dentist", ".desi", ".design", ".dev", ".diamonds", ".diet", ".digital", ".direct", ".directory", |
| 910 |
".discount", ".doctor", ".dog", ".domains", ".download", ".earth", ".eco", ".education", |
| 911 |
".email", ".energy", ".engineer", ".engineering", ".enterprises", ".equipment", ".estate", |
| 912 |
".events", ".exchange", ".expert", ".exposed", ".express", ".fail", ".faith", ".family", |
| 913 |
".fans", ".farm", ".fashion", ".feedback", ".film", ".finance", ".financial", ".fish", |
| 914 |
".fishing", ".fit", ".fitness", ".flights", ".florist", ".flowers", ".football", ".forsale", |
| 915 |
".foundation", ".fun", ".fund", ".furniture", ".futbol", ".fyi", ".gallery", ".game", ".games", |
| 916 |
".garden", ".gay", ".gdn", ".gift", ".gifts", ".gives", ".glass", ".global", ".gmbh", ".gold", |
| 917 |
".golf", ".graphics", ".gratis", ".green", ".gripe", ".group", ".guide", ".guitars", ".guru", |
| 918 |
".hamburg", ".haus", ".health", ".healthcare", ".help", ".hiphop", ".hockey", ".holdings", |
| 919 |
".holiday", ".horse", ".host", ".hosting", ".house", ".how", ".icu", ".immo", ".immobilien", |
| 920 |
".inc", ".industries", ".info", ".ink", ".institute", ".insure", ".international", ".investments", |
| 921 |
".irish", ".jetzt", ".jewelry", ".juegos", ".kaufen", ".kim", ".kitchen", ".kiwi", ".krd", |
| 922 |
".kyoto", ".land", ".lat", ".lawyer", ".lease", ".legal", ".lgbt", ".life", ".lighting", |
| 923 |
".limited", ".limo", ".link", ".live", ".llc", ".loan", ".loans", ".lol", ".london", ".love", |
| 924 |
".ltd", ".ltda", ".luxury", ".maison", ".management", ".market", ".marketing", ".mba", ".media", |
| 925 |
".melbourne", ".memorial", ".men", ".menu", ".miami", ".mobi", ".moda", ".moe", ".mom", ".money", |
| 926 |
".monster", ".mortgage", ".movie", ".nagoya", ".name", ".navy", ".net", ".network", ".new", |
| 927 |
".news", ".ninja", ".nyc", ".observer", ".okinawa", ".one", ".onl", ".online", ".org", ".osaka", |
| 928 |
".page", ".paris", ".partners", ".parts", ".party", ".photo", ".photography", ".photos", ".pics", |
| 929 |
".pictures", ".pink", ".pizza", ".place", ".plumbing", ".plus", ".poker", ".porn", ".press", |
| 930 |
".pro", ".productions", ".properties", ".property", ".protection", ".pub", ".racing", ".realty", |
| 931 |
".recipes", ".red", ".rehab", ".reise", ".reisen", ".rent", ".rentals", ".repair", ".report", |
| 932 |
".republican", ".rest", ".restaurant", ".review", ".reviews", ".rip", ".rocks", ".rodeo", |
| 933 |
".run", ".ryukyu", ".sale", ".sarl", ".school", ".schule", ".science", ".security", ".services", |
| 934 |
".sex", ".sexy", ".shiksha", ".shoes", ".shop", ".shopping", ".show", ".singles", ".site", |
| 935 |
".ski", ".soccer", ".social", ".software", ".solar", ".solutions", ".soy", ".space", ".storage", |
| 936 |
".store", ".stream", ".studio", ".study", ".style", ".sucks", ".supplies", ".supply", ".support", |
| 937 |
".surf", ".surgery", ".sydney", ".systems", ".tattoo", ".tax", ".taxi", ".team", ".tech", |
| 938 |
".technology", ".tel", ".tennis", ".theater", ".theatre", ".tienda", ".tips", ".tires", ".today", |
| 939 |
".tokyo", ".tools", ".top", ".tours", ".town", ".toys", ".trade", ".training", ".travel", |
| 940 |
".tube", ".university", ".uno", ".vacations", ".vegas", ".ventures", ".vet", ".viajes", ".video", |
| 941 |
".villas", ".vin", ".vip", ".vision", ".vodka", ".vote", ".voting", ".voto", ".voyage", ".wales", |
| 942 |
".watch", ".webcam", ".website", ".wedding", ".wiki", ".win", ".wine", ".work", ".works", |
| 943 |
".world", ".wtf", ".xn--3ds443g", ".xn--6frz82g", ".xxx", ".xyz", ".yoga", ".yokohama", ".zone" |
| 944 |
]; |
| 945 |
|
| 946 |
$host = !empty($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; |
| 947 |
|
| 948 |
// get the domain |
| 949 |
$domain = explode('.', $host); |
| 950 |
if(count($domain) >= 2) { |
| 951 |
$domain = '.' . $domain[count($domain) - 1]; |
| 952 |
} else { |
| 953 |
$domain = null; |
| 954 |
} |
| 955 |
|
| 956 |
return !in_array($domain, $valid_domains); |
| 957 |
} |
| 958 |
} |
| 959 |
|