Last active
June 9, 2025 15:38
-
-
Save mauricerenck/76340f1333bcb542c4b1678511fa9b3c to your computer and use it in GitHub Desktop.
Kirby POSSE Route
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 | |
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