PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.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 1 year ago Blocks 1 year ago GlobalElements 3 years ago Layout 4 years ago License 2 years ago Separators 4 years ago StyleManager 1 year ago Styles 4 years ago Activation.php 1 year ago Backup.php 4 years ago CustomizerImporter.php 1 year 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 1 year ago
Utils.php
844 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'] = esc_url( $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'] = esc_attr( $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 'activated_on' => Flags::get( 'kubio_activation_time', '' ),
442 );
443
444 $is_skip_cache_flag_on = defined( 'KUBIO_SKIP_CLOUD_CACHE' ) && KUBIO_SKIP_CLOUD_CACHE;
445
446 if ( Utils::isDebug() || $is_skip_cache_flag_on ) {
447 $args[ 'kbp_' . time() ] = time();
448 }
449
450 $url = add_query_arg( $args, rtrim( "$cloud_root_url/$url", '/' ) );
451
452 return $url;
453 }
454
455 public static function getStarterSitesURL() {
456 $base_url = KUBIO_CLOUD_URL;
457 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
458 $base_url = KUBIO_STARTER_SITES_BASE_URL;
459 }
460 $relative_url = '/api/project/demo-sites';
461 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
462 $relative_url = "$relative_url/?testing=true";
463 }
464 return Utils::getCloudURL( $relative_url, $base_url );
465 }
466
467 public static function getStarterPartsURL() {
468 $base_url = KUBIO_CLOUD_URL;
469 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
470 $base_url = KUBIO_STARTER_SITES_BASE_URL;
471 }
472 $relative_url = '/api/demo-sites/get-demo-content';
473 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
474 $relative_url = "$relative_url/?testing=true";
475 }
476 return self::getCloudURL( $relative_url, $base_url );
477 }
478
479 public static function getSnippetsURL( $path = '' ) {
480 $base_url = KUBIO_CLOUD_URL;
481 $path = trim( $path, '/' );
482 $relative_path = "/api/snippets/$path";
483 if ( defined( 'KUBIO_SNIPPETS_BASE_URL' ) ) {
484 $base_url = KUBIO_SNIPPETS_BASE_URL;
485 }
486
487 return self::getCloudURL( $relative_path, $base_url );
488
489 }
490
491 public static function getGlobalSnippetsURL() {
492 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
493 return self::getSnippetsURL( '/globals?testing=true' );
494 }
495
496 return self::getSnippetsURL( '/globals' );
497 }
498
499 public static function getGlobalSnippetsCategoriesURL() {
500 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
501 return self::getSnippetsURL( '/categories?testing=true' );
502 }
503
504 return self::getSnippetsURL( '/categories' );
505
506 }
507 public static function getGlobalSnippetsTagsURL() {
508
509 return self::getSnippetsURL( '/tags' );
510 }
511
512 public static function isDebug() {
513 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
514 }
515
516 public static function isCLI() {
517 return defined( 'WP_CLI' ) && WP_CLI;
518 }
519
520
521 /**
522 * return and unique autoinc id based on prefix
523 *
524 * @param string $prefix
525 * @return string
526 */
527 public static function uniqueId( $prefix = '' ) {
528 static $state;
529
530 if ( ! is_array( $state ) ) {
531 $state = array();
532 }
533
534 if ( ! isset( $state[ $prefix ] ) ) {
535 $state[ $prefix ] = 0;
536 }
537
538 $id = $state[ $prefix ]++;
539
540 return $id;
541 }
542
543 public static function getFilePath( $path ) {
544 return KUBIO_ROOT_DIR . "/$path";
545 }
546
547 public static function getURL( $path ) {
548 return KUBIO_ROOT_URL . "/$path";
549 }
550 public static function getWooIsActive() {
551 if ( static::$wooIsActive !== null ) {
552 return static::$wooIsActive;
553 }
554 $wooPluginName = 'woocommerce/woocommerce.php';
555 $activePlugins = get_option( 'active_plugins', array() );
556 static::$wooIsActive = in_array( $wooPluginName, $activePlugins );
557
558 return static::$wooIsActive;
559 }
560
561 //For the demo we only show shop content and features if the shop plugin was already activated.
562 //When shop will be released this function will always return true. This is so we can lunch kubio with the shop features
563 //But only enale them to a select few.
564 public static function getKubioShopFeatureIsActivated() {
565 return static::getKubioShopIsActive();
566 }
567
568 public static function getKubioShopIsActive() {
569 if ( static::$kubioShopIsActive !== null ) {
570 return static::$kubioShopIsActive;
571 }
572 $is_kubio_shop_active = false;
573 $activePlugins = get_option( 'active_plugins', array() );
574 $kubioShopPluginsName = array( 'kubio-shop/plugin.php', 'kubio-shop-pro/plugin.php' );
575 foreach ( $kubioShopPluginsName as $pluginName ) {
576 if ( in_array( $pluginName, $activePlugins ) ) {
577 $is_kubio_shop_active = true;
578 }
579 }
580
581 static::$kubioShopIsActive = apply_filters( 'kubio_shop/is_kubio_shop_active', $is_kubio_shop_active );
582
583 return static::$kubioShopIsActive;
584 }
585
586 public static function getThemeIsSupportedForShop() {
587 if ( static::$themeIsSupportedForShop !== null ) {
588 return static::$themeIsSupportedForShop;
589 }
590
591 //The filter is not good enough because theme code gets called after plugin code so if we want to stop some logic
592 //when files are loading we can't with the filter.
593 //return apply_filters( 'kubio/has_block_templates_support', false );
594
595 $theme_is_supported = true;
596
597 if ( ! in_array( get_option( 'template' ), static::$kubioShopSupportedThemes ) ) {
598
599 $theme_is_supported = false;
600 } else {
601 $is_customize_page = ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) );
602 $theme = get_template();
603 if ( isset( $_GET['theme'] ) && $_GET['theme'] != get_stylesheet() ) {
604 $theme = sanitize_text_field( $_GET['theme'] );
605 }
606
607 //if is theme preview
608 if ( $is_customize_page && ! in_array( $theme, static::$kubioShopSupportedThemes ) ) {
609 $theme_is_supported = false;
610 }
611 }
612
613 static::$themeIsSupportedForShop = $theme_is_supported;
614 return static::$themeIsSupportedForShop;
615 }
616 public static function getShowShopContent() {
617 return static::getKubioShopFeatureIsActivated() && static::getWooIsActive() && static::getKubioShopIsActive() && static::getThemeIsSupportedForShop();
618 }
619
620 public static function getCurrentPageIsWooPage() {
621 if ( static::$currentPageIsWooPage !== null ) {
622 return static::$currentPageIsWooPage;
623 }
624
625 if ( ! static::getWooIsActive() ) {
626 static::$currentPageIsWooPage = false;
627 return static::$currentPageIsWooPage;
628 }
629
630 $is_woo_page = is_cart() || is_checkout() || is_account_page();
631
632 static::$currentPageIsWooPage = $is_woo_page;
633 return static::$currentPageIsWooPage;
634 }
635 public static function getCurrentPageIsWooTemplate() {
636 if ( static::$currentPageIsWooTemplate !== null ) {
637 return static::$currentPageIsWooTemplate;
638 }
639
640 if ( ! static::getWooIsActive() ) {
641 static::$currentPageIsWooTemplate = false;
642 return static::$currentPageIsWooTemplate;
643 }
644
645 $is_shop_page = \kubio_woocommerce_is_product_archive_page();
646 $is_woo_template = is_product() || $is_shop_page;
647 static::$currentPageIsWooTemplate = $is_woo_template;
648 return static::$currentPageIsWooTemplate;
649 }
650
651 public static function getCurrentPageIsWoo() {
652 return static::getCurrentPageIsWooPage() || static::getCurrentPageIsWooTemplate();
653 }
654
655 public static function getWooIsUpgradedToShopBlocks() {
656 return static::getKubioShopFeatureIsActivated() &&
657 static::getWooIsActive() &&
658 static::getKubioShopIsActive() &&
659 static::getWooIsUpgradedToShopBlocksSetting();
660 }
661 public static function getWooIsUpgradedToShopBlocksSetting() {
662 return Flags::getSetting( 'kubioShop.convertedShortcodesToBlocks', false );
663 }
664 /**
665 * Check if the referer is the Kubio editor page
666 *
667 * @return boolean
668 */
669 public static function hasKubioEditorReferer() {
670 $referer = wp_get_referer();
671
672 if ( ! $referer ) {
673 return false;
674 }
675
676 if ( strpos( $referer, admin_url( 'admin.php' ) ) !== 0 ) {
677 return false;
678 }
679
680 parse_str( parse_url( $referer, PHP_URL_QUERY ), $args );
681
682 return Arr::get( $args, 'page', null ) === 'kubio';
683 }
684
685 public static function kubioGetEditorURL( $args = array() ) {
686 return add_query_arg(
687 array_merge( array( 'page' => 'kubio' ), $args ),
688 admin_url( 'admin.php' )
689 );
690 }
691
692 public static function isTrue( $value ) {
693
694 if ( empty( $value ) ) {
695 return false;
696 }
697
698 return in_array( $value, array( 'on', 'true', '1', 1, true ), true );
699 }
700
701 public static function isFalse( $value ) {
702 return ! static::isTrue( $value );
703 }
704
705
706 public static function isRestRequest() {
707 return defined( 'REST_REQUEST' ) && REST_REQUEST;
708 }
709
710 public static function maybeJSONDecode( $data ) {
711 if ( ! is_string( $data ) ) {
712 return $data;
713 }
714
715 $decoded = json_decode( $data, true );
716
717 if ( json_last_error() === JSON_ERROR_NONE ) {
718 return $decoded;
719 }
720
721 $decoded = json_decode( urldecode( $data ), true );
722
723 if ( json_last_error() === JSON_ERROR_NONE ) {
724 return $decoded;
725 }
726
727 return $data;
728
729 }
730
731 public static function isTryOnlineEnabled() {
732 return kubio_is_pro() ? false : apply_filters( 'kubio/enable_try_online', false );
733 }
734
735 public static function humanizeArray( $array, $spacer = "\t", $prefix = '', $level = 0 ) {
736
737 if ( ! is_array( $array ) ) {
738 return $array;
739 }
740
741 $decorators = array( '-', '*', '#' );
742 $decorator = $decorators[ $level % count( $decorators ) ];
743 $structure_text_lines = array();
744 $indent = str_repeat( $spacer, $level );
745
746 foreach ( $array as $index => $value ) {
747
748 $index = is_numeric( $index ) ? intval( $index ) + 1 : $index;
749
750 list($label, $desc) = array_replace( array( '', '' ), explode( '#', $prefix . strval( $index ) ) );
751
752 if ( $desc ) {
753 $desc = " ( {$desc} ),";
754 }
755
756 if ( is_array( $value ) ) {
757 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}", ', ' );
758 $structure_text_lines[] = static::humanizeArray( $value, $spacer, $prefix, $level + 1 );
759 } else {
760
761 if ( is_string( $value ) ) {
762 $lines = explode( "\n", $value );
763 if ( count( $lines ) > 1 ) {
764 $line_prefix = str_repeat( $spacer, $level + 1 );
765 foreach ( $lines as $l_index => $line ) {
766 $lines[ $l_index ] = "{$line_prefix}{$line}";
767 }
768
769 $value = "\n" . implode( "\n", $lines );
770 }
771 }
772
773 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}{$value}", ', ' );
774 }
775 }
776
777 if ( $level === 0 ) {
778 return trim( implode( "\n", $structure_text_lines ) );
779 }
780
781 return implode( "\n", $structure_text_lines ) . "\n";
782 }
783
784 public static function pageHasCustomTemplate($post_id = null): bool {
785 if ( ! $post_id ) {
786 $post_id = get_the_ID();
787 }
788
789 $template_slug = get_post_meta( $post_id, '_wp_page_template', true );
790 if ( $template_slug && $template_slug !== 'default' ) {
791 return true;
792 }
793
794 return false;
795 }
796
797 public static function getThemeTemplateParts() {
798 $default_language = apply_filters( 'wpml_default_language', null );
799 $query = new \WP_Query( array(
800 'post_type' => 'wp_template_part',
801 'post_status' => [ 'publish' ],
802 'posts_per_page' => - 1,
803 'tax_query' => array(
804 array(
805 'taxonomy' => 'wp_theme',
806 'field' => 'name',
807 'terms' => get_stylesheet(),
808 )
809 ),
810 'kubio_filter' => array(
811 'language_code' => $default_language
812 )
813 ) );
814
815 if ( $query->have_posts() ) {
816 return array_map( function ( $post ) {
817 return $post->post_name;
818 }, $query->posts );
819 }
820
821 return null;
822 }
823
824 public static function getPageTemplate( $post_id ) {
825 if ( ! $post_id ) {
826 return null;
827 }
828
829 $permalink = get_permalink( $post_id );
830 $url = add_query_arg( '_wp-find-template', "true", $permalink );
831 $res = wp_remote_get( $url );
832 $data = wp_remote_retrieve_body( $res );
833 $body = json_decode( $data );
834
835 if ( $body ) {
836 // FSE theme
837 return $body->data;
838 }
839
840 // Non FSE theme
841 return null;
842 }
843 }
844