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();
foreach ($forms as $form) {
$settings = $form->getSettings();
if (!is_array($settings)) continue;
$settings['form_placement'] = [
FormEntity::DISPLAY_TYPE_POPUP => [
'enabled' => $settings['form_placement_popup_enabled'],

View File

@@ -18,7 +18,7 @@ class FormEntity {
use UpdatedAtTrait;
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_POPUP = 'popup';
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 SETUP = [
FormEntity::DISPLAY_TYPE_BELOW_POST => [
'switch' => 'form_placement_bellow_posts_enabled',
'post' => 'place_form_bellow_all_posts',
'page' => 'place_form_bellow_all_pages',
],
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',
],
const TYPES = [
FormEntity::DISPLAY_TYPE_BELOW_POST,
FormEntity::DISPLAY_TYPE_POPUP,
FormEntity::DISPLAY_TYPE_FIXED_BAR,
FormEntity::DISPLAY_TYPE_SLIDE_IN,
];
/** @var WPFunctions */
@@ -128,7 +112,7 @@ class DisplayFormInWPContent {
private function filterOneFormInEachDisplayType($forms): array {
$formsFiltered = [];
foreach ($forms as $form) {
foreach (array_keys(self::SETUP) as $displayType) {
foreach (self::TYPES as $displayType) {
if ($this->shouldDisplayFormType($form, $displayType)) {
$formsFiltered[$displayType] = $form;
}
@@ -145,6 +129,7 @@ class DisplayFormInWPContent {
'settings' => $form->getSettings(),
];
$formSettings = $form->getSettings();
if (!is_array($formSettings)) return '';
$htmlId = 'mp_form_' . $displayType . $form->getId();
$templateData = [
'form_html_id' => $htmlId,
@@ -168,8 +153,8 @@ class DisplayFormInWPContent {
((int)$_GET['mailpoet_error'] === $form->getId())
);
$templateData['delay'] = $formSettings[$displayType . '_form_delay'] ?? 0;
$templateData['position'] = $formSettings[$displayType . '_form_position'] ?? '';
$templateData['delay'] = $formSettings[$displayType]['delay'] ?? 0;
$templateData['position'] = $formSettings[$displayType]['position'] ?? '';
$templateData['backgroundColor'] = $formSettings['backgroundColor'] ?? '';
$templateData['fontFamily'] = $formSettings['font_family'] ?? '';
@@ -183,24 +168,29 @@ class DisplayFormInWPContent {
private function shouldDisplayFormType(FormEntity $form, string $formType): bool {
$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'];
if (!isset($settings[$switchKey]) || ($settings[$switchKey] !== '1')) {
// this form type display is disabled
$setup = $settings['form_placement'][$formType];
if ($setup['enabled'] !== '1') {
return false;
}
$key = '';
if ($this->wp->isSingular('post')) {
$key = $keys['post'];
$key = 'posts';
}
if ($this->wp->isPage()) {
$key = $keys['page'];
$key = 'pages';
}
// 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 {
$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 (isset($formSettings['popup_styles']['width'])) {
$width = $this->getWidthValue($formSettings['popup_styles']['width']);
if (isset($width)) {
$styles[] = "width: $width";
$styles[] = "max-width: 100vw";
} else { // BC compatibilty
@@ -137,8 +140,7 @@ class Styles {
$styles[] = 'max-width: 560px';
}
} elseif ($displayType === FormEntity::DISPLAY_TYPE_SLIDE_IN) {
if (isset($formSettings['slide_in_styles']['width'])) {
$width = $this->getWidthValue($formSettings['slide_in_styles']['width']);
if (isset($width)) {
$styles[] = "width: $width";
$styles[] = "max-width: 100vw";
} else { // BC compatibilty
@@ -146,21 +148,18 @@ class Styles {
$styles[] = 'min-width: 350px';
}
} elseif ($displayType === FormEntity::DISPLAY_TYPE_FIXED_BAR) {
if (isset($formSettings['fixed_bar_styles']['width'])) {
$width = $this->getWidthValue($formSettings['fixed_bar_styles']['width']);
if (isset($width)) {
$styles[] = "width: $width";
$styles[] = "max-width: 100%";
} else { // BC compatibilty
$styles[] = 'max-width: 960px';
}
} elseif ($displayType === FormEntity::DISPLAY_TYPE_BELOW_POST) {
if (isset($formSettings['below_post_styles']['width'])) {
$width = $this->getWidthValue($formSettings['below_post_styles']['width']);
if (isset($width)) {
$styles[] = "width: $width";
}
} elseif ($displayType === FormEntity::DISPLAY_TYPE_OTHERS) {
if (isset($formSettings['other_styles']['width'])) {
$width = $this->getWidthValue($formSettings['other_styles']['width']);
if (isset($width)) {
$styles[] = "width: $width";
}
}

View File

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

View File

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

View File

@@ -170,7 +170,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'pixel',
'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);
list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 900px;');
@@ -189,7 +193,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent',
'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);
list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;max-width: 100%;');
@@ -207,7 +215,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'pixel',
'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);
list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 900px;max-width: 100vw;');
@@ -225,7 +237,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent',
'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);
list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;max-width: 100vw;');
@@ -244,7 +260,11 @@ class StylesTest extends \MailPoetUnitTest {
'unit' => 'percent',
'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);
list($styleWithoutMedia) = explode('@media ', $styles);
expect($styleWithoutMedia)->contains('width: 90%;');