Files
piratepoet/lib/Form/Util/Styles.php
2020-01-14 15:22:42 +01:00

131 lines
2.9 KiB
PHP

<?php
namespace MailPoet\Form\Util;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Features\FeaturesController;
use MailPoetVendor\Sabberworm\CSS\Parser as CSSParser;
class Styles {
private $defaultStyles = <<<EOL
/* form */
.mailpoet_form {
}
/* paragraphs (label + input) */
.mailpoet_paragraph {
line-height:20px;
}
/* labels */
.mailpoet_segment_label,
.mailpoet_text_label,
.mailpoet_textarea_label,
.mailpoet_select_label,
.mailpoet_radio_label,
.mailpoet_checkbox_label,
.mailpoet_list_label,
.mailpoet_date_label {
display:block;
[LABELS_FONT_WEIGHT_RULE]
}
/* inputs */
.mailpoet_text,
.mailpoet_textarea,
.mailpoet_select,
.mailpoet_date_month,
.mailpoet_date_day,
.mailpoet_date_year,
.mailpoet_date {
display:block;
}
.mailpoet_text,
.mailpoet_textarea {
[TEXT_INPUTS_WIDTH_RULE]
}
.mailpoet_checkbox {
}
.mailpoet_submit input {
}
.mailpoet_divider {
}
.mailpoet_message {
}
.mailpoet_validate_success {
font-weight: 600;
color:#468847;
}
.mailpoet_validate_error {
color:#B94A48;
}
.mailpoet_form_loading {
width: 30px;
text-align: center;
line-height: normal;
}
.mailpoet_form_loading > span {
width: 5px;
height: 5px;
background-color: #5b5b5b;
}
EOL;
/** @var FeaturesController */
private $featuresController;
/**
* @param FeaturesController $featuresController
*/
public function __construct(
FeaturesController $featuresController = null
) {
if ($featuresController === null) {
$featuresController = ContainerWrapper::getInstance()->get(FeaturesController::class);
}
$this->featuresController = $featuresController;
}
public function getDefaultStyles() {
if ($this->featuresController->isSupported(FeaturesController::NEW_FORM_EDITOR)) {
$textInputWidth = 'width: 100%;';
$labelFontWeight = 'font-weight: normal;';
} else {
$textInputWidth = 'width: 200px;';
$labelFontWeight = 'font-weight: bold;';
}
$styles = str_replace('[TEXT_INPUTS_WIDTH_RULE]', $textInputWidth, $this->defaultStyles);
$styles = str_replace('[LABELS_FONT_WEIGHT_RULE]', $labelFontWeight, $styles);
return $styles;
}
public function render($stylesheet, $prefix = '') {
if (!$stylesheet) return;
$styles = new CSSParser($stylesheet);
$styles = $styles->parse();
$formattedStyles = [];
foreach ($styles->getAllDeclarationBlocks() as $styleDeclaration) {
$selectors = array_map(function($selector) use ($prefix) {
return sprintf('%s %s', $prefix, $selector->__toString());
}, $styleDeclaration->getSelectors());
$selectors = implode(', ', $selectors);
$rules = array_map(function($rule) {
return $rule->__toString();
}, $styleDeclaration->getRules());
$rules = sprintf('{ %s }', implode(' ', $rules));
$formattedStyles[] = sprintf('%s %s', $selectors, $rules);
}
return implode(PHP_EOL, $formattedStyles);
}
}