Skip to content

Commit

Permalink
Reworked caching to be tied closely with page cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 8, 2017
1 parent 40cdec0 commit cf3ee58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v2.1.0
## xx/xx/2017

1. [](#improved)
* Reworked logic so form caching is based on `Pages::getPagesCacheId()`
1. [](#bugfix)
* Fixed issue with `honeypot` field not throwing exception properly

# v2.0.10
## 02/08/2017

Expand Down
46 changes: 18 additions & 28 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class FormPlugin extends Plugin
'blueprints' => 1000
];

/**
* @var bool
*/
protected $active = false;

/**
* @var Form
*/
Expand All @@ -38,8 +33,6 @@ class FormPlugin extends Plugin

protected $json_response = [];

protected $cache_id = 'plugin-form';

protected $recache_forms = false;


Expand All @@ -61,6 +54,8 @@ public function onPluginsInitialized()
{
require_once(__DIR__ . '/classes/form.php');



if ($this->isAdmin()) {
$this->enable([
'onPagesInitialized' => ['onPagesInitialized', 0]
Expand All @@ -74,19 +69,6 @@ public function onPluginsInitialized()
'onTwigInitialized' => ['onTwigInitialized', 0],
'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
]);

// Get and set the cache of forms if it exists
list($forms, $flat_forms) = $this->grav['cache']->fetch($this->cache_id);

// Only store the forms if they are an array
if (is_array($forms)) {
$this->forms = $forms;
}

// Only store the flat_forms if they are an array
if (is_array($flat_forms)) {
$this->flat_forms = $flat_forms;
}
}

/**
Expand Down Expand Up @@ -144,6 +126,21 @@ public function onPagesInitialized()
{
$submitted = false;

$cache_id = $this->grav['pages']->getPagesCacheId() . '-form-plugin';

// Get and set the cache of forms if it exists
list($forms, $flat_forms) = $this->grav['cache']->fetch($cache_id);

// Only store the forms if they are an array
if (is_array($forms)) {
$this->forms = $forms;
}

// Only store the flat_forms if they are an array
if (is_array($flat_forms)) {
$this->flat_forms = $flat_forms;
}

if ($this->isAdmin() && !empty($_POST)) {

$page = $this->grav['page'];
Expand All @@ -154,7 +151,6 @@ public function onPagesInitialized()
$header = $page->header();

if (isset($header->form) && is_array($header->form)) {
$this->active = true;
// Create form
$this->form = new Form($page);
$this->enable([
Expand All @@ -179,11 +175,9 @@ public function onPagesInitialized()

// Save the current state of the forms to cache
if ($this->recache_forms) {
$this->grav['cache']->save($this->cache_id, [$this->forms, $this->flat_forms]);
$this->grav['cache']->save($cache_id, [$this->forms, $this->flat_forms]);
}

$this->active = true;

// Handle posting if needed.
if (!empty($_POST)) {

Expand Down Expand Up @@ -253,10 +247,6 @@ public function onTwigTemplatePaths()
*/
public function onTwigVariables(Event $event = null)
{
if (!$this->active) {
return;
}

if ($event && isset($event['page'])) {
$page = $event['page'];
} else {
Expand Down

0 comments on commit cf3ee58

Please sign in to comment.