Skip to content

Instantly share code, notes, and snippets.

@Jsewill
Forked from jpdevries/cacheguard.php
Last active February 28, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jsewill/d66be704f0ab2478e2f0a04e33ad3d6c to your computer and use it in GitHub Desktop.
Save Jsewill/d66be704f0ab2478e2f0a04e33ad3d6c to your computer and use it in GitHub Desktop.
Untick Empty Cache Checkbox for Unpublished Resources in MODX Revolution Plugin
<?php
/**
* cacheguard plugin for cacheguard extra
*
* Copyright 2013 by JP DeVries jp@modx.com
* Created on 06-21-2013
*
* cacheguard is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* cacheguard is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* cacheguard; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package cacheguard
*/
/**
* Description
* -----------
* Sets Clear Cache tickbox to false for unpublished Resources
*
* Variables
* ---------
* @var $modx modX
* @var $scriptProperties array
*
* @package cacheguard
**/
$eventName = $modx->event->name;
switch($eventName) {
case 'OnDocFormRender':
if($resource->get('published') != '1') {
$selector = ($resource->get('class_key') == 'Article') ? 'modx-resource-clearcache' : 'modx-resource-syncsite';
$modx->regClientStartupHTMLBlock("<script>
var cacheguard_protected = false;
Ext.onReady(function(){
if(!cacheguard_protected)Ext.getCmp('$selector').setValue(0);
cacheguard_protected = true;
});</script>
");
}
break;
case 'OnDocFormSave':
if($resource->get('published') != '1') {
$resourceCache = $modx->getCacheManager()->getCacheProvider(
$modx->getOption('cache_resource_key', null, 'resource'),
array(
xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null, $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
xPDO::OPT_CACHE_EXPIRES => $modx->getOption('cache_resource_expires', null, $modx->getOption(xPDO::OPT_CACHE_EXPIRES)),
)
);
$resourceCache->delete("{$resource->context_key}/resources/{$resource->id}");
}
// Make sure auto_publish cache partition is updated if need be.
if ($resource->get('pub_date') || $resource->get('unpub_date')) {
$ctx = $resource->get('context_key');
$providers = array(
'auto_publish' => array('contexts' => $ctx),
);
$modx->cacheManager->refresh($providers);
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment