Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

purge_cloudflare #723

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function init()
// Action `litespeed_purge_finalize` // @previous API::hook_purge($tags)
add_action('litespeed_purge', __NAMESPACE__ . '\Purge::add'); // @previous API::purge($tags)
add_action('litespeed_purge_all', __NAMESPACE__ . '\Purge::purge_all');
add_action('litespeed_purge_cloudflare', __NAMESPACE__ . '\Purge::purge_cloudflare');
add_action('litespeed_purge_post', array($this, 'purge_post')); // @previous API::purge_post( $pid )
add_action('litespeed_purge_posttype', __NAMESPACE__ . '\Purge::purge_posttype');
add_action('litespeed_purge_url', array($this, 'purge_url'));
Expand Down
31 changes: 31 additions & 0 deletions src/cdn/cloudflare.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ private function _purge_all()
Admin_Display::succeed($msg);
}
}

/**
* Purge Cloudflare cache
*
* @access public
*/
private function purge_all()
{
Debug2::debug('[Cloudflare] purge_all');

$cf_on = $this->conf(self::O_CDN_CLOUDFLARE);
if (!$cf_on) {
Debug2::debug('[Cloudflare] purge_all : Cloudflare API is set to off');
return;
}

$this->try_refresh_zone();
$zone = $this->_zone();
if (!$zone) {
return;
}

$url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/purge_cache';
$data = array('purge_everything' => true);

$res = $this->_cloudflare_call($url, 'DELETE', $data);

if ($res) {
Debug2::debug('[Cloudflare] purge_all : Notified Cloudflare to purge all successfully');
}
}

/**
* Get current Cloudflare zone from cfg
Expand Down
36 changes: 36 additions & 0 deletions src/purge.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ private function _purge_all($reason = false)
do_action('litespeed_purged_all');
}

/**
* Shortcut to purge cloudflare cache
*
* @access public
*/
public static function purge_cloudflare($reason = false)
{
self::cls()->_purge_cloudflare($reason);
}

/**
* Purge cloudflare caches
*
* @access private
*/
private function _purge_cloudflare($reason = false)
{

$this->cls('CDN\\Cloudflare')->purge_all();

if (!is_string($reason)) {
$reason = false;
}

if ($reason) {
$reason = "( $reason )";
}

self::debug('Purge cloudflare ' . $reason, 3);

$msg = __('Purged cloudflare caches successfully.', 'litespeed-cache');
!defined('LITESPEED_PURGE_SILENT') && Admin_Display::succeed($msg);

do_action('litespeed_purged_cloudflare');
}

/**
* Alerts LiteSpeed Web Server to purge all pages.
*
Expand Down