diff --git a/CHANGELOG.md b/CHANGELOG.md index b86a1f6a..bd70a3b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Added the ability to enforce spam prevention on front-end forms using Cloudflare Turnstile ([#447](https://github.com/putyourlightson/craft-campaign/issues/447)). +- Added the `resave/campaigns`, `resave/contacts` and `resave/mailing-lists` console commands ([#481](https://github.com/putyourlightson/craft-campaign/issues/481)). ## 2.15.3 - 2024-05-06 diff --git a/src/Campaign.php b/src/Campaign.php index b472e090..4fe349ab 100644 --- a/src/Campaign.php +++ b/src/Campaign.php @@ -7,8 +7,11 @@ use Craft; use craft\base\Plugin; +use craft\console\Controller as ConsoleController; +use craft\console\controllers\ResaveController; use craft\controllers\PreviewController; use craft\elements\User; +use craft\events\DefineConsoleActionsEvent; use craft\events\DefineFieldLayoutFieldsEvent; use craft\events\FieldEvent; use craft\events\PluginEvent; @@ -223,6 +226,10 @@ public function init(): void $this->controllerMap = ['t' => TrackerController::class]; } + if (Craft::$app->getRequest()->getIsConsoleRequest()) { + $this->registerResaveCommands(); + } + if (Craft::$app->getRequest()->getIsCpRequest()) { $this->registerNativeFields(); $this->registerAssetBundles(); @@ -879,4 +886,60 @@ function(RegisterUserPermissionsEvent $event) { } ); } + + /** + * Registers resave commands. + * + * @since 2.16.0 + */ + private function registerResaveCommands(): void + { + Event::on(ResaveController::class, ConsoleController::EVENT_DEFINE_ACTIONS, + function(DefineConsoleActionsEvent $event) { + $event->actions['campaigns'] = [ + 'action' => function(): int { + /** @var ResaveController $controller */ + $controller = Craft::$app->controller; + $criteria = []; + if ($controller->type !== null) { + $criteria['campaignType'] = explode(',', $controller->type); + } + return $controller->resaveElements(CampaignElement::class, $criteria); + }, + 'options' => ['type'], + 'helpSummary' => 'Re-saves Campaign campaigns.', + 'optionsHelp' => [ + 'type' => 'The campaign type handle(s) of the campaigns to resave.', + ], + ]; + + $event->actions['mailing-lists'] = [ + 'action' => function(): int { + /** @var ResaveController $controller */ + $controller = Craft::$app->controller; + $criteria = []; + if ($controller->type !== null) { + $criteria['mailingListType'] = explode(',', $controller->type); + } + return $controller->resaveElements(MailingListElement::class, $criteria); + }, + 'options' => ['type'], + 'helpSummary' => 'Re-saves Campaign mailing lists.', + 'optionsHelp' => [ + 'type' => 'The mailing lists type handle(s) of the mailing lists to resave.', + ], + ]; + + $event->actions['contacts'] = [ + 'action' => function(): int { + /** @var ResaveController $controller */ + $controller = Craft::$app->controller; + return $controller->resaveElements(ContactElement::class); + }, + 'options' => [], + 'helpSummary' => 'Re-saves Campaign contacts.', + ]; + } + ); + } }