Skip to content

Commit

Permalink
Added field layout designers, removed reCAPTCHA v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 11, 2022
1 parent ad15d5a commit 472f61b
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 344 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## 2.0.0-beta.1 - Unreleased
### Added
- Added compatibility with Craft 4.
- Added field layout designers to campaign types, mailing list types and contacts ([#163](https://github.com/putyourlightson/craft-campaign/issues/163), [#198](https://github.com/putyourlightson/craft-campaign/issues/198)).

### Removed
- Removed support for reCAPTCHA version 2, leaving support for version 3 only.

## 1.23.0 - 2022-02-28
### Added
Expand Down
219 changes: 144 additions & 75 deletions src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use craft\base\Plugin;
use craft\controllers\LivePreviewController;
use craft\elements\User;
use craft\errors\MissingComponentException;
use craft\events\DefineFieldLayoutFieldsEvent;
use craft\events\FieldEvent;
use craft\events\PluginEvent;
use craft\events\RebuildConfigEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\fieldlayoutelements\TitleField;
use craft\helpers\App;
use craft\helpers\ArrayHelper;
use craft\helpers\MailerHelper;
Expand All @@ -24,6 +25,7 @@
use craft\mail\Mailer;
use craft\mail\Message;
use craft\mail\transportadapters\Sendmail;
use craft\models\FieldLayout;
use craft\services\Elements;
use craft\services\Fields;
use craft\services\Plugins;
Expand All @@ -41,6 +43,7 @@
use putyourlightson\campaign\elements\MailingListElement;
use putyourlightson\campaign\elements\SegmentElement;
use putyourlightson\campaign\elements\SendoutElement;
use putyourlightson\campaign\fieldlayoutelements\contacts\EmailField;
use putyourlightson\campaign\fields\CampaignsField;
use putyourlightson\campaign\fields\ContactsField;
use putyourlightson\campaign\fields\MailingListsField;
Expand Down Expand Up @@ -162,54 +165,24 @@ public function init()
$this->_registerProjectConfigListeners();
$this->_registerTemplateHooks();
$this->_registerAllowedOrigins();
$this->_registerTwigExtensions();
$this->_registerVariables();

// Register tracker controller shorthand for site requests
if (Craft::$app->getRequest()->getIsSiteRequest()) {
$this->controllerMap = ['t' => TrackerController::class];
}

// Register Twig extension
Craft::$app->view->registerTwigExtension(new CampaignTwigExtension());

// Register variable
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT,
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('campaign', CampaignVariable::class);
}
);

if (Craft::$app->getRequest()->getIsCpRequest()) {
// Register universal CSS
Craft::$app->view->registerAssetBundle(UniversalAsset::class);

// Register CP URL rules event
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
$event->rules = array_merge($event->rules, $this->getCpRoutes());
}
);

// Register utility
Event::on(Utilities::class, Utilities::EVENT_REGISTER_UTILITY_TYPES,
function(RegisterComponentTypesEvent $event) {
if (Craft::$app->getUser()->checkPermission('campaign:utility')) {
$event->types[] = CampaignUtility::class;
}
}
);
$this->_registerNativeFields();
$this->_registerAssetBundles();
$this->_registerCpUrlRules();
$this->_registerUtilities();
}

// If Craft edition is pro
if (Craft::$app->getEdition() === Craft::Pro) {
// Register user permissions
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
$event->permissions['Campaign'] = $this->getCpPermissions();
}
);

$this->_registerUserPermissions();
$this->sync->registerUserEvents();
}
}
Expand Down Expand Up @@ -420,43 +393,6 @@ protected function getCpRoutes(): array
];
}

/**
* Returns the CP permissions.
*/
protected function getCpPermissions(): array
{
$permissions = [
'campaign:reports' => ['label' => Craft::t('campaign', 'Manage reports')],
'campaign:campaigns' => ['label' => Craft::t('campaign', 'Manage campaigns')],
'campaign:contacts' => [
'label' => Craft::t('campaign', 'Manage contacts'),
'nested' => [
'campaign:importContacts' => ['label' => Craft::t('campaign', 'Import contacts')],
'campaign:exportContacts' => ['label' => Craft::t('campaign', 'Export contacts')],
],
],
'campaign:mailingLists' => ['label' => Craft::t('campaign', 'Manage mailing lists')],
];

if ($this->getIsPro()) {
$permissions['campaign:contacts']['nested']['campaign:syncContacts'] = ['label' => Craft::t('campaign', 'Sync contacts')];
$permissions['campaign:segments'] = ['label' => Craft::t('campaign', 'Manage segments')];
}

$permissions['campaign:sendouts'] = [
'label' => Craft::t('campaign', 'Manage sendouts'),
'nested' => [
'campaign:sendSendouts' => ['label' => Craft::t('campaign', 'Send sendouts')],
],
];

$permissions['campaign:settings'] = ['label' => Craft::t('campaign', 'Manage plugin settings')];

$permissions['campaign:utility'] = ['label' => Craft::t('campaign', 'Access utility')];

return $permissions;
}

/**
* Registers components.
*/
Expand Down Expand Up @@ -666,4 +602,137 @@ function(ActionEvent $event) {
}
);
}

/**
* Registers Twig extensions.
*
* @since 2.0.0
*/
private function _registerTwigExtensions()
{
Craft::$app->view->registerTwigExtension(new CampaignTwigExtension());
}

/**
* Registers variables.
*
* @since 2.0.0
*/
private function _registerVariables()
{
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT,
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('campaign', CampaignVariable::class);
}
);
}

/**
* Registers native fields.
*
* @since 2.0.0
*/
private function _registerNativeFields()
{
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_NATIVE_FIELDS,
function(DefineFieldLayoutFieldsEvent $event) {
/** @var FieldLayout $layout */
$layout = $event->sender;

if ($layout->type === CampaignElement::class || $layout->type === MailingListElement::class) {
$event->fields[] = TitleField::class;
}

if ($layout->type === ContactElement::class) {
$event->fields[] = EmailField::class;
}
}
);
}

/**
* Registers asset bundles.
*
* @since 2.0.0
*/
private function _registerAssetBundles()
{
Craft::$app->view->registerAssetBundle(UniversalAsset::class);
}

/**
* Registers CP URL rules.
*
* @since 2.0.0
*/
private function _registerCpUrlRules()
{
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
$event->rules = array_merge($event->rules, $this->getCpRoutes());
}
);
}

/**
* Registers utilities.
*
* @since 2.0.0
*/
private function _registerUtilities()
{
Event::on(Utilities::class, Utilities::EVENT_REGISTER_UTILITY_TYPES,
function(RegisterComponentTypesEvent $event) {
if (Craft::$app->getUser()->checkPermission('campaign:utility')) {
$event->types[] = CampaignUtility::class;
}
}
);
}

/**
* Registers user permissions.
*
* @since 2.0.0
*/
private function _registerUserPermissions()
{
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
$permissions = [
'campaign:reports' => ['label' => Craft::t('campaign', 'Manage reports')],
'campaign:campaigns' => ['label' => Craft::t('campaign', 'Manage campaigns')],
'campaign:contacts' => [
'label' => Craft::t('campaign', 'Manage contacts'),
'nested' => [
'campaign:importContacts' => ['label' => Craft::t('campaign', 'Import contacts')],
'campaign:exportContacts' => ['label' => Craft::t('campaign', 'Export contacts')],
],
],
'campaign:mailingLists' => ['label' => Craft::t('campaign', 'Manage mailing lists')],
];

if ($this->getIsPro()) {
$permissions['campaign:contacts']['nested']['campaign:syncContacts'] = ['label' => Craft::t('campaign', 'Sync contacts')];
$permissions['campaign:segments'] = ['label' => Craft::t('campaign', 'Manage segments')];
}

$permissions['campaign:sendouts'] = [
'label' => Craft::t('campaign', 'Manage sendouts'),
'nested' => [
'campaign:sendSendouts' => ['label' => Craft::t('campaign', 'Send sendouts')],
],
];
$permissions['campaign:settings'] = ['label' => Craft::t('campaign', 'Manage plugin settings')];
$permissions['campaign:utility'] = ['label' => Craft::t('campaign', 'Access utility')];

$event->permissions[] = [
'heading' => 'Campaign',
'permissions' => $permissions,
];
}
);
}
}
12 changes: 0 additions & 12 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@
// Enable reCAPTCHA to protect mailing list subscription forms from bots
//'reCaptcha' => false,

// The reCAPTCHA version
//'reCaptchaVersion' => 2,

// The reCAPTCHA site key
//'reCaptchaSiteKey' => 'aBcDeFgHiJkLmNoP',

Expand All @@ -110,15 +107,6 @@
// The reCAPTCHA error message
//'reCaptchaErrorMessage' => 'Your form submission was blocked. Please go back and verify that you are human.',

// The size of the reCAPTCHA widget
// 'reCaptchaSize' => 'normal',

// The color theme of the reCAPTCHA widget
// 'reCaptchaTheme' => 'light',

// The position of the reCAPTCHA badge (when invisible)
// 'reCaptchaBadge' => 'bottomright',

// The maximum number of pending contacts to store per email address and mailing list
//'maxPendingContacts' => 5,

Expand Down
4 changes: 0 additions & 4 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,9 @@ public function actionSaveRecaptcha(): ?Response

// Set the simple stuff
$settings->reCaptcha = Craft::$app->getRequest()->getBodyParam('reCaptcha', $settings->reCaptcha);
$settings->reCaptchaVersion = Craft::$app->getRequest()->getBodyParam('reCaptchaVersion', $settings->reCaptchaVersion);
$settings->reCaptchaSiteKey = Craft::$app->getRequest()->getBodyParam('reCaptchaSiteKey', $settings->reCaptchaSiteKey);
$settings->reCaptchaSecretKey = Craft::$app->getRequest()->getBodyParam('reCaptchaSecretKey', $settings->reCaptchaSecretKey);
$settings->reCaptchaErrorMessage = Craft::$app->getRequest()->getBodyParam('reCaptchaErrorMessage', $settings->reCaptchaErrorMessage);
$settings->reCaptchaSize = Craft::$app->getRequest()->getBodyParam('reCaptchaSize', $settings->reCaptchaSize);
$settings->reCaptchaTheme = Craft::$app->getRequest()->getBodyParam('reCaptchaTheme', $settings->reCaptchaTheme);
$settings->reCaptchaBadge = Craft::$app->getRequest()->getBodyParam('reCaptchaBadge', $settings->reCaptchaBadge);

// Save it
if (!Campaign::$plugin->settings->saveSettings($settings)) {
Expand Down
32 changes: 20 additions & 12 deletions src/elements/CampaignElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use craft\elements\actions\Edit;
use craft\elements\actions\Restore;
use craft\helpers\UrlHelper;
use craft\models\FieldLayout;
use craft\validators\DateTimeValidator;
use craft\web\View;
use DateTime;
Expand Down Expand Up @@ -183,6 +184,23 @@ protected static function defineSources(string $context = null): array
return $sources;
}

/**
* @inheritdoc
* @since 2.0.0
*/
protected static function defineFieldLayouts(string $source): array
{
return [];
$fieldLayouts = [];
if (
preg_match('/^group:(.+)$/', $source, $matches) &&
($group = Craft::$app->getCategories()->getGroupByUid($matches[1]))
) {
$fieldLayouts[] = $group->getFieldLayout();
}
return $fieldLayouts;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -493,19 +511,9 @@ public function getReportUrl(): string
/**
* @inheritdoc
*/
public function getEditorHtml(): string
public function getFieldLayout(): ?FieldLayout
{
// Get the title field
$html = Craft::$app->getView()->renderTemplate('campaign/campaigns/_includes/titlefield', [
'campaign' => $this,
]);

// Set the field layout ID
$this->fieldLayoutId = $this->getCampaignType()->fieldLayoutId;

$html .= parent::getEditorHtml();

return $html;
return parent::getFieldLayout() ?? $this->getCampaignType()->getFieldLayout();
}

/**
Expand Down
Loading

0 comments on commit 472f61b

Please sign in to comment.