Move fix for missing confirmation email hash to migrations

[MAILPOET-6273]
This commit is contained in:
Rostislav Wolny
2024-10-15 13:09:59 +02:00
committed by Aschepikov
parent d7acf7b34d
commit 3e27192f83
2 changed files with 33 additions and 5 deletions

View File

@@ -28,7 +28,6 @@ use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Settings\SettingsController;
use MailPoet\UnexpectedValueException;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\Security;
use MailPoet\WP\Emoji;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
@@ -328,10 +327,6 @@ class Newsletters extends APIEndpoint {
$newsletter->setBody(
json_decode($this->emoji->encodeForUTF8Column($newslettersTableName, 'body', $data['body']), true)
);
// ensure newsletter has hash
if (!$newsletter->getHash()) {
$newsletter->setHash(Security::generateHash());
}
$this->newslettersRepository->flush();
$response = $this->newslettersResponseBuilder->build($newsletter);

View File

@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);
namespace MailPoet\Migrations\App;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Migrator\AppMigration;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\ConfirmationEmailCustomizer;
use MailPoet\Util\Security;
/**
* Fixes confirmation emails with missing hash.
* These emails were created by plugin before we fixed [MAILPOET-6273]
*/
class Migration_20241015_105511_App extends AppMigration {
public function run(): void {
$settings = $this->container->get(SettingsController::class);
$confirmationEmailTemplateId = (int)$settings->get(ConfirmationEmailCustomizer::SETTING_EMAIL_ID, null);
if (!$confirmationEmailTemplateId) {
return;
}
$repository = $this->container->get(NewslettersRepository::class);
$confirmationEmail = $repository->findOneById($confirmationEmailTemplateId);
if (!$confirmationEmail instanceof NewsletterEntity || $confirmationEmail->getHash()) {
return;
}
$confirmationEmail->setHash(Security::generateHash());
$repository->flush();
}
}