PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.6.3
Kubio AI Page Builder v2.6.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 11 months ago StyleManager 1 year ago Styles 1 year ago Activation.php 1 year ago Backup.php 1 year ago CustomizerImporter.php 11 months ago Deactivation.php 1 year ago EditInKubioCustomizerPanel.php 1 year ago Element.php 1 year ago ElementBase.php 4 years ago Importer.php 11 months ago InnerBlocks.php 1 year ago KubioFrontPageRevertNotice.php 1 year ago LodashBasic.php 1 year ago Registry.php 1 year ago ThirdPartyPluginAssetLoaderInEditor.php 11 months ago Utils.php 11 months ago
Utils.php
862 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 getShouldUseAiSitesWithTesting() {
509 $with_tests = defined( 'KUBIO_INCLUDE_TEST_AI_SITES' ) && \KUBIO_INCLUDE_TEST_AI_SITES;
510 $is_ai_site_editor = defined( 'KUBIO_IS_AI_SITE_EDITOR' ) && \KUBIO_IS_AI_SITE_EDITOR;
511 $with_tests = $with_tests || $is_ai_site_editor;
512 return $with_tests;
513 }
514 public static function isDebug() {
515 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
516 }
517
518 public static function isCLI() {
519 return defined( 'WP_CLI' ) && WP_CLI;
520 }
521
522
523
524 /**
525 * return and unique autoinc id based on prefix
526 *
527 * @param string $prefix
528 * @return string
529 */
530 public static function uniqueId( $prefix = '' ) {
531 static $state;
532
533 if ( ! is_array( $state ) ) {
534 $state = array();
535 }
536
537 if ( ! isset( $state[ $prefix ] ) ) {
538 $state[ $prefix ] = 0;
539 }
540
541 $id = $state[ $prefix ]++;
542
543 return $id;
544 }
545
546 public static function getFilePath( $path ) {
547 return KUBIO_ROOT_DIR . "/$path";
548 }
549
550 public static function getURL( $path ) {
551 return KUBIO_ROOT_URL . "/$path";
552 }
553 public static function getWooIsActive() {
554 if ( static::$wooIsActive !== null ) {
555 return static::$wooIsActive;
556 }
557 $wooPluginName = 'woocommerce/woocommerce.php';
558 $activePlugins = get_option( 'active_plugins', array() );
559 static::$wooIsActive = in_array( $wooPluginName, $activePlugins );
560
561 return static::$wooIsActive;
562 }
563
564 //For the demo we only show shop content and features if the shop plugin was already activated.
565 //When shop will be released this function will always return true. This is so we can lunch kubio with the shop features
566 //But only enale them to a select few.
567 public static function getKubioShopFeatureIsActivated() {
568 return static::getKubioShopIsActive();
569 }
570
571 public static function getKubioShopIsActive() {
572 if ( static::$kubioShopIsActive !== null ) {
573 return static::$kubioShopIsActive;
574 }
575 $is_kubio_shop_active = false;
576 $activePlugins = get_option( 'active_plugins', array() );
577 $kubioShopPluginsName = array( 'kubio-shop/plugin.php', 'kubio-shop-pro/plugin.php' );
578 foreach ( $kubioShopPluginsName as $pluginName ) {
579 if ( in_array( $pluginName, $activePlugins ) ) {
580 $is_kubio_shop_active = true;
581 }
582 }
583
584 static::$kubioShopIsActive = apply_filters( 'kubio_shop/is_kubio_shop_active', $is_kubio_shop_active );
585
586 return static::$kubioShopIsActive;
587 }
588
589 public static function getThemeIsSupportedForShop() {
590 if ( static::$themeIsSupportedForShop !== null ) {
591 return static::$themeIsSupportedForShop;
592 }
593
594 //The filter is not good enough because theme code gets called after plugin code so if we want to stop some logic
595 //when files are loading we can't with the filter.
596 //return apply_filters( 'kubio/has_block_templates_support', false );
597
598 $theme_is_supported = true;
599
600 if ( ! in_array( get_option( 'template' ), static::$kubioShopSupportedThemes ) ) {
601
602 $theme_is_supported = false;
603 } else {
604 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
605 $is_customize_page = ( is_admin() && isset( $_SERVER['PHP_SELF'] ) && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) );
606 $theme = get_template();
607 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
608 if ( isset( $_GET['theme'] ) && $_GET['theme'] !== get_stylesheet() ) {
609 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.NonceVerification.Recommended
610 $theme = sanitize_text_field( $_GET['theme'] );
611 }
612
613 //if is theme preview
614 if ( $is_customize_page && ! in_array( $theme, static::$kubioShopSupportedThemes, true ) ) {
615 $theme_is_supported = false;
616 }
617 }
618
619 static::$themeIsSupportedForShop = $theme_is_supported;
620 return static::$themeIsSupportedForShop;
621 }
622 public static function getShowShopContent() {
623 return static::getKubioShopFeatureIsActivated() && static::getWooIsActive() && static::getKubioShopIsActive() && static::getThemeIsSupportedForShop();
624 }
625
626 public static function getCurrentPageIsWooPage() {
627 if ( static::$currentPageIsWooPage !== null ) {
628 return static::$currentPageIsWooPage;
629 }
630
631 if ( ! static::getWooIsActive() ) {
632 static::$currentPageIsWooPage = false;
633 return static::$currentPageIsWooPage;
634 }
635
636 $is_woo_page = is_cart() || is_checkout() || is_account_page();
637
638 static::$currentPageIsWooPage = $is_woo_page;
639 return static::$currentPageIsWooPage;
640 }
641 public static function getCurrentPageIsWooTemplate() {
642 if ( static::$currentPageIsWooTemplate !== null ) {
643 return static::$currentPageIsWooTemplate;
644 }
645
646 if ( ! static::getWooIsActive() ) {
647 static::$currentPageIsWooTemplate = false;
648 return static::$currentPageIsWooTemplate;
649 }
650
651 $is_shop_page = \kubio_woocommerce_is_product_archive_page();
652 $is_woo_template = is_product() || $is_shop_page;
653 static::$currentPageIsWooTemplate = $is_woo_template;
654 return static::$currentPageIsWooTemplate;
655 }
656
657 public static function getCurrentPageIsWoo() {
658 return static::getCurrentPageIsWooPage() || static::getCurrentPageIsWooTemplate();
659 }
660
661 public static function getWooIsUpgradedToShopBlocks() {
662 return static::getKubioShopFeatureIsActivated() &&
663 static::getWooIsActive() &&
664 static::getKubioShopIsActive() &&
665 static::getWooIsUpgradedToShopBlocksSetting();
666 }
667 public static function getWooIsUpgradedToShopBlocksSetting() {
668 return Flags::getSetting( 'kubioShop.convertedShortcodesToBlocks', false );
669 }
670 /**
671 * Check if the referer is the Kubio editor page
672 *
673 * @return boolean
674 */
675 public static function hasKubioEditorReferer() {
676 $referer = wp_get_referer();
677
678 if ( ! $referer ) {
679 return false;
680 }
681
682 if ( strpos( $referer, admin_url( 'admin.php' ) ) !== 0 ) {
683 return false;
684 }
685
686 parse_str( wp_parse_url( $referer, PHP_URL_QUERY ), $args );
687
688 return Arr::get( $args, 'page', null ) === 'kubio';
689 }
690
691 public static function kubioGetEditorURL( $args = array() ) {
692 return add_query_arg(
693 array_merge( array( 'page' => 'kubio' ), $args ),
694 admin_url( 'admin.php' )
695 );
696 }
697
698 public static function isTrue( $value ) {
699
700 if ( empty( $value ) ) {
701 return false;
702 }
703
704 return in_array( $value, array( 'on', 'true', '1', 1, true ), true );
705 }
706
707 public static function isFalse( $value ) {
708 return ! static::isTrue( $value );
709 }
710
711
712 public static function isRestRequest() {
713 return defined( 'REST_REQUEST' ) && REST_REQUEST;
714 }
715
716 public static function maybeJSONDecode( $data ) {
717 if ( ! is_string( $data ) ) {
718 return $data;
719 }
720
721 $decoded = json_decode( $data, true );
722
723 if ( json_last_error() === JSON_ERROR_NONE ) {
724 return $decoded;
725 }
726
727 $decoded = json_decode( urldecode( $data ), true );
728
729 if ( json_last_error() === JSON_ERROR_NONE ) {
730 return $decoded;
731 }
732
733 return $data;
734 }
735
736 public static function isTryOnlineEnabled() {
737 return apply_filters( 'kubio/enable_try_online', false );
738 }
739
740 public static function humanizeArray( $array, $spacer = "\t", $prefix = '', $level = 0 ) {
741
742 if ( ! is_array( $array ) ) {
743 return $array;
744 }
745
746 $decorators = array( '-', '*', '#' );
747 $decorator = $decorators[ $level % count( $decorators ) ];
748 $structure_text_lines = array();
749 $indent = str_repeat( $spacer, $level );
750
751 foreach ( $array as $index => $value ) {
752
753 $index = is_numeric( $index ) ? intval( $index ) + 1 : $index;
754
755 list($label, $desc) = array_replace( array( '', '' ), explode( '#', $prefix . strval( $index ) ) );
756
757 if ( $desc ) {
758 $desc = " ( {$desc} ),";
759 }
760
761 if ( is_array( $value ) ) {
762 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}", ', ' );
763 $structure_text_lines[] = static::humanizeArray( $value, $spacer, $prefix, $level + 1 );
764 } else {
765
766 if ( is_string( $value ) ) {
767 $lines = explode( "\n", $value );
768 if ( count( $lines ) > 1 ) {
769 $line_prefix = str_repeat( $spacer, $level + 1 );
770 foreach ( $lines as $l_index => $line ) {
771 $lines[ $l_index ] = "{$line_prefix}{$line}";
772 }
773
774 $value = "\n" . implode( "\n", $lines );
775 }
776 }
777
778 $structure_text_lines[] = rtrim( "{$indent}{$decorator} {$label}: {$desc}{$value}", ', ' );
779 }
780 }
781
782 if ( $level === 0 ) {
783 return trim( implode( "\n", $structure_text_lines ) );
784 }
785
786 return implode( "\n", $structure_text_lines ) . "\n";
787 }
788
789 public static function pageHasCustomTemplate( $post_id = null ): bool {
790 if ( ! $post_id ) {
791 $post_id = get_the_ID();
792 }
793
794 $template_slug = get_post_meta( $post_id, '_wp_page_template', true );
795 if ( $template_slug && $template_slug !== 'default' ) {
796 return true;
797 }
798
799 return false;
800 }
801
802 public static function getThemeTemplateParts() {
803 $default_language = apply_filters( 'wpml_default_language', null );
804 $query = new \WP_Query(
805 array(
806 'post_type' => 'wp_template_part',
807 'post_status' => array( 'publish' ),
808 'posts_per_page' => - 1,
809 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
810 'tax_query' => array(
811 array(
812 'taxonomy' => 'wp_theme',
813 'field' => 'name',
814 'terms' => get_stylesheet(),
815 ),
816 ),
817 'kubio_filter' => array(
818 'language_code' => $default_language,
819 ),
820 )
821 );
822
823 if ( $query->have_posts() ) {
824 return array_map(
825 function ( $post ) {
826 return $post->post_name;
827 },
828 $query->posts
829 );
830 }
831
832 return null;
833 }
834
835 public static function getIsAISiteEditor() {
836 return defined('KUBIO_IS_AI_SITE_EDITOR') && \KUBIO_IS_AI_SITE_EDITOR;
837 }
838 public static function getPageTemplate( $post_id ) {
839 if ( ! $post_id ) {
840 return null;
841 }
842
843 $permalink = get_permalink( $post_id );
844 $url = add_query_arg( '_wp-find-template', 'true', $permalink );
845 $res = wp_remote_get( $url );
846 $data = wp_remote_retrieve_body( $res );
847 $body = json_decode( $data );
848
849 if ( $body ) {
850 // FSE theme
851 return $body->data;
852 }
853
854 // Non FSE theme
855 return null;
856 }
857
858 public static function getIsImageHubPluginActive() {
859 return defined('IMAGE_HUB_PLUGIN_NAME');
860 }
861 }
862