Warning Make sure you have the old and new blueprint available when running this command.
Otherwise this command will wipe the currently stored content.
Always make sure to test the command first and have a backup of your content!
Last active
October 31, 2023 10:08
-
-
Save lukaskleinschmidt/83b58a8416b9a2a758996f729e849c51 to your computer and use it in GitHub Desktop.
Kirby CLI Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Kirby\CLI\CLI; | |
use Kirby\Cms\App; | |
use Kirby\Cms\Page; | |
App::plugin('site/commands', [ | |
'commands' => [ | |
'template:update' => [ | |
'description' => 'Update page templates without any permission or sanity checks.', | |
'args' => [ | |
'from' => [ | |
'prefix' => 'f', | |
'longPrefix' => 'from', | |
'description' => 'The the current template name', | |
], | |
'to' => [ | |
'prefix' => 't', | |
'longPrefix' => 'to', | |
'description' => 'The the new template name', | |
], | |
'parent' => [ | |
'prefix' => 'p', | |
'longPrefix' => 'parent', | |
'description' => 'The parent to to find descendants from', | |
], | |
], | |
'command' => function (CLI $cli) { | |
$kirby = $cli->kirby(); | |
$from = $cli->argOrPrompt('from', 'What is the current template name?'); | |
$to = $cli->argOrPrompt('to', 'What is the new template name?'); | |
$parent = $cli->arg('parent'); | |
$parent = $parent ? page($parent) : site(); | |
if (! $parent) { | |
return $cli->climate()->lightRed('Parent not found'); | |
} | |
Page::$methods['updateTemplate'] = function (string $template) { | |
return $this->convertTo($template); | |
}; | |
$pages = $kirby->impersonate('kirby', function () use ($parent, $from, $to) { | |
return $parent->index(true)->filter(function (Page $page) use ($from, $to) { | |
if ($page->intendedTemplate()->name() !== $from) { | |
return false; | |
} | |
return $page->updateTemplate($to); | |
}); | |
}); | |
$cli->climate()->lightBlue('Updated pages count: ' . $pages->count()); | |
}, | |
], | |
], | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment