PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / Utils.php
kubio / lib / src / Core Last commit date
Background 2 years ago Blocks 3 years ago GlobalElements 3 years ago Layout 4 years ago License 2 years ago Separators 4 years ago StyleManager 2 years ago Styles 4 years ago Activation.php 2 years ago Backup.php 4 years ago CustomizerImporter.php 3 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 2 years ago ElementBase.php 4 years ago Importer.php 2 years ago InnerBlocks.php 4 years ago LodashBasic.php 2 years ago Registry.php 3 years ago Utils.php 2 years ago
Utils.php
783 lines
1 <?php
2
3
4 namespace Kubio\Core;
5
6 use IlluminateAgnostic\Arr\Support\Arr;
7 use Kubio\Config;
8 use Kubio\Flags;
9 use \WP_Error;
10
11 class Utils {
12
13
14 private static $execute_start_time;
15 protected static $wooIsActive = null;
16 protected static $kubioShopIsActive = null;
17 protected static $currentPageIsWooPage = null;
18 protected static $currentPageIsWooTemplate = null;
19 public static $kubioShopSupportedThemes = array( 'elevate-wp', 'pathway', 'pixy', 'ketos', 'consus', 'zeka', 'niveau', 'rainbow' );
20 public static $themeIsSupportedForShop = null;
21
22 public static function mapHideClassesByMedia(
23 $hiddenByMedia,
24 $negateValue = false
25 ) {
26 $mapHideClassesByMedia = array();
27 foreach ( $hiddenByMedia as $media => $isHidden ) {
28 if ( $negateValue ) {
29 $isHidden = ! $isHidden;
30 }
31 if ( $isHidden ) {
32 array_push( $mapHideClassesByMedia, "kubio-hide-on-$media" );
33 }
34 }
35
36 return $mapHideClassesByMedia;
37 }
38
39 public static function useJSComponentProps( $name, $settings = array() ) {
40 $prefix = Config::$name;
41
42 return array(
43 "data-{$prefix}-component" => $name,
44 "data-{$prefix}-settings" => wp_json_encode( $settings ),
45 );
46 }
47
48 public static function getLinkAttributes( $linkObject ) {
49 $defaultValue = array(
50 'value' => '',
51 'typeOpenLink' => 'sameWindow',
52 'noFollow' => false,
53 'lightboxMedia' => '',
54 );
55 $mergedLinkObject = LodashBasic::merge( array(), $defaultValue, $linkObject );
56 $linkAttributes = array(
57 'href' => null,
58 'target' => null,
59 'rel' => null,
60 'data-kubio-component' => null,
61 );
62
63 if ( $mergedLinkObject ) {
64 if ( $mergedLinkObject['value'] ) {
65 $linkAttributes['href'] = $mergedLinkObject['value'];
66 }
67 $linkType = LodashBasic::get( $mergedLinkObject, 'typeOpenLink', '' );
68 if ( $linkType === 'newWindow' ) {
69 $linkAttributes['target'] = '_blank';
70 }
71
72 if ( $linkType === 'lightbox' ) {
73 $lightboxType = $mergedLinkObject['lightboxMedia'];
74 if ( $lightboxType === '' ) {
75 $lightboxType = null;
76 }
77 $linkAttributes['data-default-type'] = $lightboxType;
78 $linkAttributes['data-fancybox'] = rand() . '';
79 }
80 if ( $mergedLinkObject['noFollow'] ) {
81 $linkAttributes['rel'] = 'nofollow';
82 }
83 }
84
85 return $linkAttributes;
86 }
87
88 public static function shortcodeDecode( $data ) {
89 return urldecode( base64_decode( $data ) );
90 }
91
92 public static function getDefaultAssetsURL( $url ) {
93 $staticUrl = kubio_url( 'static/default-assets' );
94
95 return $staticUrl . '/' . ltrim( $url, '/' );
96 }
97
98 public static function canEdit() {
99 return current_user_can( 'edit_theme_options' ) && current_user_can( 'edit_posts' );
100 }
101
102 public static function getEmptyShortcodePlaceholder() {
103 if ( is_user_logged_in() ) {
104 return static::getFrontendPlaceHolder(
105 sprintf(
106 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
107 __( 'Shortcode is empty.', 'kubio' ),
108 __( 'Edit this page to insert a shortcode or delete this block.', 'kubio' )
109 )
110 );
111 } else {
112 return '';
113 }
114
115 }
116
117 public static function getEmptyPlaceholder( $block_name, $items_type ) {
118 if ( is_user_logged_in() ) {
119 return static::getFrontendPlaceHolder(
120 sprintf(
121 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
122 __( sprintf( '%s has no %s.', $block_name, $items_type ), 'kubio' ),
123 __( sprintf( 'Edit this page to insert %s or delete this block.', $items_type ), 'kubio' )
124 )
125 );
126 }
127
128 return '';
129 }
130
131 //the production build does not include the patterns' folder, we can use this to determine if the build is dev or prod
132 public static function isProduction() {
133 $isProd = ! file_exists( KUBIO_ROOT_DIR . '/static/patterns/content-converted.json' );
134
135 return $isProd;
136 }
137
138 public static function getFrontendPlaceHolder( $message, $options = array() ) {
139
140 $options = array_merge(
141 array(
142 'info' => true,
143 'title' => __( 'Kubio info', 'kubio' ),
144 'if_logged' => true,
145 ),
146 $options
147 );
148
149 if ( $options['if_logged'] ) {
150 if ( ! is_user_logged_in() ) {
151 return;
152 }
153 }
154
155 if ( is_callable( $message ) ) {
156 $message = call_user_func( $message );
157 }
158
159 $info = '';
160 if ( $options['info'] ) {
161 $info = sprintf(
162 '<div class="kubio-frontent-placeholder--info">' .
163 ' <div class="kubio-frontent-placeholder--logo">%s</div>' .
164 ' <div class="kubio-frontent-placeholder--title">%s</div>' .
165 '</div>',
166 wp_kses_post( KUBIO_LOGO_SVG ),
167 $options['title']
168 );
169 }
170
171 return sprintf( '<div class="kubio-frontent-placeholder"><div>%s</div><div>%s</div></div>', $info, $message );
172 }
173
174 public static function kubioCacheGet( $name, $default = null ) {
175
176 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
177 $value = $default;
178
179 if ( self::kubioCacheHas( $name ) ) {
180 $value = $kubio_cache[ $name ];
181 }
182
183 return $value;
184
185 }
186
187 public static function kubioCacheHas( $name ) {
188 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
189
190 return array_key_exists( $name, $kubio_cache );
191 }
192
193 public static function kubioCacheSet( $name, $value ) {
194 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
195 $kubio_cache[ $name ] = $value;
196
197 $GLOBALS['__kubio_plugin_cache__'] = $kubio_cache;
198
199 }
200
201 /**
202 * Remove empty branches from array
203 *
204 * @param array $array the array to walk
205 *
206 * @return array
207 */
208 public static function arrayRecursiveRemoveEmptyBranches( array &$array ) {
209 foreach ( $array as $key => &$value ) {
210 if ( is_array( $value ) ) {
211 $array[ $key ] = static::arrayRecursiveRemoveEmptyBranches( $value );
212
213 if ( empty( $value ) ) {
214 unset( $array[ $key ] );
215 }
216 }
217 }
218
219 return $array;
220 }
221
222 public static function walkBlocks( &$blocks, $callback ) {
223 array_walk(
224 $blocks,
225 function ( &$block ) use ( $callback ) {
226 if ( isset( $block['blockName'] ) ) {
227 $callback( $block );
228 }
229 if ( isset( $block['innerBlocks'] ) ) {
230 static::walkBlocks( $block['innerBlocks'], $callback );
231 }
232 }
233 );
234 }
235
236 public static function kses( $text, $allowed_protocols = array() ) {
237
238 static $allowed_html;
239
240 if ( ! $allowed_html ) {
241 $allowed_html = wp_kses_allowed_html( 'post' );
242 }
243
244 // fix the issue with rgb / rgba colors in style atts
245
246 $rgbRegex = '#rgb\(((?:\s*\d+\s*,){2}\s*[\d]+)\)#i';
247 $text = preg_replace( $rgbRegex, 'rgb__$1__rgb', $text );
248
249 $rgbaRegex = '#rgba\(((\s*\d+\s*,){3}[\d\.]+)\)#i';
250 $text = preg_replace( $rgbaRegex, 'rgba__$1__rgb', $text );
251
252 $text = wp_kses( $text, $allowed_html, $allowed_protocols );
253
254 $text = str_replace( 'rgba__', 'rgba(', $text );
255 $text = str_replace( 'rgb__', 'rgb(', $text );
256 $text = str_replace( '__rgb', ')', $text );
257
258 return $text;
259 }
260
261 /**
262 * Compares version string to WP base version ( e.g. X.Y.Z without looking for -beta* -RC* suffixes )
263 *
264 * @param string $compare_to - semver version number
265 * @param string $operator - version_compare operator
266 * @return void
267 */
268 public static function wpVersionCompare( $compare_to, $operator ) {
269 global $wp_version;
270 $version_parts = sscanf( $wp_version, '%d.%d.%d' );
271 $version = array();
272
273 foreach ( $version_parts as $version_part ) {
274 if ( $version_part !== null ) {
275 $version[] = $version_part;
276 }
277 }
278
279 $version = implode( '.', $version );
280 return version_compare( $version, $compare_to, $operator );
281 }
282
283 public static function ksesSVG( $svg_content ) {
284 $allowed_html = wp_kses_allowed_html( 'post' );
285 return wp_kses( $svg_content, $allowed_html );
286 }
287
288 /**
289 * Check if the execution time has enough remaining seconds
290 *
291 * @param integer $compare_to_time - necessary time in seconds
292 * @return boolean
293 */
294 public static function hasEnoughRemainingTime( $compare_to_time = 10 ) {
295
296 if ( ! static::$execute_start_time ) {
297 static::$execute_start_time = intval( Arr::get( $_SERVER, 'REQUEST_TIME_FLOAT', time() ) );
298 }
299
300 $diff = time() - static::$execute_start_time;
301
302 $max_exec_time = @ini_get( 'max_execution_time' );
303
304 // assume 30 seconds if not available
305 if ( ! $max_exec_time ) {
306 $max_exec_time = 30;
307 }
308
309 return ( intval( $max_exec_time ) - $diff >= $compare_to_time );
310 }
311
312 /**
313 * Check if current WordPress installation validates plugin requirements
314 *
315 * @return boolean|\WP_Error
316 */
317 public static function validateRequirements() {
318 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
319 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
320 $required_wp = ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : false;
321 $required_php = ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : false;
322
323 if ( defined( 'KUBIO_MINIMUM_WP_VERSION' ) && KUBIO_MINIMUM_WP_VERSION ) {
324 $required_wp = KUBIO_MINIMUM_WP_VERSION;
325 }
326
327 $compatible_wp = $required_wp ? Utils::wpVersionCompare( $required_wp, '>=' ) : true;
328 $compatible_php = version_compare( phpversion(), $required_php, '>=' );
329
330 $php_update_message = '</p><p>' . sprintf(
331 /* translators: %s: URL to Update PHP page. */
332 __( '<a href="%s">Learn more about updating PHP</a>' ),
333 esc_url( wp_get_update_php_url() )
334 );
335
336 $update_wp_core = sprintf(
337 /* translators: %s: URL to Update PHP page. */
338 __( '<a href="%s">Update WordPress now!</a>', 'kubio' ),
339 esc_url( admin_url( 'update-core.php' ) )
340 );
341
342 if ( ! $compatible_wp && ! $compatible_php ) {
343 return new WP_Error(
344 'plugin_wp_php_incompatible',
345 '<p>' . sprintf(
346 /* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */
347 _x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'kubio' ),
348 get_bloginfo( 'version' ),
349 phpversion(),
350 $plugin_headers['Name'],
351 $required_wp,
352 $required_php
353 ) . $php_update_message . '<br/>' . $update_wp_core . '</p>'
354 );
355 } elseif ( ! $compatible_php ) {
356 return new WP_Error(
357 'plugin_php_incompatible',
358 '<p>' . sprintf(
359 /* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */
360 _x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'kubio' ),
361 phpversion(),
362 $plugin_headers['Name'],
363 $required_php
364 ) . $php_update_message . '</p>'
365 );
366 } elseif ( ! $compatible_wp ) {
367 return new WP_Error(
368 'plugin_wp_incompatible',
369 '<p>' . sprintf(
370 /* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */
371 _x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'kubio' ),
372 get_bloginfo( 'version' ),
373 $plugin_headers['Name'],
374 $required_wp
375 ) . '&nbsp;' . $update_wp_core . '</p>'
376 );
377 }
378
379 }
380
381 public static function getPluginVersions( $skip_current = false ) {
382 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
383 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
384 $version = ! empty( $plugin_headers['Version'] ) ? $plugin_headers['Version'] : false;
385 $name = ! empty( $plugin_headers['Name'] ) ? $plugin_headers['Name'] : false;
386 $url = apply_filters(
387 'kubio/previous-versions/url',
388 sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s.json', KUBIO_SLUG )
389 );
390
391 $response = wp_remote_get( $url );
392
393 if ( is_wp_error( $response ) ) {
394 return null;
395 }
396
397 $response = wp_remote_retrieve_body( $response );
398
399 if ( is_serialized( $response ) ) {
400 $response = maybe_unserialize( $response );
401 } else {
402 $response = json_decode( $response );
403 }
404
405 if ( ! is_object( $response ) ) {
406 return null;
407 }
408 if ( ! isset( $response->versions ) ) {
409 return null;
410 }
411
412 $versions = array();
413 foreach ( $response->versions as $key => $value ) {
414
415 $version = is_object( $value ) ? $value->version : $key;
416
417 if ( $version === 'trunk' ) {
418 continue;
419 }
420
421 if ( $skip_current && $version === \KUBIO_VERSION ) {
422 continue;
423 }
424
425 $versions[ $version ] = array(
426 'version' => $version,
427 'named_version' => sprintf( '%s v%s', $name, $version ),
428 'url' => is_object( $value ) ? $value->file : $value,
429 );
430 }
431
432 return $versions;
433 }
434
435 public static function getCloudURL( $url = '', $cloud_root_url = KUBIO_CLOUD_URL ) {
436 $url = trim( $url, '/' );
437
438 $args = array(
439 'kubio_version' => KUBIO_VERSION,
440 'kubio_build' => KUBIO_BUILD_NUMBER,
441 );
442
443 $is_skip_cache_flag_on = defined( 'KUBIO_SKIP_CLOUD_CACHE' ) && KUBIO_SKIP_CLOUD_CACHE;
444
445 if ( Utils::isDebug() || $is_skip_cache_flag_on ) {
446 $args[ 'kbp_' . time() ] = time();
447 }
448
449 $url = add_query_arg( $args, rtrim( "$cloud_root_url/$url", '/' ) );
450
451 return $url;
452 }
453
454 public static function getStarterSitesURL() {
455 $base_url = KUBIO_CLOUD_URL;
456 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
457 $base_url = KUBIO_STARTER_SITES_BASE_URL;
458 }
459 $relative_url = '/api/project/demo-sites';
460 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
461 $relative_url = "$relative_url/?testing=true";
462 }
463 return Utils::getCloudURL( $relative_url, $base_url );
464 }
465
466 public static function getStarterPartsURL() {
467 $base_url = KUBIO_CLOUD_URL;
468 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
469 $base_url = KUBIO_STARTER_SITES_BASE_URL;
470 }
471 $relative_url = '/api/demo-sites/get-demo-content';
472 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
473 $relative_url = "$relative_url/?testing=true";
474 }
475 return self::getCloudURL( $relative_url, $base_url );
476 }
477
478 public static function getSnippetsURL( $path = '' ) {
479 $base_url = KUBIO_CLOUD_URL;
480 $path = trim( $path, '/' );
481 $relative_path = "/api/snippets/$path";
482 if ( defined( 'KUBIO_SNIPPETS_BASE_URL' ) ) {
483 $base_url = KUBIO_SNIPPETS_BASE_URL;
484 }
485
486 return self::getCloudURL( $relative_path, $base_url );
487
488 }
489
490 public static function getGlobalSnippetsURL() {
491 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
492 return self::getSnippetsURL( '/globals?testing=true' );
493 }
494
495 return self::getSnippetsURL( '/globals' );
496 }
497
498 public static function getGlobalSnippetsCategoriesURL() {
499 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
500 return self::getSnippetsURL( '/categories?testing=true' );
501 }
502
503 return self::getSnippetsURL( '/categories' );
504
505 }
506 public static function getGlobalSnippetsTagsURL() {
507
508 return self::getSnippetsURL( '/tags' );
509 }
510
511 public static function isDebug() {
512 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
513 }
514
515 public static function isCLI() {
516 return defined( 'WP_CLI' ) && WP_CLI;
517 }
518
519
520 /**
521 * return and unique autoinc id based on prefix
522 *
523 * @param string $prefix
524 * @return string
525 */
526 public static function uniqueId( $prefix = '' ) {
527 static $state;
528
529 if ( ! is_array( $state ) ) {
530 $state = array();
531 }
532
533 if ( ! isset( $state[ $prefix ] ) ) {
534 $state[ $prefix ] = 0;
535 }
536
537 $id = $state[ $prefix ]++;
538
539 return $id;
540 }
541
542 public static function getFilePath( $path ) {
543 return KUBIO_ROOT_DIR . "/$path";
544 }
545
546 public static function getURL( $path ) {
547 return KUBIO_ROOT_URL . "/$path";
548 }
549 public static function getWooIsActive() {
550 if ( static::$wooIsActive !== null ) {
551 return static::$wooIsActive;
552 }
553 $wooPluginName = 'woocommerce/woocommerce.php';
554 $activePlugins = get_option( 'active_plugins', array() );
555 static::$wooIsActive = in_array( $wooPluginName, $activePlugins );
556
557 return static::$wooIsActive;
558 }
559
560 //For the demo we only show shop content and features if the shop plugin was already activated.
561 //When shop will be released this function will always return true. This is so we can lunch kubio with the shop features
562 //But only enale them to a select few.
563 public static function getKubioShopFeatureIsActivated() {
564 return static::getKubioShopIsActive();
565 }
566
567 public static function getKubioShopIsActive() {
568 if ( static::$kubioShopIsActive !== null ) {
569 return static::$kubioShopIsActive;
570 }
571 $is_kubio_shop_active = false;
572 $activePlugins = get_option( 'active_plugins', array() );
573 $kubioShopPluginsName = array( 'kubio-shop/plugin.php', 'kubio-shop-pro/plugin.php' );
574 foreach ( $kubioShopPluginsName as $pluginName ) {
575 if ( in_array( $pluginName, $activePlugins ) ) {
576 $is_kubio_shop_active = true;
577 }
578 }
579
580 static::$kubioShopIsActive = apply_filters( 'kubio_shop/is_kubio_shop_active', $is_kubio_shop_active );
581
582 return static::$kubioShopIsActive;
583 }
584
585 public static function getThemeIsSupportedForShop() {
586 if ( static::$themeIsSupportedForShop !== null ) {
587 return static::$themeIsSupportedForShop;
588 }
589
590 //The filter is not good enough because theme code gets called after plugin code so if we want to stop some logic
591 //when files are loading we can't with the filter.
592 //return apply_filters( 'kubio/has_block_templates_support', false );
593
594 $theme_is_supported = true;
595
596 if ( ! in_array( get_option( 'template' ), static::$kubioShopSupportedThemes ) ) {
597
598 $theme_is_supported = false;
599 } else {
600 $is_customize_page = ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) );
601 $theme = get_template();
602 if ( isset( $_GET['theme'] ) && $_GET['theme'] != get_stylesheet() ) {
603 $theme = sanitize_text_field( $_GET['theme'] );
604 }
605
606 //if is theme preview
607 if ( $is_customize_page && ! in_array( $theme, static::$kubioShopSupportedThemes ) ) {
608 $theme_is_supported = false;
609 }
610 }
611
612 static::$themeIsSupportedForShop = $theme_is_supported;
613 return static::$themeIsSupportedForShop;
614 }
615 public static function getShowShopContent() {
616 return static::getKubioShopFeatureIsActivated() && static::getWooIsActive() && static::getKubioShopIsActive() && static::getThemeIsSupportedForShop();
617 }
618
619 public static function getCurrentPageIsWooPage() {
620 if ( static::$currentPageIsWooPage !== null ) {
621 return static::$currentPageIsWooPage;
622 }
623
624 if ( ! static::getWooIsActive() ) {
625 static::$currentPageIsWooPage = false;
626 return static::$currentPageIsWooPage;
627 }
628
629 $is_woo_page = is_cart() || is_checkout() || is_account_page();
630
631 static::$currentPageIsWooPage = $is_woo_page;
632 return static::$currentPageIsWooPage;
633 }
634 public static function getCurrentPageIsWooTemplate() {
635 if ( static::$currentPageIsWooTemplate !== null ) {
636 return static::$currentPageIsWooTemplate;
637 }
638
639 if ( ! static::getWooIsActive() ) {
640 static::$currentPageIsWooTemplate = false;
641 return static::$currentPageIsWooTemplate;
642 }
643
644 $is_shop_page = \kubio_woocommerce_is_product_archive_page();
645 $is_woo_template = is_product() || $is_shop_page;
646 static::$currentPageIsWooTemplate = $is_woo_template;
647 return static::$currentPageIsWooTemplate;
648 }
649
650 public static function getCurrentPageIsWoo() {
651 return static::getCurrentPageIsWooPage() || static::getCurrentPageIsWooTemplate();
652 }
653
654 public static function getWooIsUpgradedToShopBlocks() {
655 return static::getKubioShopFeatureIsActivated() &&
656 static::getWooIsActive() &&
657 static::getKubioShopIsActive() &&
658 static::getWooIsUpgradedToShopBlocksSetting();
659 }
660 public static function getWooIsUpgradedToShopBlocksSetting() {
661 return Flags::getSetting( 'kubioShop.convertedShortcodesToBlocks', false );
662 }
663 /**
664 * Check if the referer is the Kubio editor page
665 *
666 * @return boolean
667 */
668 public static function hasKubioEditorReferer() {
669 $referer = wp_get_referer();
670
671 if ( ! $referer ) {
672 return false;
673 }
674
675 if ( strpos( $referer, admin_url( 'admin.php' ) ) !== 0 ) {
676 return false;
677 }
678
679 parse_str( parse_url( $referer, PHP_URL_QUERY ), $args );
680
681 return Arr::get( $args, 'page', null ) === 'kubio';
682 }
683
684 public static function kubioGetEditorURL( $args = array() ) {
685 return add_query_arg(
686 array_merge( array( 'page' => 'kubio' ), $args ),
687 admin_url( 'admin.php' )
688 );
689 }
690
691 public static function isTrue( $value ) {
692
693 if ( empty( $value ) ) {
694 return false;
695 }
696
697 return in_array( $value, array( 'on', 'true', '1', 1, true ), true );
698 }
699
700 public static function isFalse( $value ) {
701 return ! static::isTrue( $value );
702 }
703
704
705 public static function isRestRequest() {
706 return defined( 'REST_REQUEST' ) && REST_REQUEST;
707 }
708
709 public static function maybeJSONDecode( $data ) {
710 if ( ! is_string( $data ) ) {
711 return $data;
712 }
713
714 $decoded = json_decode( $data, true );
715
716 if ( json_last_error() === JSON_ERROR_NONE ) {
717 return $decoded;
718 }
719
720 $decoded = json_decode( urldecode( $data ), true );
721
722 if ( json_last_error() === JSON_ERROR_NONE ) {
723 return $decoded;
724 }
725
726 return $data;
727
728 }
729
730 public static function isTryOnlineEnabled() {
731 return kubio_is_pro() ? false : apply_filters( 'kubio/enable_try_online', false );
732 }
733
734 public static function humanizeArray( $array, $spacer = "\t", $prefix = '', $level = 0 ) {
735
736 if ( ! is_array( $array ) ) {
737 return $array;
738 }
739
740 $decorators = array( '-', '*', '#' );
741 $decorator = $decorators[ $level % count( $decorators ) ];
742 $structure_text_lines = array();
743 $indent = str_repeat( $spacer, $level );
744
745 foreach ( $array as $index => $value ) {
746
747 $index = is_numeric( $index ) ? intval( $index ) + 1 : $index;
748
749 list($label, $desc) = array_replace( array( '', '' ), explode( '#', $prefix . strval( $index ) ) );
750
751 if ( $desc ) {
752 $desc = " ( {$desc} ),";
753 }
754
755 if ( is_array( $value ) ) {
756 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}", ', ' );
757 $structure_text_lines[] = static::humanizeArray( $value, $spacer, $prefix, $level + 1 );
758 } else {
759
760 if ( is_string( $value ) ) {
761 $lines = explode( "\n", $value );
762 if ( count( $lines ) > 1 ) {
763 $line_prefix = str_repeat( $spacer, $level + 1 );
764 foreach ( $lines as $l_index => $line ) {
765 $lines[ $l_index ] = "{$line_prefix}{$line}";
766 }
767
768 $value = "\n" . implode( "\n", $lines );
769 }
770 }
771
772 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}{$value}", ', ' );
773 }
774 }
775
776 if ( $level === 0 ) {
777 return trim( implode( "\n", $structure_text_lines ) );
778 }
779
780 return implode( "\n", $structure_text_lines ) . "\n";
781 }
782 }
783