PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.2.3
Kubio AI Page Builder v1.2.3
2.8.6 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 4 years ago Blocks 4 years ago GlobalElements 4 years ago Layout 4 years ago License 4 years ago Separators 4 years ago StyleManager 4 years ago Styles 4 years ago Activation.php 4 years ago Backup.php 4 years ago CustomizerImporter.php 4 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 4 years ago ElementBase.php 4 years ago Importer.php 4 years ago InnerBlocks.php 4 years ago LodashBasic.php 4 years ago Registry.php 4 years ago Utils.php 4 years ago
Utils.php
420 lines
1 <?php
2
3
4 namespace Kubio\Core;
5
6 use IlluminateAgnostic\Arr\Support\Arr;
7 use Kubio\Config;
8 use function json_encode;
9 use \WP_Error;
10
11 class Utils {
12
13
14 private static $execute_start_time;
15
16 public static function mapHideClassesByMedia(
17 $hiddenByMedia,
18 $negateValue = false
19 ) {
20 $mapHideClassesByMedia = array();
21 foreach ( $hiddenByMedia as $media => $isHidden ) {
22 if ( $negateValue ) {
23 $isHidden = ! $isHidden;
24 }
25 if ( $isHidden ) {
26 array_push( $mapHideClassesByMedia, "kubio-hide-on-$media" );
27 }
28 }
29
30 return $mapHideClassesByMedia;
31 }
32
33 public static function useJSComponentProps( $name, $settings = array() ) {
34 $prefix = Config::$name;
35
36 return array(
37 "data-${prefix}-component" => $name,
38 "data-${prefix}-settings" => json_encode( $settings ),
39 );
40 }
41
42 public static function getLinkAttributes( $linkObject ) {
43 $defaultValue = array(
44 'value' => '',
45 'typeOpenLink' => 'sameWindow',
46 'noFollow' => false,
47 'lightboxMedia' => '',
48 );
49 $mergedLinkObject = LodashBasic::merge( array(), $defaultValue, $linkObject );
50 $linkAttributes = array(
51 'href' => null,
52 'target' => null,
53 'rel' => null,
54 'data-kubio-component' => null,
55 );
56
57 if ( $mergedLinkObject ) {
58 if ( $mergedLinkObject['value'] ) {
59 $linkAttributes['href'] = $mergedLinkObject['value'];
60 }
61 $linkType = LodashBasic::get( $mergedLinkObject, 'typeOpenLink', '' );
62 if ( $linkType === 'newWindow' ) {
63 $linkAttributes['target'] = '_blank';
64 }
65
66 if ( $linkType === 'lightbox' ) {
67 $lightboxType = $mergedLinkObject['lightboxMedia'];
68 if ( $lightboxType === '' ) {
69 $lightboxType = null;
70 }
71 $linkAttributes['data-default-type'] = $lightboxType;
72 $linkAttributes['data-fancybox'] = rand() . '';
73 }
74 if ( $mergedLinkObject['noFollow'] ) {
75 $linkAttributes['rel'] = 'nofollow';
76 }
77 }
78
79 return $linkAttributes;
80 }
81
82 public static function shortcodeDecode( $data ) {
83 return urldecode( base64_decode( $data ) );
84 }
85
86 public static function getDefaultAssetsUrl( $url ) {
87 $staticUrl = kubio_url( 'static/default-assets' );
88
89 return $staticUrl . '/' . ltrim( $url, '/' );
90 }
91
92 public static function canEdit() {
93 return current_user_can( 'edit_theme_options' ) && current_user_can( 'edit_posts' );
94 }
95
96 public static function getEmptyShortcodePlaceholder() {
97 if ( is_user_logged_in() ) {
98 return static::getFrontendPlaceHolder(
99 sprintf(
100 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
101 __( 'Shortcode is empty.', 'kubio' ),
102 __( 'Edit this page to insert a shortcode or delete this block.', 'kubio' )
103 )
104 );
105 } else {
106 return '';
107 }
108
109 }
110
111 //the production build does not include the patterns folder, we can use this to determine if the build is dev or prod
112 public static function isProduction() {
113 $isProd = ! file_exists( KUBIO_ROOT_DIR . '/static/patterns/content-converted.json' );
114
115 return $isProd;
116 }
117
118 public static function getFrontendPlaceHolder( $message, $options = array() ) {
119
120 $options = array_merge(
121 array(
122 'info' => true,
123 'title' => __( 'Kubio info', 'kubio' ),
124 'if_logged' => true,
125 ),
126 $options
127 );
128
129 if ( $options['if_logged'] ) {
130 if ( ! is_user_logged_in() ) {
131 return;
132 }
133 }
134
135 if ( is_callable( $message ) ) {
136 $message = call_user_func( $message );
137 }
138
139 $info = '';
140 if ( $options['info'] ) {
141 $info = sprintf(
142 '<div class="kubio-frontent-placeholder--info">' .
143 ' <div class="kubio-frontent-placeholder--logo">%s</div>' .
144 ' <div class="kubio-frontent-placeholder--title">%s</div>' .
145 '</div>',
146 wp_kses_post( KUBIO_LOGO_SVG ),
147 $options['title']
148 );
149 }
150
151 return sprintf( '<div class="kubio-frontent-placeholder"><div>%s</div><div>%s</div></div>', $info, $message );
152 }
153
154 public static function kubioCacheGet( $name, $default = null ) {
155
156 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
157 $value = $default;
158
159 if ( self::kubioCacheHas( $name ) ) {
160 $value = $kubio_cache[ $name ];
161 }
162
163 return $value;
164
165 }
166
167 public static function kubioCacheHas( $name ) {
168 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
169
170 return array_key_exists( $name, $kubio_cache );
171 }
172
173 public static function kubioCacheSet( $name, $value ) {
174 $kubio_cache = isset( $GLOBALS['__kubio_plugin_cache__'] ) ? $GLOBALS['__kubio_plugin_cache__'] : array();
175 $kubio_cache[ $name ] = $value;
176
177 $GLOBALS['__kubio_plugin_cache__'] = $kubio_cache;
178
179 }
180
181 /**
182 * Remove empty branches from array
183 *
184 * @param array $array the array to walk
185 *
186 * @return array
187 */
188 public static function arrayRecursiveRemoveEmptyBranches( array &$array ) {
189 foreach ( $array as $key => &$value ) {
190 if ( is_array( $value ) ) {
191 $array[ $key ] = static::arrayRecursiveRemoveEmptyBranches( $value );
192
193 if ( empty( $value ) ) {
194 unset( $array[ $key ] );
195 }
196 }
197 }
198
199 return $array;
200 }
201
202 public static function walkBlocks( &$blocks, $callback ) {
203 array_walk(
204 $blocks,
205 function ( &$block ) use ( $callback ) {
206 if ( isset( $block['blockName'] ) ) {
207 $callback( $block );
208 }
209 if ( isset( $block['innerBlocks'] ) ) {
210 static::walkBlocks( $block['innerBlocks'], $callback );
211 }
212 }
213 );
214 }
215
216 public static function kses( $text, $allowed_protocols = array() ) {
217
218 static $allowed_html;
219
220 if ( ! $allowed_html ) {
221 $allowed_html = wp_kses_allowed_html( 'post' );
222 }
223
224 // fix the issue with rgb / rgba colors in style atts
225
226 $rgbRegex = '#rgb\(((?:\s*\d+\s*,){2}\s*[\d]+)\)#i';
227 $text = preg_replace( $rgbRegex, 'rgb__$1__rgb', $text );
228
229 $rgbaRegex = '#rgba\(((\s*\d+\s*,){3}[\d\.]+)\)#i';
230 $text = preg_replace( $rgbaRegex, 'rgba__$1__rgb', $text );
231
232 $text = wp_kses( $text, $allowed_html, $allowed_protocols );
233
234 $text = str_replace( 'rgba__', 'rgba(', $text );
235 $text = str_replace( 'rgb__', 'rgb(', $text );
236 $text = str_replace( '__rgb', ')', $text );
237
238 return $text;
239 }
240
241 /**
242 * Compares version string to WP base version ( e.g. X.Y.Z without looking for -beta* -RC* suffixes )
243 *
244 * @param string $compare_to - semver version number
245 * @param string $operator - version_compare operator
246 * @return void
247 */
248 public static function wpVersionCompare( $compare_to, $operator ) {
249 global $wp_version;
250 $version_parts = sscanf( $wp_version, '%d.%d.%d' );
251 $version = array();
252
253 foreach ( $version_parts as $version_part ) {
254 $version_part = trim( $version_part );
255 if ( ! empty( $version_part ) ) {
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 isDebug() {
417 return defined( 'KUBIO_DEBUG' ) && KUBIO_DEBUG;
418 }
419 }
420