diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index 24e0e9825a..4609fbe35f 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -172,6 +172,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Form\FormsRepository::class); $container->autowire(\MailPoet\Form\Renderer::class); $container->autowire(\MailPoet\Form\BlocksRenderer::class); + $container->autowire(\MailPoet\Form\Block\Base::class); $container->autowire(\MailPoet\Form\Block\Checkbox::class); $container->autowire(\MailPoet\Form\Block\Date::class); $container->autowire(\MailPoet\Form\Block\Divider::class); diff --git a/lib/Form/Block/Base.php b/lib/Form/Block/Base.php index a779fbba1e..0b65d6c691 100644 --- a/lib/Form/Block/Base.php +++ b/lib/Form/Block/Base.php @@ -6,7 +6,7 @@ use MailPoet\Form\Util\FieldNameObfuscator; use MailPoet\Models\ModelValidator; use MailPoet\WP\Functions as WPFunctions; -abstract class Base { +class Base { /** @var FieldNameObfuscator */ private $fieldNameObfuscator; @@ -19,7 +19,7 @@ abstract class Base { $this->wp = $wp; } - protected function getInputValidation($block, $extraRules = []) { + public function getInputValidation($block, $extraRules = []) { $rules = []; if ($block['id'] === 'email') { @@ -78,7 +78,7 @@ abstract class Base { return join(' ', $validation); } - protected function renderLabel($block) { + public function renderLabel($block) { $html = ''; if ( isset($block['params']['hide_label']) @@ -106,7 +106,7 @@ abstract class Base { return $html; } - protected function renderInputPlaceholder($block) { + public function renderInputPlaceholder($block) { $html = ''; // if the label is displayed as a placeholder, if ( @@ -126,7 +126,7 @@ abstract class Base { } // return field name depending on block data - protected function getFieldName($block = []) { + public function getFieldName($block = []) { if ((int)$block['id'] > 0) { return 'cf_' . $block['id']; } elseif (isset($block['params']['obfuscate']) && !$block['params']['obfuscate']) { @@ -136,19 +136,19 @@ abstract class Base { } } - protected function getFieldLabel($block = []) { + public function getFieldLabel($block = []) { return (isset($block['params']['label']) && strlen(trim($block['params']['label'])) > 0) ? trim($block['params']['label']) : ''; } - protected function getFieldValue($block = []) { + public function getFieldValue($block = []) { return (isset($block['params']['value']) && strlen(trim($block['params']['value'])) > 0) ? $this->wp->escAttr(trim($block['params']['value'])) : ''; } - protected function getInputModifiers($block = []) { + public function getInputModifiers($block = []) { $modifiers = []; if (isset($block['params']['readonly']) && $block['params']['readonly']) { diff --git a/lib/Form/Block/Checkbox.php b/lib/Form/Block/Checkbox.php index 1e53022cf8..3bcf0d7560 100644 --- a/lib/Form/Block/Checkbox.php +++ b/lib/Form/Block/Checkbox.php @@ -2,24 +2,37 @@ namespace MailPoet\Form\Block; -class Checkbox extends Base { +use MailPoet\WP\Functions as WPFunctions; + +class Checkbox { + + /** @var Base */ + private $baseRenderer; + + /** @var WPFunctions */ + private $wp; + + public function __construct(Base $baseRenderer, WPFunctions $wp) { + $this->baseRenderer = $baseRenderer; + $this->wp = $wp; + } public function render($block) { $html = ''; - $fieldName = 'data[' . $this->getFieldName($block) . ']'; - $fieldValidation = $this->getInputValidation($block); + $fieldName = 'data[' . $this->baseRenderer->getFieldName($block) . ']'; + $fieldValidation = $this->baseRenderer->getInputValidation($block); $html .= '

'; - $html .= $this->renderLabel($block); + $html .= $this->baseRenderer->renderLabel($block); $options = (!empty($block['params']['values']) ? $block['params']['values'] : [] ); - $selectedValue = self::getFieldValue($block); + $selectedValue = $this->baseRenderer->getFieldValue($block); foreach ($options as $option) { $html .= ''; } diff --git a/lib/Form/Block/Date.php b/lib/Form/Block/Date.php index 14bd94d224..43394f01d6 100644 --- a/lib/Form/Block/Date.php +++ b/lib/Form/Block/Date.php @@ -2,14 +2,19 @@ namespace MailPoet\Form\Block; -use MailPoet\WP\Functions as WPFunctions; +class Date { -class Date extends Base { + /** @var Base */ + private $baseRenderer; + + public function __construct(Base $baseRenderer) { + $this->baseRenderer = $baseRenderer; + } public function render($block) { $html = ''; $html .= '

'; - $html .= $this->renderLabel($block); + $html .= $this->baseRenderer->renderLabel($block); $html .= $this->renderDateSelect($block); $html .= '

'; @@ -19,7 +24,7 @@ class Date extends Base { private function renderDateSelect($block = []) { $html = ''; - $fieldName = 'data[' . $this->getFieldName($block) . ']'; + $fieldName = 'data[' . $this->baseRenderer->getFieldName($block) . ']'; $dateFormats = $this->getDateFormats(); @@ -38,24 +43,24 @@ class Date extends Base { foreach ($dateSelectors as $dateSelector) { if ($dateSelector === 'DD') { $html .= ''; } else if ($dateSelector === 'MM') { $html .= ''; } else if ($dateSelector === 'YYYY') { $html .= ''; if (isset($block['params']['label_within']) && $block['params']['label_within']) { - $label = $this->getFieldLabel($block); + $label = $this->baseRenderer->getFieldLabel($block); if (!empty($block['params']['required'])) { $label .= ' *'; } @@ -38,7 +51,7 @@ class Select extends Base { $isSelected = ( (isset($option['is_checked']) && $option['is_checked']) || - (self::getFieldValue($block) === $option['value']) + ($this->baseRenderer->getFieldValue($block) === $option['value']) ) ? ' selected="selected"' : ''; $isDisabled = (!empty($option['is_disabled'])) ? ' disabled="disabled"' : ''; diff --git a/lib/Form/Block/Submit.php b/lib/Form/Block/Submit.php index c8b45ddd37..a1bccb6608 100644 --- a/lib/Form/Block/Submit.php +++ b/lib/Form/Block/Submit.php @@ -2,14 +2,21 @@ namespace MailPoet\Form\Block; -class Submit extends Base { +class Submit { + + /** @var Base */ + private $baseRenderer; + + public function __construct(Base $baseRenderer) { + $this->baseRenderer = $baseRenderer; + } public function render($block) { $html = ''; $html .= '

getFieldLabel($block) . '" '; + $html .= 'value="' . $this->baseRenderer->getFieldLabel($block) . '" '; $html .= 'data-automation-id="subscribe-submit-button" '; diff --git a/lib/Form/Block/Text.php b/lib/Form/Block/Text.php index acc4a7cd57..8ed106ea43 100644 --- a/lib/Form/Block/Text.php +++ b/lib/Form/Block/Text.php @@ -2,7 +2,14 @@ namespace MailPoet\Form\Block; -class Text extends Base { +class Text { + + /** @var Base */ + private $baseRenderer; + + public function __construct(Base $baseRenderer) { + $this->baseRenderer = $baseRenderer; + } public function render($block) { $type = 'text'; @@ -14,23 +21,23 @@ class Text extends Base { $html = '

'; - $html .= $this->renderLabel($block); + $html .= $this->baseRenderer->renderLabel($block); $html .= 'getFieldName($block) . ']" '; + $html .= 'name="data[' . $this->baseRenderer->getFieldName($block) . ']" '; - $html .= 'title="' . $this->getFieldLabel($block) . '" '; + $html .= 'title="' . $this->baseRenderer->getFieldLabel($block) . '" '; - $html .= 'value="' . $this->getFieldValue($block) . '" '; + $html .= 'value="' . $this->baseRenderer->getFieldValue($block) . '" '; $html .= $automationId; - $html .= $this->renderInputPlaceholder($block); + $html .= $this->baseRenderer->renderInputPlaceholder($block); - $html .= $this->getInputValidation($block); + $html .= $this->baseRenderer->getInputValidation($block); - $html .= $this->getInputModifiers($block); + $html .= $this->baseRenderer->getInputModifiers($block); $html .= '/>'; diff --git a/lib/Form/Block/Textarea.php b/lib/Form/Block/Textarea.php index cea6ba0503..a241b39e9a 100644 --- a/lib/Form/Block/Textarea.php +++ b/lib/Form/Block/Textarea.php @@ -2,27 +2,34 @@ namespace MailPoet\Form\Block; -class Textarea extends Base { +class Textarea { + /** @var Base */ + private $baseRenderer; + + public function __construct(Base $baseRenderer) { + $this->baseRenderer = $baseRenderer; + } + public function render($block) { $html = ''; $html .= '

'; - $html .= $this->renderLabel($block); + $html .= $this->baseRenderer->renderLabel($block); $lines = (isset($block['params']['lines']) ? (int)$block['params']['lines'] : 1); $html .= ''; + $html .= '>' . $this->baseRenderer->getFieldValue($block) . ''; $html .= '

'; diff --git a/tests/unit/Form/Block/SelectTest.php b/tests/unit/Form/Block/SelectTest.php index b4faf7c817..cb63b27699 100644 --- a/tests/unit/Form/Block/SelectTest.php +++ b/tests/unit/Form/Block/SelectTest.php @@ -2,8 +2,8 @@ namespace MailPoet\Test\Form\Block; +use MailPoet\Form\Block\Base; use MailPoet\Form\Block\Select; -use MailPoet\Form\Util\FieldNameObfuscator; use MailPoet\Models\Subscriber; use MailPoet\WP\Functions; use PHPUnit\Framework\MockObject\MockObject; @@ -18,16 +18,19 @@ class SelectTest extends \MailPoetUnitTest { /** @var MockObject | Functions */ private $wpMock; - /** @var MockObject | FieldNameObfuscator */ - private $fieldNameObfuscatorMock; + /** @var MockObject | Base */ + private $baseMock; public function _before() { parent::_before(); $this->wpMock = $this->createMock(Functions::class); $this->wpMock->method('escAttr')->will($this->returnArgument(0)); - $this->fieldNameObfuscatorMock = $this->createMock(FieldNameObfuscator::class); - $this->fieldNameObfuscatorMock->method('obfuscate')->will($this->returnArgument(0)); - $this->selectBlock = new Select($this->fieldNameObfuscatorMock, $this->wpMock); + $this->baseMock = $this->createMock(Base::class); + $this->baseMock->method('getFieldName')->will($this->returnValue('select')); + $this->baseMock->method('renderLabel')->will($this->returnValue('')); + $this->baseMock->method('getFieldLabel')->will($this->returnValue('Field label')); + $this->baseMock->method('getFieldValue')->will($this->returnValue('1')); + $this->selectBlock = new Select($this->baseMock, $this->wpMock); $this->block = [ 'id' => 'status', 'type' => 'select',