forked from getkirby/kirby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
690 lines (632 loc) · 16.5 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<?php
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\Helpers;
use Kirby\Cms\Html;
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Cms\Response;
use Kirby\Cms\Site;
use Kirby\Cms\Url;
use Kirby\Filesystem\Asset;
use Kirby\Filesystem\F;
use Kirby\Http\Router;
use Kirby\Image\QrCode;
use Kirby\Template\Slot;
use Kirby\Template\Snippet;
use Kirby\Toolkit\Date;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\V;
if (Helpers::hasOverride('asset') === false) { // @codeCoverageIgnore
/**
* Helper to create an asset object
*/
function asset(string $path): Asset
{
return new Asset($path);
}
}
if (Helpers::hasOverride('attr') === false) { // @codeCoverageIgnore
/**
* Generates a list of HTML attributes
*
* @param array|null $attr A list of attributes as key/value array
* @param string|null $before An optional string that will be prepended if the result is not empty
* @param string|null $after An optional string that will be appended if the result is not empty
*/
function attr(
array|null $attr = null,
string|null $before = null,
string|null $after = null
): string|null {
return Html::attr($attr, null, $before, $after);
}
}
if (Helpers::hasOverride('collection') === false) { // @codeCoverageIgnore
/**
* Returns the result of a collection by name
*
* @return \Kirby\Toolkit\Collection|null
* @todo 5.0 Add return type declaration
*/
function collection(string $name, array $options = [])
{
return App::instance()->collection($name, $options);
}
}
if (Helpers::hasOverride('csrf') === false) { // @codeCoverageIgnore
/**
* Checks / returns a CSRF token
*
* @param string|null $check Pass a token here to compare it to the one in the session
* @return string|bool Either the token or a boolean check result
*/
function csrf(string|null $check = null): string|bool
{
// check explicitly if there have been no arguments at all;
// checking for null introduces a security issue because null could come
// from user input or bugs in the calling code!
if (func_num_args() === 0) {
return App::instance()->csrf();
}
return App::instance()->csrf($check);
}
}
if (Helpers::hasOverride('css') === false) { // @codeCoverageIgnore
/**
* Creates one or multiple CSS link tags
*
* @param string|array $url Relative or absolute URLs, an array of URLs or `@auto` for automatic template css loading
* @param string|array|null $options Pass an array of attributes for the link tag or a media attribute string
*/
function css(
string|array $url,
string|array|null $options = null
): string|null {
return Html::css($url, $options);
}
}
if (Helpers::hasOverride('deprecated') === false) { // @codeCoverageIgnore
/**
* Triggers a deprecation warning if debug mode is active
* @since 3.3.0
*
* @return bool Whether the warning was triggered
*/
function deprecated(string $message): bool
{
return Helpers::deprecated($message);
}
}
if (Helpers::hasOverride('dump') === false) { // @codeCoverageIgnore
/**
* Simple object and variable dumper
* to help with debugging.
*/
function dump(mixed $variable, bool $echo = true): string
{
return Helpers::dump($variable, $echo);
}
}
if (Helpers::hasOverride('e') === false) { // @codeCoverageIgnore
/**
* Smart version of echo with an if condition as first argument
*
* @param mixed $value The string to be echoed if the condition is true
* @param mixed $alternative An alternative string which should be echoed when the condition is false
*/
function e(mixed $condition, mixed $value, mixed $alternative = null): void
{
echo $condition ? $value : $alternative;
}
}
if (Helpers::hasOverride('endslot') === false) { // @codeCoverageIgnore
/**
* Ends the last started template slot
*/
function endslot(): void
{
Slot::end();
}
}
if (Helpers::hasOverride('endsnippet') === false) { // @codeCoverageIgnore
/**
* Renders the currently active snippet with slots
*/
function endsnippet(): void
{
Snippet::end();
}
}
if (Helpers::hasOverride('esc') === false) { // @codeCoverageIgnore
/**
* Escape context specific output
*
* @param string $string Untrusted data
* @param string $context Location of output (`html`, `attr`, `js`, `css`, `url` or `xml`)
* @return string Escaped data
*/
function esc(string $string, string $context = 'html'): string
{
return Str::esc($string, $context);
}
}
if (Helpers::hasOverride('get') === false) { // @codeCoverageIgnore
/**
* Shortcut for $kirby->request()->get()
*
* @param mixed $key The key to look for. Pass false or null to return the entire request array.
* @param mixed $default Optional default value, which should be returned if no element has been found
*/
function get(mixed $key = null, mixed $default = null): mixed
{
return App::instance()->request()->get($key, $default);
}
}
if (Helpers::hasOverride('gist') === false) { // @codeCoverageIgnore
/**
* Embeds a Github Gist
*/
function gist(string $url, string|null $file = null): string
{
return App::instance()->kirbytag([
'gist' => $url,
'file' => $file,
]);
}
}
if (Helpers::hasOverride('go') === false) { // @codeCoverageIgnore
/**
* Redirects to the given Urls
* Urls can be relative or absolute.
*/
function go(string $url = '/', int $code = 302): never
{
Response::go($url, $code);
}
}
if (Helpers::hasOverride('h') === false) { // @codeCoverageIgnore
/**
* Shortcut for html()
*
* @param string|null $string unencoded text
*/
function h(string|null $string, bool $keepTags = false): string
{
return Html::encode($string, $keepTags);
}
}
if (Helpers::hasOverride('html') === false) { // @codeCoverageIgnore
/**
* Creates safe html by encoding special characters
*
* @param string|null $string unencoded text
*/
function html(string|null $string, bool $keepTags = false): string
{
return Html::encode($string, $keepTags);
}
}
if (Helpers::hasOverride('image') === false) { // @codeCoverageIgnore
/**
* Return an image from any page
* specified by the path
*
* Example:
* <?= image('some/page/myimage.jpg') ?>
*/
function image(string|null $path = null): File|null
{
return App::instance()->image($path);
}
}
if (Helpers::hasOverride('invalid') === false) { // @codeCoverageIgnore
/**
* Runs a number of validators on a set of data and checks if the data is invalid
*/
function invalid(
array $data = [],
array $rules = [],
array $messages = []
): array {
return V::invalid($data, $rules, $messages);
}
}
if (Helpers::hasOverride('js') === false) { // @codeCoverageIgnore
/**
* Creates a script tag to load a javascript file
*/
function js(
string|array $url,
string|array|bool|null $options = null
): string|null {
return Html::js($url, $options);
}
}
if (Helpers::hasOverride('kirby') === false) { // @codeCoverageIgnore
/**
* Returns the Kirby object in any situation
*/
function kirby(): App
{
return App::instance();
}
}
if (Helpers::hasOverride('kirbytag') === false) { // @codeCoverageIgnore
/**
* Makes it possible to use any defined Kirbytag as standalone function
*/
function kirbytag(
string|array $type,
string|null $value = null,
array $attr = [],
array $data = []
): string {
return App::instance()->kirbytag($type, $value, $attr, $data);
}
}
if (Helpers::hasOverride('kirbytags') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags in the given string. Shortcut
* for `$kirby->kirbytags($text, $data)`
*/
function kirbytags(string|null $text = null, array $data = []): string
{
return App::instance()->kirbytags($text, $data);
}
}
if (Helpers::hasOverride('kirbytext') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags and Markdown in the
* given string. Shortcut for `$kirby->kirbytext()`
*/
function kirbytext(string|null $text = null, array $data = []): string
{
return App::instance()->kirbytext($text, $data);
}
}
if (Helpers::hasOverride('kirbytextinline') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags and inline Markdown in the
* given string.
* @since 3.1.0
*/
function kirbytextinline(string|null $text = null, array $options = []): string
{
$options['markdown']['inline'] = true;
return App::instance()->kirbytext($text, $options);
}
}
if (Helpers::hasOverride('kt') === false) { // @codeCoverageIgnore
/**
* Shortcut for `kirbytext()` helper
*/
function kt(string|null $text = null, array $data = []): string
{
return App::instance()->kirbytext($text, $data);
}
}
if (Helpers::hasOverride('kti') === false) { // @codeCoverageIgnore
/**
* Shortcut for `kirbytextinline()` helper
* @since 3.1.0
*/
function kti(string|null $text = null, array $options = []): string
{
$options['markdown']['inline'] = true;
return App::instance()->kirbytext($text, $options);
}
}
if (Helpers::hasOverride('load') === false) { // @codeCoverageIgnore
/**
* A super simple class autoloader
*/
function load(array $classmap, string|null $base = null): void
{
F::loadClasses($classmap, $base);
}
}
if (Helpers::hasOverride('markdown') === false) { // @codeCoverageIgnore
/**
* Parses markdown in the given string. Shortcut for
* `$kirby->markdown($text)`
*/
function markdown(string|null $text = null, array $options = []): string
{
return App::instance()->markdown($text, $options);
}
}
if (Helpers::hasOverride('option') === false) { // @codeCoverageIgnore
/**
* Shortcut for `$kirby->option($key, $default)`
*/
function option(string $key, mixed $default = null): mixed
{
return App::instance()->option($key, $default);
}
}
if (Helpers::hasOverride('page') === false) { // @codeCoverageIgnore
/**
* Fetches a single page by id or
* the current page when no id is specified
*/
function page(string|null $id = null): Page|null
{
if (empty($id) === true) {
return App::instance()->site()->page();
}
return App::instance()->site()->find($id);
}
}
if (Helpers::hasOverride('pages') === false) { // @codeCoverageIgnore
/**
* Helper to build pages collection
*/
function pages(string|array ...$id): Pages|null
{
// ensure that a list of string arguments and an array
// as the first argument are treated the same
if (count($id) === 1 && is_array($id[0]) === true) {
$id = $id[0];
}
// always passes $id an array; ensures we get a
// collection even if only one ID is passed
return App::instance()->site()->find($id);
}
}
if (Helpers::hasOverride('param') === false) { // @codeCoverageIgnore
/**
* Returns a single param from the URL
*
* @psalm-return ($fallback is string ? string : string|null)
*/
function param(string $key, string|null $fallback = null): string|null
{
return App::instance()->request()->url()->params()->$key ?? $fallback;
}
}
if (Helpers::hasOverride('params') === false) { // @codeCoverageIgnore
/**
* Returns all params from the current Url
*/
function params(): array
{
return App::instance()->request()->url()->params()->toArray();
}
}
if (Helpers::hasOverride('qr') === false) { // @codeCoverageIgnore
/**
* Creates a QR code object
*/
function qr(string|ModelWithContent $data): QrCode
{
if ($data instanceof ModelWithContent) {
$data = $data->url();
}
return new QrCode($data);
}
}
if (Helpers::hasOverride('r') === false) { // @codeCoverageIgnore
/**
* Smart version of return with an if condition as first argument
*
* @param mixed $value The string to be returned if the condition is true
* @param mixed $alternative An alternative string which should be returned when the condition is false
*/
function r(mixed $condition, mixed $value, mixed $alternative = null): mixed
{
return $condition ? $value : $alternative;
}
}
if (Helpers::hasOverride('router') === false) { // @codeCoverageIgnore
/**
* Creates a micro-router and executes
* the routing action immediately
* @since 3.6.0
*/
function router(
string|null $path = null,
string $method = 'GET',
array $routes = [],
Closure|null $callback = null
): mixed {
return Router::execute($path, $method, $routes, $callback);
}
}
if (Helpers::hasOverride('site') === false) { // @codeCoverageIgnore
/**
* Returns the current site object
*/
function site(): Site
{
return App::instance()->site();
}
}
if (Helpers::hasOverride('size') === false) { // @codeCoverageIgnore
/**
* Determines the size/length of numbers, strings, arrays and countable objects
*/
function size(mixed $value): int
{
return Helpers::size($value);
}
}
if (Helpers::hasOverride('slot') === false) { // @codeCoverageIgnore
/**
* Starts a new template slot
*/
function slot(string $name = 'default'): void
{
Slot::begin($name);
}
}
if (Helpers::hasOverride('smartypants') === false) { // @codeCoverageIgnore
/**
* Enhances the given string with
* smartypants. Shortcut for `$kirby->smartypants($text)`
*/
function smartypants(string|null $text = null): string
{
return App::instance()->smartypants($text);
}
}
if (Helpers::hasOverride('snippet') === false) { // @codeCoverageIgnore
/**
* Embeds a snippet from the snippet folder
*/
function snippet(
$name,
$data = [],
bool $return = false,
bool $slots = false
): Snippet|string|null {
return App::instance()->snippet($name, $data, $return, $slots);
}
}
if (Helpers::hasOverride('svg') === false) { // @codeCoverageIgnore
/**
* Includes an SVG file by absolute or
* relative file path.
*/
function svg(string|File $file): string|false
{
return Html::svg($file);
}
}
if (Helpers::hasOverride('t') === false) { // @codeCoverageIgnore
/**
* Returns translate string for key from translation file
*/
function t(
string|array $key,
string|null $fallback = null,
string|null $locale = null
): string|array|Closure|null {
return I18n::translate($key, $fallback, $locale);
}
}
if (Helpers::hasOverride('tc') === false) { // @codeCoverageIgnore
/**
* Translates a count
*
* @param bool $formatNumber If set to `false`, the count is not formatted
*/
function tc(
string $key,
int $count,
string|null $locale = null,
bool $formatNumber = true
): mixed {
return I18n::translateCount($key, $count, $locale, $formatNumber);
}
}
if (Helpers::hasOverride('timestamp') === false) { // @codeCoverageIgnore
/**
* Rounds the minutes of the given date
* by the defined step
*
* @param int|array|null $step array of `unit` and `size` to round to nearest
*/
function timestamp(
string|null $date = null,
int|array|null $step = null
): int|null {
return Date::roundedTimestamp($date, $step);
}
}
if (Helpers::hasOverride('tt') === false) { // @codeCoverageIgnore
/**
* Translate by key and then replace
* placeholders in the text
*/
function tt(
string $key,
string|array|null $fallback = null,
array|null $replace = null,
string|null $locale = null
): string {
return I18n::template($key, $fallback, $replace, $locale);
}
}
if (Helpers::hasOverride('u') === false) { // @codeCoverageIgnore
/**
* Shortcut for url()
*/
function u(
string|null $path = null,
array|string|null $options = null
): string {
return Url::to($path, $options);
}
}
if (Helpers::hasOverride('url') === false) { // @codeCoverageIgnore
/**
* Builds an absolute URL for a given path
*/
function url(
string|null $path = null,
array|string|null $options = null
): string {
return Url::to($path, $options);
}
}
if (Helpers::hasOverride('uuid') === false) { // @codeCoverageIgnore
/**
* Creates a compliant v4 UUID
*/
function uuid(): string
{
return Str::uuid();
}
}
if (Helpers::hasOverride('video') === false) { // @codeCoverageIgnore
/**
* Creates a video embed via iframe for Youtube or Vimeo
* videos. The embed Urls are automatically detected from
* the given Url.
*/
function video(
string $url,
array $options = [],
array $attr = []
): string|null {
return Html::video($url, $options, $attr);
}
}
if (Helpers::hasOverride('vimeo') === false) { // @codeCoverageIgnore
/**
* Embeds a Vimeo video by URL in an iframe
*/
function vimeo(
string $url,
array $options = [],
array $attr = []
): string|null {
return Html::vimeo($url, $options, $attr);
}
}
if (Helpers::hasOverride('widont') === false) { // @codeCoverageIgnore
/**
* The widont function makes sure that there are no
* typographical widows at the end of a paragraph –
* that's a single word in the last line
*/
function widont(string|null $string = null): string
{
return Str::widont($string);
}
}
if (Helpers::hasOverride('youtube') === false) { // @codeCoverageIgnore
/**
* Embeds a Youtube video by URL in an iframe
*/
function youtube(
string $url,
array $options = [],
array $attr = []
): string|null {
return Html::youtube($url, $options, $attr);
}
}