Skip to content
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
8 changes: 5 additions & 3 deletions src/Cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,22 @@ private function _handleElementChange(bool $isNew, ?ElementInterface $element):
}

$className = get_class($element);
$siteHandle = $element->getSite()->handle;

if (!$isNew && $this->_shouldPurgeElementType($className)) {
$url = $this->getAbsoluteUrl($element);
if ($url !== null) {
Queue::push(
new PurgeCloudflareCache(['urls' => [$url]]),
new PurgeCloudflareCache(['urls' => [$url], 'siteHandle' => $siteHandle]),
Cloudflare::$plugin->settings->queueJobPriority,
);
}
}

// Honour any explicit rules that match this URL, regardless of whatever Element it is.
$this->rules->purgeCachesForUrl(
$element->getUrl()
$element->getUrl(),
$siteHandle,
);
}

Expand Down Expand Up @@ -363,7 +365,7 @@ private function _handleInvalidateAssetTransform(Asset $asset): void
}

Queue::push(
new PurgeCloudflareCache(['urls' => $urls]),
new PurgeCloudflareCache(['urls' => $urls, 'siteHandle' => $asset->getSite()->handle]),
Cloudflare::$plugin->settings->queueJobPriority,
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/console/controllers/PurgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class PurgeController extends Controller
* https://www.yiiframework.com/doc/guide/2.0/en/tutorial-console#arguments
*
* @param string[] $urls
* @param string|null $siteHandle
*/
public function actionPurgeUrls(array $urls): int
public function actionPurgeUrls(array $urls, ?string $siteHandle = null): int
{
$urlCount = count($urls);
$urlWord = $urlCount === 1 ? 'URL' : 'URLs';
Expand All @@ -27,7 +28,7 @@ public function actionPurgeUrls(array $urls): int
sprintf('Purging %d %s...', $urlCount, $urlWord) . PHP_EOL
);

$response = Cloudflare::$plugin->api->purgeUrls($urls);
$response = Cloudflare::$plugin->api->purgeUrls($urls, $siteHandle);

return $this->_handleResult($response);
}
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Craft;
use craft\console\Application as ConsoleApplication;
use craft\helpers\App;
use craft\helpers\Cp;
use putyourlightson\cloudflare\Cloudflare;
use putyourlightson\cloudflare\models\Settings;

Expand All @@ -35,7 +36,7 @@ public static function isConfigured(): bool
* parameters if we’re in the control panel checking unsaved settings.
* Also parses environment variables.
*/
public static function getParsedSetting(string $key): ?string
public static function getParsedSetting(string $key, ?string $siteHandle = null): ?string
{
$request = Craft::$app->getRequest();
$isConsole = Craft::$app instanceof ConsoleApplication;
Expand All @@ -49,8 +50,14 @@ public static function getParsedSetting(string $key): ?string
!empty($request->getParam($key)) &&
is_string($request->getParam($key));

$settingValue = $usePost ? $request->getParam($key) :
Cloudflare::$plugin->getSettings()->{$key} ?? null;
if ($usePost) {
$settingValue = $request->getParam($key);
} elseif ($key == 'zone') {
$siteHandle = $siteHandle ?? Cp::requestedSite()->handle;
$settingValue = Cloudflare::$plugin->getSettings()->getZone($siteHandle);
} else {
$settingValue = Cloudflare::$plugin->getSettings()->{$key} ?? null;
}

if ($settingValue) {
/** @scrutinizer ignore-call */
Expand Down
32 changes: 27 additions & 5 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\base\Model;
use craft\helpers\ConfigHelper;
use putyourlightson\cloudflare\Cloudflare;

class Settings extends Model
Expand Down Expand Up @@ -40,9 +41,9 @@ class Settings extends Model
public ?string $apiToken = null;

/**
* @var ?string This site’s related Cloudflare Zone ID.
* @var mixed This site’s related Cloudflare Zone ID.
*/
public ?string $zone = null;
public mixed $zone = null;

/**
* @var string[] List of element type classes that should be purged automatically.
Expand All @@ -67,6 +68,14 @@ class Settings extends Model
*/
public ?int $queueJobPriority = null;

/**
* Returns the localized Zone value.
*/
public function getZone(?string $siteHandle = null): ?string
{
return ConfigHelper::localizedValue($this->zone, $siteHandle);
}

/**
* Returns `true` if the Cloudflare zone ID is set in a static config file.\
*/
Expand All @@ -91,7 +100,17 @@ public function rules(): array
return [
[['authType'], 'in', 'range' => [self::AUTH_TYPE_KEY, self::AUTH_TYPE_TOKEN]],
[['purgeElements'], 'each', 'rule' => ['in', 'range' => Cloudflare::$supportedElementTypes]],
[['apiKey', 'email', 'apiToken', 'zone', 'zoneName', 'userServiceKey'], 'string'],
[['apiKey', 'email', 'apiToken', 'zoneName', 'userServiceKey'], 'string'],
[
['zone'], 'string', 'when' => static function ($model) {
return is_string($model->zone);
},
],
[
['zone'], 'each', 'rule' => ['string'], 'when' => static function ($model) {
return is_array($model->zone);
},
],
['zone', 'required'],
[
['apiKey', 'email'], 'required', 'when' => static function($model) {
Expand All @@ -106,8 +125,11 @@ public function rules(): array
];
}

private function _getStaticConfig(): array
private function _getStaticConfig(?string $siteHandle = null): array
{
return Craft::$app->getConfig()->getConfigFromFile('cloudflare');
$config = Craft::$app->getConfig()->getConfigFromFile('cloudflare');
$config['zone'] = ConfigHelper::localizedValue($config['zone'] ?? null, $siteHandle);

return $config;
}
}
7 changes: 6 additions & 1 deletion src/queue/jobs/PurgeCloudflareCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class PurgeCloudflareCache extends BaseJob
*/
public array $urls;

/**
* @var string|null Site handle to be used for purging
*/
public ?string $siteHandle = null;

/**
* @inheritdoc
*/
public function execute($queue): void
{
Cloudflare::$plugin->api->purgeUrls($this->urls);
Cloudflare::$plugin->api->purgeUrls($this->urls, $this->siteHandle);
$this->setProgress($queue, 100);
}

Expand Down
5 changes: 3 additions & 2 deletions src/services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ public function purgeZoneCache(): ?object
* https://developers.cloudflare.com/api/resources/cache/methods/purge/
*
* @param string[] $urls array of absolute URLs
* @param string|null $siteHandle optional site handle to override the default one
*/
public function purgeUrls(array $urls = []): mixed
public function purgeUrls(array $urls = [], ?string $siteHandle = null): mixed
{
if (!$this->getClient()) {
return null;
Expand All @@ -284,7 +285,7 @@ public function purgeUrls(array $urls = []): mixed
try {
$response = $this->getClient()->delete(sprintf(
'zones/%s/purge_cache',
ConfigHelper::getParsedSetting('zone')
ConfigHelper::getParsedSetting('zone', $siteHandle),
),
['body' => Json::encode(['files' => $urls])]
);
Expand Down
6 changes: 3 additions & 3 deletions src/services/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function saveRules(): void
/**
* Purge any related URLs we’ve established with custom rules.
*/
public function purgeCachesForUrl(string $url, bool $immediately = false): void
public function purgeCachesForUrl(string $url, ?string $siteHandle = null, bool $immediately = false): void
{
// max limit for Cloudflare API
$cloudflareRuleCountLimit = 30;
Expand Down Expand Up @@ -110,10 +110,10 @@ public function purgeCachesForUrl(string $url, bool $immediately = false): void
}

if ($immediately) {
Cloudflare::$plugin->api->purgeUrls($urlsToPurge);
Cloudflare::$plugin->api->purgeUrls($urlsToPurge, $siteHandle);
} else {
Queue::push(
new PurgeCloudflareCache(['urls' => $urlsToPurge]),
new PurgeCloudflareCache(['urls' => $urlsToPurge, 'siteHandle' => $siteHandle]),
Cloudflare::$plugin->settings->queueJobPriority,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
required: true,
name: 'zone',
options: zoneOptions,
value: settings.zone,
value: settings.getZone(requestedSite.handle),
autofocus: true,
errors: settings.getErrors('zone'),
instructions: 'Specify which Cloudflare Zone is utilized by this site.'|t('cloudflare')
Expand All @@ -134,7 +134,7 @@
required: true,
name: 'zone',
class: 'code',
value: settings.zone,
value: settings.getZone(requestedSite.handle),
autofocus: true,
errors: settings.getErrors('zone'),
disabled: settings.zoneIsStatic(),
Expand Down