PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.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 3 years ago Blocks 3 years ago GlobalElements 4 years ago Layout 4 years ago License 4 years ago Separators 4 years ago StyleManager 3 years ago Styles 4 years ago Activation.php 3 years ago Backup.php 4 years ago CustomizerImporter.php 3 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 4 years ago ElementBase.php 4 years ago Importer.php 3 years ago InnerBlocks.php 4 years ago LodashBasic.php 4 years ago Registry.php 4 years ago Utils.php 3 years ago
Utils.php
455 lines
1 <?php
2
3
4 namespace Kubio\Core;
5
6 use IlluminateAgnostic\Arr\Support\Arr;
7 use Kubio\Config;
8 use Kubio\Env;
9 use function json_encode;
10 use \WP_Error;
11
12 class Utils {
13
14
15 private static $execute_start_time;
16
17 public static function mapHideClassesByMedia(
18 $hiddenByMedia,
19 $negateValue = false
20 ) {
21 $mapHideClassesByMedia = array();
22 foreach ( $hiddenByMedia as $media => $isHidden ) {
23 if ( $negateValue ) {
24 $isHidden = ! $isHidden;
25 }
26 if ( $isHidden ) {
27 array_push( $mapHideClassesByMedia, "kubio-hide-on-$media" );
28 }
29 }
30
31 return $mapHideClassesByMedia;
32 }
33
34 public static function useJSComponentProps( $name, $settings = array() ) {
35 $prefix = Config::$name;
36
37 return array(
38 "data-${prefix}-component" => $name,
39 "data-${prefix}-settings" => json_encode( $settings ),
40 );
41 }
42
43 public static function getLinkAttributes( $linkObject ) {
44 $defaultValue = array(
45 'value' => '',
46 'typeOpenLink' => 'sameWindow',
47 'noFollow' => false,
48 'lightboxMedia' => '',
49 );
50 $mergedLinkObject = LodashBasic::merge( array(), $defaultValue, $linkObject );
51 $linkAttributes = array(
52 'href' => null,
53 'target' => null,
54 'rel' => null,
55 'data-kubio-component' => null,
56 );
57
58 if ( $mergedLinkObject ) {
59 if ( $mergedLinkObject['value'] ) {
60 $linkAttributes['href'] = $mergedLinkObject['value'];
61 }
62 $linkType = LodashBasic::get( $mergedLinkObject, 'typeOpenLink', '' );
63 if ( $linkType === 'newWindow' ) {
64 $linkAttributes['target'] = '_blank';
65 }
66
67 if ( $linkType === 'lightbox' ) {
68 $lightboxType = $mergedLinkObject['lightboxMedia'];
69 if ( $lightboxType === '' ) {
70 $lightboxType = null;
71 }
72 $linkAttributes['data-default-type'] = $lightboxType;
73 $linkAttributes['data-fancybox'] = rand() . '';
74 }
75 if ( $mergedLinkObject['noFollow'] ) {
76 $linkAttributes['rel'] = 'nofollow';
77 }
78 }
79
80 return $linkAttributes;
81 }
82
83 public static function shortcodeDecode( $data ) {
84 return urldecode( base64_decode( $data ) );
85 }
86
87 public static function getDefaultAssetsUrl( $url ) {
88 $staticUrl = kubio_url( 'static/default-assets' );
89
90 return $staticUrl . '/' . ltrim( $url, '/' );
91 }
92
93 public static function canEdit() {
94 return current_user_can( 'edit_theme_options' ) && current_user_can( 'edit_posts' );
95 }
96
97 public static function getEmptyShortcodePlaceholder() {
98 if ( is_user_logged_in() ) {
99 return static::getFrontendPlaceHolder(
100 sprintf(
101 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
102 __( 'Shortcode is empty.', 'kubio' ),
103 __( 'Edit this page to insert a shortcode or delete this block.', 'kubio' )
104 )
105 );
106 } else {
107 return '';
108 }
109
110 }
111
112 //the production build does not include the patterns folder, we can use this to determine if the build is dev or prod
113 public static function isProduction() {
114 $isProd = ! file_exists( KUBIO_ROOT_DIR . '/static/patterns/content-converted.json' );
115
116 return $isProd;
117 }
118
119 public static function getFrontendPlaceHolder( $message, $options = array() ) {
120
121 $options = array_merge(
122 array(
123 'info' => true,
124 'title' => __( 'Kubio info', 'kubio' ),
125 'if_logged' => true,
126 ),
127 $options
128 );
129
130 if ( $options['if_logged'] ) {
131 if ( ! is_user_logged_in() ) {
132 return;
133 }
134 }
135
136 if ( is_callable( $message ) ) {
137 $message = call_user_func( $message );
138 }
139
140 $info = '';
141 if ( $options['info'] ) {
142 $info = sprintf(
143 '<div class="kubio-frontent-placeholder--info">' .
144 ' <div class="kubio-frontent-placeholder--logo">%s</div>' .
145 ' <div class="kubio-frontent-placeholder--title">%s</div>' .
146 '</div>',
147 wp_kses_post( KUBIO_LOGO_SVG ),
148 $options['title']
149 );
150 }
151
152 return sprintf( '<div class="kubio-frontent-placeholder"><div>%s</div><div>%s</div></div>', $info, $message );
153 }
154
155 public static function kubioCacheGet( $name, $default = null ) {
156
157 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
158 $value = $default;
159
160 if ( self::kubioCacheHas( $name ) ) {
161 $value = $kubio_cache[ $name ];
162 }
163
164 return $value;
165
166 }
167
168 public static function kubioCacheHas( $name ) {
169 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
170
171 return array_key_exists( $name, $kubio_cache );
172 }
173
174 public static function kubioCacheSet( $name, $value ) {
175 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
176 $kubio_cache[ $name ] = $value;
177
178 $GLOBALS['__kubio_plugin_cache__'] = $kubio_cache;
179
180 }
181
182 /**
183 * Remove empty branches from array
184 *
185 * @param array $array the array to walk
186 *
187 * @return array
188 */
189 public static function arrayRecursiveRemoveEmptyBranches( array &$array ) {
190 foreach ( $array as $key => &$value ) {
191 if ( is_array( $value ) ) {
192 $array[ $key ] = static::arrayRecursiveRemoveEmptyBranches( $value );
193
194 if ( empty( $value ) ) {
195 unset( $array[ $key ] );
196 }
197 }
198 }
199
200 return $array;
201 }
202
203 public static function walkBlocks( &$blocks, $callback ) {
204 array_walk(
205 $blocks,
206 function ( &$block ) use ( $callback ) {
207 if ( isset( $block['blockName'] ) ) {
208 $callback( $block );
209 }
210 if ( isset( $block['innerBlocks'] ) ) {
211 static::walkBlocks( $block['innerBlocks'], $callback );
212 }
213 }
214 );
215 }
216
217 public static function kses( $text, $allowed_protocols = array() ) {
218
219 static $allowed_html;
220
221 if ( ! $allowed_html ) {
222 $allowed_html = wp_kses_allowed_html( 'post' );
223 }
224
225 // fix the issue with rgb / rgba colors in style atts
226
227 $rgbRegex = '#rgb\(((?:\s*\d+\s*,){2}\s*[\d]+)\)#i';
228 $text = preg_replace( $rgbRegex, 'rgb__$1__rgb', $text );
229
230 $rgbaRegex = '#rgba\(((\s*\d+\s*,){3}[\d\.]+)\)#i';
231 $text = preg_replace( $rgbaRegex, 'rgba__$1__rgb', $text );
232
233 $text = wp_kses( $text, $allowed_html, $allowed_protocols );
234
235 $text = str_replace( 'rgba__', 'rgba(', $text );
236 $text = str_replace( 'rgb__', 'rgb(', $text );
237 $text = str_replace( '__rgb', ')', $text );
238
239 return $text;
240 }
241
242 /**
243 * Compares version string to WP base version ( e.g. X.Y.Z without looking for -beta* -RC* suffixes )
244 *
245 * @param string $compare_to - semver version number
246 * @param string $operator - version_compare operator
247 * @return void
248 */
249 public static function wpVersionCompare( $compare_to, $operator ) {
250 global $wp_version;
251 $version_parts = sscanf( $wp_version, '%d.%d.%d' );
252 $version = array();
253
254 foreach ( $version_parts as $version_part ) {
255 if ( $version_part !== null ) {
256 $version[] = $version_part;
257 }
258 }
259
260 $version = implode( '.', $version );
261 return version_compare( $version, $compare_to, $operator );
262 }
263
264 public static function ksesSVG( $svg_content ) {
265 $allowed_html = wp_kses_allowed_html( 'post' );
266 return wp_kses( $svg_content, $allowed_html );
267 }
268
269 /**
270 * Check if the execution time has enough remaining seconds
271 *
272 * @param integer $compare_to_time - necessary time in seconds
273 * @return boolean
274 */
275 public static function hasEnoughRemainingTime( $compare_to_time = 10 ) {
276
277 if ( ! static::$execute_start_time ) {
278 static::$execute_start_time = intval( Arr::get( $_SERVER, 'REQUEST_TIME_FLOAT', time() ) );
279 }
280
281 $diff = time() - static::$execute_start_time;
282
283 $max_exec_time = @ini_get( 'max_execution_time' );
284
285 // assume 30 seconds if not available
286 if ( ! $max_exec_time ) {
287 $max_exec_time = 30;
288 }
289
290 return ( intval( $max_exec_time ) - $diff >= $compare_to_time );
291 }
292
293 /**
294 * Check if current WordPress installation validates plugin requirements
295 *
296 * @return boolean|\WP_Error
297 */
298 public static function validateRequirements() {
299 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
300 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
301 $required_wp = ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : false;
302 $required_php = ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : false;
303
304 if ( defined( 'KUBIO_MINIMUM_WP_VERSION' ) && KUBIO_MINIMUM_WP_VERSION ) {
305 $required_wp = KUBIO_MINIMUM_WP_VERSION;
306 }
307
308 $compatible_wp = $required_wp ? Utils::wpVersionCompare( $required_wp, '>=' ) : true;
309 $compatible_php = version_compare( phpversion(), $required_php, '>=' );
310
311 $php_update_message = '</p><p>' . sprintf(
312 /* translators: %s: URL to Update PHP page. */
313 __( '<a href="%s">Learn more about updating PHP</a>' ),
314 esc_url( wp_get_update_php_url() )
315 );
316
317 $update_wp_core = sprintf(
318 /* translators: %s: URL to Update PHP page. */
319 __( '<a href="%s">Update WordPress now!</a>', 'kubio' ),
320 esc_url( admin_url( 'update-core.php' ) )
321 );
322
323 if ( ! $compatible_wp && ! $compatible_php ) {
324 return new WP_Error(
325 'plugin_wp_php_incompatible',
326 '<p>' . sprintf(
327 /* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */
328 _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' ),
329 get_bloginfo( 'version' ),
330 phpversion(),
331 $plugin_headers['Name'],
332 $required_wp,
333 $required_php
334 ) . $php_update_message . '<br/>' . $update_wp_core . '</p>'
335 );
336 } elseif ( ! $compatible_php ) {
337 return new WP_Error(
338 'plugin_php_incompatible',
339 '<p>' . sprintf(
340 /* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */
341 _x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'kubio' ),
342 phpversion(),
343 $plugin_headers['Name'],
344 $required_php
345 ) . $php_update_message . '</p>'
346 );
347 } elseif ( ! $compatible_wp ) {
348 return new WP_Error(
349 'plugin_wp_incompatible',
350 '<p>' . sprintf(
351 /* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */
352 _x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'kubio' ),
353 get_bloginfo( 'version' ),
354 $plugin_headers['Name'],
355 $required_wp
356 ) . '&nbsp;' . $update_wp_core . '</p>'
357 );
358 }
359
360 }
361
362 public static function getPluginVersions( $skip_current = false ) {
363 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
364 $plugin_headers = get_plugin_data( KUBIO_ENTRY_FILE );
365 $version = ! empty( $plugin_headers['Version'] ) ? $plugin_headers['Version'] : false;
366 $name = ! empty( $plugin_headers['Name'] ) ? $plugin_headers['Name'] : false;
367 $url = apply_filters(
368 'kubio/previous-versions/url',
369 sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s.json', KUBIO_SLUG )
370 );
371
372 $response = wp_remote_get( $url );
373
374 if ( is_wp_error( $response ) ) {
375 return null;
376 }
377
378 $response = wp_remote_retrieve_body( $response );
379
380 if ( is_serialized( $response ) ) {
381 $response = maybe_unserialize( $response );
382 } else {
383 $response = json_decode( $response );
384 }
385
386 if ( ! is_object( $response ) ) {
387 return null;
388 }
389 if ( ! isset( $response->versions ) ) {
390 return null;
391 }
392
393 $versions = array();
394 foreach ( $response->versions as $key => $value ) {
395
396 $version = is_object( $value ) ? $value->version : $key;
397
398 if ( $version === 'trunk' ) {
399 continue;
400 }
401
402 if ( $skip_current && $version === \KUBIO_VERSION ) {
403 continue;
404 }
405
406 $versions[ $version ] = array(
407 'version' => $version,
408 'named_version' => sprintf( '%s v%s', $name, $version ),
409 'url' => is_object( $value ) ? $value->file : $value,
410 );
411 }
412
413 return $versions;
414 }
415
416 public static function getCloudUrl( $url = null ) {
417 $cloudRootUrl = KUBIO_CLOUD_URL;
418
419 if ( ! $url ) {
420 return $cloudRootUrl;
421 }
422 return "$cloudRootUrl/$url";
423 }
424
425 public static function isDebug() {
426 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
427 }
428
429 public static function isCLI() {
430 return defined( 'WP_CLI' ) && WP_CLI;
431 }
432
433 /**
434 * return and unique autoinc id based on prefix
435 *
436 * @param string $prefix
437 * @return string
438 */
439 public static function uniqueId( $prefix = '' ) {
440 static $state;
441
442 if ( ! is_array( $state ) ) {
443 $state = array();
444 }
445
446 if ( ! isset( $state[ $prefix ] ) ) {
447 $state[ $prefix ] = 0;
448 }
449
450 $id = $state[ $prefix ]++;
451
452 return $id;
453 }
454 }
455