PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.5.2
Kubio AI Page Builder v2.5.2
2.8.5 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 1 year ago Blocks 1 year ago GlobalElements 1 year ago Layout 1 year ago License 1 year ago Separators 1 year ago StyleManager 1 year ago Styles 1 year ago Activation.php 1 year ago Backup.php 1 year ago CustomizerImporter.php 1 year ago Deactivation.php 1 year ago EditInKubioCustomizerPanel.php 1 year ago Element.php 1 year ago ElementBase.php 4 years ago Importer.php 1 year ago InnerBlocks.php 1 year ago KubioFrontPageRevertNotice.php 1 year ago LodashBasic.php 1 year ago Registry.php 1 year ago Utils.php 1 year ago
Utils.php
848 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'] = wp_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 public static function getEmptyPlaceholder( $block_name, $items_type ) {
117 if ( is_user_logged_in() ) {
118 return static::getFrontendPlaceHolder(
119 sprintf(
120 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
121 // translators: %1$s: block name, %2$s: items type
122 sprintf( __( '%1$s has no %2$s.', 'kubio' ), $block_name, $items_type ),
123 // translators: %s: items type
124 sprintf( __( 'Edit this page to insert %s or delete this block.', 'kubio' ), $items_type )
125 )
126 );
127 }
128
129 return '';
130 }
131
132 //the production build does not include the patterns' folder, we can use this to determine if the build is dev or prod
133 public static function isProduction() {
134 $isProd = ! file_exists( KUBIO_ROOT_DIR . '/static/patterns/content-converted.json' );
135
136 return $isProd;
137 }
138
139 public static function getFrontendPlaceHolder( $message, $options = array() ) {
140
141 $options = array_merge(
142 array(
143 'info' => true,
144 'title' => __( 'Kubio info', 'kubio' ),
145 'if_logged' => true,
146 ),
147 $options
148 );
149
150 if ( $options['if_logged'] ) {
151 if ( ! is_user_logged_in() ) {
152 return;
153 }
154 }
155
156 if ( is_callable( $message ) ) {
157 $message = call_user_func( $message );
158 }
159
160 $info = '';
161 if ( $options['info'] ) {
162 $info = sprintf(
163 '<div class="kubio-frontent-placeholder--info">' .
164 ' <div class="kubio-frontent-placeholder--logo">%s</div>' .
165 ' <div class="kubio-frontent-placeholder--title">%s</div>' .
166 '</div>',
167 wp_kses_post( KUBIO_LOGO_SVG ),
168 $options['title']
169 );
170 }
171
172 return sprintf( '<div class="kubio-frontent-placeholder"><div>%s</div><div>%s</div></div>', $info, $message );
173 }
174
175 public static function kubioCacheGet( $name, $default = null ) {
176
177 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
178 $value = $default;
179
180 if ( self::kubioCacheHas( $name ) ) {
181 $value = $kubio_cache[ $name ];
182 }
183
184 return $value;
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 * Remove empty branches from array
202 *
203 * @param array $array the array to walk
204 *
205 * @return array
206 */
207 public static function arrayRecursiveRemoveEmptyBranches( array &$array ) {
208 foreach ( $array as $key => &$value ) {
209 if ( is_array( $value ) ) {
210 $array[ $key ] = static::arrayRecursiveRemoveEmptyBranches( $value );
211
212 if ( empty( $value ) ) {
213 unset( $array[ $key ] );
214 }
215 }
216 }
217
218 return $array;
219 }
220
221 public static function walkBlocks( &$blocks, $callback ) {
222 array_walk(
223 $blocks,
224 function ( &$block ) use ( $callback ) {
225 if ( isset( $block['blockName'] ) ) {
226 $callback( $block );
227 }
228 if ( isset( $block['innerBlocks'] ) ) {
229 static::walkBlocks( $block['innerBlocks'], $callback );
230 }
231 }
232 );
233 }
234
235 public static function kses( $text, $allowed_protocols = array() ) {
236
237 static $allowed_html;
238
239 if ( ! $allowed_html ) {
240 $allowed_html = wp_kses_allowed_html( 'post' );
241 }
242
243 // fix the issue with rgb / rgba colors in style atts
244
245 $rgbRegex = '#rgb\(((?:\s*\d+\s*,){2}\s*[\d]+)\)#i';
246 $text = preg_replace( $rgbRegex, 'rgb__$1__rgb', $text );
247
248 $rgbaRegex = '#rgba\(((\s*\d+\s*,){3}[\d\.]+)\)#i';
249 $text = preg_replace( $rgbaRegex, 'rgba__$1__rgb', $text );
250
251 $text = wp_kses( $text, $allowed_html, $allowed_protocols );
252
253 $text = str_replace( 'rgba__', 'rgba(', $text );
254 $text = str_replace( 'rgb__', 'rgb(', $text );
255 $text = str_replace( '__rgb', ')', $text );
256
257 return $text;
258 }
259
260 /**
261 * Compares version string to WP base version ( e.g. X.Y.Z without looking for -beta* -RC* suffixes )
262 *
263 * @param string $compare_to - semver version number
264 * @param string $operator - version_compare operator
265 * @return void
266 */
267 public static function wpVersionCompare( $compare_to, $operator ) {
268 global $wp_version;
269 $version_parts = sscanf( $wp_version, '%d.%d.%d' );
270 $version = array();
271
272 foreach ( $version_parts as $version_part ) {
273 if ( $version_part !== null ) {
274 $version[] = $version_part;
275 }
276 }
277
278 $version = implode( '.', $version );
279 return version_compare( $version, $compare_to, $operator );
280 }
281
282 public static function ksesSVG( $svg_content ) {
283 $allowed_html = wp_kses_allowed_html( 'post' );
284 return wp_kses( $svg_content, $allowed_html );
285 }
286
287 /**
288 * Check if the execution time has enough remaining seconds
289 *
290 * @param integer $compare_to_time - necessary time in seconds
291 * @return boolean
292 */
293 public static function hasEnoughRemainingTime( $compare_to_time = 10 ) {
294
295 if ( ! static::$execute_start_time ) {
296 static::$execute_start_time = intval( Arr::get( $_SERVER, 'REQUEST_TIME_FLOAT', time() ) );
297 }
298
299 $diff = time() - static::$execute_start_time;
300
301 $max_exec_time = @ini_get( 'max_execution_time' );
302
303 // assume 30 seconds if not available
304 if ( ! $max_exec_time ) {
305 $max_exec_time = 30;
306 }
307
308 return ( intval( $max_exec_time ) - $diff >= $compare_to_time );
309 }
310
311 /**
312 * Check if current WordPress installation validates plugin requirements
313 *
314 * @return boolean|\WP_Error
315 */
316 public static function validateRequirements() {
317 include_once ABSPATH . 'wp-admin/includes/plugin.php';
318 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
319 $required_wp = ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : false;
320 $required_php = ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : false;
321
322 if ( defined( 'KUBIO_MINIMUM_WP_VERSION' ) && KUBIO_MINIMUM_WP_VERSION ) {
323 $required_wp = KUBIO_MINIMUM_WP_VERSION;
324 }
325
326 $compatible_wp = $required_wp ? Utils::wpVersionCompare( $required_wp, '>=' ) : true;
327 $compatible_php = version_compare( phpversion(), $required_php, '>=' );
328
329 $php_update_message = '</p><p>' . sprintf(
330 /* translators: %s: URL to Update PHP page. */
331 __( '<a href="%s">Learn more about updating PHP</a>', 'kubio' ),
332 esc_url( wp_get_update_php_url() )
333 );
334
335 $update_wp_core = sprintf(
336 /* translators: %s: URL to Update PHP page. */
337 __( '<a href="%s">Update WordPress now!</a>', 'kubio' ),
338 esc_url( admin_url( 'update-core.php' ) )
339 );
340
341 if ( ! $compatible_wp && ! $compatible_php ) {
342 return new WP_Error(
343 'plugin_wp_php_incompatible',
344 '<p>' . sprintf(
345 /* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */
346 __( '<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' ),
347 get_bloginfo( 'version' ),
348 phpversion(),
349 $plugin_headers['Name'],
350 $required_wp,
351 $required_php
352 ) . $php_update_message . '<br/>' . $update_wp_core . '</p>'
353 );
354 } elseif ( ! $compatible_php ) {
355 return new WP_Error(
356 'plugin_php_incompatible',
357 '<p>' . sprintf(
358 /* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */
359 __( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'kubio' ),
360 phpversion(),
361 $plugin_headers['Name'],
362 $required_php
363 ) . $php_update_message . '</p>'
364 );
365 } elseif ( ! $compatible_wp ) {
366 return new WP_Error(
367 'plugin_wp_incompatible',
368 '<p>' . sprintf(
369 /* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */
370 __( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'kubio' ),
371 get_bloginfo( 'version' ),
372 $plugin_headers['Name'],
373 $required_wp
374 ) . '&nbsp;' . $update_wp_core . '</p>'
375 );
376 }
377 }
378
379 public static function getPluginVersions( $skip_current = false ) {
380 include_once ABSPATH . 'wp-admin/includes/plugin.php';
381 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
382 $version = ! empty( $plugin_headers['Version'] ) ? $plugin_headers['Version'] : false;
383 $name = ! empty( $plugin_headers['Name'] ) ? $plugin_headers['Name'] : false;
384 $url = apply_filters(
385 'kubio/previous-versions/url',
386 sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s.json', KUBIO_SLUG )
387 );
388
389 $response = wp_remote_get( $url );
390
391 if ( is_wp_error( $response ) ) {
392 return null;
393 }
394
395 $response = wp_remote_retrieve_body( $response );
396
397 if ( is_serialized( $response ) ) {
398 $response = maybe_unserialize( $response );
399 } else {
400 $response = json_decode( $response );
401 }
402
403 if ( ! is_object( $response ) ) {
404 return null;
405 }
406 if ( ! isset( $response->versions ) ) {
407 return null;
408 }
409
410 $versions = array();
411 foreach ( $response->versions as $key => $value ) {
412
413 $version = is_object( $value ) ? $value->version : $key;
414
415 if ( $version === 'trunk' ) {
416 continue;
417 }
418
419 if ( $skip_current && $version === \KUBIO_VERSION ) {
420 continue;
421 }
422
423 $versions[ $version ] = array(
424 'version' => $version,
425 'named_version' => sprintf( '%s v%s', $name, $version ),
426 'url' => is_object( $value ) ? $value->file : $value,
427 );
428 }
429
430 return $versions;
431 }
432
433 public static function getCloudURL( $url = '', $cloud_root_url = KUBIO_CLOUD_URL ) {
434 $url = trim( $url, '/' );
435
436 $args = array(
437 'kubio_version' => KUBIO_VERSION,
438 'kubio_build' => KUBIO_BUILD_NUMBER,
439 'activated_on' => Flags::get( 'kubio_activation_time', '' ),
440 );
441
442 $is_skip_cache_flag_on = defined( 'KUBIO_SKIP_CLOUD_CACHE' ) && KUBIO_SKIP_CLOUD_CACHE;
443
444 if ( Utils::isDebug() || $is_skip_cache_flag_on ) {
445 $args[ 'kbp_' . time() ] = time();
446 }
447
448 $url = add_query_arg( $args, rtrim( "$cloud_root_url/$url", '/' ) );
449
450 return $url;
451 }
452
453 public static function getStarterSitesURL() {
454 $base_url = KUBIO_CLOUD_URL;
455 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
456 $base_url = KUBIO_STARTER_SITES_BASE_URL;
457 }
458 $relative_url = '/api/project/demo-sites';
459 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
460 $relative_url = "$relative_url/?testing=true";
461 }
462 return Utils::getCloudURL( $relative_url, $base_url );
463 }
464
465 public static function getStarterPartsURL() {
466 $base_url = KUBIO_CLOUD_URL;
467 if ( defined( 'KUBIO_STARTER_SITES_BASE_URL' ) && KUBIO_STARTER_SITES_BASE_URL ) {
468 $base_url = KUBIO_STARTER_SITES_BASE_URL;
469 }
470 $relative_url = '/api/demo-sites/get-demo-content';
471 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES ) {
472 $relative_url = "$relative_url/?testing=true";
473 }
474 return self::getCloudURL( $relative_url, $base_url );
475 }
476
477 public static function getSnippetsURL( $path = '' ) {
478 $base_url = KUBIO_CLOUD_URL;
479 $path = trim( $path, '/' );
480 $relative_path = "/api/snippets/$path";
481 if ( defined( 'KUBIO_SNIPPETS_BASE_URL' ) ) {
482 $base_url = KUBIO_SNIPPETS_BASE_URL;
483 }
484
485 return self::getCloudURL( $relative_path, $base_url );
486 }
487
488 public static function getGlobalSnippetsURL() {
489 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
490 return self::getSnippetsURL( '/globals?testing=true' );
491 }
492
493 return self::getSnippetsURL( '/globals' );
494 }
495
496 public static function getGlobalSnippetsCategoriesURL() {
497 if ( defined( 'KUBIO_INCLUDE_TEST_SNIPPETS' ) && KUBIO_INCLUDE_TEST_SNIPPETS ) {
498 return self::getSnippetsURL( '/categories?testing=true' );
499 }
500
501 return self::getSnippetsURL( '/categories' );
502 }
503 public static function getGlobalSnippetsTagsURL() {
504
505 return self::getSnippetsURL( '/tags' );
506 }
507
508 public static function isDebug() {
509 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
510 }
511
512 public static function isCLI() {
513 return defined( 'WP_CLI' ) && WP_CLI;
514 }
515
516
517 /**
518 * return and unique autoinc id based on prefix
519 *
520 * @param string $prefix
521 * @return string
522 */
523 public static function uniqueId( $prefix = '' ) {
524 static $state;
525
526 if ( ! is_array( $state ) ) {
527 $state = array();
528 }
529
530 if ( ! isset( $state[ $prefix ] ) ) {
531 $state[ $prefix ] = 0;
532 }
533
534 $id = $state[ $prefix ]++;
535
536 return $id;
537 }
538
539 public static function getFilePath( $path ) {
540 return KUBIO_ROOT_DIR . "/$path";
541 }
542
543 public static function getURL( $path ) {
544 return KUBIO_ROOT_URL . "/$path";
545 }
546 public static function getWooIsActive() {
547 if ( static::$wooIsActive !== null ) {
548 return static::$wooIsActive;
549 }
550 $wooPluginName = 'woocommerce/woocommerce.php';
551 $activePlugins = get_option( 'active_plugins', array() );
552 static::$wooIsActive = in_array( $wooPluginName, $activePlugins );
553
554 return static::$wooIsActive;
555 }
556
557 //For the demo we only show shop content and features if the shop plugin was already activated.
558 //When shop will be released this function will always return true. This is so we can lunch kubio with the shop features
559 //But only enale them to a select few.
560 public static function getKubioShopFeatureIsActivated() {
561 return static::getKubioShopIsActive();
562 }
563
564 public static function getKubioShopIsActive() {
565 if ( static::$kubioShopIsActive !== null ) {
566 return static::$kubioShopIsActive;
567 }
568 $is_kubio_shop_active = false;
569 $activePlugins = get_option( 'active_plugins', array() );
570 $kubioShopPluginsName = array( 'kubio-shop/plugin.php', 'kubio-shop-pro/plugin.php' );
571 foreach ( $kubioShopPluginsName as $pluginName ) {
572 if ( in_array( $pluginName, $activePlugins ) ) {
573 $is_kubio_shop_active = true;
574 }
575 }
576
577 static::$kubioShopIsActive = apply_filters( 'kubio_shop/is_kubio_shop_active', $is_kubio_shop_active );
578
579 return static::$kubioShopIsActive;
580 }
581
582 public static function getThemeIsSupportedForShop() {
583 if ( static::$themeIsSupportedForShop !== null ) {
584 return static::$themeIsSupportedForShop;
585 }
586
587 //The filter is not good enough because theme code gets called after plugin code so if we want to stop some logic
588 //when files are loading we can't with the filter.
589 //return apply_filters( 'kubio/has_block_templates_support', false );
590
591 $theme_is_supported = true;
592
593 if ( ! in_array( get_option( 'template' ), static::$kubioShopSupportedThemes ) ) {
594
595 $theme_is_supported = false;
596 } else {
597 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
598 $is_customize_page = ( is_admin() && isset( $_SERVER['PHP_SELF'] ) && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) );
599 $theme = get_template();
600 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
601 if ( isset( $_GET['theme'] ) && $_GET['theme'] !== get_stylesheet() ) {
602 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.NonceVerification.Recommended
603 $theme = sanitize_text_field( $_GET['theme'] );
604 }
605
606 //if is theme preview
607 if ( $is_customize_page && ! in_array( $theme, static::$kubioShopSupportedThemes, true ) ) {
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( wp_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 public static function isTryOnlineEnabled() {
730 return apply_filters( 'kubio/enable_try_online', false );
731 }
732
733 public static function humanizeArray( $array, $spacer = "\t", $prefix = '', $level = 0 ) {
734
735 if ( ! is_array( $array ) ) {
736 return $array;
737 }
738
739 $decorators = array( '-', '*', '#' );
740 $decorator = $decorators[ $level % count( $decorators ) ];
741 $structure_text_lines = array();
742 $indent = str_repeat( $spacer, $level );
743
744 foreach ( $array as $index => $value ) {
745
746 $index = is_numeric( $index ) ? intval( $index ) + 1 : $index;
747
748 list($label, $desc) = array_replace( array( '', '' ), explode( '#', $prefix . strval( $index ) ) );
749
750 if ( $desc ) {
751 $desc = " ( {$desc} ),";
752 }
753
754 if ( is_array( $value ) ) {
755 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}", ', ' );
756 $structure_text_lines[] = static::humanizeArray( $value, $spacer, $prefix, $level + 1 );
757 } else {
758
759 if ( is_string( $value ) ) {
760 $lines = explode( "\n", $value );
761 if ( count( $lines ) > 1 ) {
762 $line_prefix = str_repeat( $spacer, $level + 1 );
763 foreach ( $lines as $l_index => $line ) {
764 $lines[ $l_index ] = "{$line_prefix}{$line}";
765 }
766
767 $value = "\n" . implode( "\n", $lines );
768 }
769 }
770
771 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}{$value}", ', ' );
772 }
773 }
774
775 if ( $level === 0 ) {
776 return trim( implode( "\n", $structure_text_lines ) );
777 }
778
779 return implode( "\n", $structure_text_lines ) . "\n";
780 }
781
782 public static function pageHasCustomTemplate( $post_id = null ): bool {
783 if ( ! $post_id ) {
784 $post_id = get_the_ID();
785 }
786
787 $template_slug = get_post_meta( $post_id, '_wp_page_template', true );
788 if ( $template_slug && $template_slug !== 'default' ) {
789 return true;
790 }
791
792 return false;
793 }
794
795 public static function getThemeTemplateParts() {
796 $default_language = apply_filters( 'wpml_default_language', null );
797 $query = new \WP_Query(
798 array(
799 'post_type' => 'wp_template_part',
800 'post_status' => array( 'publish' ),
801 'posts_per_page' => - 1,
802 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
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
816 if ( $query->have_posts() ) {
817 return array_map(
818 function ( $post ) {
819 return $post->post_name;
820 },
821 $query->posts
822 );
823 }
824
825 return null;
826 }
827
828 public static function getPageTemplate( $post_id ) {
829 if ( ! $post_id ) {
830 return null;
831 }
832
833 $permalink = get_permalink( $post_id );
834 $url = add_query_arg( '_wp-find-template', 'true', $permalink );
835 $res = wp_remote_get( $url );
836 $data = wp_remote_retrieve_body( $res );
837 $body = json_decode( $data );
838
839 if ( $body ) {
840 // FSE theme
841 return $body->data;
842 }
843
844 // Non FSE theme
845 return null;
846 }
847 }
848