Refactor Form\Utils\Styles to injectable service

[MAILPOET-2451]
This commit is contained in:
Rostislav Wolny
2019-12-02 17:31:58 +01:00
committed by Jack Kitterhing
parent d25de883e7
commit effd37a0ad
5 changed files with 21 additions and 15 deletions

View File

@@ -24,6 +24,9 @@ class Forms extends APIEndpoint {
/** @var FeaturesController */ /** @var FeaturesController */
private $features_controller; private $features_controller;
/** @var Util\Styles */
private $form_styles_utils;
public $permissions = [ public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_FORMS, 'global' => AccessControl::PERMISSION_MANAGE_FORMS,
]; ];
@@ -31,11 +34,13 @@ class Forms extends APIEndpoint {
function __construct( function __construct(
Listing\BulkActionController $bulk_action, Listing\BulkActionController $bulk_action,
Listing\Handler $listing_handler, Listing\Handler $listing_handler,
FeaturesController $features_controller FeaturesController $features_controller,
Util\Styles $form_styles_utils
) { ) {
$this->bulk_action = $bulk_action; $this->bulk_action = $bulk_action;
$this->listing_handler = $listing_handler; $this->listing_handler = $listing_handler;
$this->features_controller = $features_controller; $this->features_controller = $features_controller;
$this->form_styles_utils = $form_styles_utils;
} }
function get($data = []) { function get($data = []) {
@@ -134,11 +139,11 @@ class Forms extends APIEndpoint {
$html = WPFunctions::get()->doShortcode($html); $html = WPFunctions::get()->doShortcode($html);
// styles // styles
$css = new Util\Styles(FormRenderer::getStyles($data)); $css = $this->form_styles_utils->render(FormRenderer::getStyles($data));
return $this->successResponse([ return $this->successResponse([
'html' => $html, 'html' => $html,
'css' => $css->render(), 'css' => $css,
]); ]);
} }

View File

@@ -165,6 +165,7 @@ class ContainerConfigurator implements IContainerConfigurator {
// Form // Form
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true); $container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
$container->autowire(\MailPoet\Form\AssetsController::class); $container->autowire(\MailPoet\Form\AssetsController::class);
$container->autowire(\MailPoet\Form\Util\Styles::class);
// Helpscout // Helpscout
$container->autowire(\MailPoet\Helpscout\Beacon::class); $container->autowire(\MailPoet\Helpscout\Beacon::class);
// Listing // Listing

View File

@@ -15,11 +15,11 @@ class Renderer {
} }
static function renderStyles($form = [], $prefix = null) { static function renderStyles($form = [], $prefix = null) {
$styles = new Util\Styles(static::getStyles($form)); $styles = new Util\Styles();
$html = '<style type="text/css">'; $html = '<style type="text/css">';
$html .= '.mailpoet_hp_email_label{display:none;}'; // move honeypot field out of sight $html .= '.mailpoet_hp_email_label{display:none;}'; // move honeypot field out of sight
$html .= $styles->render($prefix); $html .= $styles->render(static::getStyles($form), $prefix);
$html .= '</style>'; $html .= '</style>';
return $html; return $html;
@@ -37,7 +37,8 @@ class Renderer {
&& strlen(trim($form['styles'])) > 0) { && strlen(trim($form['styles'])) > 0) {
return strip_tags($form['styles']); return strip_tags($form['styles']);
} else { } else {
return Util\Styles::$default_styles; $styles = new Util\Styles();
return $styles->getDefaultStyles();
} }
} }

View File

@@ -6,7 +6,7 @@ use MailPoetVendor\Sabberworm\CSS\Parser as CSSParser;
class Styles { class Styles {
public $styles; public $styles;
static $default_styles = <<<EOL private $default_styles = <<<EOL
/* form */ /* form */
.mailpoet_form { .mailpoet_form {
@@ -79,15 +79,14 @@ class Styles {
background-color: #5b5b5b; background-color: #5b5b5b;
} }
EOL; EOL;
private $stylesheet;
function __construct($stylesheet = null) { function getDefaultStyles() {
$this->stylesheet = $stylesheet; return $this->default_styles;
} }
function render($prefix = '') { function render($stylesheet, $prefix = '') {
if (!$this->stylesheet) return; if (!$stylesheet) return;
$styles = new CSSParser($this->stylesheet); $styles = new CSSParser($stylesheet);
$styles = $styles->parse(); $styles = $styles->parse();
$formatted_styles = []; $formatted_styles = [];
foreach ($styles->getAllDeclarationBlocks() as $style_declaration) { foreach ($styles->getAllDeclarationBlocks() as $style_declaration) {

View File

@@ -14,8 +14,8 @@ class StylesTest extends \MailPoetUnitTest {
/* some comment */ /* some comment */
input[name=first_name] , input.some_class, .some_class { color: red ; background: blue; } .another_style { fonT-siZe: 20px } input[name=first_name] , input.some_class, .some_class { color: red ; background: blue; } .another_style { fonT-siZe: 20px }
'; ';
$style_processer = new Styles($stylesheet); $style_processer = new Styles();
$extracted_and_prefixed_styles = $style_processer->render($prefix = 'mailpoet'); $extracted_and_prefixed_styles = $style_processer->render($stylesheet, $prefix = 'mailpoet');
// 1. comments should be stripped // 1. comments should be stripped
// 2. each selector should be refixed // 2. each selector should be refixed
// 3. multiple spaces, missing semicolons should be fixed // 3. multiple spaces, missing semicolons should be fixed