Skip to content

Commit

Permalink
Add resave commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Jun 18, 2024
1 parent 11d7bd5 commit 6350201
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 63 additions & 0 deletions src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.',
];
}
);
}
}

0 comments on commit 6350201

Please sign in to comment.