diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index a65c6aed7c..24e0e9825a 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -172,6 +172,16 @@ 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\Checkbox::class); + $container->autowire(\MailPoet\Form\Block\Date::class); + $container->autowire(\MailPoet\Form\Block\Divider::class); + $container->autowire(\MailPoet\Form\Block\Html::class); + $container->autowire(\MailPoet\Form\Block\Radio::class); + $container->autowire(\MailPoet\Form\Block\Segment::class); + $container->autowire(\MailPoet\Form\Block\Select::class); + $container->autowire(\MailPoet\Form\Block\Submit::class); + $container->autowire(\MailPoet\Form\Block\Text::class); + $container->autowire(\MailPoet\Form\Block\Textarea::class); $container->autowire(\MailPoet\Form\Util\Styles::class); // Helpscout $container->autowire(\MailPoet\Helpscout\Beacon::class); diff --git a/lib/Form/Block/Base.php b/lib/Form/Block/Base.php index 2778848990..f838ccbaac 100644 --- a/lib/Form/Block/Base.php +++ b/lib/Form/Block/Base.php @@ -7,7 +7,7 @@ use MailPoet\Models\ModelValidator; use MailPoet\WP\Functions as WPFunctions; abstract class Base { - protected static function getInputValidation($block, $extraRules = []) { + protected function getInputValidation($block, $extraRules = []) { $rules = []; if ($block['id'] === 'email') { @@ -66,7 +66,7 @@ abstract class Base { return join(' ', $validation); } - protected static function renderLabel($block) { + protected function renderLabel($block) { $html = ''; if ( isset($block['params']['hide_label']) @@ -94,7 +94,7 @@ abstract class Base { return $html; } - protected static function renderInputPlaceholder($block) { + protected function renderInputPlaceholder($block) { $html = ''; // if the label is displayed as a placeholder, if ( @@ -114,7 +114,7 @@ abstract class Base { } // return field name depending on block data - protected static function getFieldName($block = []) { + protected function getFieldName($block = []) { if ((int)$block['id'] > 0) { return 'cf_' . $block['id']; } elseif (isset($block['params']['obfuscate']) && !$block['params']['obfuscate']) { @@ -125,19 +125,19 @@ abstract class Base { } } - protected static function getFieldLabel($block = []) { + protected function getFieldLabel($block = []) { return (isset($block['params']['label']) && strlen(trim($block['params']['label'])) > 0) ? trim($block['params']['label']) : ''; } - protected static function getFieldValue($block = []) { + protected function getFieldValue($block = []) { return (isset($block['params']['value']) && strlen(trim($block['params']['value'])) > 0) ? WPFunctions::get()->escAttr(trim($block['params']['value'])) : ''; } - protected static function getInputModifiers($block = []) { + protected 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 ae2347f6cc..1e53022cf8 100644 --- a/lib/Form/Block/Checkbox.php +++ b/lib/Form/Block/Checkbox.php @@ -4,15 +4,15 @@ namespace MailPoet\Form\Block; class Checkbox extends Base { - public static function render($block) { + public function render($block) { $html = ''; - $fieldName = 'data[' . static::getFieldName($block) . ']'; - $fieldValidation = static::getInputValidation($block); + $fieldName = 'data[' . $this->getFieldName($block) . ']'; + $fieldValidation = $this->getInputValidation($block); $html .= '

'; - $html .= static::renderLabel($block); + $html .= $this->renderLabel($block); $options = (!empty($block['params']['values']) ? $block['params']['values'] diff --git a/lib/Form/Block/Date.php b/lib/Form/Block/Date.php index 24811550c6..aa02441cbf 100644 --- a/lib/Form/Block/Date.php +++ b/lib/Form/Block/Date.php @@ -7,22 +7,22 @@ use MailPoetVendor\Carbon\Carbon; class Date extends Base { - public static function render($block) { + public function render($block) { $html = ''; $html .= '

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

