Deprecate the form model
[MAILPOET-3644]
This commit is contained in:
@@ -2,143 +2,30 @@
|
||||
|
||||
namespace MailPoet\Models;
|
||||
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
/**
|
||||
* @property string|array $settings
|
||||
* @property string|array $body
|
||||
* @property string $name
|
||||
* @property string $status
|
||||
* @property string|null $deletedAt
|
||||
* @deprecated This model is deprecated. Use \MailPoet\Form\FormsRepository and respective Doctrine entities instead.
|
||||
* This class can be removed after 2022-04-15
|
||||
*/
|
||||
|
||||
class Form extends Model {
|
||||
public static $_table = MP_FORMS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
|
||||
|
||||
public function getSettings() {
|
||||
if (is_array($this->settings) || $this->settings === null) {
|
||||
return $this->settings;
|
||||
}
|
||||
return WPFunctions::get()->isSerialized($this->settings) ? unserialize($this->settings) : $this->settings;
|
||||
/**
|
||||
* @deprecated This is here for displaying the deprecation warning for properties.
|
||||
*/
|
||||
public function __get($key) {
|
||||
self::deprecationError('property "' . $key . '"');
|
||||
return parent::__get($key);
|
||||
}
|
||||
|
||||
public function getBody() {
|
||||
if (is_array($this->body) || $this->body === null) {
|
||||
return $this->body;
|
||||
}
|
||||
return WPFunctions::get()->isSerialized($this->body) ? unserialize($this->body) : $this->body;
|
||||
/**
|
||||
* @deprecated This is here for displaying the deprecation warning for static calls.
|
||||
*/
|
||||
public static function __callStatic($name, $arguments) {
|
||||
self::deprecationError($name);
|
||||
return parent::__callStatic($name, $arguments);
|
||||
}
|
||||
|
||||
public function asArray() {
|
||||
$model = parent::asArray();
|
||||
|
||||
$model['body'] = $this->getBody();
|
||||
$model['settings'] = $this->getSettings();
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function save() {
|
||||
$this->set('body', (is_string($this->body) && is_serialized($this->body))
|
||||
? $this->body
|
||||
: serialize($this->body)
|
||||
);
|
||||
$this->set('settings', (is_string($this->settings) && is_serialized($this->settings))
|
||||
? $this->settings
|
||||
: serialize($this->settings)
|
||||
);
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
public function getFieldList(array $body = null) {
|
||||
$body = $body ?? $this->getBody();
|
||||
if (empty($body)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$skippedTypes = ['html', 'divider', 'submit'];
|
||||
$nestedTypes = ['column', 'columns'];
|
||||
$fields = [];
|
||||
|
||||
foreach ((array)$body as $field) {
|
||||
if (!empty($field['type'])
|
||||
&& in_array($field['type'], $nestedTypes)
|
||||
&& !empty($field['body'])
|
||||
) {
|
||||
$nestedFields = $this->getFieldList($field['body']);
|
||||
if ($nestedFields) {
|
||||
$fields = array_merge($fields, $nestedFields);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($field['id'])
|
||||
|| empty($field['type'])
|
||||
|| in_array($field['type'], $skippedTypes)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((int)$field['id'] > 0) {
|
||||
$fields[] = 'cf_' . $field['id'];
|
||||
} else {
|
||||
$fields[] = $field['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $fields ?: false;
|
||||
}
|
||||
|
||||
public static function search($orm, $search = '') {
|
||||
return $orm->whereLike('name', '%' . $search . '%');
|
||||
}
|
||||
|
||||
public static function groups() {
|
||||
return [
|
||||
[
|
||||
'name' => 'all',
|
||||
'label' => __('All', 'mailpoet'),
|
||||
'count' => Form::getPublished()->count(),
|
||||
],
|
||||
[
|
||||
'name' => 'trash',
|
||||
'label' => __('Trash', 'mailpoet'),
|
||||
'count' => Form::getTrashed()->count(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function groupBy($orm, $group = null) {
|
||||
if ($group === 'trash') {
|
||||
return $orm->whereNotNull('deleted_at');
|
||||
}
|
||||
return $orm->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
public static function getDefaultSuccessMessage() {
|
||||
$settings = SettingsController::getInstance();
|
||||
if ($settings->get('signup_confirmation.enabled')) {
|
||||
return __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet');
|
||||
}
|
||||
return __('You’ve been successfully subscribed to our newsletter!', 'mailpoet');
|
||||
}
|
||||
|
||||
public static function updateSuccessMessages() {
|
||||
$rightMessage = self::getDefaultSuccessMessage();
|
||||
$wrongMessage = (
|
||||
$rightMessage === __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet')
|
||||
? __('You’ve been successfully subscribed to our newsletter!', 'mailpoet')
|
||||
: __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet')
|
||||
);
|
||||
$forms = self::findMany();
|
||||
foreach ($forms as $form) {
|
||||
$settings = $form->getSettings();
|
||||
if (isset($settings['success_message']) && $settings['success_message'] === $wrongMessage) {
|
||||
$settings['success_message'] = $rightMessage;
|
||||
$form->set('settings', serialize($settings));
|
||||
$form->save();
|
||||
}
|
||||
}
|
||||
private static function deprecationError($methodName) {
|
||||
trigger_error('Calling ' . $methodName . ' is deprecated and will be removed. Use MailPoet\Statistics\StatisticsFormsRepository and respective Doctrine entities instead.', E_USER_DEPRECATED);
|
||||
}
|
||||
}
|
||||
|
@@ -1,196 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Models;
|
||||
|
||||
use MailPoet\Form\FormMessageController;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
|
||||
class FormTest extends \MailPoetTest {
|
||||
public $form;
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
/** @var FormMessageController */
|
||||
private $messageController;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->settings = SettingsController::getInstance();
|
||||
$this->messageController = $this->diContainer->get(FormMessageController::class);
|
||||
$this->form = Form::createOrUpdate([
|
||||
'name' => 'My Form',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItCanBeCreated() {
|
||||
expect($this->form->id() > 0)->true();
|
||||
expect($this->form->getErrors())->false();
|
||||
}
|
||||
|
||||
public function testItCanBeGrouped() {
|
||||
$forms = Form::filter('groupBy', 'all')->findArray();
|
||||
expect($forms)->count(1);
|
||||
|
||||
$forms = Form::filter('groupBy', 'trash')->findArray();
|
||||
expect($forms)->count(0);
|
||||
|
||||
$this->form->trash();
|
||||
$forms = Form::filter('groupBy', 'trash')->findArray();
|
||||
expect($forms)->count(1);
|
||||
|
||||
$forms = Form::filter('groupBy', 'all')->findArray();
|
||||
expect($forms)->count(0);
|
||||
|
||||
$this->form->restore();
|
||||
$forms = Form::filter('groupBy', 'all')->findArray();
|
||||
expect($forms)->count(1);
|
||||
}
|
||||
|
||||
public function testItCanBeSearched() {
|
||||
$form = Form::filter('search', 'my F')->findOne();
|
||||
assert($form instanceof Form);
|
||||
expect($form->name)->equals('My Form');
|
||||
}
|
||||
|
||||
public function testItHasACreatedAtOnCreation() {
|
||||
$form = Form::findOne($this->form->id);
|
||||
assert($form instanceof Form);
|
||||
expect($form->createdAt)->notNull();
|
||||
}
|
||||
|
||||
public function testItHasAnUpdatedAtOnCreation() {
|
||||
$form = Form::findOne($this->form->id);
|
||||
assert($form instanceof Form);
|
||||
expect($form->updatedAt)->equals($form->createdAt);
|
||||
}
|
||||
|
||||
public function testItUpdatesTheUpdatedAtOnUpdate() {
|
||||
$form = Form::findOne($this->form->id);
|
||||
assert($form instanceof Form);
|
||||
$createdAt = $form->createdAt;
|
||||
|
||||
sleep(1);
|
||||
|
||||
$form->name = 'new name';
|
||||
$form->save();
|
||||
|
||||
$updatedForm = Form::findOne($form->id);
|
||||
assert($updatedForm instanceof Form);
|
||||
expect($updatedForm->createdAt)->equals($createdAt);
|
||||
$isTimeUpdated = (
|
||||
$updatedForm->updatedAt > $updatedForm->createdAt
|
||||
);
|
||||
expect($isTimeUpdated)->true();
|
||||
}
|
||||
|
||||
public function testItCanCreateOrUpdate() {
|
||||
$createdForm = Form::createOrUpdate([
|
||||
'name' => 'Created Form',
|
||||
]);
|
||||
expect($createdForm->id > 0)->true();
|
||||
expect($createdForm->getErrors())->false();
|
||||
|
||||
$form = Form::findOne($createdForm->id);
|
||||
assert($form instanceof Form);
|
||||
expect($form->name)->equals('Created Form');
|
||||
|
||||
$isUpdated = Form::createOrUpdate([
|
||||
'id' => $createdForm->id,
|
||||
'name' => 'Updated Form',
|
||||
]);
|
||||
$form = Form::findOne($createdForm->id);
|
||||
assert($form instanceof Form);
|
||||
expect($form->name)->equals('Updated Form');
|
||||
}
|
||||
|
||||
public function testItCanProvideAFieldList() {
|
||||
$form = Form::createOrUpdate([
|
||||
'name' => 'My Form',
|
||||
'body' => [
|
||||
[
|
||||
'type' => 'text',
|
||||
'id' => 'email',
|
||||
],
|
||||
[
|
||||
'type' => 'text',
|
||||
'id' => 2,
|
||||
],
|
||||
[
|
||||
'type' => 'columns',
|
||||
'body' => [
|
||||
[
|
||||
'type' => 'column',
|
||||
'body' => [
|
||||
[
|
||||
'type' => 'text',
|
||||
'id' => 3,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'type' => 'column',
|
||||
'body' => [
|
||||
[
|
||||
'type' => 'divider',
|
||||
'id' => 'divider',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'type' => 'submit',
|
||||
'id' => 'submit',
|
||||
],
|
||||
],
|
||||
]);
|
||||
expect($form->getFieldList())->equals(['email', 'cf_2', 'cf_3']);
|
||||
}
|
||||
|
||||
public function testItUpdatesSuccessMessagesWhenConfirmationIsDisabled() {
|
||||
$default = Form::createOrUpdate([
|
||||
'name' => 'with default message',
|
||||
'settings' => ['success_message' => 'Check your inbox or spam folder to confirm your subscription.'],
|
||||
]);
|
||||
$custom = Form::createOrUpdate([
|
||||
'name' => 'with custom message',
|
||||
'settings' => ['success_message' => 'Thanks for joining us!'],
|
||||
]);
|
||||
$this->settings->set('signup_confirmation.enabled', '0');
|
||||
$this->messageController->updateSuccessMessages();
|
||||
$default = Form::findOne($default->id);
|
||||
$custom = Form::findOne($custom->id);
|
||||
assert($default instanceof Form);
|
||||
assert($custom instanceof Form);
|
||||
$default = $default->asArray();
|
||||
$custom = $custom->asArray();
|
||||
expect($default['settings']['success_message'])->equals('You’ve been successfully subscribed to our newsletter!');
|
||||
expect($custom['settings']['success_message'])->equals('Thanks for joining us!');
|
||||
}
|
||||
|
||||
public function testItUpdatesSuccessMessagesWhenConfirmationIsEnabled() {
|
||||
$default = Form::createOrUpdate([
|
||||
'name' => 'with default message',
|
||||
'settings' => ['success_message' => 'Check your inbox or spam folder to confirm your subscription.'],
|
||||
]);
|
||||
$custom = Form::createOrUpdate([
|
||||
'name' => 'with custom message',
|
||||
'settings' => ['success_message' => 'Thanks for joining us!'],
|
||||
]);
|
||||
$this->settings->set('signup_confirmation.enabled', '1');
|
||||
$this->messageController->updateSuccessMessages();
|
||||
$default = Form::findOne($default->id);
|
||||
$custom = Form::findOne($custom->id);
|
||||
assert($default instanceof Form);
|
||||
assert($custom instanceof Form);
|
||||
$default = $default->asArray();
|
||||
$custom = $custom->asArray();
|
||||
expect($default['settings']['success_message'])->equals('Check your inbox or spam folder to confirm your subscription.');
|
||||
expect($custom['settings']['success_message'])->equals('Thanks for joining us!');
|
||||
}
|
||||
|
||||
public function _after() {
|
||||
Form::deleteMany();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user