PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.4
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.4
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / helpers.php
kirki / libraries / framework Last commit date
Collections 1 month ago Concerns 1 month ago Console 4 weeks ago Constants 2 weeks ago Contracts 2 weeks ago Database 2 weeks ago Discovery 2 weeks ago Exceptions 4 weeks ago Filesystem 4 weeks ago Http 1 week ago Managers 1 week ago Middlewares 1 month ago Polyfill 1 month ago Routing 1 week ago Supports 1 week ago Validation 3 weeks ago View 4 weeks ago Wordpress 1 week ago ApiExceptionHandler.php 1 month ago Application.php 1 week ago Container.php 1 month ago CoreServiceProvider.php 1 week ago DTO.php 1 month ago Facade.php 1 month ago Listener.php 1 month ago Resource.php 4 weeks ago Route.php 1 week ago Sanitizer.php 4 weeks ago ServiceProvider.php 1 month ago SiteExceptionHandler.php 4 weeks ago helpers.php 1 week ago
helpers.php
754 lines
1 <?php
2
3 /**
4 * The framework helper functions.
5 *
6 * @package Framework
7 * @subpackage Helpers
8 * @since 1.0.0
9 */
10 namespace Kirki\Framework;
11
12 \defined('ABSPATH') || exit;
13 use Closure;
14 use Faker\Factory;
15 use Faker\Generator;
16 use Kirki\Framework\Application;
17 use Kirki\Framework\Collections\Collection;
18 use Kirki\Framework\Database\Migrations\Migrator;
19 use Kirki\Framework\Http\Cookie;
20 use Kirki\Framework\Http\Request;
21 use Kirki\Framework\Managers\CookieManager;
22 use Kirki\Framework\Http\RedirectResponse;
23 use Kirki\Framework\Wordpress\User;
24 use Kirki\Framework\Http\Response;
25 use Kirki\Framework\Supports\Arr;
26 use Kirki\Framework\Supports\HigherOrderTapProxy;
27 use Kirki\Framework\Supports\MessagesBag;
28 use Kirki\Framework\Supports\Str;
29 use Kirki\Framework\Supports\Url;
30 use Kirki\Framework\Supports\Utils;
31 use Kirki\Framework\View\TemplateEngine;
32 use Kirki\Framework\View\View;
33 use Kirki\Framework\View\ViewContext;
34 use Symfony\Component\VarDumper\Cloner\VarCloner;
35 use Symfony\Component\VarDumper\Dumper\CliDumper;
36 use Symfony\Component\VarDumper\Dumper\HtmlDumper;
37 use Symfony\Component\VarDumper\VarDumper;
38 use function Kirki\Framework\Polyfill\array_key_first;
39 use function Kirki\Framework\Polyfill\array_key_last;
40 use function Kirki\Framework\Polyfill\is_iterable;
41 if (!\function_exists('Kirki\\Framework\\app')) {
42 /**
43 * Get the container instance.
44 *
45 * @template TClass
46 *
47 * @param string|class-string<TClass>|null $abstract
48 * @param array $parameters
49 *
50 * @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? Application : mixed))
51 */
52 function app($abstract = null, array $parameters = [])
53 {
54 if (\is_null($abstract)) {
55 return Application::get_instance();
56 }
57 return Application::get_instance()->make($abstract, $parameters);
58 }
59 }
60 if (!\function_exists('Kirki\\Framework\\deep_get')) {
61 /**
62 * Get a value from an array using a dot notation key.
63 *
64 * @param array $target The target array to get the value from.
65 * @param string|array $key The key to get the value from.
66 * @param mixed $default The default value to return if the key is not found.
67 *
68 * @return mixed The value from the array or the default value if the key is not found.
69 *
70 * @since 1.0.0
71 */
72 function deep_get($target, $key, $default = null)
73 {
74 if (\is_null($key)) {
75 return $target;
76 }
77 $key = \is_array($key) ? $key : \explode('.', $key);
78 foreach ($key as $index => $segment) {
79 unset($key[$index]);
80 if (\is_null($segment)) {
81 return $target;
82 }
83 if ($segment === '*') {
84 if ($target instanceof Collection) {
85 $target = $target->all();
86 } elseif (!is_iterable($target)) {
87 return $default;
88 }
89 $result = [];
90 foreach ($target as $item) {
91 $result[] = deep_get($item, $key);
92 }
93 return \in_array('*', $key) ? Arr::collapse($result) : $result;
94 }
95 switch ($segment) {
96 case '\\*':
97 $segment = '*';
98 break;
99 case '\\{first}':
100 $segment = '{first}';
101 break;
102 case '{first}':
103 $segment = array_key_first(Arr::from($target));
104 break;
105 case '\\{last}':
106 $segment = '{last}';
107 break;
108 case '{last}':
109 $segment = array_key_last(Arr::from($target));
110 break;
111 }
112 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
113 $target = $target[$segment];
114 } elseif (\is_object($target) && isset($target->{$segment})) {
115 $target = $target->{$segment};
116 } else {
117 return value($default);
118 }
119 }
120 return $target;
121 }
122 }
123 if (!\function_exists('Kirki\\Framework\\deep_set')) {
124 /**
125 * Set a value in an array using a dot notation key.
126 *
127 * @param array $target The target array to set the value in.
128 * @param string|array $key The key to set the value in.
129 * @param mixed $value The value to set.
130 * @return void
131 */
132 function deep_set(&$target, $key, $value, $overwrite = \true)
133 {
134 $segments = \is_array($key) ? $key : \explode('.', $key);
135 $segment = \array_shift($segments);
136 if ($segment === '*') {
137 if (!Arr::accessible($target)) {
138 $target = [];
139 }
140 if ($segments) {
141 foreach ($target as &$inner) {
142 deep_set($inner, $segments, $value, $overwrite);
143 }
144 unset($inner);
145 } elseif ($overwrite) {
146 foreach ($target as &$inner) {
147 $inner = $value;
148 }
149 }
150 } elseif (Arr::accessible($target)) {
151 if ($segments) {
152 if (!Arr::exists($target, $segment)) {
153 $target[$segment] = [];
154 }
155 deep_set($target[$segment], $segments, $value, $overwrite);
156 } elseif ($overwrite || !Arr::exists($target, $segment)) {
157 $target[$segment] = $value;
158 }
159 } elseif (\is_object($target)) {
160 if ($segments) {
161 if (!isset($target->{$segment})) {
162 $target->{$segment} = [];
163 }
164 deep_set($target->{$segment}, $segments, $value, $overwrite);
165 } elseif ($overwrite || !isset($target->{$segment})) {
166 $target->{$segment} = $value;
167 }
168 } else {
169 $target = [];
170 if ($segments) {
171 deep_set($target[$segment], $segments, $value, $overwrite);
172 } elseif ($overwrite) {
173 $target[$segment] = $value;
174 }
175 }
176 return $target;
177 }
178 }
179 if (!\function_exists('Kirki\\Framework\\config')) {
180 /**
181 * Get the config
182 *
183 * @param string|null $key
184 * @param mixed $default
185 * @return mixed
186 */
187 function config($key = null, $default = null)
188 {
189 static $cache = [];
190 $filename = \strpos($key, '.') ? \substr($key, 0, \strpos($key, '.')) : $key;
191 $key = \strpos($key, '.') ? \substr($key, \strpos($key, '.') + 1) : null;
192 if (!isset($cache[$filename])) {
193 $path = app()->config_path("{$filename}.php");
194 if (\file_exists($path)) {
195 $cache[$filename] = (include $path);
196 } else {
197 $cache[$filename] = null;
198 }
199 }
200 if (\is_null($cache[$filename])) {
201 return value($default);
202 }
203 return deep_get($cache[$filename], $key, $default);
204 }
205 }
206 if (!\function_exists('Kirki\\Framework\\user')) {
207 /**
208 * Get the user instance.
209 *
210 * @return User
211 */
212 function user($user_id = null)
213 {
214 return app()->make(User::class, ['user_id' => $user_id]);
215 }
216 }
217 if (!\function_exists('Kirki\\Framework\\response')) {
218 /**
219 * Get the response instance.
220 *
221 * @return Response
222 */
223 function response()
224 {
225 return app()->make(Response::class)->with_headers(['X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'Referrer-Policy' => 'no-referrer-when-downgrade', 'Cache-Control' => 'public, max-age=60, stale-while-revalidate=30']);
226 }
227 }
228 if (!\function_exists('Kirki\\Framework\\cookie')) {
229 /**
230 * Create a cookie instance, or get the cookie manager when called without arguments.
231 *
232 * The returned cookie is not sent. Queue it with the Cookie facade or attach it
233 * to a response to have it emitted.
234 *
235 * @param string|null $name The name of the cookie.
236 * @param string $value The value of the cookie.
237 * @param int $minutes The number of minutes the cookie lives, zero for a session cookie.
238 * @param string|null $path The path the cookie is scoped to.
239 * @param string|null $domain The domain the cookie is scoped to.
240 * @param bool|null $secure Whether the cookie is restricted to secure connections.
241 * @param bool $http_only Whether the cookie is hidden from client side scripts.
242 * @param bool $raw Whether the value is sent without URL encoding.
243 * @param string|null $same_site The same site policy of the cookie.
244 *
245 * @return ($name is null ? CookieManager : Cookie)
246 *
247 * @since 1.0.0
248 */
249 function cookie($name = null, $value = '', $minutes = 0, $path = null, $domain = null, $secure = null, $http_only = \true, $raw = \false, $same_site = null)
250 {
251 $manager = app('cookie');
252 if (\is_null($name)) {
253 return $manager;
254 }
255 return $manager->make($name, $value, $minutes, $path, $domain, $secure, $http_only, $raw, $same_site);
256 }
257 }
258 if (!\function_exists('Kirki\\Framework\\view')) {
259 /**
260 * Create a view instance for the given template.
261 *
262 * @param string $template The template name in dot notation.
263 * @param array $data The data to pass to the template.
264 *
265 * @return View
266 *
267 * @since 1.0.0
268 */
269 function view(string $template, array $data = [])
270 {
271 return new View($template, $data);
272 }
273 }
274 if (!\function_exists('Kirki\\Framework\\view_data')) {
275 /**
276 * Read data from the innermost authorized view context.
277 *
278 * Supports dot notation for nested keys (e.g. `product.name`).
279 *
280 * @param string|null $key Data key (dot notation allowed), or null for the full array.
281 * @param mixed $default Default when missing or unauthorized.
282 *
283 * @return mixed
284 *
285 * @since 2.1.2
286 */
287 function view_data($key = null, $default = null)
288 {
289 return app(ViewContext::class)->get($key, $default);
290 }
291 }
292 if (!\function_exists('Kirki\\Framework\\template_engine')) {
293 /**
294 * Get the template engine instance.
295 *
296 * @return TemplateEngine
297 */
298 function template_engine()
299 {
300 return app(TemplateEngine::class);
301 }
302 }
303 if (!\function_exists('Kirki\\Framework\\include_view')) {
304 /**
305 * Include a nested pure-PHP view partial (Laravel @include equivalent).
306 *
307 * Explicit $data (plus TemplateEngine shared data) is pushed as a child
308 * ViewContext frame. The partial must read values via view_data().
309 *
310 * @param string $view The view name in dot notation.
311 * @param array $data Data for the partial.
312 *
313 * @return void
314 *
315 * @since 2.1.2
316 * @throws \RuntimeException When the view cannot be resolved.
317 */
318 function include_view(string $view, array $data = [])
319 {
320 app(\Kirki\Framework\View\TemplateEngine::class)->include($view, $data, \false);
321 }
322 }
323 if (!\function_exists('Kirki\\Framework\\redirect')) {
324 /**
325 * Create a redirect response.
326 *
327 * @param string $url The redirect target URL.
328 * @param int $status The HTTP status code.
329 *
330 * @return RedirectResponse
331 *
332 * @since 1.0.0
333 */
334 function redirect(string $url, int $status = 302)
335 {
336 return new RedirectResponse($url, $status);
337 }
338 }
339 if (!\function_exists('Kirki\\Framework\\request')) {
340 /**
341 * Get the request instance.
342 *
343 * @param string|null $key
344 * @param mixed $default
345 *
346 * @return ($key is null ? Request : ($key is array ? array : mixed))
347 *
348 * @since 1.0.0
349 */
350 function request($key = null, $default = null)
351 {
352 if (\is_null($key)) {
353 return app('request');
354 }
355 if (\is_array($key)) {
356 return app('request')->only($key);
357 }
358 $value = app('request')->get($key, $default);
359 return \is_null($value) ? value($default) : $value;
360 }
361 }
362 if (!\function_exists('Kirki\\Framework\\with_prefix')) {
363 /**
364 * Get the key with prefix applied.
365 *
366 * @param string $key
367 * @return string
368 */
369 function with_prefix(string $key)
370 {
371 $prefix = app()->prefix();
372 if (Str::starts_with($key, $prefix)) {
373 return $key;
374 }
375 return $prefix . $key;
376 }
377 }
378 if (!\function_exists('Kirki\\Framework\\without_prefix')) {
379 /**
380 * Get the key without prefix applied.
381 *
382 * @param string $key
383 * @return string
384 */
385 function without_prefix(string $key)
386 {
387 $prefix = app()->prefix();
388 if (!Str::starts_with($key, $prefix)) {
389 return $key;
390 }
391 return \substr($key, \strlen($prefix));
392 }
393 }
394 if (!\function_exists('Kirki\\Framework\\redirect')) {
395 /**
396 * Redirect to the given location.
397 *
398 * @param string $location
399 * @return void
400 *
401 * @since 1.0.0
402 */
403 function redirect($location)
404 {
405 Url::redirect($location);
406 }
407 }
408 if (!\function_exists('Kirki\\Framework\\is_valid_json')) {
409 /**
410 * Check if the string is a valid JSON.
411 *
412 * @param string $string
413 * @return bool
414 */
415 function is_valid_json($string)
416 {
417 if (!\is_string($string)) {
418 return \false;
419 }
420 \json_decode($string);
421 return \json_last_error() === \JSON_ERROR_NONE;
422 }
423 }
424 if (!\function_exists('Kirki\\Framework\\clean_path')) {
425 /**
426 * Clean and normalize file paths for consistency.
427 *
428 * @param string $path
429 * @param bool $trailing_slash Add a trailing slash? Default true.
430 * @return string
431 */
432 function clean_path(string $path, bool $trailing_slash = \true)
433 {
434 $path = wp_normalize_path($path);
435 return $trailing_slash ? trailingslashit($path) : untrailingslashit($path);
436 }
437 }
438 if (!\function_exists('Kirki\\Framework\\uuid')) {
439 /**
440 * Generate a UUID.
441 *
442 * @return string
443 */
444 function uuid()
445 {
446 return Utils::uuid();
447 }
448 }
449 if (!\function_exists('Kirki\\Framework\\url')) {
450 /**
451 * Generate a URL.
452 *
453 * @param string $url
454 * @param array $query_vars
455 * @return string
456 */
457 function url($url, $query_vars = [])
458 {
459 return Url::make($url, $query_vars);
460 }
461 }
462 if (!\function_exists('Kirki\\Framework\\is_block_theme')) {
463 /**
464 * Check if the site is using a block template
465 *
466 * This function will return true if the site is using a block template and false otherwise.
467 *
468 * @return bool True if the site is using a block template, false otherwise.
469 */
470 function is_block_theme()
471 {
472 return \function_exists('wp_is_block_theme') && wp_is_block_theme();
473 }
474 }
475 if (!\function_exists('Kirki\\Framework\\migrator')) {
476 /**
477 * Get the migrator instance.
478 *
479 * @return Migrator
480 */
481 function migrator()
482 {
483 return app()->make(Migrator::class);
484 }
485 }
486 if (!\function_exists('Kirki\\Framework\\tap')) {
487 /**
488 * Call the given Closure with the given value.
489 *
490 * @param mixed $value
491 * @param \Closure $callback
492 * @return mixed
493 */
494 function tap($value, $callback = null)
495 {
496 if (\is_null($callback)) {
497 return new HigherOrderTapProxy($value);
498 }
499 $callback($value);
500 return $value;
501 }
502 }
503 if (!\function_exists('Kirki\\Framework\\faker')) {
504 /**
505 * Get the fake instance.
506 *
507 * @return Generator
508 */
509 function faker()
510 {
511 return app()->make(Factory::class);
512 }
513 }
514 if (!\function_exists('Kirki\\Framework\\configure_dumper')) {
515 function configure_dumper()
516 {
517 static $configured = \false;
518 if ($configured) {
519 return;
520 }
521 $configured = \true;
522 $is_cli = \defined('WP_CLI') && \WP_CLI;
523 if ($is_cli) {
524 VarDumper::setHandler(function ($var) {
525 $dumper = new CliDumper();
526 $dumper->dump((new VarCloner())->cloneVar($var));
527 });
528 return;
529 }
530 VarDumper::setHandler(function ($var) {
531 $dumper = new HtmlDumper();
532 $dumper->dump((new VarCloner())->cloneVar($var));
533 });
534 }
535 }
536 if (!\function_exists('Kirki\\Framework\\dump')) {
537 /**
538 * Dump the given arguments.
539 *
540 * @param mixed ...$args
541 *
542 * @return void
543 *
544 * @since 1.0.0
545 */
546 function dump(...$args)
547 {
548 if (!\class_exists(VarDumper::class) || !app()->is_dev_mode()) {
549 return;
550 }
551 configure_dumper();
552 foreach ($args as $arg) {
553 VarDumper::dump($arg);
554 }
555 }
556 }
557 if (!\function_exists('Kirki\\Framework\\dd')) {
558 /**
559 * Dump the given arguments and die.
560 *
561 * @param mixed ...$args
562 *
563 * @return void
564 *
565 * @since 1.0.0
566 */
567 function dd(...$args)
568 {
569 dump(...$args);
570 die(1);
571 }
572 }
573 if (!\function_exists('Kirki\\Framework\\app_path')) {
574 /**
575 * Get the path to the application directory.
576 *
577 * @param string $path
578 * @return string
579 */
580 function app_path($path = '')
581 {
582 return app()->path($path);
583 }
584 }
585 if (!\function_exists('Kirki\\Framework\\config_path')) {
586 /**
587 * Get the path to the config directory.
588 *
589 * @param string $path
590 * @return string
591 */
592 function config_path($path = '')
593 {
594 return app()->config_path($path);
595 }
596 }
597 if (!\function_exists('Kirki\\Framework\\database_path')) {
598 /**
599 * Get the path to the database directory.
600 *
601 * @param string $path
602 * @return string
603 */
604 function database_path($path = '')
605 {
606 return app()->database_path($path);
607 }
608 }
609 if (!\function_exists('Kirki\\Framework\\base_path')) {
610 /**
611 * Get the path to the base directory.
612 *
613 * @param string $path
614 * @return string
615 */
616 function base_path($path = '')
617 {
618 return app()->base_path($path);
619 }
620 }
621 if (!\function_exists('Kirki\\Framework\\resource_path')) {
622 /**
623 * Get the path to the resources directory.
624 *
625 * @param string $path
626 * @return string
627 */
628 function resource_path($path = '')
629 {
630 return app()->resource_path($path);
631 }
632 }
633 if (!\function_exists('Kirki\\Framework\\view_path')) {
634 /**
635 * Get the path to the views directory.
636 *
637 * @param string $path
638 *
639 * @return string
640 *
641 * @since 1.0.0
642 */
643 function view_path($path = '')
644 {
645 return app()->view_path($path);
646 }
647 }
648 if (!\function_exists('Kirki\\Framework\\bootstrap_path')) {
649 /**
650 * Get the path to the bootstrap directory.
651 *
652 * @param string $path
653 * @return string
654 */
655 function bootstrap_path($path = '')
656 {
657 return app()->bootstrap_path($path);
658 }
659 }
660 if (!\function_exists('Kirki\\Framework\\collection')) {
661 /**
662 * Create a collection instance from an array.
663 *
664 * @param array $array
665 * @return Collection
666 */
667 function collection(array $array = [])
668 {
669 return new Collection($array);
670 }
671 }
672 if (!\function_exists('Kirki\\Framework\\resource_url')) {
673 /**
674 * Get the path to the resources directory.
675 *
676 * @param string $path
677 * @return string
678 */
679 function resource_url($path = '')
680 {
681 return app()->base_url(path_join('resources', $path));
682 }
683 }
684 if (!\function_exists('Kirki\\Framework\\json_decoded_data')) {
685 /**
686 * Get the decoded JSON data from a file.
687 *
688 * @param string $file_path
689 * @param bool $associative
690 * @return mixed
691 */
692 function json_decoded_data(string $file_path, bool $associative = \true)
693 {
694 if (!\file_exists($file_path)) {
695 return null;
696 }
697 $content = \file_get_contents($file_path);
698 return \json_decode($content, $associative);
699 }
700 }
701 if (!\function_exists('Kirki\\Framework\\value')) {
702 /**
703 * Get the value of a variable.
704 *
705 * @param mixed $value
706 * @param mixed ...$args
707 * @return mixed
708 */
709 function value($value, ...$args)
710 {
711 return $value instanceof Closure ? $value(...$args) : $value;
712 }
713 }
714 if (!\function_exists('Kirki\\Framework\\is_rest_request')) {
715 /**
716 * Check if the current request is a REST request.
717 *
718 * @return bool
719 */
720 function is_rest_request()
721 {
722 $is_rest = \defined('REST_REQUEST') && REST_REQUEST;
723 if ($is_rest) {
724 return \true;
725 }
726 $rest_route = Sanitizer::apply_rule(\filter_input(\INPUT_GET, 'rest_route', \FILTER_UNSAFE_RAW), Sanitizer::BOOL);
727 if ($rest_route) {
728 return \true;
729 }
730 $request_uri = \filter_input(\INPUT_SERVER, 'REQUEST_URI', \FILTER_SANITIZE_URL);
731 if (empty($request_uri)) {
732 return \false;
733 }
734 $is_rest = \strpos($request_uri, '/' . rest_get_url_prefix() . '/') !== \false;
735 return $is_rest;
736 }
737 }
738 if (!\function_exists('Kirki\\Framework\\message')) {
739 /**
740 * Get a message by key.
741 *
742 * @param string $key the key of the message
743 * @param mixed $args the arguments to pass to the message
744 *
745 * @return string|null
746 *
747 * @since 1.0.0
748 */
749 function message($key, ...$args)
750 {
751 return app()->make(MessagesBag::class)->get($key, ...$args);
752 }
753 }
754