Update to Kirby 4.7.0

This commit is contained in:
Paul Nicoué 2025-04-21 18:57:21 +02:00
parent 02a9ab387c
commit ba25a9a198
509 changed files with 26604 additions and 14872 deletions

View file

@ -6,6 +6,7 @@ use Kirby\Exception\DuplicateException;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Exception\PermissionException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
/**
@ -22,12 +23,9 @@ class PageRules
/**
* Validates if the sorting number of the page can be changed
*
* @param \Kirby\Cms\Page $page
* @param int|null $num
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException If the given number is invalid
*/
public static function changeNum(Page $page, int $num = null): bool
public static function changeNum(Page $page, int|null $num = null): bool
{
if ($num !== null && $num < 0) {
throw new InvalidArgumentException(['key' => 'page.num.invalid']);
@ -39,9 +37,6 @@ class PageRules
/**
* Validates if the slug for the page can be changed
*
* @param \Kirby\Cms\Page $page
* @param string $slug
* @return bool
* @throws \Kirby\Exception\DuplicateException If a page with this slug already exists
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the slug
*/
@ -57,6 +52,7 @@ class PageRules
}
self::validateSlugLength($slug);
self::validateSlugProtectedPaths($page, $slug);
$siblings = $page->parentModel()->children();
$drafts = $page->parentModel()->drafts();
@ -85,14 +81,13 @@ class PageRules
/**
* Validates if the status for the page can be changed
*
* @param \Kirby\Cms\Page $page
* @param string $status
* @param int|null $position
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException If the given status is invalid
*/
public static function changeStatus(Page $page, string $status, int $position = null): bool
{
public static function changeStatus(
Page $page,
string $status,
int|null $position = null
): bool {
if (isset($page->blueprint()->status()[$status]) === false) {
throw new InvalidArgumentException(['key' => 'page.status.invalid']);
}
@ -108,11 +103,9 @@ class PageRules
/**
* Validates if a page can be converted to a draft
*
* @param \Kirby\Cms\Page $page
* @return bool
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status or the page cannot be converted to a draft
*/
public static function changeStatusToDraft(Page $page)
public static function changeStatusToDraft(Page $page): bool
{
if ($page->permissions()->changeStatus() !== true) {
throw new PermissionException([
@ -138,13 +131,10 @@ class PageRules
/**
* Validates if the status of a page can be changed to listed
*
* @param \Kirby\Cms\Page $page
* @param int $position
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException If the given position is invalid
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status or the status for the page cannot be changed by any user
*/
public static function changeStatusToListed(Page $page, int $position)
public static function changeStatusToListed(Page $page, int $position): bool
{
// no need to check for status changing permissions,
// instead we need to check for sorting permissions
@ -173,8 +163,6 @@ class PageRules
/**
* Validates if the status of a page can be changed to unlisted
*
* @param \Kirby\Cms\Page $page
* @return bool
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the status
*/
public static function changeStatusToUnlisted(Page $page)
@ -187,9 +175,6 @@ class PageRules
/**
* Validates if the template of the page can be changed
*
* @param \Kirby\Cms\Page $page
* @param string $template
* @return bool
* @throws \Kirby\Exception\LogicException If the template of the page cannot be changed at all
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the template
*/
@ -222,9 +207,6 @@ class PageRules
/**
* Validates if the title of the page can be changed
*
* @param \Kirby\Cms\Page $page
* @param string $title
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException If the new title is empty
* @throws \Kirby\Exception\PermissionException If the user is not allowed to change the title
*/
@ -239,11 +221,7 @@ class PageRules
]);
}
if (Str::length($title) === 0) {
throw new InvalidArgumentException([
'key' => 'page.changeTitle.empty',
]);
}
static::validateTitleLength($title);
return true;
}
@ -251,8 +229,6 @@ class PageRules
/**
* Validates if the page can be created
*
* @param \Kirby\Cms\Page $page
* @return bool
* @throws \Kirby\Exception\DuplicateException If the same page or a draft already exists
* @throws \Kirby\Exception\InvalidArgumentException If the slug is invalid
* @throws \Kirby\Exception\PermissionException If the user is not allowed to create this page
@ -269,6 +245,7 @@ class PageRules
}
self::validateSlugLength($page->slug());
self::validateSlugProtectedPaths($page, $page->slug());
if ($page->exists() === true) {
throw new DuplicateException([
@ -303,9 +280,6 @@ class PageRules
/**
* Validates if the page can be deleted
*
* @param \Kirby\Cms\Page $page
* @param bool $force
* @return bool
* @throws \Kirby\Exception\LogicException If the page has children and should not be force-deleted
* @throws \Kirby\Exception\PermissionException If the user is not allowed to delete the page
*/
@ -330,14 +304,13 @@ class PageRules
/**
* Validates if the page can be duplicated
*
* @param \Kirby\Cms\Page $page
* @param string $slug
* @param array $options
* @return bool
* @throws \Kirby\Exception\PermissionException If the user is not allowed to duplicate the page
*/
public static function duplicate(Page $page, string $slug, array $options = []): bool
{
public static function duplicate(
Page $page,
string $slug,
array $options = []
): bool {
if ($page->permissions()->duplicate() !== true) {
throw new PermissionException([
'key' => 'page.duplicate.permission',
@ -352,12 +325,95 @@ class PageRules
return true;
}
/**
* Check if the page can be moved
* to the given parent
*/
public static function move(Page $page, Site|Page $parent): bool
{
// if nothing changes, there's no need for checks
if ($parent->is($page->parent()) === true) {
return true;
}
if ($page->permissions()->move() !== true) {
throw new PermissionException([
'key' => 'page.move.permission',
'data' => [
'slug' => $page->slug()
]
]);
}
// the page cannot be moved into itself
if ($parent instanceof Page && ($page->is($parent) === true || $page->isAncestorOf($parent) === true)) {
throw new LogicException([
'key' => 'page.move.ancestor',
]);
}
// check for duplicates
if ($parent->childrenAndDrafts()->find($page->slug())) {
throw new DuplicateException([
'key' => 'page.move.duplicate',
'data' => [
'slug' => $page->slug(),
]
]);
}
$allowed = [];
// collect all allowed subpage templates
// from all pages sections in the blueprint
// (only consider page sections that list pages
// of the targeted new parent page)
$sections = array_filter(
$parent->blueprint()->sections(),
fn ($section) =>
$section->type() === 'pages' &&
$section->parent()->is($parent)
);
// check if the parent has at least one pages section
if ($sections === []) {
throw new LogicException([
'key' => 'page.move.noSections',
'data' => [
'parent' => $parent->id() ?? '/',
]
]);
}
// go through all allowed templates and
// add the name to the allowlist
foreach ($sections as $section) {
foreach ($section->templates() as $template) {
$allowed[] = $template;
}
}
// check if the template of this page is allowed as subpage type
// for the potential new parent
if (
$allowed !== [] &&
in_array($page->intendedTemplate()->name(), $allowed) === false
) {
throw new PermissionException([
'key' => 'page.move.template',
'data' => [
'template' => $page->intendedTemplate()->name(),
'parent' => $parent->id() ?? '/',
]
]);
}
return true;
}
/**
* Check if the page can be published
* (status change from draft to listed or unlisted)
*
* @param Page $page
* @return bool
*/
public static function publish(Page $page): bool
{
@ -383,9 +439,6 @@ class PageRules
/**
* Validates if the page can be updated
*
* @param \Kirby\Cms\Page $page
* @param array $content
* @return bool
* @throws \Kirby\Exception\PermissionException If the user is not allowed to update the page
*/
public static function update(Page $page, array $content = []): bool
@ -406,11 +459,9 @@ class PageRules
* Ensures that the slug is not empty and doesn't exceed the maximum length
* to make sure that the directory name will be accepted by the filesystem
*
* @param string $slug New slug to check
* @return void
* @throws \Kirby\Exception\InvalidArgumentException If the slug is empty or too long
*/
protected static function validateSlugLength(string $slug): void
public static function validateSlugLength(string $slug): void
{
$slugLength = Str::length($slug);
@ -433,4 +484,48 @@ class PageRules
}
}
}
/**
* Ensure that a top-level page path does not start with one of
* the reserved URL paths, e.g. for API or the Panel
*
* @throws \Kirby\Exception\InvalidArgumentException If the page ID starts as one of the disallowed paths
*/
protected static function validateSlugProtectedPaths(
Page $page,
string $slug
): void {
if ($page->parent() === null) {
$paths = A::map(
['api', 'assets', 'media', 'panel'],
fn ($url) => $page->kirby()->url($url, true)->path()->toString()
);
$index = array_search($slug, $paths);
if ($index !== false) {
throw new InvalidArgumentException([
'key' => 'page.changeSlug.reserved',
'data' => [
'path' => $paths[$index]
]
]);
}
}
}
/**
* Ensures that the page title is not empty
*
* @throws \Kirby\Exception\InvalidArgumentException If the title is empty
*/
public static function validateTitleLength(string $title): void
{
if (Str::length($title) === 0) {
throw new InvalidArgumentException([
'key' => 'page.changeTitle.empty',
]);
}
}
}