Refactor Form\Utils\Styles to injectable service
[MAILPOET-2451]
This commit is contained in:
committed by
Jack Kitterhing
parent
d25de883e7
commit
effd37a0ad
@ -24,6 +24,9 @@ class Forms extends APIEndpoint {
|
||||
/** @var FeaturesController */
|
||||
private $features_controller;
|
||||
|
||||
/** @var Util\Styles */
|
||||
private $form_styles_utils;
|
||||
|
||||
public $permissions = [
|
||||
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
|
||||
];
|
||||
@ -31,11 +34,13 @@ class Forms extends APIEndpoint {
|
||||
function __construct(
|
||||
Listing\BulkActionController $bulk_action,
|
||||
Listing\Handler $listing_handler,
|
||||
FeaturesController $features_controller
|
||||
FeaturesController $features_controller,
|
||||
Util\Styles $form_styles_utils
|
||||
) {
|
||||
$this->bulk_action = $bulk_action;
|
||||
$this->listing_handler = $listing_handler;
|
||||
$this->features_controller = $features_controller;
|
||||
$this->form_styles_utils = $form_styles_utils;
|
||||
}
|
||||
|
||||
function get($data = []) {
|
||||
@ -134,11 +139,11 @@ class Forms extends APIEndpoint {
|
||||
$html = WPFunctions::get()->doShortcode($html);
|
||||
|
||||
// styles
|
||||
$css = new Util\Styles(FormRenderer::getStyles($data));
|
||||
$css = $this->form_styles_utils->render(FormRenderer::getStyles($data));
|
||||
|
||||
return $this->successResponse([
|
||||
'html' => $html,
|
||||
'css' => $css->render(),
|
||||
'css' => $css,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -165,6 +165,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
// Form
|
||||
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Form\AssetsController::class);
|
||||
$container->autowire(\MailPoet\Form\Util\Styles::class);
|
||||
// Helpscout
|
||||
$container->autowire(\MailPoet\Helpscout\Beacon::class);
|
||||
// Listing
|
||||
|
@ -15,11 +15,11 @@ class Renderer {
|
||||
}
|
||||
|
||||
static function renderStyles($form = [], $prefix = null) {
|
||||
$styles = new Util\Styles(static::getStyles($form));
|
||||
$styles = new Util\Styles();
|
||||
|
||||
$html = '<style type="text/css">';
|
||||
$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>';
|
||||
|
||||
return $html;
|
||||
@ -37,7 +37,8 @@ class Renderer {
|
||||
&& strlen(trim($form['styles'])) > 0) {
|
||||
return strip_tags($form['styles']);
|
||||
} else {
|
||||
return Util\Styles::$default_styles;
|
||||
$styles = new Util\Styles();
|
||||
return $styles->getDefaultStyles();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ use MailPoetVendor\Sabberworm\CSS\Parser as CSSParser;
|
||||
|
||||
class Styles {
|
||||
public $styles;
|
||||
static $default_styles = <<<EOL
|
||||
private $default_styles = <<<EOL
|
||||
/* form */
|
||||
.mailpoet_form {
|
||||
|
||||
@ -79,15 +79,14 @@ class Styles {
|
||||
background-color: #5b5b5b;
|
||||
}
|
||||
EOL;
|
||||
private $stylesheet;
|
||||
|
||||
function __construct($stylesheet = null) {
|
||||
$this->stylesheet = $stylesheet;
|
||||
function getDefaultStyles() {
|
||||
return $this->default_styles;
|
||||
}
|
||||
|
||||
function render($prefix = '') {
|
||||
if (!$this->stylesheet) return;
|
||||
$styles = new CSSParser($this->stylesheet);
|
||||
function render($stylesheet, $prefix = '') {
|
||||
if (!$stylesheet) return;
|
||||
$styles = new CSSParser($stylesheet);
|
||||
$styles = $styles->parse();
|
||||
$formatted_styles = [];
|
||||
foreach ($styles->getAllDeclarationBlocks() as $style_declaration) {
|
||||
|
@ -14,8 +14,8 @@ class StylesTest extends \MailPoetUnitTest {
|
||||
/* some comment */
|
||||
input[name=first_name] , input.some_class, .some_class { color: red ; background: blue; } .another_style { fonT-siZe: 20px }
|
||||
';
|
||||
$style_processer = new Styles($stylesheet);
|
||||
$extracted_and_prefixed_styles = $style_processer->render($prefix = 'mailpoet');
|
||||
$style_processer = new Styles();
|
||||
$extracted_and_prefixed_styles = $style_processer->render($stylesheet, $prefix = 'mailpoet');
|
||||
// 1. comments should be stripped
|
||||
// 2. each selector should be refixed
|
||||
// 3. multiple spaces, missing semicolons should be fixed
|
||||
|
Reference in New Issue
Block a user