'; return $html; } - private static function renderDateSelect($block = []) { + private function renderDateSelect($block = []) { $html = ''; - $fieldName = 'data[' . static::getFieldName($block) . ']'; + $fieldName = 'data[' . $this->getFieldName($block) . ']'; - $dateFormats = static::getDateFormats(); + $dateFormats = $this->getDateFormats(); // automatically select first date format $dateFormat = $dateFormats[$block['params']['date_type']][0]; @@ -39,27 +39,27 @@ class Date extends Base { foreach ($dateSelectors as $dateSelector) { if ($dateSelector === 'DD') { $html .= ''; } else if ($dateSelector === 'MM') { $html .= ''; } else if ($dateSelector === 'YYYY') { $html .= ''; } } @@ -69,7 +69,7 @@ class Date extends Base { return $html; } - public static function getDateTypes() { + public function getDateTypes() { return [ 'year_month_day' => WPFunctions::get()->__('Year, month, day', 'mailpoet'), 'year_month' => WPFunctions::get()->__('Year, month', 'mailpoet'), @@ -78,7 +78,7 @@ class Date extends Base { ]; } - public static function getDateFormats() { + public function getDateFormats() { return [ 'year_month_day' => ['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD'], 'year_month' => ['MM/YYYY', 'YYYY/MM'], @@ -86,14 +86,14 @@ class Date extends Base { 'month' => ['MM'], ]; } - public static function getMonthNames() { + 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'), ]; } - public static function getMonths($block = []) { + private function getMonths($block = []) { $defaults = [ 'selected' => null, ]; @@ -105,7 +105,7 @@ class Date extends Base { // merge block with defaults $block = array_merge($defaults, $block); - $monthNames = static::getMonthNames(); + $monthNames = $this->getMonthNames(); $html = ''; @@ -122,7 +122,7 @@ class Date extends Base { return $html; } - public static function getYears($block = []) { + private function getYears($block = []) { $defaults = [ 'selected' => null, 'from' => (int)strftime('%Y') - 100, @@ -151,7 +151,7 @@ class Date extends Base { return $html; } - public static function getDays($block = []) { + private function getDays($block = []) { $defaults = [ 'selected' => null, ]; @@ -177,7 +177,7 @@ class Date extends Base { return $html; } - public static function convertDateToDatetime($date, $dateFormat) { + public function convertDateToDatetime($date, $dateFormat) { $datetime = false; if ($dateFormat === 'datetime') { $datetime = $date; diff --git a/lib/Form/Block/Divider.php b/lib/Form/Block/Divider.php index f3605ddf1f..a5e3f35fc2 100644 --- a/lib/Form/Block/Divider.php +++ b/lib/Form/Block/Divider.php @@ -4,7 +4,7 @@ namespace MailPoet\Form\Block; class Divider { - public static function render() { + public function render() { return '
'; } -} \ No newline at end of file +} diff --git a/lib/Form/Block/Html.php b/lib/Form/Block/Html.php index 7710e7926a..d0a8146362 100644 --- a/lib/Form/Block/Html.php +++ b/lib/Form/Block/Html.php @@ -4,7 +4,7 @@ namespace MailPoet\Form\Block; class Html { - public static function render($block) { + public function render($block) { $html = ''; $text = ''; diff --git a/lib/Form/Block/Radio.php b/lib/Form/Block/Radio.php index 5935fa1401..230812b247 100644 --- a/lib/Form/Block/Radio.php +++ b/lib/Form/Block/Radio.php @@ -4,22 +4,22 @@ namespace MailPoet\Form\Block; class Radio extends Base { - public static function render($block) { + public function render($block) { $html = ''; - $fieldName = 'data[' . static::getFieldName($block) . ']'; - $fieldValidation = static::getInputValidation($block); + $fieldName = 'data[' . $this->getFieldName($block) . ']'; + $fieldValidation = $this->getInputValidation($block); $html .= '

'; - $html .= static::renderLabel($block); + $html .= $this->renderLabel($block); $options = (!empty($block['params']['values']) ? $block['params']['values'] : [] ); - $selectedValue = self::getFieldValue($block); + $selectedValue = $this->getFieldValue($block); foreach ($options as $option) { $html .= '

'; - $html .= static::renderLabel($block); + $html .= $this->renderLabel($block); $options = (!empty($block['params']['values']) ? $block['params']['values'] diff --git a/lib/Form/Block/Select.php b/lib/Form/Block/Select.php index 326191e8e8..57407ef24d 100644 --- a/lib/Form/Block/Select.php +++ b/lib/Form/Block/Select.php @@ -6,18 +6,18 @@ use MailPoet\WP\Functions as WPFunctions; class Select extends Base { - public static function render($block) { + public function render($block) { $html = ''; - $fieldName = 'data[' . static::getFieldName($block) . ']'; - $fieldValidation = static::getInputValidation($block); + $fieldName = 'data[' . $this->getFieldName($block) . ']'; + $fieldValidation = $this->getInputValidation($block); $automationId = ($block['id'] == 'status') ? 'data-automation-id="form_status"' : ''; $html .= '

'; - $html .= static::renderLabel($block); + $html .= $this->renderLabel($block); $html .= 'getFieldLabel($block) . '" '; $html .= 'data-automation-id="subscribe-submit-button" '; diff --git a/lib/Form/Block/Text.php b/lib/Form/Block/Text.php index da460c525b..acc4a7cd57 100644 --- a/lib/Form/Block/Text.php +++ b/lib/Form/Block/Text.php @@ -4,7 +4,7 @@ namespace MailPoet\Form\Block; class Text extends Base { - public static function render($block) { + public function render($block) { $type = 'text'; $automationId = ' '; if ($block['id'] === 'email') { @@ -14,23 +14,23 @@ class Text extends Base { $html = '

'; - $html .= static::renderLabel($block); + $html .= $this->renderLabel($block); $html .= 'getFieldName($block) . ']" '; - $html .= 'title="' . static::getFieldLabel($block) . '" '; + $html .= 'title="' . $this->getFieldLabel($block) . '" '; - $html .= 'value="' . static::getFieldValue($block) . '" '; + $html .= 'value="' . $this->getFieldValue($block) . '" '; $html .= $automationId; - $html .= static::renderInputPlaceholder($block); + $html .= $this->renderInputPlaceholder($block); - $html .= static::getInputValidation($block); + $html .= $this->getInputValidation($block); - $html .= static::getInputModifiers($block); + $html .= $this->getInputModifiers($block); $html .= '/>'; diff --git a/lib/Form/Block/Textarea.php b/lib/Form/Block/Textarea.php index d256839369..cea6ba0503 100644 --- a/lib/Form/Block/Textarea.php +++ b/lib/Form/Block/Textarea.php @@ -3,26 +3,26 @@ namespace MailPoet\Form\Block; class Textarea extends Base { - public static function render($block) { + public function render($block) { $html = ''; $html .= '

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

'; diff --git a/lib/Form/BlocksRenderer.php b/lib/Form/BlocksRenderer.php index 20d55598ab..fd05fe3b63 100644 --- a/lib/Form/BlocksRenderer.php +++ b/lib/Form/BlocksRenderer.php @@ -2,48 +2,113 @@ namespace MailPoet\Form; +use MailPoet\Form\Block\Checkbox; +use MailPoet\Form\Block\Date; +use MailPoet\Form\Block\Divider; +use MailPoet\Form\Block\Html; +use MailPoet\Form\Block\Radio; +use MailPoet\Form\Block\Segment; +use MailPoet\Form\Block\Select; +use MailPoet\Form\Block\Submit; +use MailPoet\Form\Block\Text; +use MailPoet\Form\Block\Textarea; + class BlocksRenderer { + /** @var Checkbox */ + private $checkbox; + + /** @var Date */ + private $date; + + /** @var Divider */ + private $divider; + + /** @var Html */ + private $html; + + /** @var Radio */ + private $radio; + + /** @var Segment */ + private $segment; + + /** @var Select */ + private $select; + + /** @var Submit */ + private $submit; + + /** @var Text */ + private $text; + + /** @var Textarea */ + private $textarea; + + public function __construct( + Checkbox $checkbox, + Date $date, + Divider $divider, + Html $html, + Radio $radio, + Segment $segment, + Select $select, + Submit $submit, + Text $text, + Textarea $textarea + ) { + $this->checkbox = $checkbox; + $this->date = $date; + $this->divider = $divider; + $this->html = $html; + $this->radio = $radio; + $this->segment = $segment; + $this->select = $select; + $this->submit = $submit; + $this->text = $text; + $this->textarea = $textarea; + } + public function renderBlock(array $block = []): string { $html = ''; switch ($block['type']) { case 'html': - $html .= Block\Html::render($block); + $html .= $this->html->render($block); break; case 'divider': - $html .= Block\Divider::render(); + $html .= $this->divider->render(); break; case 'checkbox': - $html .= Block\Checkbox::render($block); + $html .= $this->checkbox->render($block); break; case 'radio': - $html .= Block\Radio::render($block); + $html .= $this->radio->render($block); break; case 'segment': - $html .= Block\Segment::render($block); + $html .= $this->segment->render($block); break; case 'date': - $html .= Block\Date::render($block); + $html .= $this->date->render($block); break; case 'select': - $html .= Block\Select::render($block); + $html .= $this->select->render($block); break; case 'text': - $html .= Block\Text::render($block); + $html .= $this->text->render($block); break; case 'textarea': - $html .= Block\Textarea::render($block); + $html .= $this->textarea->render($block); break; case 'submit': - $html .= Block\Submit::render($block); + $html .= $this->submit->render($block); break; } return $html; diff --git a/tests/unit/Form/Block/DateTest.php b/tests/unit/Form/Block/DateTest.php index 0b6d7d806a..f617f7afbc 100644 --- a/tests/unit/Form/Block/DateTest.php +++ b/tests/unit/Form/Block/DateTest.php @@ -5,6 +5,15 @@ namespace MailPoet\Test\Form\Block; use MailPoet\Form\Block\Date; class DateTest extends \MailPoetUnitTest { + + /** @var Date */ + private $date; + + public function _before() { + parent::_before(); + $this->date = new Date(); + } + public function testItCanConvertDateMonthYearFormatToDatetime() { $date = [ 'MM/DD/YYYY' => '05/10/2016', @@ -13,7 +22,7 @@ class DateTest extends \MailPoetUnitTest { 'YYYY/DD/MM' => '2016/10/05', ]; foreach ($date as $dateFormat => $date) { - expect(Date::convertDateToDatetime($date, $dateFormat)) + expect($this->date->convertDateToDatetime($date, $dateFormat)) ->equals('2016-05-10 00:00:00'); } } @@ -24,37 +33,37 @@ class DateTest extends \MailPoetUnitTest { 'YYYY/MM' => '2016/05', ]; foreach ($date as $dateFormat => $date) { - expect(Date::convertDATEToDatetime($date, $dateFormat)) + expect($this->date->convertDATEToDatetime($date, $dateFormat)) ->equals('2016-05-01 00:00:00'); } } public function testItCanConvertMonthToDatetime() { $currentYear = date('Y'); - expect(Date::convertDateToDatetime('05', 'MM')) + expect($this->date->convertDateToDatetime('05', 'MM')) ->equals(sprintf('%s-05-01 00:00:00', $currentYear)); } public function testItCanConvertYearToDatetime() { - expect(Date::convertDateToDatetime('2016', 'YYYY')) + expect($this->date->convertDateToDatetime('2016', 'YYYY')) ->equals('2016-01-01 00:00:00'); } public function testItCanConvertDatetimeToDatetime() { - expect(Date::convertDateToDatetime('2016-05-10 00:00:00', 'datetime')) + expect($this->date->convertDateToDatetime('2016-05-10 00:00:00', 'datetime')) ->equals('2016-05-10 00:00:00'); } public function testItCanClearDate() { - expect(Date::convertDateToDatetime('0/10/5', 'YYYY/MM/DD')) + expect($this->date->convertDateToDatetime('0/10/5', 'YYYY/MM/DD')) ->equals(date('Y') . '-10-05 00:00:00'); - expect(Date::convertDateToDatetime('0/0/5', 'YYYY/MM/DD')) + expect($this->date->convertDateToDatetime('0/0/5', 'YYYY/MM/DD')) ->equals(date('Y') . '-' . date('m') . '-05 00:00:00'); - expect(Date::convertDateToDatetime('0/0/0', 'YYYY/MM/DD')) + expect($this->date->convertDateToDatetime('0/0/0', 'YYYY/MM/DD')) ->equals(''); - expect(Date::convertDateToDatetime('0', 'YYYY')) + expect($this->date->convertDateToDatetime('0', 'YYYY')) ->equals(''); - expect(Date::convertDateToDatetime('0', 'MM')) + expect($this->date->convertDateToDatetime('0', 'MM')) ->equals(''); } }