Refactor form blocks to use composition instead of inheritance
[MAILPOET-2665]
This commit is contained in:
committed by
Jack Kitterhing
parent
20926d56b7
commit
c6ca749cdc
@ -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);
|
||||
|
@ -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']) {
|
||||
|
@ -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 .= '<p class="mailpoet_paragraph">';
|
||||
|
||||
$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 .= '<label class="mailpoet_checkbox_label">';
|
||||
@ -39,7 +52,7 @@ class Checkbox extends Base {
|
||||
|
||||
$html .= $fieldValidation;
|
||||
|
||||
$html .= ' /> ' . esc_attr($option['value']);
|
||||
$html .= ' /> ' . $this->wp->escAttr($option['value']);
|
||||
|
||||
$html .= '</label>';
|
||||
}
|
||||
|
@ -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 .= '<p class="mailpoet_paragraph">';
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
$html .= $this->renderDateSelect($block);
|
||||
$html .= '</p>';
|
||||
|
||||
@ -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 .= '<select class="mailpoet_date_day" ';
|
||||
$html .= $this->getInputValidation($block, [
|
||||
'required-message' => WPFunctions::get()->__('Please select a day', 'mailpoet'),
|
||||
$html .= $this->baseRenderer->getInputValidation($block, [
|
||||
'required-message' => __('Please select a day', 'mailpoet'),
|
||||
]);
|
||||
$html .= 'name="' . $fieldName . '[day]" placeholder="' . __('Day', 'mailpoet') . '">';
|
||||
$html .= $this->getDays($block);
|
||||
$html .= '</select>';
|
||||
} else if ($dateSelector === 'MM') {
|
||||
$html .= '<select class="mailpoet_select mailpoet_date_month" ';
|
||||
$html .= $this->getInputValidation($block, [
|
||||
'required-message' => WPFunctions::get()->__('Please select a month', 'mailpoet'),
|
||||
$html .= $this->baseRenderer->getInputValidation($block, [
|
||||
'required-message' => __('Please select a month', 'mailpoet'),
|
||||
]);
|
||||
$html .= 'name="' . $fieldName . '[month]" placeholder="' . __('Month', 'mailpoet') . '">';
|
||||
$html .= $this->getMonths($block);
|
||||
$html .= '</select>';
|
||||
} else if ($dateSelector === 'YYYY') {
|
||||
$html .= '<select class="mailpoet_date_year" ';
|
||||
$html .= $this->getInputValidation($block, [
|
||||
'required-message' => WPFunctions::get()->__('Please select a year', 'mailpoet'),
|
||||
$html .= $this->baseRenderer->getInputValidation($block, [
|
||||
'required-message' => __('Please select a year', 'mailpoet'),
|
||||
]);
|
||||
$html .= 'name="' . $fieldName . '[year]" placeholder="' . __('Year', 'mailpoet') . '">';
|
||||
$html .= $this->getYears($block);
|
||||
@ -70,10 +75,10 @@ class Date extends Base {
|
||||
|
||||
public function getDateTypes() {
|
||||
return [
|
||||
'year_month_day' => WPFunctions::get()->__('Year, month, day', 'mailpoet'),
|
||||
'year_month' => WPFunctions::get()->__('Year, month', 'mailpoet'),
|
||||
'month' => WPFunctions::get()->__('Month (January, February,...)', 'mailpoet'),
|
||||
'year' => WPFunctions::get()->__('Year', 'mailpoet'),
|
||||
'year_month_day' => __('Year, month, day', 'mailpoet'),
|
||||
'year_month' => __('Year, month', 'mailpoet'),
|
||||
'month' => __('Month (January, February,...)', 'mailpoet'),
|
||||
'year' => __('Year', 'mailpoet'),
|
||||
];
|
||||
}
|
||||
|
||||
@ -86,9 +91,9 @@ class Date extends Base {
|
||||
];
|
||||
}
|
||||
public function getMonthNames() {
|
||||
return [__('January', 'mailpoet'), WPFunctions::get()->__('February', 'mailpoet'), WPFunctions::get()->__('March', 'mailpoet'), WPFunctions::get()->__('April', 'mailpoet'),
|
||||
WPFunctions::get()->__('May', 'mailpoet'), WPFunctions::get()->__('June', 'mailpoet'), WPFunctions::get()->__('July', 'mailpoet'), WPFunctions::get()->__('August', 'mailpoet'), WPFunctions::get()->__('September', 'mailpoet'),
|
||||
WPFunctions::get()->__('October', 'mailpoet'), WPFunctions::get()->__('November', 'mailpoet'), WPFunctions::get()->__('December', 'mailpoet'),
|
||||
return [__('January', 'mailpoet'), __('February', 'mailpoet'), __('March', 'mailpoet'), __('April', 'mailpoet'),
|
||||
__('May', 'mailpoet'), __('June', 'mailpoet'), __('July', 'mailpoet'), __('August', 'mailpoet'), __('September', 'mailpoet'),
|
||||
__('October', 'mailpoet'), __('November', 'mailpoet'), __('December', 'mailpoet'),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -2,24 +2,31 @@
|
||||
|
||||
namespace MailPoet\Form\Block;
|
||||
|
||||
class Radio extends Base {
|
||||
class Radio {
|
||||
|
||||
/** @var Base */
|
||||
private $baseRenderer;
|
||||
|
||||
public function __construct(Base $baseRenderer) {
|
||||
$this->baseRenderer = $baseRenderer;
|
||||
}
|
||||
|
||||
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 .= '<p class="mailpoet_paragraph">';
|
||||
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
|
||||
$options = (!empty($block['params']['values'])
|
||||
? $block['params']['values']
|
||||
: []
|
||||
);
|
||||
|
||||
$selectedValue = $this->getFieldValue($block);
|
||||
$selectedValue = $this->baseRenderer->getFieldValue($block);
|
||||
|
||||
foreach ($options as $option) {
|
||||
$html .= '<label class="mailpoet_radio_label">';
|
||||
|
@ -2,17 +2,24 @@
|
||||
|
||||
namespace MailPoet\Form\Block;
|
||||
|
||||
class Segment extends Base {
|
||||
class Segment {
|
||||
|
||||
/** @var Base */
|
||||
private $baseRenderer;
|
||||
|
||||
public function __construct(Base $baseRenderer) {
|
||||
$this->baseRenderer = $baseRenderer;
|
||||
}
|
||||
|
||||
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 .= '<p class="mailpoet_paragraph">';
|
||||
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
|
||||
$options = (!empty($block['params']['values'])
|
||||
? $block['params']['values']
|
||||
|
@ -2,19 +2,32 @@
|
||||
|
||||
namespace MailPoet\Form\Block;
|
||||
|
||||
class Select extends Base {
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class Select {
|
||||
|
||||
/** @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) . ']';
|
||||
$fieldName = 'data[' . $this->baseRenderer->getFieldName($block) . ']';
|
||||
$automationId = ($block['id'] == 'status') ? 'data-automation-id="form_status"' : '';
|
||||
$html .= '<p class="mailpoet_paragraph">';
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
$html .= '<select class="mailpoet_select" name="' . $fieldName . '" ' . $automationId . '>';
|
||||
|
||||
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"' : '';
|
||||
|
@ -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 .= '<p class="mailpoet_paragraph"><input type="submit" class="mailpoet_submit" ';
|
||||
|
||||
$html .= 'value="' . $this->getFieldLabel($block) . '" ';
|
||||
$html .= 'value="' . $this->baseRenderer->getFieldLabel($block) . '" ';
|
||||
|
||||
$html .= 'data-automation-id="subscribe-submit-button" ';
|
||||
|
||||
|
@ -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 = '<p class="mailpoet_paragraph">';
|
||||
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
|
||||
$html .= '<input type="' . $type . '" class="mailpoet_text" ';
|
||||
|
||||
$html .= 'name="data[' . $this->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 .= '/>';
|
||||
|
||||
|
@ -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 .= '<p class="mailpoet_paragraph">';
|
||||
|
||||
$html .= $this->renderLabel($block);
|
||||
$html .= $this->baseRenderer->renderLabel($block);
|
||||
|
||||
$lines = (isset($block['params']['lines']) ? (int)$block['params']['lines'] : 1);
|
||||
|
||||
$html .= '<textarea class="mailpoet_textarea" rows="' . $lines . '" ';
|
||||
|
||||
$html .= 'name="data[' . $this->getFieldName($block) . ']"';
|
||||
$html .= 'name="data[' . $this->baseRenderer->getFieldName($block) . ']"';
|
||||
|
||||
$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 .= '>' . $this->getFieldValue($block) . '</textarea>';
|
||||
$html .= '>' . $this->baseRenderer->getFieldValue($block) . '</textarea>';
|
||||
|
||||
$html .= '</p>';
|
||||
|
||||
|
@ -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('<label></label>'));
|
||||
$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',
|
||||
|
Reference in New Issue
Block a user