From c4a667294a8eb753dba9ee0ade2c655ed3acb47d Mon Sep 17 00:00:00 2001 From: Bart Vandeputte Date: Fri, 12 Apr 2019 15:13:43 +0200 Subject: [PATCH] Init --- .gitignore | 26 ++++++++++++++++ LICENSE.md | 21 +++++++++++++ composer.json | 27 +++++++++++++++++ index.php | 39 ++++++++++++++++++++++++ readme.md | 73 +++++++++++++++++++++++++++++++++++++++++++++ src/Autopublish.php | 47 +++++++++++++++++++++++++++++ worker.php | 21 +++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100755 LICENSE.md create mode 100755 composer.json create mode 100644 index.php create mode 100644 readme.md create mode 100644 src/Autopublish.php create mode 100644 worker.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2871bd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# OS files +.DS_Store +.php_cs.cache + +/vendor/**/.markdown + +# files of Composer dependencies that are not needed for the plugin +/vendor/**/.* +/vendor/**/*.json +/vendor/**/*.txt +/vendor/**/*.md +/vendor/**/*.yml +/vendor/**/*.yaml +/vendor/**/*.xml +/vendor/**/*.dist +/vendor/**/readme.php +/vendor/**/LICENSE +/vendor/**/COPYING +/vendor/**/VERSION +/vendor/**/docs/* +/vendor/**/example/* +/vendor/**/examples/* +/vendor/**/test/* +/vendor/**/tests/* +/vendor/**/php4/* +/vendor/getkirby/composer-installer \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100755 index 0000000..15bc72f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..403b5f6 --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "name": "bvdputte/kirby-autopublish", + "description": "Kirby 3 plugin to schedule the automatic publishing of pages on a certain date+time. It is built to work with enabled cache.", + "license": "MIT", + "type": "kirby-plugin", + "authors": [ + { + "name": "Bart Vandeputte", + "email": "bart.vdputte@gmail.com" + } + ], + "keywords": [ + "kirby3", + "kirby3-plugin", + "schedule", + "autopublish", + "publish", + "tasks", + "background tasks" + ], + "require": { + "getkirby/composer-installer": "^1.1" + }, + "config": { + "optimize-autoloader": true + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..c2e4f17 --- /dev/null +++ b/index.php @@ -0,0 +1,39 @@ + [ + 'fieldName' => 'autopublish', + 'poormanscron' => false, + 'poormanscron.interval' => 1, // in minutes + 'cache.poormanscron' => true + ], + 'collections' => [ + 'autoPublishedDrafts' => function ($site) { + $autopublishfield = option("bvdputte.kirbyAutopublish.fieldName"); + $drafts = $site->pages()->drafts(); + $autoPublishedDrafts = $drafts->filter(function ($draft) use ($autopublishfield) { + return ($draft->$autopublishfield()->exists()) && (!$draft->$autopublishfield()->isEmpty()); + }); + + return $autoPublishedDrafts; + } + ], + 'hooks' => [ + 'route:before' => function ($route, $path, $method) { + /* + * For servers without cron, enable "poormanscron" + * ⚠️ Ugly, non-performant hack to bypass cache + * @TODO: Fix this as soon as this is possible: + * https://github.com/getkirby/ideas/issues/23 + */ + if (option("bvdputte.kirbyAutopublish.poormanscron")) { + bvdputte\kirbyAutopublish\Autopublish::poorManCronRun(); + } + } + ] +]); \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..be08944 --- /dev/null +++ b/readme.md @@ -0,0 +1,73 @@ +# Kirby autopublish plugin + +Kirby 3 plugin to schedule the automatic publishing of pages (drafts) on a certain date+time. +It is built to work with enabled cache. + +## Installation + +- unzip [master.zip](https://github.com/bvdputte/kirby-autopublish/archive/master.zip) as folder `site/plugins/kirby-autopublish` or +- `git submodule add https://github.com/bvdputte/kirby-autopublish.git site/plugins/kirby-autopublish` or +- `composer require bvdputte/kirby-autopublish` + +⚠️ Highly recommended to also install the [kirby-log plugin](https://github.com/bvdputte/kirby-log), to get page-publication logs. + +## Usage + +### Setup worker + +#### 1. Via Cron + +Add the worker file `site/plugins/kirby-autopublish/worker.php` to [cron](https://en.wikipedia.org/wiki/Cron) or similar at the desired interval (.e.g. each minute). + +💡 This is the preferred method for setting up kirby-autopublish. + +#### 2. Poor man's cron + +When cron is not installed on your server, you can _fake_ cron by [enabling this](#options-and-opinionated-defaults) in `config.php`. + +### Setup field in blueprint + +```yaml +autopublish: + label: Autopublish on + type: date + time: true + default: now +``` + +## Options and opinionated defaults + +Set in `config.php`: + +### Field name + +Autopublish searches for a date-field. By default the name is `autopublish`, but can be changed: + +```php +'fieldName' => 'myautopublishfieldname' +``` + +### Poor man's cron + +By default, this is disabled. Enable: + +```php +// Enable poor man's cron +'bvdputte.kirbyAutopublish.poormanscron' => true +``` + +The default interval for poor man's cron to check is each minute. Change this to e.g. quarterly: + +```php +'bvdputte.kirbyAutopublish.poormanscron.interval' => 15 +``` + +## Disclaimer + +This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/bvdputte/kirby-queue/issues/new). + +## License + +[MIT](https://opensource.org/licenses/MIT) + +It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech. diff --git a/src/Autopublish.php b/src/Autopublish.php new file mode 100644 index 0000000..6907c91 --- /dev/null +++ b/src/Autopublish.php @@ -0,0 +1,47 @@ +collection("autoPublishedDrafts") + ->filter(function ($draft) use ($autopublishfield) { + $publishTime = new \Datetime($draft->$autopublishfield()); + return $publishTime->getTimestamp() < time(); + }); + + // Publish pages which are due + kirby()->impersonate("kirby"); + foreach($pagesToPublish as $p) { + try { + $p->changeStatus("listed"); + if(function_exists('kirbyLog')) { + kirbyLog("autopublish.log")->log("Autopublished " . $p->id(), "info"); + } + } catch (Exception $e) { + if(function_exists('kirbyLog')) { + kirbyLog("autopublish.log")->log("Error adding " . $newPage->id() . " to autopublish queue", "error", [$e->getMessage()]); + } else { + error_log("Error adding " . $newPage->id() . " to autopublish queue"); + } + } + } + } + + public static function poorManCronRun() + { + $pmcCache = kirby()->cache("bvdputte.kirbyAutopublish.poormanscron"); + $lastRun = $pmcCache->get("lastrun"); + + if ($lastRun === null) { + self::publish(); + $expire = option("bvdputte.kirbyAutopublish.poormanscron.interval"); + $pmcCache->set("lastrun", time(), $expire); + } + } + +} \ No newline at end of file diff --git a/worker.php b/worker.php new file mode 100644 index 0000000..8d65729 --- /dev/null +++ b/worker.php @@ -0,0 +1,21 @@ + [ + 'debug' => true, + ], + 'roots' => [ + 'kirby' => $kirbyPath + ], +]); + +// Work the queue +Autopublish::publish(); +exit(); \ No newline at end of file