Add block renderers to DI container

[MAILPOET-2665]
This commit is contained in:
Rostislav Wolny
2020-01-29 17:05:36 +01:00
committed by Jack Kitterhing
parent d9b9e13a04
commit 6827eb93e6
14 changed files with 169 additions and 85 deletions

View File

@ -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);

View File

@ -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']) {

View File

@ -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 .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= $this->renderLabel($block);
$options = (!empty($block['params']['values'])
? $block['params']['values']

View File

@ -7,22 +7,22 @@ use MailPoetVendor\Carbon\Carbon;
class Date extends Base {
public static function render($block) {
public function render($block) {
$html = '';
$html .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= static::renderDateSelect($block);
$html .= $this->renderLabel($block);
$html .= $this->renderDateSelect($block);
$html .= '</p>';
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 .= '<select class="mailpoet_date_day" ';
$html .= static::getInputValidation($block, [
$html .= $this->getInputValidation($block, [
'required-message' => WPFunctions::get()->__('Please select a day', 'mailpoet'),
]);
$html .= 'name="' . $fieldName . '[day]" placeholder="' . __('Day', 'mailpoet') . '">';
$html .= static::getDays($block);
$html .= $this->getDays($block);
$html .= '</select>';
} else if ($dateSelector === 'MM') {
$html .= '<select class="mailpoet_select mailpoet_date_month" ';
$html .= static::getInputValidation($block, [
$html .= $this->getInputValidation($block, [
'required-message' => WPFunctions::get()->__('Please select a month', 'mailpoet'),
]);
$html .= 'name="' . $fieldName . '[month]" placeholder="' . __('Month', 'mailpoet') . '">';
$html .= static::getMonths($block);
$html .= $this->getMonths($block);
$html .= '</select>';
} else if ($dateSelector === 'YYYY') {
$html .= '<select class="mailpoet_date_year" ';
$html .= static::getInputValidation($block, [
$html .= $this->getInputValidation($block, [
'required-message' => WPFunctions::get()->__('Please select a year', 'mailpoet'),
]);
$html .= 'name="' . $fieldName . '[year]" placeholder="' . __('Year', 'mailpoet') . '">';
$html .= static::getYears($block);
$html .= $this->getYears($block);
$html .= '</select>';
}
}
@ -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;

View File

@ -4,7 +4,7 @@ namespace MailPoet\Form\Block;
class Divider {
public static function render() {
public function render() {
return '<hr class="mailpoet_divider" />';
}
}

View File

@ -4,7 +4,7 @@ namespace MailPoet\Form\Block;
class Html {
public static function render($block) {
public function render($block) {
$html = '';
$text = '';

View File

@ -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 .= '<p class="mailpoet_paragraph">';
$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 .= '<label class="mailpoet_radio_label">';

View File

@ -4,15 +4,15 @@ namespace MailPoet\Form\Block;
class Segment 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 .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= $this->renderLabel($block);
$options = (!empty($block['params']['values'])
? $block['params']['values']

View File

@ -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 .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= $this->renderLabel($block);
$html .= '<select class="mailpoet_select" name="' . $fieldName . '" ' . $automationId . '>';
if (isset($block['params']['label_within']) && $block['params']['label_within']) {
$label = static::getFieldLabel($block);
$label = $this->getFieldLabel($block);
if (!empty($block['params']['required'])) {
$label .= ' *';
}

View File

@ -4,12 +4,12 @@ namespace MailPoet\Form\Block;
class Submit extends Base {
public static function render($block) {
public function render($block) {
$html = '';
$html .= '<p class="mailpoet_paragraph"><input type="submit" class="mailpoet_submit" ';
$html .= 'value="' . static::getFieldLabel($block) . '" ';
$html .= 'value="' . $this->getFieldLabel($block) . '" ';
$html .= 'data-automation-id="subscribe-submit-button" ';

View File

@ -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 = '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= $this->renderLabel($block);
$html .= '<input type="' . $type . '" class="mailpoet_text" ';
$html .= 'name="data[' . static::getFieldName($block) . ']" ';
$html .= 'name="data[' . $this->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 .= '/>';

View File

@ -3,26 +3,26 @@
namespace MailPoet\Form\Block;
class Textarea extends Base {
public static function render($block) {
public function render($block) {
$html = '';
$html .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block);
$html .= $this->renderLabel($block);
$lines = (isset($block['params']['lines']) ? (int)$block['params']['lines'] : 1);
$html .= '<textarea class="mailpoet_textarea" rows="' . $lines . '" ';
$html .= 'name="data[' . static::getFieldName($block) . ']"';
$html .= 'name="data[' . $this->getFieldName($block) . ']"';
$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 .= '>' . static::getFieldValue($block) . '</textarea>';
$html .= '>' . $this->getFieldValue($block) . '</textarea>';
$html .= '</p>';

View File

@ -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;

View File

@ -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('');
}
}