Clean up uncustomised partials if they are no longer provided.

An exception was being thrown in some instances if a partial had been added to the DB that was provided by a plugin that no longer exists, or had been disabled. This will remove any partials provided by non-existent plugins, only if they haven't been subsequently customised by the developer.

Fixes https://github.com/octobercms/october/issues/5065
This commit is contained in:
Ben Thomson 2020-08-31 14:18:53 +08:00
parent ce8c96b66f
commit dce3931f20
No known key found for this signature in database
GPG Key ID: 8BDB18DD0909BE22
1 changed files with 14 additions and 4 deletions

View File

@ -72,14 +72,24 @@ class MailPartial extends Model
*/
public static function createPartials()
{
$dbPartials = self::lists('code', 'code');
$partials = MailManager::instance()->listRegisteredPartials();
$dbPartials = self::lists('is_custom', 'code');
$newPartials = array_diff_key($partials, $dbPartials);
$definitions = MailManager::instance()->listRegisteredPartials();
foreach ($definitions as $code => $path) {
if (array_key_exists($code, $dbPartials)) {
/*
* Clean up non-customized partials
*/
foreach ($dbPartials as $code => $isCustom) {
if ($isCustom) {
continue;
}
if (!array_key_exists($code, $partials)) {
self::whereCode($code)->delete();
}
}
foreach ($newPartials as $code => $path) {
$partial = new static;
$partial->code = $code;
$partial->is_custom = 0;