forked from bvdputte/kirby-autopublish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
64 lines (60 loc) · 2.54 KB
/
index.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
<?php
require __DIR__ . DS . "src" . DS . "Autopublish.php";
// For composer
@include_once __DIR__ . '/vendor/autoload.php';
Kirby::plugin('bvdputte/kirbyAutopublish', [
'options' => [
'fieldName' => 'autopublish',
'fieldNameUnpublish' => 'autounpublish',
'poormanscron' => false,
'poormanscron.interval' => 1, // in minutes
'cache.poormanscron' => true,
'webhookToken' => false,
],
'collections' => [
'autoPublishedDrafts' => function ($site) {
$autopublishfield = option("bvdputte.kirbyAutopublish.fieldName");
$drafts = $site->index()->drafts();
$autoPublishedDrafts = $drafts->filter(function ($draft) use ($autopublishfield) {
return ($draft->$autopublishfield()->exists()) && ($draft->$autopublishfield()->isNotEmpty()) && (empty($draft->errors()) === true);
});
return $autoPublishedDrafts;
},
'autoUnpublishedListed' => function ($site) {
$autounpublishfield = option("bvdputte.kirbyAutopublish.fieldNameUnpublish");
$listed = $site->index()->children()->listed();
$autoUnpublishedListed = $listed->filter(function ($listedPage) use ($autounpublishfield) {
return ($listedPage->$autounpublishfield()->exists()) && ($listedPage->$autounpublishfield()->isNotEmpty()) && (empty($listedPage->errors()) === true);
});
return $autoUnpublishedListed;
}
],
'hooks' => [
'route:before' => function ($route, $path, $method) {
/*
* For servers without cron, enable "poormanscron"
* ⚠️ Ugly, non-performant hack to bypass cache
*/
if (option("bvdputte.kirbyAutopublish.poormanscron")) {
bvdputte\kirbyAutopublish\Autopublish::poorManCronRun();
}
}
],
'routes' => [
[
'pattern' => 'kirby-autopublish/(:any)',
'action' => function ($token) {
if (
$token !== option('bvdputte.kirbyAutopublish.webhookToken', false) ||
option('bvdputte.kirbyAutopublish.webhookToken', false) === false
) {
throw new Exception('Invalid token');
return false;
}
bvdputte\kirbyAutopublish\Autopublish::publish();
bvdputte\kirbyAutopublish\Autopublish::unpublish();
return new Response('done', 'text/html');
}
],
]
]);