PluginProbe
Core Framework / trunk
Core Framework vtrunk
2.0.2 2.0.1 2.0.0 1.10.4 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.3.10 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.1.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.8.0 All 32 releases
core-framework / wp / Common / Functions.php

Functions.php in Core Framework trunk, at wp/Common/Functions.php

621 lines 22.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * CoreFramework
5 *
6 * @package CoreFramework
7 * @author Core Framework <hello@coreframework.com>
8 * @copyright 2023 Core Framework
9 * @license MIT
10 * @link https://coreframework.com
11 */
12
13 declare(strict_types=1);
14
15 namespace CoreFramework\Common;
16
17 use CoreFramework\Common\Abstracts\Base;
18
19 /**
20 * Main function class for external uses
21 *
22 * @see CoreFramework()
23 * @package CoreFramework\Common
24 */
25 class Functions extends Base {
26
27 /**
28 * Get plugin data by using CoreFramework()->getData()
29 *
30 * @since 0.0.0
31 */
32 public function getData(): array {
33 return $this->plugin->data();
34 }
35
36 /**
37 * Read .env file
38 *
39 * @since 0.0.0
40 */
41 private function readENV() {
42 if ( ! function_exists( 'is_readable' ) ) {
43 return false;
44 }
45
46 $env = CORE_FRAMEWORK_DIR_ROOT . '.env';
47 if ( ! file_exists( $env ) ) {
48 return false;
49 }
50
51 $env = CORE_FRAMEWORK_DIR_ROOT . '.env';
52
53 if ( ! is_readable( $env ) ) {
54 return false;
55 }
56
57 return file_get_contents( $env );
58 }
59
60 /**
61 * Create settings during activation (in Setup): CoreFramework()->createSettings()
62 *
63 * @since 0.0.0
64 */
65 public function createSettings(): void {
66 $preferences = array(
67 'bricks' => CoreFrameworkBricks()->is_bricks(),
68 'oxygen' => CoreFrameworkOxygen()->is_oxygen(),
69 'gutenberg' => false,
70 'figma' => false,
71 'selected_id' => '',
72 'delete_data' => false,
73 );
74
75 \add_option( 'core_framework_main', $preferences, '', false );
76 \add_option( 'core_framework_db_version', CORE_FRAMEWORK_DB_VER, '', false );
77 }
78
79 /**
80 * Database upgrade function
81 *
82 * @since 0.0.3
83 */
84 public function db_upgrade(): void {
85 $preferences = get_option( 'core_framework_main' );
86
87 /* Legacy properties */
88 $properties_to_remove = array( 'root_font_size', 'postcss', 'min_screen_width', 'max_screen_width', 'is_rem' );
89
90 foreach ( $properties_to_remove as $property ) {
91 if ( isset( $preferences[ $property ] ) ) {
92 unset( $preferences[ $property ] );
93 }
94 }
95
96 update_option( 'core_framework_main', $preferences, false );
97 update_option( 'core_framework_db_version', CORE_FRAMEWORK_DB_VER, false );
98 }
99
100 /**
101 * Create table 'core_framework_presets' during activation (in Setup): CoreFramework()->createTable()
102 *
103 * @since 0.0.0
104 */
105 public function createTable(): void {
106 global $wpdb;
107 $charset_collate = $wpdb->get_charset_collate();
108 $table_name = \esc_sql( $wpdb->prefix . 'core_framework_presets' );
109 // The identifier is the WordPress-controlled table prefix plus a fixed plugin suffix.
110 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
111 $presetsTableSql = "CREATE TABLE IF NOT EXISTS {$table_name} (
112 id varchar(50) NOT NULL,
113 time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
114 data longtext NOT NULL,
115 PRIMARY KEY (id)
116 ) {$charset_collate};";
117
118 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
119 dbDelta( $presetsTableSql );
120 }
121
122 /**
123 * Determine if is development by using CoreFramework()->isDev()
124 *
125 * @return array
126 * @since 0.0.0
127 */
128 public function isDev(): bool {
129 $env_content = $this->readENV();
130 if ( ! $env_content ) {
131 return false;
132 }
133
134 return strpos( $env_content, 'APP_ENV=development' ) !== false;
135 }
136
137 /**
138 * Get development URL by using CoreFramework()->getDevURL()
139 *
140 * @return mixed
141 */
142 public function getDevURL() {
143 $defaultURL = 'hakken.local';
144 $env_content = $this->readENV();
145 if ( ! $env_content ) {
146 return $defaultURL;
147 }
148
149 preg_match( '/DEV_URL=(.*?)\n/', $env_content, $matches );
150 return ! empty( $matches ) && isset( $matches[1] ) ? $matches[1] : $defaultURL;
151 }
152
153 /**
154 * Remove plugin options from the database
155 *
156 * @since 0.0.0
157 */
158 public function removeSettings(): void {
159 $prefix = 'core_framework_';
160 $options_to_delete = array(
161 'main',
162 'db_version',
163 'selected_preset_backup',
164 'grouped_classes',
165 'colors',
166 'oxygen_css_helper',
167 'variables',
168 );
169
170 foreach ( $options_to_delete as $option ) {
171 $name = $prefix . $option;
172 \delete_option( $name );
173 }
174 }
175
176 /**
177 * Delete table core_framework_presets from the database
178 *
179 * @since 0.0.1
180 */
181 public function removeTable(): void {
182 global $wpdb;
183
184 $table_name = \esc_sql( $wpdb->prefix . 'core_framework_presets' );
185 // The identifier is the WordPress-controlled table prefix plus a fixed plugin suffix.
186 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange
187 $wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
188 }
189
190 /**
191 * Array.Prototype.some, but for PHP
192 */
193 public function array_some( array $array, callable $callback ): bool {
194 foreach ( $array as $key => $value ) {
195 if ( $callback( $value, $key, $array ) ) {
196 return true;
197 }
198 }
199
200 return false;
201 }
202
203 public function enqueue_core_framework_connector() {
204 $core_framework_options = get_option( 'core_framework_main', array() );
205 $core_framework_connector = array(
206 'oxygen_enable_variable_dropdown' => isset( $core_framework_options['oxygen_enable_variable_dropdown'] ) ? $core_framework_options['oxygen_enable_variable_dropdown'] : true,
207 'oxygen_enable_dark_mode_preview' => isset( $core_framework_options['oxygen_enable_dark_mode_preview'] ) ? $core_framework_options['oxygen_enable_dark_mode_preview'] : true,
208 'oxygen_variable_ui' => isset( $core_framework_options['oxygen_variable_ui'] ) ? $core_framework_options['oxygen_variable_ui'] : true,
209 'oxygen_enable_variable_ui_auto_hide' => isset( $core_framework_options['oxygen_enable_variable_ui_auto_hide'] ) ? $core_framework_options['oxygen_enable_variable_ui_auto_hide'] : true,
210 'oxygen_enable_variable_ui_hint' => isset( $core_framework_options['oxygen_enable_variable_ui_hint'] ) ? $core_framework_options['oxygen_enable_variable_ui_hint'] : true,
211 'oxygen_apply_class_on_hover' => isset( $core_framework_options['oxygen_apply_class_on_hover'] ) ? $core_framework_options['oxygen_apply_class_on_hover'] : true,
212 'oxygen_enable_variable_context_menu' => isset( $core_framework_options['oxygen_enable_variable_context_menu'] ) ? $core_framework_options['oxygen_enable_variable_context_menu'] : true,
213 'oxygen_enable_unit_and_value_preview' => isset( $core_framework_options['oxygen_enable_unit_and_value_preview'] ) ? $core_framework_options['oxygen_enable_unit_and_value_preview'] : true,
214 'bricks_enable_variable_dropdown' => isset( $core_framework_options['bricks_enable_variable_dropdown'] ) ? $core_framework_options['bricks_enable_variable_dropdown'] : true,
215 'bricks_enable_dark_mode_preview' => isset( $core_framework_options['bricks_enable_dark_mode_preview'] ) ? $core_framework_options['bricks_enable_dark_mode_preview'] : true,
216 'bricks_variable_ui' => isset( $core_framework_options['bricks_variable_ui'] ) ? $core_framework_options['bricks_variable_ui'] : true,
217 'bricks_enable_variable_ui_auto_hide' => isset( $core_framework_options['bricks_enable_variable_ui_auto_hide'] ) ? $core_framework_options['bricks_enable_variable_ui_auto_hide'] : true,
218 'bricks_enable_variable_ui_hint' => isset( $core_framework_options['bricks_enable_variable_ui_hint'] ) ? $core_framework_options['bricks_enable_variable_ui_hint'] : true,
219 'bricks_apply_class_on_hover' => isset( $core_framework_options['bricks_apply_class_on_hover'] ) ? $core_framework_options['bricks_apply_class_on_hover'] : true,
220 'bricks_apply_variable_on_hover' => isset( $core_framework_options['bricks_apply_variable_on_hover'] ) ? $core_framework_options['bricks_apply_variable_on_hover'] : true,
221 'bricks_enable_variable_context_menu' => isset( $core_framework_options['bricks_enable_variable_context_menu'] ) ? $core_framework_options['bricks_enable_variable_context_menu'] : true,
222 'bricks_bem_generator' => isset( $core_framework_options['bricks_bem_generator'] ) ? $core_framework_options['bricks_bem_generator'] : true,
223 'gutenberg_enable_dark_mode_preview' => isset( $core_framework_options['gutenberg_enable_dark_mode_preview'] ) ? $core_framework_options['gutenberg_enable_dark_mode_preview'] : true,
224 'gutenberg_place_controls_at_the_top' => isset( $core_framework_options['gutenberg_place_controls_at_the_top'] ) ? $core_framework_options['gutenberg_place_controls_at_the_top'] : true,
225 'gutenberg_close_widget_default' => isset( $core_framework_options['gutenberg_close_widget_default'] ) ? $core_framework_options['gutenberg_close_widget_default'] : false,
226 'plugin_name' => isset( $core_framework_options['plugin_name'] ) ? $core_framework_options['plugin_name'] : $this->plugin->name(),
227 'theme_mode' => isset( $core_framework_options['theme_mode'] ) ? $core_framework_options['theme_mode'] : 'light',
228 );
229
230 $js = 'window.core_framework_connector = ' . \wp_json_encode( $core_framework_connector ) . ';';
231 $name = 'core-framework-builders-connector';
232
233 \wp_register_script( $name, '', array(), CORE_FRAMEWORK_VERSION, true );
234 \wp_enqueue_script( $name );
235 \wp_add_inline_script( $name, $js, 'before' );
236 }
237
238 public function str_replace_first( $needle, $replace, $haystack ): string {
239 if ( $needle === '' ) {
240 return $haystack;
241 }
242
243 $pos = strpos( $haystack, $needle );
244 if ( $pos !== false ) {
245 $haystack = substr_replace( $haystack, $replace, $pos, strlen( $needle ) );
246 }
247
248 return $haystack;
249 }
250
251 public function get_wp_kses_options() {
252 $attributes = array(
253 'xmlns' => array(),
254 'aria-hidden' => array(),
255 'accent-height' => array(),
256 'accumulate' => array(),
257 'additive' => array(),
258 'alignment-baseline' => array(),
259 'alphabetic' => array(),
260 'amplitude' => array(),
261 'arabic-form' => array(),
262 'ascent' => array(),
263 'attributeName' => array(),
264 'attributeType' => array(),
265 'azimuth' => array(),
266 'baseFrequency' => array(),
267 'baseline-shift' => array(),
268 'baseProfile' => array(),
269 'bbox' => array(),
270 'begin' => array(),
271 'bias' => array(),
272 'by' => array(),
273 'calcMode' => array(),
274 'cap-height' => array(),
275 'class' => array(),
276 'clip' => array(),
277 'clip-path' => array(),
278 'clip-rule' => array(),
279 'clipPathUnits' => array(),
280 'color' => array(),
281 'color-interpolation' => array(),
282 'color-interpolation-filters' => array(),
283 'color-profile' => array(),
284 'cursor' => array(),
285 'cx' => array(),
286 'cy' => array(),
287 'd' => array(),
288 'data-*' => array(),
289 'decoding' => array(),
290 'descent' => array(),
291 'diffuseConstant' => array(),
292 'direction' => array(),
293 'display' => array(),
294 'divisor' => array(),
295 'dominant-baseline' => array(),
296 'dur' => array(),
297 'dx' => array(),
298 'dy' => array(),
299 'edgeMode' => array(),
300 'elevation' => array(),
301 'enable-background' => array(),
302 'end' => array(),
303 'exponent' => array(),
304 'fill' => array(),
305 'fill-opacity' => array(),
306 'fill-rule' => array(),
307 'filter' => array(),
308 'filterUnits' => array(),
309 'flood-color' => array(),
310 'flood-opacity' => array(),
311 'font-family' => array(),
312 'font-size' => array(),
313 'font-size-adjust' => array(),
314 'font-stretch' => array(),
315 'font-style' => array(),
316 'font-variant' => array(),
317 'font-weight' => array(),
318 'fr' => array(),
319 'from' => array(),
320 'fx' => array(),
321 'fy' => array(),
322 'g1' => array(),
323 'g2' => array(),
324 'glyph-name' => array(),
325 'glyph-orientation-horizontal' => array(),
326 'glyph-orientation-vertical' => array(),
327 'gradientTransform' => array(),
328 'gradientUnits' => array(),
329 'hanging' => array(),
330 'height' => array(),
331 'horiz-adv-x' => array(),
332 'horiz-origin-x' => array(),
333 'horiz-origin-y' => array(),
334 'href' => array(),
335 'id' => array(),
336 'ideographic' => array(),
337 'image-rendering' => array(),
338 'in' => array(),
339 'in2' => array(),
340 'intercept' => array(),
341 'k' => array(),
342 'k1' => array(),
343 'k2' => array(),
344 'k3' => array(),
345 'k4' => array(),
346 'kernelMatrix' => array(),
347 'kernelUnitLength' => array(),
348 'kerning' => array(),
349 'keyPoints' => array(),
350 'keySplines' => array(),
351 'keyTimes' => array(),
352 'lang' => array(),
353 'lengthAdjust' => array(),
354 'letter-spacing' => array(),
355 'lighting-color' => array(),
356 'limitingConeAngle' => array(),
357 'marker-end' => array(),
358 'marker-mid' => array(),
359 'marker-start' => array(),
360 'markerHeight' => array(),
361 'markerUnits' => array(),
362 'markerWidth' => array(),
363 'mask' => array(),
364 'maskContentUnits' => array(),
365 'maskUnits' => array(),
366 'mathematical' => array(),
367 'max' => array(),
368 'media' => array(),
369 'method' => array(),
370 'min' => array(),
371 'mode' => array(),
372 'name' => array(),
373 'numOctaves' => array(),
374 'opacity' => array(),
375 'operator' => array(),
376 'order' => array(),
377 'orient' => array(),
378 'orientation' => array(),
379 'origin' => array(),
380 'overflow' => array(),
381 'overline-position' => array(),
382 'overline-thickness' => array(),
383 'paint-order' => array(),
384 'panose-1' => array(),
385 'path' => array(),
386 'pathLength' => array(),
387 'patternContentUnits' => array(),
388 'patternTransform' => array(),
389 'patternUnits' => array(),
390 'pointer-events' => array(),
391 'points' => array(),
392 'pointsAtX' => array(),
393 'pointsAtY' => array(),
394 'pointsAtZ' => array(),
395 'preserveAlpha' => array(),
396 'preserveAspectRatio' => array(),
397 'primitiveUnits' => array(),
398 'r' => array(),
399 'radius' => array(),
400 'refX' => array(),
401 'refY' => array(),
402 'repeatCount' => array(),
403 'repeatDur' => array(),
404 'requiredFeatures' => array(),
405 'restart' => array(),
406 'result' => array(),
407 'rotate' => array(),
408 'rx' => array(),
409 'ry' => array(),
410 'scale' => array(),
411 'seed' => array(),
412 'shape-rendering' => array(),
413 'side' => array(),
414 'slope' => array(),
415 'spacing' => array(),
416 'specularConstant' => array(),
417 'specularExponent' => array(),
418 'spreadMethod' => array(),
419 'startOffset' => array(),
420 'stdDeviation' => array(),
421 'stemh' => array(),
422 'stemv' => array(),
423 'stitchTiles' => array(),
424 'stop-color' => array(),
425 'stop-opacity' => array(),
426 'strikethrough-position' => array(),
427 'strikethrough-thickness' => array(),
428 'string' => array(),
429 'stroke' => array(),
430 'stroke-dasharray' => array(),
431 'stroke-dashoffset' => array(),
432 'stroke-linecap' => array(),
433 'stroke-linejoin' => array(),
434 'stroke-miterlimit' => array(),
435 'stroke-opacity' => array(),
436 'stroke-width' => array(),
437 'style' => array(),
438 'surfaceScale' => array(),
439 'systemLanguage' => array(),
440 'tabindex' => array(),
441 'tableValues' => array(),
442 'target' => array(),
443 'targetX' => array(),
444 'targetY' => array(),
445 'text-anchor' => array(),
446 'text-decoration' => array(),
447 'text-rendering' => array(),
448 'textLength' => array(),
449 'to' => array(),
450 'transform' => array(),
451 'transform-origin' => array(),
452 'type' => array(),
453 'u1' => array(),
454 'u2' => array(),
455 'underline-position' => array(),
456 'underline-thickness' => array(),
457 'unicode' => array(),
458 'unicode-bidi' => array(),
459 'unicode-range' => array(),
460 'units-per-em' => array(),
461 'v-alphabetic' => array(),
462 'v-hanging' => array(),
463 'v-ideographic' => array(),
464 'v-mathematical' => array(),
465 'values' => array(),
466 'vector-effect' => array(),
467 'version' => array(),
468 'vert-adv-y' => array(),
469 'vert-origin-x' => array(),
470 'vert-origin-y' => array(),
471 'viewBox' => array(),
472 'visibility' => array(),
473 'width' => array(),
474 'widths' => array(),
475 'word-spacing' => array(),
476 'writing-mode' => array(),
477 'x' => array(),
478 'x-height' => array(),
479 'x1' => array(),
480 'x2' => array(),
481 'xChannelSelector' => array(),
482 'xlink:arcrole' => array(),
483 'xlink:href' => array(),
484 'xlink:show' => array(),
485 'xlink:title' => array(),
486 'xlink:type' => array(),
487 'xml:base' => array(),
488 'xml:lang' => array(),
489 'xml:space' => array(),
490 'y' => array(),
491 'y1' => array(),
492 'y2' => array(),
493 'yChannelSelector' => array(),
494 'z' => array(),
495 'zoomAndPan' => array(),
496 );
497
498 foreach ( $attributes as $key => $value ) {
499 $attributes[ strtolower( $key ) ] = $value;
500 }
501
502 $tag_names = array(
503 'svg',
504 'span',
505 'i',
506 'abbr',
507 'a',
508 'altGlyph',
509 'altGlyphDef',
510 'altGlyphItem',
511 'animate',
512 'animateColor',
513 'animateMotion',
514 'animateTransform',
515 'animation',
516 'audio',
517 'canvas',
518 'circle',
519 'clipPath',
520 'color-profile',
521 'cursor',
522 'defs',
523 'desc',
524 'discard',
525 'ellipse',
526 'feBlend',
527 'feColorMatrix',
528 'feComponentTransfer',
529 'feComposite',
530 'feConvolveMatrix',
531 'feDiffuseLighting',
532 'feDisplacementMap',
533 'feDistantLight',
534 'feDropShadow',
535 'feFlood',
536 'feFuncA',
537 'feFuncB',
538 'feFuncG',
539 'feFuncR',
540 'feGaussianBlur',
541 'feImage',
542 'feMerge',
543 'feMergeNode',
544 'feMorphology',
545 'feOffset',
546 'fePointLight',
547 'feSpecularLighting',
548 'feSpotLight',
549 'feTile',
550 'feTurbulence',
551 'filter',
552 'font',
553 'font-face',
554 'font-face-format',
555 'font-face-name',
556 'font-face-src',
557 'font-face-uri',
558 'foreignObject',
559 'g',
560 'glyph',
561 'glyphRef',
562 'handler',
563 'hkern',
564 'image',
565 'line',
566 'linearGradient',
567 'listener',
568 'marker',
569 'mask',
570 'metadata',
571 'missing-glyph',
572 'mpath',
573 'path',
574 'pattern',
575 'polygon',
576 'polyline',
577 'prefetch',
578 'radialGradient',
579 'rect',
580 'set',
581 'solidColor',
582 'stop',
583 'style',
584 'svg',
585 'switch',
586 'symbol',
587 'tbreak',
588 'text',
589 'textArea',
590 'textPath',
591 'title',
592 'tref',
593 'tspan',
594 'unknown',
595 'use',
596 'video',
597 'view',
598 'vkern',
599 );
600
601 foreach ( $tag_names as $tag_name ) {
602 $options[ $tag_name ] = $attributes;
603 $options['svg'][ $tag_name ] = $attributes;
604 }
605
606 return $options;
607 }
608
609 public function purge_cache() {
610 if ( \is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
611 // This is LiteSpeed Cache's documented third-party purge hook.
612 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
613 \do_action( 'litespeed_purge_all' );
614 }
615 }
616
617 public function get_random_id( $length = 26 ) {
618 return substr( str_shuffle( '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ), 0, $length );
619 }
620 }
621