Use refactored in form displaying

[MAILPOET-3072]
This commit is contained in:
Pavel Dohnal
2020-08-27 15:28:40 +02:00
committed by Veljko V
parent 698b30f770
commit 401b780dce
7 changed files with 143 additions and 83 deletions

View File

@@ -747,6 +747,7 @@ class Populator {
$forms = $this->formsRepository->findAll(); $forms = $this->formsRepository->findAll();
foreach ($forms as $form) { foreach ($forms as $form) {
$settings = $form->getSettings(); $settings = $form->getSettings();
if (!is_array($settings)) continue;
$settings['form_placement'] = [ $settings['form_placement'] = [
FormEntity::DISPLAY_TYPE_POPUP => [ FormEntity::DISPLAY_TYPE_POPUP => [
'enabled' => $settings['form_placement_popup_enabled'], 'enabled' => $settings['form_placement_popup_enabled'],

View File

@@ -18,7 +18,7 @@ class FormEntity {
use UpdatedAtTrait; use UpdatedAtTrait;
use DeletedAtTrait; use DeletedAtTrait;
const DISPLAY_TYPE_BELOW_POST = 'below_post'; const DISPLAY_TYPE_BELOW_POST = 'below_posts';
const DISPLAY_TYPE_FIXED_BAR = 'fixed_bar'; const DISPLAY_TYPE_FIXED_BAR = 'fixed_bar';
const DISPLAY_TYPE_POPUP = 'popup'; const DISPLAY_TYPE_POPUP = 'popup';
const DISPLAY_TYPE_SLIDE_IN = 'slide_in'; const DISPLAY_TYPE_SLIDE_IN = 'slide_in';

View File

@@ -12,27 +12,11 @@ class DisplayFormInWPContent {
const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content'; const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content';
const SETUP = [ const TYPES = [
FormEntity::DISPLAY_TYPE_BELOW_POST => [ FormEntity::DISPLAY_TYPE_BELOW_POST,
'switch' => 'form_placement_bellow_posts_enabled', FormEntity::DISPLAY_TYPE_POPUP,
'post' => 'place_form_bellow_all_posts', FormEntity::DISPLAY_TYPE_FIXED_BAR,
'page' => 'place_form_bellow_all_pages', FormEntity::DISPLAY_TYPE_SLIDE_IN,
],
FormEntity::DISPLAY_TYPE_POPUP => [
'switch' => 'form_placement_popup_enabled',
'post' => 'place_popup_form_on_all_posts',
'page' => 'place_popup_form_on_all_pages',
],
FormEntity::DISPLAY_TYPE_FIXED_BAR => [
'switch' => 'form_placement_fixed_bar_enabled',
'post' => 'place_fixed_bar_form_on_all_posts',
'page' => 'place_fixed_bar_form_on_all_pages',
],
FormEntity::DISPLAY_TYPE_SLIDE_IN => [
'switch' => 'form_placement_slide_in_enabled',
'post' => 'place_slide_in_form_on_all_posts',
'page' => 'place_slide_in_form_on_all_pages',
],
]; ];
/** @var WPFunctions */ /** @var WPFunctions */
@@ -128,7 +112,7 @@ class DisplayFormInWPContent {
private function filterOneFormInEachDisplayType($forms): array { private function filterOneFormInEachDisplayType($forms): array {
$formsFiltered = []; $formsFiltered = [];
foreach ($forms as $form) { foreach ($forms as $form) {
foreach (array_keys(self::SETUP) as $displayType) { foreach (self::TYPES as $displayType) {
if ($this->shouldDisplayFormType($form, $displayType)) { if ($this->shouldDisplayFormType($form, $displayType)) {
$formsFiltered[$displayType] = $form; $formsFiltered[$displayType] = $form;
} }
@@ -145,6 +129,7 @@ class DisplayFormInWPContent {
'settings' => $form->getSettings(), 'settings' => $form->getSettings(),
]; ];
$formSettings = $form->getSettings(); $formSettings = $form->getSettings();
if (!is_array($formSettings)) return '';
$htmlId = 'mp_form_' . $displayType . $form->getId(); $htmlId = 'mp_form_' . $displayType . $form->getId();
$templateData = [ $templateData = [
'form_html_id' => $htmlId, 'form_html_id' => $htmlId,
@@ -168,8 +153,8 @@ class DisplayFormInWPContent {
((int)$_GET['mailpoet_error'] === $form->getId()) ((int)$_GET['mailpoet_error'] === $form->getId())
); );
$templateData['delay'] = $formSettings[$displayType . '_form_delay'] ?? 0; $templateData['delay'] = $formSettings[$displayType]['delay'] ?? 0;
$templateData['position'] = $formSettings[$displayType . '_form_position'] ?? ''; $templateData['position'] = $formSettings[$displayType]['position'] ?? '';
$templateData['backgroundColor'] = $formSettings['backgroundColor'] ?? ''; $templateData['backgroundColor'] = $formSettings['backgroundColor'] ?? '';
$templateData['fontFamily'] = $formSettings['font_family'] ?? ''; $templateData['fontFamily'] = $formSettings['font_family'] ?? '';
@@ -183,24 +168,29 @@ class DisplayFormInWPContent {
private function shouldDisplayFormType(FormEntity $form, string $formType): bool { private function shouldDisplayFormType(FormEntity $form, string $formType): bool {
$settings = $form->getSettings(); $settings = $form->getSettings();
if (!is_array($settings)) return false; // check the structure just to be sure
$keys = self::SETUP[$formType]; if (!is_array($settings)
|| !isset($settings['form_placement'][$formType])
|| !is_array($settings['form_placement'][$formType])
) return false;
$switchKey = $keys['switch']; $setup = $settings['form_placement'][$formType];
if (!isset($settings[$switchKey]) || ($settings[$switchKey] !== '1')) { if ($setup['enabled'] !== '1') {
// this form type display is disabled
return false; return false;
} }
$key = ''; $key = '';
if ($this->wp->isSingular('post')) { if ($this->wp->isSingular('post')) {
$key = $keys['post']; $key = 'posts';
} }
if ($this->wp->isPage()) { if ($this->wp->isPage()) {
$key = $keys['page']; $key = 'pages';
} }
// is enabled for this page? // is enabled for this page?
return (isset($settings[$key]) && ($settings[$key] === '1')); return (isset($setup[$key])
&& isset($setup[$key]['all'])
&& $setup[$key]['all'] === '1');
} }
} }

View File

@@ -127,9 +127,12 @@ class Styles {
private function renderWidthStyles(array $formSettings, string $selector, string $displayType): string { private function renderWidthStyles(array $formSettings, string $selector, string $displayType): string {
$styles = []; $styles = [];
if (isset($formSettings['form_placement'][$displayType]['styles']['width'])) {
$width = $this->getWidthValue($formSettings['form_placement'][$displayType]['styles']['width']);
}
if ($displayType === FormEntity::DISPLAY_TYPE_POPUP) { if ($displayType === FormEntity::DISPLAY_TYPE_POPUP) {
if (isset($formSettings['popup_styles']['width'])) { if (isset($width)) {
$width = $this->getWidthValue($formSettings['popup_styles']['width']);
$styles[] = "width: $width"; $styles[] = "width: $width";
$styles[] = "max-width: 100vw"; $styles[] = "max-width: 100vw";
} else { // BC compatibilty } else { // BC compatibilty
@@ -137,8 +140,7 @@ class Styles {
$styles[] = 'max-width: 560px'; $styles[] = 'max-width: 560px';
} }
} elseif ($displayType === FormEntity::DISPLAY_TYPE_SLIDE_IN) { } elseif ($displayType === FormEntity::DISPLAY_TYPE_SLIDE_IN) {
if (isset($formSettings['slide_in_styles']['width'])) { if (isset($width)) {
$width = $this->getWidthValue($formSettings['slide_in_styles']['width']);
$styles[] = "width: $width"; $styles[] = "width: $width";
$styles[] = "max-width: 100vw"; $styles[] = "max-width: 100vw";
} else { // BC compatibilty } else { // BC compatibilty
@@ -146,21 +148,18 @@ class Styles {
$styles[] = 'min-width: 350px'; $styles[] = 'min-width: 350px';
} }
} elseif ($displayType === FormEntity::DISPLAY_TYPE_FIXED_BAR) { } elseif ($displayType === FormEntity::DISPLAY_TYPE_FIXED_BAR) {
if (isset($formSettings['fixed_bar_styles']['width'])) { if (isset($width)) {
$width = $this->getWidthValue($formSettings['fixed_bar_styles']['width']);
$styles[] = "width: $width"; $styles[] = "width: $width";
$styles[] = "max-width: 100%"; $styles[] = "max-width: 100%";
} else { // BC compatibilty } else { // BC compatibilty
$styles[] = 'max-width: 960px'; $styles[] = 'max-width: 960px';
} }
} elseif ($displayType === FormEntity::DISPLAY_TYPE_BELOW_POST) { } elseif ($displayType === FormEntity::DISPLAY_TYPE_BELOW_POST) {
if (isset($formSettings['below_post_styles']['width'])) { if (isset($width)) {
$width = $this->getWidthValue($formSettings['below_post_styles']['width']);
$styles[] = "width: $width"; $styles[] = "width: $width";
} }
} elseif ($displayType === FormEntity::DISPLAY_TYPE_OTHERS) { } elseif ($displayType === FormEntity::DISPLAY_TYPE_OTHERS) {
if (isset($formSettings['other_styles']['width'])) { if (isset($width)) {
$width = $this->getWidthValue($formSettings['other_styles']['width']);
$styles[] = "width: $width"; $styles[] = "width: $width";
} }
} }

View File

@@ -59,8 +59,13 @@ class Form {
* @return $this * @return $this
*/ */
public function withDisplayBelowPosts() { public function withDisplayBelowPosts() {
$this->data['settings']['place_form_bellow_all_posts'] = '1'; $this->data['settings']['form_placement'] = [
$this->data['settings']['form_placement_bellow_posts_enabled'] = '1'; 'below_posts' => [
'enabled' => '1',
'pages' => ['all' => ''],
'posts' => ['all' => '1'],
],
];
return $this; return $this;
} }

View File

@@ -52,9 +52,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '1', 'below_posts' => [
'form_placement_bellow_posts_enabled' => '1', 'enabled' => '1',
'pages' => ['all' => ''],
'posts' => ['all' => '1'],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -86,9 +90,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '', 'below_posts' => [
'form_placement_bellow_posts_enabled' => '', 'enabled' => '',
'pages' => ['all' => ''],
'posts' => ['all' => ''],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -109,9 +117,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '', 'below_posts' => [
'form_placement_bellow_posts_enabled' => '1', 'enabled' => '1',
'pages' => ['all' => ''],
'posts' => ['all' => ''],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -139,9 +151,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '1', 'below_posts' => [
'form_placement_bellow_posts_enabled' => '1', 'enabled' => '1',
'pages' => ['all' => ''],
'posts' => ['all' => '1'],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -168,9 +184,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '1', 'form_placement' => [
'place_form_bellow_all_posts' => '', 'below_posts' => [
'form_placement_bellow_posts_enabled' => '1', 'enabled' => '1',
'pages' => ['all' => '1'],
'posts' => ['all' => ''],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -225,12 +245,18 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '', 'below_posts' => [
'place_popup_form_on_all_pages' => '1', 'enabled' => '',
'place_popup_form_on_all_posts' => '', 'pages' => ['all' => ''],
'form_placement_bellow_posts_enabled' => '', 'posts' => ['all' => ''],
'form_placement_popup_enabled' => '1', ],
'popup' => [
'enabled' => '1',
'pages' => ['all' => '1'],
'posts' => ['all' => ''],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -258,14 +284,23 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form = new FormEntity('My Form'); $form = new FormEntity('My Form');
$form->setSettings([ $form->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_form_bellow_all_pages' => '', 'form_placement' => [
'place_form_bellow_all_posts' => '', 'below_posts' => [
'place_popup_form_on_all_pages' => '', 'enabled' => '',
'place_popup_form_on_all_posts' => '', 'pages' => ['all' => ''],
'place_fixed_bar_form_on_all_pages' => '1', 'posts' => ['all' => ''],
'place_fixed_bar_form_on_all_posts' => '1', ],
'form_placement_bellow_posts_enabled' => '', 'popup' => [
'form_placement_fixed_bar_enabled' => '1', 'enabled' => '1',
'pages' => ['all' => ''],
'posts' => ['all' => ''],
],
'fixed_bar' => [
'enabled' => '1',
'pages' => ['all' => '1'],
'posts' => ['all' => '1'],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form->setBody([[ $form->setBody([[
@@ -289,8 +324,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form1 = new FormEntity('My Form'); $form1 = new FormEntity('My Form');
$form1->setSettings([ $form1->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_fixed_bar_form_on_all_pages' => '1', 'form_placement' => [
'form_placement_fixed_bar_enabled' => '1', 'fixed_bar' => [
'enabled' => '1',
'pages' => ['all' => '1'],
'posts' => ['all' => '1'],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form1->setBody([[ $form1->setBody([[
@@ -302,8 +342,13 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$form2 = new FormEntity('My Form'); $form2 = new FormEntity('My Form');
$form2->setSettings([ $form2->setSettings([
'segments' => ['3'], 'segments' => ['3'],
'place_fixed_bar_form_on_all_pages' => '1', 'form_placement' => [
'form_placement_fixed_bar_enabled' => '1', 'fixed_bar' => [
'enabled' => '1',
'pages' => ['all' => '1'],
'posts' => ['all' => '1'],
],
],
'success_message' => 'Hello', 'success_message' => 'Hello',
]); ]);
$form2->setBody([[ $form2->setBody([[

View File

@@ -170,7 +170,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'pixel', 'unit' => 'pixel',
'value' => '900', 'value' => '900',
]; ];
$form['settings'] = ['below_post_styles' => ['width' => $width]]; $form['settings'] = [
'form_placement' => [
'below_posts' => ['styles' => ['width' => $width]],
],
];
$styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_BELOW_POST); $styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_BELOW_POST);
list($styleWithoutMedia) = explode('@media ', $styles); list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 900px;'); expect($styleWithoutMedia)->contains('width: 900px;');
@@ -189,7 +193,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent', 'unit' => 'percent',
'value' => '90', 'value' => '90',
]; ];
$form['settings'] = ['fixed_bar_styles' => ['width' => $width]]; $form['settings'] = [
'form_placement' => [
'fixed_bar' => ['styles' => ['width' => $width]],
],
];
$styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_FIXED_BAR); $styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_FIXED_BAR);
list($styleWithoutMedia) = explode('@media ', $styles); list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;max-width: 100%;'); expect($styleWithoutMedia)->contains('width: 90%;max-width: 100%;');
@@ -207,7 +215,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'pixel', 'unit' => 'pixel',
'value' => '900', 'value' => '900',
]; ];
$form['settings'] = ['popup_styles' => ['width' => $width]]; $form['settings'] = [
'form_placement' => [
'popup' => ['styles' => ['width' => $width]],
],
];
$styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_POPUP); $styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_POPUP);
list($styleWithoutMedia) = explode('@media ', $styles); list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 900px;max-width: 100vw;'); expect($styleWithoutMedia)->contains('width: 900px;max-width: 100vw;');
@@ -225,7 +237,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent', 'unit' => 'percent',
'value' => '90', 'value' => '90',
]; ];
$form['settings'] = ['slide_in_styles' => ['width' => $width]]; $form['settings'] = [
'form_placement' => [
'slide_in' => ['styles' => ['width' => $width]],
],
];
$styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_SLIDE_IN); $styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_SLIDE_IN);
list($styleWithoutMedia) = explode('@media ', $styles); list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;max-width: 100vw;'); expect($styleWithoutMedia)->contains('width: 90%;max-width: 100vw;');
@@ -244,7 +260,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent', 'unit' => 'percent',
'value' => '90', 'value' => '90',
]; ];
$form['settings'] = ['other_styles' => ['width' => $width]]; $form['settings'] = [
'form_placement' => [
'others' => ['styles' => ['width' => $width]],
],
];
$styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_OTHERS); $styles = $this->styles->renderFormSettingsStyles($form, '#prefix', FormEntity::DISPLAY_TYPE_OTHERS);
list($styleWithoutMedia) = explode('@media ', $styles); list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;'); expect($styleWithoutMedia)->contains('width: 90%;');