diff --git a/lib/Form/Block/BlockRendererHelper.php b/lib/Form/Block/BlockRendererHelper.php index 7dd72729b1..623076b95f 100644 --- a/lib/Form/Block/BlockRendererHelper.php +++ b/lib/Form/Block/BlockRendererHelper.php @@ -26,7 +26,7 @@ class BlockRendererHelper { $this->wp = $wp; } - public function getInputValidation(array $block, array $extraRules = []): string { + public function getInputValidation(array $block, array $extraRules = [], ?int $formId = null): string { $rules = []; $blockId = $this->wp->escAttr($block['id']); @@ -51,7 +51,7 @@ class BlockRendererHelper { $rules['required'] = true; $rules['mincheck'] = 1; $rules['group'] = $blockId; - $rules['errors-container'] = '.mailpoet_error_' . $blockId; + $rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : ''); $rules['required-message'] = __('Please select a list.', 'mailpoet'); } @@ -71,13 +71,13 @@ class BlockRendererHelper { if (in_array($block['type'], ['radio', 'checkbox'])) { $rules['group'] = 'custom_field_' . $blockId; - $rules['errors-container'] = '.mailpoet_error_' . $blockId; + $rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : ''); $rules['required-message'] = __('Please select at least one option.', 'mailpoet'); } if ($block['type'] === 'date') { $rules['group'] = 'custom_field_' . $blockId; - $rules['errors-container'] = '.mailpoet_error_' . $blockId; + $rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : ''); } $validation = []; diff --git a/lib/Form/Block/Checkbox.php b/lib/Form/Block/Checkbox.php index 41aa9fefd6..f84b322be1 100644 --- a/lib/Form/Block/Checkbox.php +++ b/lib/Form/Block/Checkbox.php @@ -26,11 +26,11 @@ class Checkbox { $this->wp = $wp; } - public function render(array $block, array $formSettings): string { + public function render(array $block, array $formSettings, ?int $formId = null): string { $html = ''; $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; - $fieldValidation = $this->rendererHelper->getInputValidation($block); + $fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId); $html .= $this->rendererHelper->renderLabel($block, $formSettings); @@ -70,7 +70,7 @@ class Checkbox { $html .= ''; } - $html .= ''; + $html .= ''; return $this->wrapper->render($block, $html); } diff --git a/lib/Form/Block/Date.php b/lib/Form/Block/Date.php index f679e93960..05f97bd4ce 100644 --- a/lib/Form/Block/Date.php +++ b/lib/Form/Block/Date.php @@ -33,14 +33,14 @@ class Date { $this->wp = $wp; } - public function render(array $block, array $formSettings): string { + public function render(array $block, array $formSettings, ?int $formId = null): string { $html = ''; $html .= $this->rendererHelper->renderLabel($block, $formSettings); - $html .= $this->renderDateSelect($block, $formSettings); + $html .= $this->renderDateSelect($formId, $block, $formSettings); return $this->wrapper->render($block, $html); } - private function renderDateSelect(array $block = [], $formSettings = []): string { + private function renderDateSelect(?int $formId, array $block = [], $formSettings = []): string { $html = ''; $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; @@ -65,7 +65,7 @@ class Date { $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; $html .= $this->rendererHelper->getInputValidation($block, [ 'required-message' => __('Please select a day', 'mailpoet'), - ]); + ], $formId); $html .= 'name="' . $fieldName . '[day]" placeholder="' . __('Day', 'mailpoet') . '">'; $html .= $this->getDays($block); $html .= ''; @@ -74,7 +74,7 @@ class Date { $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; $html .= $this->rendererHelper->getInputValidation($block, [ 'required-message' => __('Please select a month', 'mailpoet'), - ]); + ], $formId); $html .= 'name="' . $fieldName . '[month]" placeholder="' . __('Month', 'mailpoet') . '">'; $html .= $this->getMonths($block); $html .= ''; @@ -83,14 +83,14 @@ class Date { $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; $html .= $this->rendererHelper->getInputValidation($block, [ 'required-message' => __('Please select a year', 'mailpoet'), - ]); + ], $formId); $html .= 'name="' . $fieldName . '[year]" placeholder="' . __('Year', 'mailpoet') . '">'; $html .= $this->getYears($block); $html .= ''; } } - $html .= ''; + $html .= ''; return $html; } diff --git a/lib/Form/Block/Radio.php b/lib/Form/Block/Radio.php index a01e700479..91bb461036 100644 --- a/lib/Form/Block/Radio.php +++ b/lib/Form/Block/Radio.php @@ -26,11 +26,11 @@ class Radio { $this->wp = $wp; } - public function render(array $block, array $formSettings): string { + public function render(array $block, array $formSettings, ?int $formId = null): string { $html = ''; $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; - $fieldValidation = $this->rendererHelper->getInputValidation($block); + $fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId); $html .= $this->rendererHelper->renderLabel($block, $formSettings); @@ -73,7 +73,7 @@ class Radio { $html .= ''; } - $html .= ''; + $html .= ''; return $this->wrapper->render($block, $html); } diff --git a/lib/Form/Block/Segment.php b/lib/Form/Block/Segment.php index a048f3ef92..4e1089a705 100644 --- a/lib/Form/Block/Segment.php +++ b/lib/Form/Block/Segment.php @@ -32,11 +32,11 @@ class Segment { $this->segmentsRepository = $segmentsRepository; } - public function render(array $block, array $formSettings): string { + public function render(array $block, array $formSettings, ?int $formId = null): string { $html = ''; $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; - $fieldValidation = $this->rendererHelper->getInputValidation($block); + $fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId); $html .= $this->rendererHelper->renderLabel($block, $formSettings); @@ -67,7 +67,7 @@ class Segment { $html .= ''; } - $html .= ''; + $html .= ''; return $this->wrapper->render($block, $html); } diff --git a/lib/Form/BlocksRenderer.php b/lib/Form/BlocksRenderer.php index 832595b05e..00a1ec7bf2 100644 --- a/lib/Form/BlocksRenderer.php +++ b/lib/Form/BlocksRenderer.php @@ -99,7 +99,7 @@ class BlocksRenderer { $this->paragraph = $paragraph; } - public function renderBlock(array $block, array $formSettings): string { + public function renderBlock(array $block, array $formSettings, ?int $formId): string { $html = ''; switch ($block['type']) { case FormEntity::HTML_BLOCK_TYPE: @@ -123,19 +123,19 @@ class BlocksRenderer { break; case FormEntity::CHECKBOX_BLOCK_TYPE: - $html .= $this->checkbox->render($block, $formSettings); + $html .= $this->checkbox->render($block, $formSettings, $formId); break; case FormEntity::RADIO_BLOCK_TYPE: - $html .= $this->radio->render($block, $formSettings); + $html .= $this->radio->render($block, $formSettings, $formId); break; case FormEntity::SEGMENT_SELECTION_BLOCK_TYPE: - $html .= $this->segment->render($block, $formSettings); + $html .= $this->segment->render($block, $formSettings, $formId); break; case FormEntity::DATE_BLOCK_TYPE: - $html .= $this->date->render($block, $formSettings); + $html .= $this->date->render($block, $formSettings, $formId); break; case FormEntity::SELECT_BLOCK_TYPE: diff --git a/lib/Form/Renderer.php b/lib/Form/Renderer.php index e77a8f6044..6b1f73a89a 100644 --- a/lib/Form/Renderer.php +++ b/lib/Form/Renderer.php @@ -44,7 +44,7 @@ class Renderer { public function renderHTML(FormEntity $form = null): string { if (($form instanceof FormEntity) && !empty($form->getBody()) && is_array($form->getSettings())) { - return $this->renderBlocks($form->getBody(), $form->getSettings() ?? []); + return $this->renderBlocks($form->getBody(), $form->getSettings() ?? [], $form->getId()); } return ''; } @@ -57,7 +57,13 @@ class Renderer { } } - public function renderBlocks(array $blocks = [], array $formSettings = [], bool $honeypotEnabled = true, bool $captchaEnabled = true): string { + public function renderBlocks( + array $blocks = [], + array $formSettings = [], + ?int $formId = null, + bool $honeypotEnabled = true, + bool $captchaEnabled = true + ): string { // add honeypot for spambots $html = ($honeypotEnabled) ? $this->renderHoneypot() : ''; foreach ($blocks as $key => $block) { @@ -69,9 +75,9 @@ class Renderer { } if (in_array($block['type'], [FormEntity::COLUMN_BLOCK_TYPE, FormEntity::COLUMNS_BLOCK_TYPE])) { $blocks = $block['body'] ?? []; - $html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, false)) . PHP_EOL; + $html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, $formId, false)) . PHP_EOL; } else { - $html .= $this->blocksRenderer->renderBlock($block, $formSettings) . PHP_EOL; + $html .= $this->blocksRenderer->renderBlock($block, $formSettings, $formId) . PHP_EOL; } } return $html; diff --git a/lib/Subscription/CaptchaRenderer.php b/lib/Subscription/CaptchaRenderer.php index 25e552fe9a..70ef8d9bbf 100644 --- a/lib/Subscription/CaptchaRenderer.php +++ b/lib/Subscription/CaptchaRenderer.php @@ -120,7 +120,7 @@ class CaptchaRenderer { $formHtml .= '

'; // subscription form - $formHtml .= $this->formRenderer->renderBlocks($form, [], $honeypot = false); + $formHtml .= $this->formRenderer->renderBlocks($form, [], null, $honeypot = false); $formHtml .= ''; $formHtml .= $this->renderFormMessages($formModel, $showSuccessMessage, $showErrorMessage); $formHtml .= ''; diff --git a/lib/Subscription/ManageSubscriptionFormRenderer.php b/lib/Subscription/ManageSubscriptionFormRenderer.php index 963aa88b2c..a909324fe4 100644 --- a/lib/Subscription/ManageSubscriptionFormRenderer.php +++ b/lib/Subscription/ManageSubscriptionFormRenderer.php @@ -106,7 +106,7 @@ class ManageSubscriptionFormRenderer { 'email' => $subscriber->email, 'token' => $this->linkTokens->getToken($subscriberEntity), 'editEmailInfo' => __('Need to change your email address? Unsubscribe here, then simply sign up again.', 'mailpoet'), - 'formHtml' => $this->formRenderer->renderBlocks($form, [], $honeypot = false, $captcha = false), + 'formHtml' => $this->formRenderer->renderBlocks($form, [], null, $honeypot = false, $captcha = false), 'formState' => $formState, ]; diff --git a/tests/unit/Form/RendererTest.php b/tests/unit/Form/RendererTest.php index ff20e2265a..793dba0542 100644 --- a/tests/unit/Form/RendererTest.php +++ b/tests/unit/Form/RendererTest.php @@ -97,7 +97,7 @@ class RendererTest extends \MailPoetUnitTest { ->method('get') ->with('captcha.type') ->willReturn(Captcha::TYPE_DISABLED); - $html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'), [], false); + $html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'), [], null, false); $hpLabel = $this->htmlParser->findByXpath($html, "//label[@class='mailpoet_hp_email_label']"); expect($hpLabel->length)->equals(0); $recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']");