Skip to content

Commit b35edd8

Browse files
committed
Added support for new awesome Grav 1.6 Scheduler
1 parent a024a76 commit b35edd8

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# v2.0.0
2-
## mm/dd/2018
1+
# v2.0.0-rc.4
2+
## 10/04/2018
33

4+
1. [](#new)
5+
* Added support for new awesome Grav 1.6 Scheduler
46
1. [](#improved)
57
* Fixed alignment of the git icon in the Wizard (#115)
68
* Prevent Wizard modal to get canceled when clicking on the overlay background (#115)

blueprints.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ form:
102102
validate:
103103
type: bool
104104

105+
CronNotice:
106+
type: spacer
107+
markdown: true
108+
text: |
109+
! You can also let Grav Scheduler automatically synchronize at a given time. You could for example disable all the automatic synchronizations above and just enable the Scheduler below. **Requires Grav 1.6**
110+
sync.cron_enable:
111+
type: toggle
112+
label: Add Job to Scheduler
113+
help: Add GitSync Job to the Scheduler so it can automatically perform synchronization at a given time
114+
default: 0
115+
highlight: 1
116+
options:
117+
1: PLUGIN_ADMIN.YES
118+
0: PLUGIN_ADMIN.NO
119+
validate:
120+
type: bool
121+
122+
sync.cron_at:
123+
type: cron
124+
label: Run Scheduled Job
125+
help: When should the scheduler run the automatic synchronization
126+
default: '0 12,23 * * *'
127+
105128
Repo:
106129
type: section
107130
title: Git Repository Settings

classes/Helper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ public static function decrypt($enc_password)
7878
}
7979
}
8080

81+
public static function synchronize()
82+
{
83+
if (!Helper::isGitInstalled() || !Helper::isGitInitialized()) {
84+
return true;
85+
}
86+
87+
$git = new GitSync();
88+
89+
if ($git->hasChangesToCommit()) {
90+
$git->commit();
91+
}
92+
93+
// synchronize with remote
94+
$git->sync();
95+
96+
return true;
97+
}
98+
8199
public static function preventReadablePassword($str, $password) {
82100
return str_replace(urlencode(self::decrypt($password)), '{password}', $str);
83101
}

git-sync.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Grav\Plugin;
44

55
use Grav\Common\Data\Data;
6+
use Grav\Common\Grav;
67
use Grav\Common\Plugin;
78
use Grav\Plugin\GitSync\AdminController;
89
use Grav\Plugin\GitSync\GitSync;
@@ -25,9 +26,10 @@ class GitSyncPlugin extends Plugin
2526
public static function getSubscribedEvents()
2627
{
2728
return [
28-
'onPluginsInitialized' => ['onPluginsInitialized', 1000],
29-
'onPageInitialized' => ['onPageInitialized', 0],
30-
'onFormProcessed' => ['onFormProcessed', 0]
29+
'onPluginsInitialized' => ['onPluginsInitialized', 1000],
30+
'onPageInitialized' => ['onPageInitialized', 0],
31+
'onFormProcessed' => ['onFormProcessed', 0],
32+
'onSchedulerInitialized' => ['onSchedulerInitialized', 0]
3133
];
3234
}
3335

@@ -212,6 +214,22 @@ public function synchronize()
212214
return true;
213215
}
214216

217+
public function onSchedulerInitialized(Event $event)
218+
{
219+
220+
/** @var Config $config */
221+
$config = Grav::instance()['config'];
222+
$run_at = $config->get('plugins.git-sync.sync.cron_at', '0 12,23 * * *');
223+
224+
225+
if ($config->get('plugins.git-sync.sync.cron_enable', false)) {
226+
/** @var Scheduler $scheduler */
227+
$scheduler = $event['scheduler'];
228+
$job = $scheduler->addFunction('Grav\Plugin\GitSync\Helper::synchronize', [], 'GitSync');
229+
$job->at($run_at);
230+
}
231+
}
232+
215233
public function reset()
216234
{
217235
if (!Helper::isGitInstalled() || !Helper::isGitInitialized()) {

0 commit comments

Comments
 (0)