Collections
2 weeks ago
Concerns
2 weeks ago
Console
2 weeks ago
Constants
2 weeks ago
Contracts
2 weeks ago
Database
2 weeks ago
Discovery
2 weeks ago
Exceptions
2 weeks ago
Filesystem
2 weeks ago
Http
2 weeks ago
Managers
2 weeks ago
Middlewares
2 weeks ago
Polyfill
2 weeks ago
Supports
2 weeks ago
Validation
2 weeks ago
Wordpress
2 weeks ago
ApiExceptionHandler.php
2 weeks ago
Application.php
2 weeks ago
Container.php
2 weeks ago
CoreServiceProvider.php
2 weeks ago
DTO.php
2 weeks ago
Facade.php
2 weeks ago
Listener.php
2 weeks ago
Resource.php
2 weeks ago
Route.php
2 weeks ago
Sanitizer.php
2 weeks ago
ServiceProvider.php
2 weeks ago
helpers.php
2 weeks ago
helpers.php
559 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\Request; |
| 20 | use Kirki\Framework\Wordpress\User; |
| 21 | use Kirki\Framework\Http\Response; |
| 22 | use Kirki\Framework\Supports\Arr; |
| 23 | use Kirki\Framework\Supports\HigherOrderTapProxy; |
| 24 | use Kirki\Framework\Supports\MessagesBag; |
| 25 | use Kirki\Framework\Supports\Str; |
| 26 | use Kirki\Framework\Supports\Url; |
| 27 | use Kirki\Framework\Supports\Utils; |
| 28 | use Symfony\Component\VarDumper\Cloner\VarCloner; |
| 29 | use Symfony\Component\VarDumper\Dumper\CliDumper; |
| 30 | use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
| 31 | use Symfony\Component\VarDumper\VarDumper; |
| 32 | use function Kirki\Framework\Polyfill\array_key_first; |
| 33 | use function Kirki\Framework\Polyfill\array_key_last; |
| 34 | use function Kirki\Framework\Polyfill\is_iterable; |
| 35 | if (!\function_exists('Kirki\\Framework\\app')) { |
| 36 | /** |
| 37 | * Get the container instance. |
| 38 | * |
| 39 | * @template TClass |
| 40 | * |
| 41 | * @param string|class-string<TClass>|null $abstract |
| 42 | * @param array $parameters |
| 43 | * |
| 44 | * @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? Application : mixed)) |
| 45 | */ |
| 46 | function app($abstract = null, array $parameters = []) |
| 47 | { |
| 48 | if (\is_null($abstract)) { |
| 49 | return Application::get_instance(); |
| 50 | } |
| 51 | return Application::get_instance()->make($abstract, $parameters); |
| 52 | } |
| 53 | } |
| 54 | if (!\function_exists('Kirki\\Framework\\deep_get')) { |
| 55 | /** |
| 56 | * Get a value from an array using a dot notation key. |
| 57 | * |
| 58 | * @param array $target The target array to get the value from. |
| 59 | * @param string|array $key The key to get the value from. |
| 60 | * @param mixed $default The default value to return if the key is not found. |
| 61 | * |
| 62 | * @return mixed The value from the array or the default value if the key is not found. |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | function deep_get($target, $key, $default = null) |
| 67 | { |
| 68 | if (\is_null($key)) { |
| 69 | return $target; |
| 70 | } |
| 71 | $key = \is_array($key) ? $key : \explode('.', $key); |
| 72 | foreach ($key as $index => $segment) { |
| 73 | unset($key[$index]); |
| 74 | if (\is_null($segment)) { |
| 75 | return $target; |
| 76 | } |
| 77 | if ($segment === '*') { |
| 78 | if ($target instanceof Collection) { |
| 79 | $target = $target->all(); |
| 80 | } elseif (!is_iterable($target)) { |
| 81 | return $default; |
| 82 | } |
| 83 | $result = []; |
| 84 | foreach ($target as $item) { |
| 85 | $result[] = deep_get($item, $key); |
| 86 | } |
| 87 | return \in_array('*', $key) ? Arr::collapse($result) : $result; |
| 88 | } |
| 89 | switch ($segment) { |
| 90 | case '\\*': |
| 91 | $segment = '*'; |
| 92 | break; |
| 93 | case '\\{first}': |
| 94 | $segment = '{first}'; |
| 95 | break; |
| 96 | case '{first}': |
| 97 | $segment = array_key_first(Arr::from($target)); |
| 98 | break; |
| 99 | case '\\{last}': |
| 100 | $segment = '{last}'; |
| 101 | break; |
| 102 | case '{last}': |
| 103 | $segment = array_key_last(Arr::from($target)); |
| 104 | break; |
| 105 | } |
| 106 | if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
| 107 | $target = $target[$segment]; |
| 108 | } elseif (\is_object($target) && isset($target->{$segment})) { |
| 109 | $target = $target->{$segment}; |
| 110 | } else { |
| 111 | return $default; |
| 112 | } |
| 113 | } |
| 114 | return $target; |
| 115 | } |
| 116 | } |
| 117 | if (!\function_exists('Kirki\\Framework\\config')) { |
| 118 | /** |
| 119 | * Get the config |
| 120 | * |
| 121 | * @param string|null $key |
| 122 | * @param mixed $default |
| 123 | * @return mixed |
| 124 | */ |
| 125 | function config($key = null, $default = null) |
| 126 | { |
| 127 | static $cache = []; |
| 128 | $filename = \strpos($key, '.') ? \substr($key, 0, \strpos($key, '.')) : $key; |
| 129 | $key = \strpos($key, '.') ? \substr($key, \strpos($key, '.') + 1) : null; |
| 130 | if (!isset($cache[$filename])) { |
| 131 | $path = app()->config_path("{$filename}.php"); |
| 132 | if (\file_exists($path)) { |
| 133 | $cache[$filename] = (include $path); |
| 134 | } else { |
| 135 | $cache[$filename] = null; |
| 136 | } |
| 137 | } |
| 138 | if (\is_null($cache[$filename])) { |
| 139 | return value($default); |
| 140 | } |
| 141 | return deep_get($cache[$filename], $key, $default); |
| 142 | } |
| 143 | } |
| 144 | if (!\function_exists('Kirki\\Framework\\user')) { |
| 145 | /** |
| 146 | * Get the user instance. |
| 147 | * |
| 148 | * @return User |
| 149 | */ |
| 150 | function user($user_id = null) |
| 151 | { |
| 152 | return app()->make(User::class, ['user_id' => $user_id]); |
| 153 | } |
| 154 | } |
| 155 | if (!\function_exists('Kirki\\Framework\\response')) { |
| 156 | /** |
| 157 | * Get the response instance. |
| 158 | * |
| 159 | * @return Response |
| 160 | */ |
| 161 | function response() |
| 162 | { |
| 163 | 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']); |
| 164 | } |
| 165 | } |
| 166 | if (!\function_exists('Kirki\\Framework\\request')) { |
| 167 | /** |
| 168 | * Get the request instance. |
| 169 | * |
| 170 | * @param string|null $key |
| 171 | * @param mixed $default |
| 172 | * |
| 173 | * @return ($key is null ? Request : ($key is array ? array : mixed)) |
| 174 | * |
| 175 | * @since 1.0.0 |
| 176 | */ |
| 177 | function request($key = null, $default = null) |
| 178 | { |
| 179 | if (\is_null($key)) { |
| 180 | return app('request'); |
| 181 | } |
| 182 | if (\is_array($key)) { |
| 183 | return app('request')->only($key); |
| 184 | } |
| 185 | $value = app('request')->get($key, $default); |
| 186 | return \is_null($value) ? value($default) : $value; |
| 187 | } |
| 188 | } |
| 189 | if (!\function_exists('Kirki\\Framework\\with_prefix')) { |
| 190 | /** |
| 191 | * Get the key with prefix applied. |
| 192 | * |
| 193 | * @param string $key |
| 194 | * @return string |
| 195 | */ |
| 196 | function with_prefix(string $key) |
| 197 | { |
| 198 | $prefix = app()->prefix(); |
| 199 | if (Str::starts_with($key, $prefix)) { |
| 200 | return $key; |
| 201 | } |
| 202 | return $prefix . $key; |
| 203 | } |
| 204 | } |
| 205 | if (!\function_exists('Kirki\\Framework\\without_prefix')) { |
| 206 | /** |
| 207 | * Get the key without prefix applied. |
| 208 | * |
| 209 | * @param string $key |
| 210 | * @return string |
| 211 | */ |
| 212 | function without_prefix(string $key) |
| 213 | { |
| 214 | $prefix = app()->prefix(); |
| 215 | if (!Str::starts_with($key, $prefix)) { |
| 216 | return $key; |
| 217 | } |
| 218 | return \substr($key, \strlen($prefix)); |
| 219 | } |
| 220 | } |
| 221 | if (!\function_exists('Kirki\\Framework\\redirect')) { |
| 222 | /** |
| 223 | * Redirect to the given location. |
| 224 | */ |
| 225 | function redirect($location) |
| 226 | { |
| 227 | Url::redirect($location); |
| 228 | } |
| 229 | } |
| 230 | if (!\function_exists('Kirki\\Framework\\is_valid_json')) { |
| 231 | /** |
| 232 | * Check if the string is a valid JSON. |
| 233 | * |
| 234 | * @param string $string |
| 235 | * @return bool |
| 236 | */ |
| 237 | function is_valid_json($string) |
| 238 | { |
| 239 | if (!\is_string($string)) { |
| 240 | return \false; |
| 241 | } |
| 242 | \json_decode($string); |
| 243 | return \json_last_error() === \JSON_ERROR_NONE; |
| 244 | } |
| 245 | } |
| 246 | if (!\function_exists('Kirki\\Framework\\clean_path')) { |
| 247 | /** |
| 248 | * Clean and normalize file paths for consistency. |
| 249 | * |
| 250 | * @param string $path |
| 251 | * @param bool $trailing_slash Add a trailing slash? Default true. |
| 252 | * @return string |
| 253 | */ |
| 254 | function clean_path(string $path, bool $trailing_slash = \true) |
| 255 | { |
| 256 | $path = wp_normalize_path($path); |
| 257 | return $trailing_slash ? trailingslashit($path) : untrailingslashit($path); |
| 258 | } |
| 259 | } |
| 260 | if (!\function_exists('Kirki\\Framework\\uuid')) { |
| 261 | /** |
| 262 | * Generate a UUID. |
| 263 | * |
| 264 | * @return string |
| 265 | */ |
| 266 | function uuid() |
| 267 | { |
| 268 | return Utils::uuid(); |
| 269 | } |
| 270 | } |
| 271 | if (!\function_exists('Kirki\\Framework\\url')) { |
| 272 | /** |
| 273 | * Generate a URL. |
| 274 | * |
| 275 | * @param string $url |
| 276 | * @param array $query_vars |
| 277 | * @return string |
| 278 | */ |
| 279 | function url($url, $query_vars = []) |
| 280 | { |
| 281 | return Url::make($url, $query_vars); |
| 282 | } |
| 283 | } |
| 284 | if (!\function_exists('Kirki\\Framework\\is_block_theme')) { |
| 285 | /** |
| 286 | * Check if the site is using a block template |
| 287 | * |
| 288 | * This function will return true if the site is using a block template and false otherwise. |
| 289 | * |
| 290 | * @return bool True if the site is using a block template, false otherwise. |
| 291 | */ |
| 292 | function is_block_theme() |
| 293 | { |
| 294 | return \function_exists('wp_is_block_theme') && wp_is_block_theme(); |
| 295 | } |
| 296 | } |
| 297 | if (!\function_exists('Kirki\\Framework\\migrator')) { |
| 298 | /** |
| 299 | * Get the migrator instance. |
| 300 | * |
| 301 | * @return Migrator |
| 302 | */ |
| 303 | function migrator() |
| 304 | { |
| 305 | return app()->make(Migrator::class); |
| 306 | } |
| 307 | } |
| 308 | if (!\function_exists('Kirki\\Framework\\tap')) { |
| 309 | /** |
| 310 | * Call the given Closure with the given value. |
| 311 | * |
| 312 | * @param mixed $value |
| 313 | * @param \Closure $callback |
| 314 | * @return mixed |
| 315 | */ |
| 316 | function tap($value, $callback = null) |
| 317 | { |
| 318 | if (\is_null($callback)) { |
| 319 | return new HigherOrderTapProxy($value); |
| 320 | } |
| 321 | $callback($value); |
| 322 | return $value; |
| 323 | } |
| 324 | } |
| 325 | if (!\function_exists('Kirki\\Framework\\faker')) { |
| 326 | /** |
| 327 | * Get the fake instance. |
| 328 | * |
| 329 | * @return Generator |
| 330 | */ |
| 331 | function faker() |
| 332 | { |
| 333 | return app()->make(Factory::class); |
| 334 | } |
| 335 | } |
| 336 | if (!\function_exists('Kirki\\Framework\\configure_dumper')) { |
| 337 | function configure_dumper() |
| 338 | { |
| 339 | static $configured = \false; |
| 340 | if ($configured) { |
| 341 | return; |
| 342 | } |
| 343 | $configured = \true; |
| 344 | $is_cli = \defined('WP_CLI') && WP_CLI; |
| 345 | if ($is_cli) { |
| 346 | VarDumper::setHandler(function ($var) { |
| 347 | $dumper = new CliDumper(); |
| 348 | $dumper->dump((new VarCloner())->cloneVar($var)); |
| 349 | }); |
| 350 | return; |
| 351 | } |
| 352 | VarDumper::setHandler(function ($var) { |
| 353 | $dumper = new HtmlDumper(); |
| 354 | $dumper->dump((new VarCloner())->cloneVar($var)); |
| 355 | }); |
| 356 | } |
| 357 | } |
| 358 | if (!\function_exists('Kirki\\Framework\\dump')) { |
| 359 | /** |
| 360 | * Dump the given arguments. |
| 361 | * |
| 362 | * @param mixed ...$args |
| 363 | * |
| 364 | * @return void |
| 365 | * |
| 366 | * @since 1.0.0 |
| 367 | */ |
| 368 | function dump(...$args) |
| 369 | { |
| 370 | if (!\class_exists(VarDumper::class) || !app()->is_dev_mode()) { |
| 371 | return; |
| 372 | } |
| 373 | configure_dumper(); |
| 374 | foreach ($args as $arg) { |
| 375 | VarDumper::dump($arg); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | if (!\function_exists('Kirki\\Framework\\dd')) { |
| 380 | /** |
| 381 | * Dump the given arguments and die. |
| 382 | * |
| 383 | * @param mixed ...$args |
| 384 | * |
| 385 | * @return void |
| 386 | * |
| 387 | * @since 1.0.0 |
| 388 | */ |
| 389 | function dd(...$args) |
| 390 | { |
| 391 | dump(...$args); |
| 392 | die(1); |
| 393 | } |
| 394 | } |
| 395 | if (!\function_exists('Kirki\\Framework\\app_path')) { |
| 396 | /** |
| 397 | * Get the path to the application directory. |
| 398 | * |
| 399 | * @param string $path |
| 400 | * @return string |
| 401 | */ |
| 402 | function app_path($path = '') |
| 403 | { |
| 404 | return app()->path($path); |
| 405 | } |
| 406 | } |
| 407 | if (!\function_exists('Kirki\\Framework\\config_path')) { |
| 408 | /** |
| 409 | * Get the path to the config directory. |
| 410 | * |
| 411 | * @param string $path |
| 412 | * @return string |
| 413 | */ |
| 414 | function config_path($path = '') |
| 415 | { |
| 416 | return app()->config_path($path); |
| 417 | } |
| 418 | } |
| 419 | if (!\function_exists('Kirki\\Framework\\database_path')) { |
| 420 | /** |
| 421 | * Get the path to the database directory. |
| 422 | * |
| 423 | * @param string $path |
| 424 | * @return string |
| 425 | */ |
| 426 | function database_path($path = '') |
| 427 | { |
| 428 | return app()->database_path($path); |
| 429 | } |
| 430 | } |
| 431 | if (!\function_exists('Kirki\\Framework\\base_path')) { |
| 432 | /** |
| 433 | * Get the path to the base directory. |
| 434 | * |
| 435 | * @param string $path |
| 436 | * @return string |
| 437 | */ |
| 438 | function base_path($path = '') |
| 439 | { |
| 440 | return app()->base_path($path); |
| 441 | } |
| 442 | } |
| 443 | if (!\function_exists('Kirki\\Framework\\resource_path')) { |
| 444 | /** |
| 445 | * Get the path to the resources directory. |
| 446 | * |
| 447 | * @param string $path |
| 448 | * @return string |
| 449 | */ |
| 450 | function resource_path($path = '') |
| 451 | { |
| 452 | return app()->resource_path($path); |
| 453 | } |
| 454 | } |
| 455 | if (!\function_exists('Kirki\\Framework\\bootstrap_path')) { |
| 456 | /** |
| 457 | * Get the path to the bootstrap directory. |
| 458 | * |
| 459 | * @param string $path |
| 460 | * @return string |
| 461 | */ |
| 462 | function bootstrap_path($path = '') |
| 463 | { |
| 464 | return app()->bootstrap_path($path); |
| 465 | } |
| 466 | } |
| 467 | if (!\function_exists('Kirki\\Framework\\collection')) { |
| 468 | /** |
| 469 | * Create a collection instance from an array. |
| 470 | * |
| 471 | * @param array $array |
| 472 | * @return Collection |
| 473 | */ |
| 474 | function collection(array $array = []) |
| 475 | { |
| 476 | return new Collection($array); |
| 477 | } |
| 478 | } |
| 479 | if (!\function_exists('Kirki\\Framework\\resource_url')) { |
| 480 | /** |
| 481 | * Get the path to the resources directory. |
| 482 | * |
| 483 | * @param string $path |
| 484 | * @return string |
| 485 | */ |
| 486 | function resource_url($path = '') |
| 487 | { |
| 488 | return app()->base_url(path_join('resources', $path)); |
| 489 | } |
| 490 | } |
| 491 | if (!\function_exists('Kirki\\Framework\\json_decoded_data')) { |
| 492 | /** |
| 493 | * Get the decoded JSON data from a file. |
| 494 | * |
| 495 | * @param string $file_path |
| 496 | * @param bool $associative |
| 497 | * @return mixed |
| 498 | */ |
| 499 | function json_decoded_data(string $file_path, bool $associative = \true) |
| 500 | { |
| 501 | if (!\file_exists($file_path)) { |
| 502 | return null; |
| 503 | } |
| 504 | $content = \file_get_contents($file_path); |
| 505 | return \json_decode($content, $associative); |
| 506 | } |
| 507 | } |
| 508 | if (!\function_exists('Kirki\\Framework\\value')) { |
| 509 | /** |
| 510 | * Get the value of a variable. |
| 511 | * |
| 512 | * @param mixed $value |
| 513 | * @param mixed ...$args |
| 514 | * @return mixed |
| 515 | */ |
| 516 | function value($value, ...$args) |
| 517 | { |
| 518 | return $value instanceof Closure ? $value(...$args) : $value; |
| 519 | } |
| 520 | } |
| 521 | if (!\function_exists('Kirki\\Framework\\is_rest_request')) { |
| 522 | /** |
| 523 | * Check if the current request is a REST request. |
| 524 | * |
| 525 | * @return bool |
| 526 | */ |
| 527 | function is_rest_request() |
| 528 | { |
| 529 | $is_rest = \defined('REST_REQUEST') && REST_REQUEST; |
| 530 | if ($is_rest) { |
| 531 | return \true; |
| 532 | } |
| 533 | $rest_route = Sanitizer::apply_rule(\filter_input(\INPUT_GET, 'rest_route', \FILTER_UNSAFE_RAW), Sanitizer::BOOL); |
| 534 | if ($rest_route) { |
| 535 | return \true; |
| 536 | } |
| 537 | $request_uri = \filter_input(\INPUT_SERVER, 'REQUEST_URI', \FILTER_SANITIZE_URL); |
| 538 | if (empty($request_uri)) { |
| 539 | return \false; |
| 540 | } |
| 541 | $is_rest = \strpos($request_uri, '/' . rest_get_url_prefix() . '/') !== \false; |
| 542 | return $is_rest; |
| 543 | } |
| 544 | } |
| 545 | if (!\function_exists('Kirki\\Framework\\message')) { |
| 546 | /** |
| 547 | * Get a message by key. |
| 548 | * |
| 549 | * @param string $key the key of the message |
| 550 | * @param mixed $args the arguments to pass to the message |
| 551 | * |
| 552 | * @return string |
| 553 | */ |
| 554 | function message($key, ...$args) |
| 555 | { |
| 556 | return app()->make(MessagesBag::class)->get($key, ...$args); |
| 557 | } |
| 558 | } |
| 559 |