Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 21, 2017 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sepiariver/5713bae4f7e98e69076a to your computer and use it in GitHub Desktop.
Save sepiariver/5713bae4f7e98e69076a to your computer and use it in GitHub Desktop.
<?php
/*
* Copyright (c) YJ Tso <info@sepiariver.com>
*
* GPL2, do what you want at your own risk. No warranties whatsoever.
*
*/
// Get &resources property from snippet call
$ids = array_map('trim', explode(',', $modx->getOption('resources', $scriptProperties, '')));
// Debugging
$debug = false;
// Loop through resources
foreach ($ids as $id) {
// check and get
if (empty($id)) continue;
$res = $modx->getObject('modResource', (int) $id);
if ($debug) echo var_dump($res->pagetitle) . ' Line: ' . __LINE__ . PHP_EOL;
// clear the standard MODX cache
// * this is kinda heavy-handed, would not suggest doing this on page request, but rather Manager action only
$modx->cacheManager->refresh();
// assemble curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, $modx->getOption('regenerate_useragent', $scriptProperties, 'MODX RegenCache'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-StatCache-Regen: 1'));
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($debug) {
curl_setopt($curl, CURLOPT_NOBODY, false);
} else {
curl_setopt($curl, CURLOPT_NOBODY, true);
}
// request the resource to trigger cache regen
$url = $modx->makeUrl($res->get('id'), '', '', 'full');
if (!empty($url)) {
$modx->log(modX::LOG_LEVEL_INFO, "Requesting resource at {$url}");
if ($debug) echo var_dump($url) . ' Line: ' . __LINE__ . PHP_EOL;
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
if ($debug) echo var_dump($response) . ' Line: ' . __LINE__ . PHP_EOL;
$modx->log(modX::LOG_LEVEL_INFO, "cURL request complete for resource at {$url}");
}
curl_close($curl);
}
// Returns nothing
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment