Refactor creating of default form to use templates
[MAILPOET-2985]
This commit is contained in:
committed by
Veljko V
parent
4fc3d7b686
commit
14aa0b1393
@ -2,7 +2,6 @@
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Config\PopulatorData\DefaultForm;
|
||||
use MailPoet\Cron\CronTrigger;
|
||||
use MailPoet\Cron\Workers\AuthorizedSendingEmailsCheck;
|
||||
use MailPoet\Cron\Workers\Beamer;
|
||||
@ -12,7 +11,7 @@ use MailPoet\Cron\Workers\SubscriberLinkTokens;
|
||||
use MailPoet\Cron\Workers\UnsubscribeTokens;
|
||||
use MailPoet\Entities\UserFlagEntity;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\Util\Styles;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\Newsletter;
|
||||
@ -50,18 +49,22 @@ class Populator {
|
||||
const TEMPLATES_NAMESPACE = '\MailPoet\Config\PopulatorData\Templates\\';
|
||||
/** @var FeaturesController */
|
||||
private $flagsController;
|
||||
/** @var FormFactory */
|
||||
private $formFactory;
|
||||
|
||||
public function __construct(
|
||||
SettingsController $settings,
|
||||
WPFunctions $wp,
|
||||
Captcha $captcha,
|
||||
ReferralDetector $referralDetector,
|
||||
FeaturesController $flagsController
|
||||
FeaturesController $flagsController,
|
||||
FormFactory $formFactory
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->wp = $wp;
|
||||
$this->captcha = $captcha;
|
||||
$this->referralDetector = $referralDetector;
|
||||
$this->formFactory = $formFactory;
|
||||
$this->prefix = Env::$dbPrefix;
|
||||
$this->models = [
|
||||
'newsletter_option_fields',
|
||||
@ -334,7 +337,8 @@ class Populator {
|
||||
WP::synchronizeUsers();
|
||||
|
||||
// Default segment
|
||||
if (Segment::where('type', 'default')->count() === 0) {
|
||||
$defaultSegment = Segment::where('type', 'default')->orderByAsc('id')->limit(1)->findOne();
|
||||
if (!$defaultSegment instanceof Segment) {
|
||||
$defaultSegment = Segment::create();
|
||||
$newList = [
|
||||
'name' => $this->wp->__('My First List', 'mailpoet'),
|
||||
@ -347,21 +351,11 @@ class Populator {
|
||||
$defaultSegment->hydrate($newList);
|
||||
$defaultSegment->save();
|
||||
}
|
||||
return $defaultSegment;
|
||||
}
|
||||
|
||||
private function createDefaultForm($defaultSegment) {
|
||||
if (Form::count() === 0) {
|
||||
$factory = new DefaultForm(new Styles());
|
||||
if (!$defaultSegment) {
|
||||
$defaultSegment = Segment::where('type', 'default')->orderByAsc('id')->limit(1)->findOne();
|
||||
}
|
||||
Form::createOrUpdate([
|
||||
'name' => $factory->getName(),
|
||||
'body' => serialize($factory->getBody()),
|
||||
'settings' => serialize($factory->getSettings($defaultSegment)),
|
||||
'styles' => $factory->getStyles(),
|
||||
]);
|
||||
}
|
||||
private function createDefaultForm(Segment $defaultSegment) {
|
||||
$this->formFactory->ensureDefaultFormExists((int)$defaultSegment->id());
|
||||
}
|
||||
|
||||
protected function newsletterOptionFields() {
|
||||
|
@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Config\PopulatorData;
|
||||
|
||||
use MailPoet\Form\Util\Styles;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DefaultForm {
|
||||
/** @var Styles */
|
||||
private $formStyles;
|
||||
|
||||
public function __construct(Styles $formStyles) {
|
||||
$this->formStyles = $formStyles;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return WPFunctions::get()->_x('My First Form', 'default name of form (GDPR friendly) to capture emails', 'mailpoet');
|
||||
}
|
||||
|
||||
public function getBody() {
|
||||
return [
|
||||
[
|
||||
'type' => 'text',
|
||||
'name' => WPFunctions::get()->_x('First name', 'Form label', 'mailpoet'),
|
||||
'id' => 'first_name',
|
||||
'unique' => '1',
|
||||
'static' => '0',
|
||||
'params' => ['label' => WPFunctions::get()->_x('First name', 'Form label', 'mailpoet')],
|
||||
'position' => '1',
|
||||
],
|
||||
[
|
||||
'type' => 'text',
|
||||
'name' => WPFunctions::get()->_x('Email', 'Form label', 'mailpoet'),
|
||||
'id' => 'email',
|
||||
'unique' => '0',
|
||||
'static' => '1',
|
||||
'params' => ['label' => WPFunctions::get()->_x('Email', 'Form label', 'mailpoet'), 'required' => 'true'],
|
||||
'position' => '2',
|
||||
],
|
||||
[
|
||||
'type' => 'submit',
|
||||
'name' => WPFunctions::get()->_x('Submit', 'Form label', 'mailpoet'),
|
||||
'id' => 'submit',
|
||||
'unique' => '0',
|
||||
'static' => '1',
|
||||
'params' => ['label' => WPFunctions::get()->_x('Subscribe!', 'Form label', 'mailpoet')],
|
||||
'position' => '3',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettings(Segment $defaultSegment) {
|
||||
return [
|
||||
'segments' => [$defaultSegment->id()],
|
||||
'on_success' => 'message',
|
||||
'success_message' => Form::getDefaultSuccessMessage(),
|
||||
'success_page' => '5',
|
||||
'segments_selected_by' => 'admin',
|
||||
];
|
||||
}
|
||||
|
||||
public function getStyles() {
|
||||
return $this->formStyles->getDefaultCustomStyles();
|
||||
}
|
||||
}
|
@ -21,8 +21,10 @@ class FormFactory {
|
||||
$this->formTemplateRepository = $formTemplateRepository;
|
||||
}
|
||||
|
||||
public function createFormFromTemplate(string $templateId): FormEntity {
|
||||
public function createFormFromTemplate(string $templateId, array $settings = []): FormEntity {
|
||||
$formEntity = $this->formTemplateRepository->getFormEntityForTemplate($templateId);
|
||||
$formSettings = $formEntity->getSettings() ?? [];
|
||||
$formEntity->setSettings(array_merge($formSettings, $settings));
|
||||
$this->formRepository->persist($formEntity);
|
||||
$this->formRepository->flush();
|
||||
return $formEntity;
|
||||
@ -31,4 +33,18 @@ class FormFactory {
|
||||
public function createEmptyForm(): FormEntity {
|
||||
return $this->createFormFromTemplate(TemplateRepository::INITIAL_FORM_TEMPLATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $defaultSegmentId
|
||||
* @return FormEntity|null
|
||||
*/
|
||||
public function ensureDefaultFormExists(int $defaultSegmentId) {
|
||||
if ($this->formRepository->count()) {
|
||||
return null;
|
||||
}
|
||||
return $this->createFormFromTemplate(
|
||||
TemplateRepository::DEFAULT_FORM_TEMPLATE,
|
||||
['segments' => [(string)$defaultSegmentId]]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -26,4 +26,12 @@ class FormsRepository extends Repository {
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function count(): int {
|
||||
return (int)$this->doctrineRepository
|
||||
->createQueryBuilder('f')
|
||||
->select('count(f.id)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use MailPoet\API\JSON\v1\Setup;
|
||||
use MailPoet\Config\Activator;
|
||||
use MailPoet\Config\Populator;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Referrals\ReferralDetector;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Settings\SettingsRepository;
|
||||
@ -31,7 +32,14 @@ class SetupTest extends \MailPoetTest {
|
||||
|
||||
$settings = SettingsController::getInstance();
|
||||
$referralDetector = new ReferralDetector($wp, $settings);
|
||||
$populator = new Populator($settings, $wp, new Captcha(), $referralDetector, $featuresController);
|
||||
$populator = new Populator(
|
||||
$settings,
|
||||
$wp,
|
||||
new Captcha(),
|
||||
$referralDetector,
|
||||
$featuresController,
|
||||
$this->diContainer->get(FormFactory::class)
|
||||
);
|
||||
$router = new Setup($wp, new Activator($settings, $populator));
|
||||
$response = $router->reset();
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
|
@ -15,6 +15,7 @@ use MailPoet\Cron\Workers\StatsNotifications\Scheduler as StatsNotificationsSche
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Entities\NewsletterEntity;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Logging\LoggerFactory;
|
||||
use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\Newsletter;
|
||||
@ -72,7 +73,14 @@ class SendingQueueTest extends \MailPoetTest {
|
||||
$this->settings = SettingsController::getInstance();
|
||||
$referralDetector = new ReferralDetector(WPFunctions::get(), $this->settings);
|
||||
$featuresController = Stub::makeEmpty(FeaturesController::class);
|
||||
$populator = new Populator($this->settings, WPFunctions::get(), new Captcha, $referralDetector, $featuresController);
|
||||
$populator = new Populator(
|
||||
$this->settings,
|
||||
WPFunctions::get(),
|
||||
new Captcha,
|
||||
$referralDetector,
|
||||
$featuresController,
|
||||
$this->diContainer->get(FormFactory::class)
|
||||
);
|
||||
$populator->up();
|
||||
$this->subscriber = Subscriber::create();
|
||||
$this->subscriber->email = 'john@doe.com';
|
||||
|
@ -7,6 +7,7 @@ use Codeception\Stub\Expected;
|
||||
use MailPoet\Config\Populator;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Referrals\ReferralDetector;
|
||||
@ -30,7 +31,14 @@ class MailerTest extends \MailPoetTest {
|
||||
$this->settings = SettingsController::getInstance();
|
||||
$referralDetector = new ReferralDetector(WPFunctions::get(), $this->settings);
|
||||
$featuresController = Stub::makeEmpty(FeaturesController::class);
|
||||
$populator = new Populator($this->settings, WPFunctions::get(), new Captcha, $referralDetector, $featuresController);
|
||||
$populator = new Populator(
|
||||
$this->settings,
|
||||
WPFunctions::get(),
|
||||
new Captcha,
|
||||
$referralDetector,
|
||||
$featuresController,
|
||||
$this->diContainer->get(FormFactory::class)
|
||||
);
|
||||
$populator->up();
|
||||
$this->mailerTask = new MailerTask();
|
||||
$this->sender = $this->settings->get('sender');
|
||||
|
@ -38,4 +38,36 @@ class FormFactoryTest extends \MailPoetTest {
|
||||
expect($formEntity->getSettings())->notEmpty();
|
||||
expect($formEntity->getStyles())->string();
|
||||
}
|
||||
|
||||
public function testItCanOverrideTemplateSettings() {
|
||||
$settings = [
|
||||
'success_message' => 'Hello Buddy!',
|
||||
'segments' => [1, 2, 3],
|
||||
];
|
||||
$formEntity = $this->formFactory->createFormFromTemplate(TemplateRepository::INITIAL_FORM_TEMPLATE, $settings);
|
||||
assert($formEntity instanceof FormEntity);
|
||||
$formSettings = $formEntity->getSettings() ?? [];
|
||||
expect($formSettings['success_message'])->equals('Hello Buddy!');
|
||||
expect($formSettings['segments'])->equals([1, 2, 3]);
|
||||
}
|
||||
|
||||
public function testItCanEnsureDefaultFormExists() {
|
||||
$this->cleanup();
|
||||
$formEntity = $this->formFactory->ensureDefaultFormExists(2);
|
||||
assert($formEntity instanceof FormEntity);
|
||||
$formSettings = $formEntity->getSettings() ?? [];
|
||||
expect($formSettings['segments'])->equals(['2']);
|
||||
// Doesn't create any form if some exists
|
||||
$formEntity = $this->formFactory->ensureDefaultFormExists(2);
|
||||
expect($formEntity)->null();
|
||||
}
|
||||
|
||||
public function _after() {
|
||||
parent::_after();
|
||||
$this->cleanup();
|
||||
}
|
||||
|
||||
private function cleanup() {
|
||||
$this->truncateEntity(FormEntity::class);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Test\Newsletter;
|
||||
use Codeception\Util\Stub;
|
||||
use MailPoet\Config\Populator;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
@ -42,7 +43,14 @@ class ShortcodesTest extends \MailPoetTest {
|
||||
$this->settings = SettingsController::getInstance();
|
||||
$referralDetector = new ReferralDetector(WPFunctions::get(), $this->settings);
|
||||
$featuresController = Stub::makeEmpty(FeaturesController::class);
|
||||
$populator = new Populator($this->settings, WPFunctions::get(), new Captcha, $referralDetector, $featuresController);
|
||||
$populator = new Populator(
|
||||
$this->settings,
|
||||
WPFunctions::get(),
|
||||
new Captcha,
|
||||
$referralDetector,
|
||||
$featuresController,
|
||||
$this->diContainer->get(FormFactory::class)
|
||||
);
|
||||
$populator->up();
|
||||
$this->wPUser = $this->_createWPUser();
|
||||
$this->wPPost = $this->_createWPPost();
|
||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Test\Subscription;
|
||||
use Codeception\Util\Stub;
|
||||
use MailPoet\Config\Populator;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Referrals\ReferralDetector;
|
||||
use MailPoet\Router\Router;
|
||||
@ -28,7 +29,14 @@ class UrlTest extends \MailPoetTest {
|
||||
$this->settings = SettingsController::getInstance();
|
||||
$referralDetector = new ReferralDetector(WPFunctions::get(), $this->settings);
|
||||
$featuresController = Stub::makeEmpty(FeaturesController::class);
|
||||
$populator = new Populator($this->settings, WPFunctions::get(), new Captcha, $referralDetector, $featuresController);
|
||||
$populator = new Populator(
|
||||
$this->settings,
|
||||
WPFunctions::get(),
|
||||
new Captcha,
|
||||
$referralDetector,
|
||||
$featuresController,
|
||||
$this->diContainer->get(FormFactory::class)
|
||||
);
|
||||
$populator->up();
|
||||
$this->url = new SubscriptionUrlFactory(WPFunctions::get(), $this->settings, new LinkTokens);
|
||||
}
|
||||
|
@ -33,12 +33,12 @@ class TemplatesRepositoryTest extends \MailPoetUnitTest {
|
||||
$this->settingsMock->method('get')->willReturnOnConsecutiveCalls([true, false]);
|
||||
$formEntity = $this->repository->getFormEntityForTemplate(TemplateRepository::INITIAL_FORM_TEMPLATE);
|
||||
expect($formEntity)->isInstanceOf(FormEntity::class);
|
||||
$settings = $formEntity->getSettings();
|
||||
$settings = $formEntity->getSettings() ?? [];
|
||||
expect($settings['success_message'])->equals('Check your inbox or spam folder to confirm your subscription.');
|
||||
|
||||
$formEntity = $this->repository->getFormEntityForTemplate(TemplateRepository::INITIAL_FORM_TEMPLATE);
|
||||
expect($formEntity)->isInstanceOf(FormEntity::class);
|
||||
$settings = $formEntity->getSettings();
|
||||
$settings = $formEntity->getSettings() ?? [];
|
||||
expect($settings['success_message'])->equals('You’ve been successfully subscribed to our newsletter!');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user