Create a default form

[MAILPOET-1449]
This commit is contained in:
Pavel Dohnal
2018-11-13 15:38:46 +01:00
parent a393ad5b61
commit ad85c7e4b1
3 changed files with 97 additions and 8 deletions

View File

@ -1,12 +1,12 @@
<?php
namespace MailPoet\Config;
use MailPoet\Config\PopulatorData\DefaultForm;
use MailPoet\Cron\CronTrigger;
use MailPoet\Mailer\MailerLog;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterTemplate;
use MailPoet\Models\Form;
use MailPoet\Models\Segment;
use MailPoet\Models\SendingQueue;
use MailPoet\Models\StatisticsForms;
use MailPoet\Models\Subscriber;
use MailPoet\Segments\WP;
@ -24,6 +24,7 @@ class Populator {
public $prefix;
public $models;
public $templates;
private $default_segment;
const TEMPLATES_NAMESPACE = '\MailPoet\Config\PopulatorData\Templates\\';
function __construct() {
@ -87,6 +88,7 @@ class Populator {
array_map(array($this, 'populate'), $this->models);
$this->createDefaultSegments();
$this->createDefaultForm();
$this->createDefaultSettings();
$this->createMailPoetPage();
$this->createSourceForSubscribers();
@ -195,13 +197,28 @@ class Populator {
// Default segment
if(Segment::where('type', 'default')->count() === 0) {
$default_segment = Segment::create();
$default_segment->hydrate(array(
$this->default_segment = Segment::create();
$this->default_segment->hydrate([
'name' => __('My First List', 'mailpoet'),
'description' =>
__('This list is automatically created when you install MailPoet.', 'mailpoet')
));
$default_segment->save();
]);
$this->default_segment->save();
}
}
private function createDefaultForm() {
if(Form::count() === 0) {
$factory = new DefaultForm();
if(!$this->default_segment) {
$this->default_segment = Segment::where('type', 'default')->orderByAsc('id')->limit(1)->findOne();
}
Form::createOrUpdate([
'name' => $factory->getName(),
'body' => serialize($factory->getBody()),
'settings' => serialize($factory->getSettings($this->default_segment)),
'styles' => $factory->getStyles(),
]);
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace MailPoet\Config\PopulatorData;
use MailPoet\Form\Util\Styles;
use MailPoet\Models\Segment;
class DefaultForm {
public function getName() {
return _x('A GDPR friendly form', 'default name of form (GDPR friendly) to capture emails', 'mailpoet');
}
public function getBody() {
return [
[
'type' => 'text',
'name' => _x('First name', 'Form label', 'mailpoet'),
'id' => 'first_name',
'unique' => '1',
'static' => '0',
'params' => ['label' => _x('First name', 'Form label', 'mailpoet')],
'position' => '1',
],
[
'type' => 'text',
'name' => _x('Email', 'Form label', 'mailpoet'),
'id' => 'email',
'unique' => '0',
'static' => '1',
'params' => ['label' => _x('Email', 'Form label', 'mailpoet'), 'required' => 'true'],
'position' => '2',
],
[
'type' => 'html',
'name' => _x('Custom text or HTML', 'Form label', 'mailpoet'),
'id' => 'html',
'unique' => '0',
'static' => '0',
'params' => [
'text' => __('We keep your data private and share your data only with third parties that make this service possible. <a href="">Read our Privacy Policy.</a>', 'mailpoet'),
'nl2br' => '0',
],
'position' => '3',
],
[
'type' => 'submit',
'name' => _x('Submit', 'Form label', 'mailpoet'),
'id' => 'submit',
'unique' => '0',
'static' => '1',
'params' => ['label' => __('Subscribe!', 'Form label', 'mailpoet')],
'position' => '4',
],
];
}
public function getSettings(Segment $default_segment) {
return [
'segments' => [$default_segment->id()],
'on_success' => 'message',
'success_message' => __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet'),
'success_page' => '5',
'segments_selected_by' => 'admin',
];
}
public function getStyles() {
return Styles::$default_styles;
}
}

View File

@ -447,7 +447,7 @@ class MP2MigratorTest extends \MailPoetTest {
$this->initImport();
$this->loadMP2Fixtures();
$this->invokeMethod($this->MP2Migrator, 'importForms');
expect(Form::count())->equals(2);
expect(Form::count())->equals(3);
// Check a form data
$this->initImport();
@ -495,7 +495,7 @@ class MP2MigratorTest extends \MailPoetTest {
));
$this->invokeMethod($this->MP2Migrator, 'importForms');
$table = MP_FORMS_TABLE;
$form = $wpdb->get_row("SELECT * FROM $table WHERE id=" . 1);
$form = $wpdb->get_row("SELECT * FROM $table WHERE id=" . 2);
expect($form->name)->equals($name);
$settings = unserialize(($form->settings));
expect($settings['on_success'])->equals('message');