Skip to content

Commit

Permalink
Propagate multisite deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Feb 18, 2024
1 parent 770eb6d commit cdf435a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Database/Traits/Multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function initializeMultisite()

$this->bindEvent('model.saveComplete', [$this, 'multisiteSaveComplete']);

$this->bindEvent('model.afterDelete', [$this, 'multisiteAfterDelete']);

$this->defineMultisiteRelations();
}

Expand Down Expand Up @@ -123,6 +125,24 @@ public function multisiteAfterCreate()
;
}

/**
* multisiteAfterDelete
*/
public function multisiteAfterDelete()
{
if (!$this->isMultisiteSyncEnabled() || !$this->getMultisiteConfig('delete', true)) {
return;
}

Site::withGlobalContext(function() {
foreach ($this->getMultisiteSyncSites() as $siteId) {
if (!$this->isModelUsingSameSite($siteId)) {
$this->deleteForSite($siteId);
}
}
});
}

/**
* defineMultisiteRelations will spin over every relation and apply propagation config
*/
Expand Down Expand Up @@ -376,6 +396,30 @@ protected function findOtherSiteModel($siteId = null)
return $otherModel;
}

/**
* deleteForSite runs the delete command on a model for another site, useful for cleaning
* up records for other sites when the parent is deleted.
*/
public function deleteForSite($siteId = null)
{
$otherModel = $this->findForSite($siteId);
if (!$otherModel) {
return;
}

$useSoftDeletes = $this->isClassInstanceOf(\October\Contracts\Database\SoftDeleteInterface::class);
if ($useSoftDeletes && !$this->isSoftDelete()) {
static::withoutEvents(function() use ($otherModel) {
$otherModel->forceDelete();
});
return;
}

static::withoutEvents(function() use ($otherModel) {
$otherModel->delete();
});
}

/**
* isModelUsingSameSite
*/
Expand Down

0 comments on commit cdf435a

Please sign in to comment.