/
Page.php
1543 lines (1347 loc) · 35.6 KB
/
Page.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Kirby\Cms;
use Kirby\Exception\Exception;
use Kirby\Exception\NotFoundException;
use Kirby\Http\Uri;
use Kirby\Toolkit\A;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Str;
/**
* The `$page` object is the heart and
* soul of Kirby. It is used to construct
* pages and all their dependencies like
* children, files, content, etc.
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://getkirby.com/license
*/
class Page extends ModelWithContent
{
const CLASS_ALIAS = 'page';
use PageActions;
use PageSiblings;
use HasChildren;
use HasFiles;
use HasMethods;
use HasSiblings;
/**
* All registered page methods
*
* @var array
*/
public static $methods = [];
/**
* Registry with all Page models
*
* @var array
*/
public static $models = [];
/**
* The PageBlueprint object
*
* @var \Kirby\Cms\PageBlueprint
*/
protected $blueprint;
/**
* Nesting level
*
* @var int
*/
protected $depth;
/**
* Sorting number + slug
*
* @var string
*/
protected $dirname;
/**
* Path of dirnames
*
* @var string
*/
protected $diruri;
/**
* Draft status flag
*
* @var bool
*/
protected $isDraft;
/**
* The Page id
*
* @var string
*/
protected $id;
/**
* The template, that should be loaded
* if it exists
*
* @var \Kirby\Cms\Template
*/
protected $intendedTemplate;
/**
* @var array
*/
protected $inventory;
/**
* The sorting number
*
* @var int|null
*/
protected $num;
/**
* The parent page
*
* @var \Kirby\Cms\Page|null
*/
protected $parent;
/**
* Absolute path to the page directory
*
* @var string
*/
protected $root;
/**
* The parent Site object
*
* @var \Kirby\Cms\Site|null
*/
protected $site;
/**
* The URL-appendix aka slug
*
* @var string
*/
protected $slug;
/**
* The intended page template
*
* @var string
*/
protected $template;
/**
* The page url
*
* @var string|null
*/
protected $url;
/**
* Magic caller
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call(string $method, array $arguments = [])
{
// public property access
if (isset($this->$method) === true) {
return $this->$method;
}
// page methods
if ($this->hasMethod($method)) {
return $this->callMethod($method, $arguments);
}
// return page content otherwise
return $this->content()->get($method, $arguments);
}
/**
* Creates a new page object
*
* @param array $props
*/
public function __construct(array $props)
{
// set the slug as the first property
$this->slug = $props['slug'] ?? null;
// add all other properties
$this->setProperties($props);
}
/**
* Improved `var_dump` output
*
* @return array
*/
public function __debugInfo(): array
{
return array_merge($this->toArray(), [
'content' => $this->content(),
'children' => $this->children(),
'siblings' => $this->siblings(),
'translations' => $this->translations(),
'files' => $this->files(),
]);
}
/**
* Returns the url to the api endpoint
*
* @internal
* @param bool $relative
* @return string
*/
public function apiUrl(bool $relative = false): string
{
if ($relative === true) {
return 'pages/' . $this->panelId();
} else {
return $this->kirby()->url('api') . '/pages/' . $this->panelId();
}
}
/**
* Returns the blueprint object
*
* @return \Kirby\Cms\PageBlueprint
*/
public function blueprint()
{
if (is_a($this->blueprint, 'Kirby\Cms\PageBlueprint') === true) {
return $this->blueprint;
}
return $this->blueprint = PageBlueprint::factory('pages/' . $this->intendedTemplate(), 'pages/default', $this);
}
/**
* Returns an array with all blueprints that are available for the page
*
* @param string $inSection
* @return array
*/
public function blueprints(string $inSection = null): array
{
if ($inSection !== null) {
return $this->blueprint()->section($inSection)->blueprints();
}
$blueprints = [];
$templates = $this->blueprint()->changeTemplate() ?? $this->blueprint()->options()['changeTemplate'] ?? [];
$currentTemplate = $this->intendedTemplate()->name();
if (is_array($templates) === false) {
$templates = [];
}
// add the current template to the array if it's not already there
if (in_array($currentTemplate, $templates) === false) {
array_unshift($templates, $currentTemplate);
}
// make sure every template is only included once
$templates = array_unique($templates);
foreach ($templates as $template) {
try {
$props = Blueprint::load('pages/' . $template);
$blueprints[] = [
'name' => basename($props['name']),
'title' => $props['title'],
];
} catch (Exception $e) {
// skip invalid blueprints
}
}
return array_values($blueprints);
}
/**
* Builds the cache id for the page
*
* @param string $contentType
* @return string
*/
protected function cacheId(string $contentType): string
{
$cacheId = [$this->id()];
if ($this->kirby()->multilang() === true) {
$cacheId[] = $this->kirby()->language()->code();
}
$cacheId[] = $contentType;
return implode('.', $cacheId);
}
/**
* Prepares the content for the write method
*
* @internal
* @param array $data
* @param string $languageCode
* @return array
*/
public function contentFileData(array $data, string $languageCode = null): array
{
return A::prepend($data, [
'title' => $data['title'] ?? null,
'slug' => $data['slug'] ?? null
]);
}
/**
* Returns the content text file
* which is found by the inventory method
*
* @internal
* @param string $languageCode
* @return string
*/
public function contentFileName(string $languageCode = null): string
{
return $this->intendedTemplate()->name();
}
/**
* Call the page controller
*
* @internal
* @param array $data
* @param string $contentType
* @return array
*/
public function controller($data = [], $contentType = 'html'): array
{
// create the template data
$data = array_merge($data, [
'kirby' => $kirby = $this->kirby(),
'site' => $site = $this->site(),
'pages' => $site->children(),
'page' => $site->visit($this)
]);
// call the template controller if there's one.
return array_merge($kirby->controller($this->template()->name(), $data, $contentType), $data);
}
/**
* Returns a number indicating how deep the page
* is nested within the content folder
*
* @return int
*/
public function depth(): int
{
return $this->depth = $this->depth ?? (substr_count($this->id(), '/') + 1);
}
/**
* Sorting number + Slug
*
* @return string
*/
public function dirname(): string
{
if ($this->dirname !== null) {
return $this->dirname;
}
if ($this->num() !== null) {
return $this->dirname = $this->num() . Dir::$numSeparator . $this->uid();
} else {
return $this->dirname = $this->uid();
}
}
/**
* Sorting number + Slug
*
* @return string
*/
public function diruri(): string
{
if (is_string($this->diruri) === true) {
return $this->diruri;
}
if ($this->isDraft() === true) {
$dirname = '_drafts/' . $this->dirname();
} else {
$dirname = $this->dirname();
}
if ($parent = $this->parent()) {
return $this->diruri = $parent->diruri() . '/' . $dirname;
} else {
return $this->diruri = $dirname;
}
}
/**
* Provides a kirbytag or markdown
* tag for the page, which will be
* used in the panel, when the page
* gets dragged onto a textarea
*
* @internal
* @param string $type (null|auto|kirbytext|markdown)
* @return string
*/
public function dragText(string $type = null): string
{
$type = $type ?? 'auto';
if ($type === 'auto') {
$type = option('panel.kirbytext', true) ? 'kirbytext' : 'markdown';
}
switch ($type) {
case 'markdown':
return '[' . $this->title() . '](' . $this->url() . ')';
default:
return '(link: ' . $this->id() . ' text: ' . $this->title() . ')';
}
}
/**
* Checks if the page exists on disk
*
* @return bool
*/
public function exists(): bool
{
return is_dir($this->root()) === true;
}
/**
* Constructs a Page object and also
* takes page models into account.
*
* @internal
* @param mixed $props
* @return self
*/
public static function factory($props)
{
if (empty($props['model']) === false) {
return static::model($props['model'], $props);
}
return new static($props);
}
/**
* Checks if the intended template
* for the page exists.
*
* @return bool
*/
public function hasTemplate(): bool
{
return $this->intendedTemplate() === $this->template();
}
/**
* Returns the Page Id
*
* @return string
*/
public function id(): string
{
if ($this->id !== null) {
return $this->id;
}
// set the id, depending on the parent
if ($parent = $this->parent()) {
return $this->id = $parent->id() . '/' . $this->uid();
}
return $this->id = $this->uid();
}
/**
* Returns the template that should be
* loaded if it exists.
*
* @return \Kirby\Cms\Template
*/
public function intendedTemplate()
{
if ($this->intendedTemplate !== null) {
return $this->intendedTemplate;
}
return $this->setTemplate($this->inventory()['template'])->intendedTemplate();
}
/**
* Returns the inventory of files
* children and content files
*
* @internal
* @return array
*/
public function inventory(): array
{
if ($this->inventory !== null) {
return $this->inventory;
}
$kirby = $this->kirby();
return $this->inventory = Dir::inventory(
$this->root(),
$kirby->contentExtension(),
$kirby->contentIgnore(),
$kirby->multilang()
);
}
/**
* Compares the current object with the given page object
*
* @param \Kirby\Cms\Page|string $page
* @return bool
*/
public function is($page): bool
{
if (is_a($page, 'Kirby\Cms\Page') === false) {
if (is_string($page) === false) {
return false;
}
$page = $this->kirby()->page($page);
}
if (is_a($page, 'Kirby\Cms\Page') === false) {
return false;
}
return $this->id() === $page->id();
}
/**
* Checks if the page is the current page
*
* @return bool
*/
public function isActive(): bool
{
if ($page = $this->site()->page()) {
if ($page->is($this) === true) {
return true;
}
}
return false;
}
/**
* Checks if the page is a direct or indirect ancestor of the given $page object
*
* @param Page $child
* @return bool
*/
public function isAncestorOf(Page $child): bool
{
return $child->parents()->has($this->id()) === true;
}
/**
* Checks if the page can be cached in the
* pages cache. This will also check if one
* of the ignore rules from the config kick in.
*
* @return bool
*/
public function isCacheable(): bool
{
$kirby = $this->kirby();
$cache = $kirby->cache('pages');
$options = $cache->options();
$ignore = $options['ignore'] ?? null;
// the pages cache is switched off
if (($options['active'] ?? false) === false) {
return false;
}
// inspect the current request
$request = $kirby->request();
// disable the pages cache for any request types but GET or HEAD
if (in_array($request->method(), ['GET', 'HEAD']) === false) {
return false;
}
// disable the pages cache when there's request data
if (empty($request->data()) === false) {
return false;
}
// disable the pages cache when there are any params
if ($request->params()->isNotEmpty()) {
return false;
}
// check for a custom ignore rule
if (is_a($ignore, 'Closure') === true) {
if ($ignore($this) === true) {
return false;
}
}
// ignore pages by id
if (is_array($ignore) === true) {
if (in_array($this->id(), $ignore) === true) {
return false;
}
}
return true;
}
/**
* Checks if the page is a child of the given page
*
* @param \Kirby\Cms\Page|string $parent
* @return bool
*/
public function isChildOf($parent): bool
{
if ($parentObj = $this->parent()) {
return $parentObj->is($parent);
}
return false;
}
/**
* Checks if the page is a descendant of the given page
*
* @param \Kirby\Cms\Page|string $parent
* @return bool
*/
public function isDescendantOf($parent): bool
{
if (is_string($parent) === true) {
$parent = $this->site()->find($parent);
}
if (!$parent) {
return false;
}
return $this->parents()->has($parent->id()) === true;
}
/**
* Checks if the page is a descendant of the currently active page
*
* @return bool
*/
public function isDescendantOfActive(): bool
{
if ($active = $this->site()->page()) {
return $this->isDescendantOf($active);
}
return false;
}
/**
* Checks if the current page is a draft
*
* @return bool
*/
public function isDraft(): bool
{
return $this->isDraft;
}
/**
* Checks if the page is the error page
*
* @return bool
*/
public function isErrorPage(): bool
{
return $this->id() === $this->site()->errorPageId();
}
/**
* Check if the page can be read by the current user
*
* @return bool
*/
public function isReadable(): bool
{
static $readable = [];
$template = $this->intendedTemplate()->name();
if (isset($readable[$template]) === true) {
return $readable[$template];
}
return $readable[$template] = $this->permissions()->can('read');
}
/**
* Checks if the page is the home page
*
* @return bool
*/
public function isHomePage(): bool
{
return $this->id() === $this->site()->homePageId();
}
/**
* It's often required to check for the
* home and error page to stop certain
* actions. That's why there's a shortcut.
*
* @return bool
*/
public function isHomeOrErrorPage(): bool
{
return $this->isHomePage() === true || $this->isErrorPage() === true;
}
/**
* @deprecated 3.0.0 Use `Page::isUnlisted()` instead
* @return bool
*/
public function isInvisible(): bool
{
deprecated('$page->isInvisible() is deprecated, use $page->isUnlisted() instead. $page->isInvisible() will be removed in Kirby 3.5.0.');
return $this->isUnlisted();
}
/**
* Checks if the page has a sorting number
*
* @return bool
*/
public function isListed(): bool
{
return $this->num() !== null;
}
/**
* Checks if the page is open.
* Open pages are either the current one
* or descendants of the current one.
*
* @return bool
*/
public function isOpen(): bool
{
if ($this->isActive() === true) {
return true;
}
if ($page = $this->site()->page()) {
if ($page->parents()->has($this->id()) === true) {
return true;
}
}
return false;
}
/**
* Checks if the page is sortable
*
* @return bool
*/
public function isSortable(): bool
{
return $this->permissions()->can('sort');
}
/**
* Checks if the page has no sorting number
*
* @return bool
*/
public function isUnlisted(): bool
{
return $this->isListed() === false;
}
/**
* @deprecated 3.0.0 Use `Page::isListed()` instead
* @return bool
*/
public function isVisible(): bool
{
deprecated('$page->isVisible() is deprecated, use $page->isListed() instead. $page->isVisible() will be removed in Kirby 3.5.0.');
return $this->isListed();
}
/**
* Checks if the page access is verified.
* This is only used for drafts so far.
*
* @internal
* @param string $token
* @return bool
*/
public function isVerified(string $token = null)
{
if (
$this->isDraft() === false &&
$this->parents()->findBy('status', 'draft') === null
) {
return true;
}
if ($token === null) {
return false;
}
return $this->token() === $token;
}
/**
* Returns the root to the media folder for the page
*
* @internal
* @return string
*/
public function mediaRoot(): string
{
return $this->kirby()->root('media') . '/pages/' . $this->id();
}
/**
* The page's base URL for any files
*
* @internal
* @return string
*/
public function mediaUrl(): string
{
return $this->kirby()->url('media') . '/pages/' . $this->id();
}
/**
* Creates a page model if it has been registered
*
* @internal
* @param string $name
* @param array $props
* @return self
*/
public static function model(string $name, array $props = [])
{
if ($class = (static::$models[$name] ?? null)) {
$object = new $class($props);
if (is_a($object, 'Kirby\Cms\Page') === true) {
return $object;
}
}
return new static($props);
}
/**
* Returns the last modification date of the page
*
* @param string $format
* @param string|null $handler
* @return int|string
*/
public function modified(string $format = null, string $handler = null)
{
return F::modified($this->contentFile(), $format, $handler ?? $this->kirby()->option('date.handler', 'date'));
}
/**
* Returns the sorting number
*
* @return int|null
*/
public function num(): ?int
{
return $this->num;
}
/**
* Returns the panel icon definition
* according to the blueprint settings
*
* @internal
* @param array $params
* @return array
*/
public function panelIcon(array $params = null): array
{
if ($icon = $this->blueprint()->icon()) {
$params['type'] = $icon;
// check for emojis
if (strlen($icon) !== Str::length($icon)) {
$params['emoji'] = true;
}
}
return parent::panelIcon($params);
}
/**
* Returns the escaped Id, which is
* used in the panel to make routing work properly
*
* @internal
* @return string
*/
public function panelId(): string
{
return str_replace('/', '+', $this->id());
}
/**
* Returns the image file object based on provided query
*
* @internal
* @param string|null $query
* @return \Kirby\Cms\File|\Kirby\Cms\Asset|null
*/
protected function panelImageSource(string $query = null)
{
if ($query === null) {
$query = 'page.image';
}
return parent::panelImageSource($query);
}
/**
* Returns the full path without leading slash
*
* @internal
* @return string
*/
public function panelPath(): string
{
return 'pages/' . $this->panelId();
}
/**
* Prepares the response data for page pickers
* and page fields
*
* @param array|null $params
* @return array
*/
public function panelPickerData(array $params = []): array
{
$image = $this->panelImage($params['image'] ?? []);
$icon = $this->panelIcon($image);
return [
'dragText' => $this->dragText(),
'hasChildren' => $this->hasChildren(),
'icon' => $icon,
'id' => $this->id(),
'image' => $image,
'info' => $this->toString($params['info'] ?? false),
'link' => $this->panelUrl(true),
'text' => $this->toString($params['text'] ?? '{{ page.title }}'),
'url' => $this->url(),
];
}
/**
* Returns the url to the editing view
* in the panel
*
* @internal
* @param bool $relative
* @return string
*/
public function panelUrl(bool $relative = false): string
{
if ($relative === true) {
return '/' . $this->panelPath();
} else {
return $this->kirby()->url('panel') . '/' . $this->panelPath();
}
}