Skip to content

Instantly share code, notes, and snippets.

@mauricerenck
Last active June 9, 2025 15:38
Show Gist options
  • Save mauricerenck/76340f1333bcb542c4b1678511fa9b3c to your computer and use it in GitHub Desktop.
Save mauricerenck/76340f1333bcb542c4b1678511fa9b3c to your computer and use it in GitHub Desktop.
Kirby POSSE Route
<?php
return [
'routes' => [
[
[
'pattern' => 'posse',
'method' => 'POST',
'action' => function () {
$request = kirby()->request();
$requestData = $request->data();
$authHeader = $request->header('Authorization');
$token = $authHeader ? explode(' ', $authHeader)[1] : null;
if ($token !== option('mauricerenck.posse.token', '')) {
return new Response('Unauthorized', 'text/plain', 401);
}
$year = date('Y');
$parent = kirby()->page('notes/' . $year);
if (is_null($parent)) {
return new Response('Not Found', 'text/plain', 404);
}
$template = 'note';
$autoPublish = isset($requestData['autopublish']) && filter_var($requestData['autopublish'], FILTER_VALIDATE_BOOLEAN);
$newData = [
'title' => empty($requestData['title']) ? 'Bookmark ' . date('Y-m-d') : trim($requestData['title']),
'text' => !empty($requestData['posttext']) ? trim($requestData['posttext']) : '',
'icSkipUrl' => isset($requestData['skipUrl']) && filter_var($requestData['skipUrl'], FILTER_VALIDATE_BOOLEAN),
'enableExternalPosting' => isset($requestData['externalPost']) && filter_var($requestData['externalPost'], FILTER_VALIDATE_BOOLEAN)
];
if (!empty($url) && V::url($url)) {
$newData['title'] = 'Link: ' . $newData['title'];
$newData['link'] = $url;
$template = 'bookmark';
}
$slug = Str::slug($newData['title']);
$unusedSlug = false;
while ($unusedSlug === false) {
$unusedSlug = is_null($parent->childrenAndDrafts()->find($slug));
if (!$unusedSlug) {
$slug = $slug . '-' . uniqid();
}
}
kirby()->impersonate('kirby');
$newPage = Page::create([
'parent' => $parent,
'slug' => $slug,
'template' => $template,
'content' => $newData
]);
if ($autoPublish === true) {
$newPage->changeStatus('listed');
}
$response = [
'url' => $newPage->url(),
'status' => $autoPublish ? 'published' : 'draft',
];
return new Response(json_encode($response), 'application/json', 201);
},
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment