Skip to content

Commit

Permalink
Merge branch '4.x' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	composer.json
#	tests/TESTS.md
  • Loading branch information
bencroker committed Jul 4, 2024
2 parents 3e5a4fc + 3e3c9c2 commit a4daba0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 31 deletions.
4 changes: 3 additions & 1 deletion src/Blitz.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ function(DeleteElementEvent $event) {
function(ElementEvent|MultiElementActionEvent $event) {
/** @var Element $element */
$element = $event->element;
$element->attachBehavior(ElementChangedBehavior::BEHAVIOR_NAME, ElementChangedBehavior::class);
if ($this->refreshCache->isRefreshableElement($element)) {
$element->attachBehavior(ElementChangedBehavior::BEHAVIOR_NAME, ElementChangedBehavior::class);
}
}
);
}
Expand Down
34 changes: 32 additions & 2 deletions src/behaviors/ElementChangedBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Craft;
use craft\base\Element;
use craft\elements\Asset;
use craft\helpers\ElementHelper;
use putyourlightson\blitz\helpers\ElementTypeHelper;
use yii\base\Behavior;

Expand Down Expand Up @@ -35,6 +36,11 @@ class ElementChangedBehavior extends Behavior
*/
public ?Element $originalElement = null;

/**
* @var array<int,bool> The original element’s site statuses.
*/
public array $originalElementSiteStatuses = [];

/**
* @var string[] The attributes that changed.
*/
Expand Down Expand Up @@ -75,6 +81,10 @@ public function attach($owner): void
}

$this->originalElement = Craft::$app->getElements()->getElementById($element->id, $element::class, $element->siteId);

if ($this->originalElement !== null) {
$this->originalElementSiteStatuses = ElementHelper::siteStatusesForElement($this->originalElement);
}
}

/**
Expand Down Expand Up @@ -131,7 +141,7 @@ public function getHasBeenDeleted(): bool
}

/**
* Returns whether the element’s status has changed.
* Returns whether the element’s status or any site statuses have changed.
*/
public function getHasStatusChanged(): bool
{
Expand All @@ -141,7 +151,27 @@ public function getHasStatusChanged(): bool
return false;
}

return $element->getStatus() != $this->originalElement->getStatus();
if ($element->getStatus() != $this->originalElement->getStatus()) {
return true;
}

$supportedSites = ElementHelper::supportedSitesForElement($element);

foreach ($supportedSites as $supportedSite) {
$siteId = $supportedSite['siteId'];
$siteStatus = $element->getEnabledForSite($siteId);
$originalSiteStatus = $this->originalElementSiteStatuses[$siteId] ?? null;

if (
$siteStatus !== null
&& $originalSiteStatus !== null
&& $siteStatus !== $originalSiteStatus
) {
return true;
}
}

return false;
}

/**
Expand Down
75 changes: 47 additions & 28 deletions src/services/RefreshCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,34 +175,7 @@ public function addElementIds(string $elementType, array $elementIds): void
*/
public function addElement(ElementInterface $element): void
{
// Don’t proceed if not an actual element
if (!($element instanceof Element)) {
return;
}

// Don’t proceed if the element is an asset that is being indexed
if ($element instanceof Asset && $element->getScenario() === Asset::SCENARIO_INDEX) {
return;
}

// Refresh the entire cache if this is a global set since they are populated on every request
if ($element instanceof GlobalSet) {
if (Blitz::$plugin->settings->refreshCacheAutomaticallyForGlobals) {
$this->refreshAll();
}

return;
}

$elementType = $element::class;

// Don’t proceed if not a cacheable element type
if (!ElementTypeHelper::getIsCacheableElementType($elementType)) {
return;
}

// Don’t proceed if element is a draft or revision
if (ElementHelper::isDraftOrRevision($element)) {
if (!$this->isRefreshableElement($element)) {
return;
}

Expand Down Expand Up @@ -292,6 +265,52 @@ public function addElementExpiryDate(Element $element, DateTime $expiryDate): vo
->execute();
}

/**
* Returns whether an element is refreshable.
*
* @since 4.19.0
*/
public function isRefreshableElement(ElementInterface $element): bool
{
// Don’t proceed if not an actual element
if (!($element instanceof Element)) {
return false;
}

// Don’t proceed if element is a draft or revision
if (ElementHelper::isDraftOrRevision($element)) {
return false;
}

// Don’t proceed if propagating
if ($element->propagating) {
return false;
}

// Don’t proceed if the element is an asset that is being indexed
if ($element instanceof Asset && $element->getScenario() === Asset::SCENARIO_INDEX) {
return false;
}

// Refresh the entire cache if this is a global set since they are populated on every request
if ($element instanceof GlobalSet) {
if (Blitz::$plugin->settings->refreshCacheAutomaticallyForGlobals) {
$this->refreshAll();
}

return false;
}

$elementType = $element::class;

// Don’t proceed if not a cacheable element type
if (!ElementTypeHelper::getIsCacheableElementType($elementType)) {
return false;
}

return true;
}

/**
* Generates element expiry dates.
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/pest/Feature/RefreshCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
->toBeTracked();
});

test('Element is tracked when its status for another site is changed', function() {
$entry = createEntry();
$entry->setEnabledForSite([
2 => false,
]);
Blitz::$plugin->refreshCache->addElement($entry);

expect($entry)
->toBeTracked();
});

test('Element is tracked when it expires', function() {
$entry = createEntry();
$entry->expiryDate = new DateTime('20010101');
Expand Down
2 changes: 2 additions & 0 deletions tests/pest/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function createEntry(bool $enabled = true, bool $batchMode = false): Entry
->enabled($enabled)
->create();

$entry->setEnabledForSite($enabled);

$entry->attachBehavior(ElementChangedBehavior::BEHAVIOR_NAME, ElementChangedBehavior::class);

Blitz::$plugin->generateCache->reset();
Expand Down

0 comments on commit a4daba0

Please sign in to comment.