Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Vandeputte committed Apr 12, 2019
0 parents commit c4a6672
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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
}
}
39 changes: 39 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

require __DIR__ . DS . "src" . DS . "Autopublish.php";

// For composer
@include_once __DIR__ . '/vendor/autoload.php';

Kirby::plugin('bvdputte/kirbyAutopublish', [
'options' => [
'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();
}
}
]
]);
73 changes: 73 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions src/Autopublish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace bvdputte\kirbyAutopublish;

class Autopublish {

public static function publish()
{
$kirby = kirby();
$autopublishfield = option("bvdputte.kirbyAutopublish.fieldName");
$pagesToPublish = $kirby->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);
}
}

}
21 changes: 21 additions & 0 deletions worker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use bvdputte\kirbyAutopublish\Autopublish;

// Bootstrap Kirby (from with the plugin's folder)
require '../../../kirby/bootstrap.php';
$kirbyPath = dirname(__FILE__) . "/../../../../";

// Instantiate Kirby
$kirby = new Kirby([
'options' => [
'debug' => true,
],
'roots' => [
'kirby' => $kirbyPath
],
]);

// Work the queue
Autopublish::publish();
exit();

0 comments on commit c4a6672

Please sign in to comment.