init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Form\Util;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions;
|
||||
|
||||
class CustomFonts {
|
||||
const FONT_CHUNK_SIZE = 25;
|
||||
const FONTS = [
|
||||
'Abril FatFace',
|
||||
'Alegreya',
|
||||
'Alegreya Sans',
|
||||
'Amatic SC',
|
||||
'Anonymous Pro',
|
||||
'Architects Daughter',
|
||||
'Archivo',
|
||||
'Archivo Narrow',
|
||||
'Asap',
|
||||
'Barlow',
|
||||
'BioRhyme',
|
||||
'Bonbon',
|
||||
'Cabin',
|
||||
'Cairo',
|
||||
'Cardo',
|
||||
'Chivo',
|
||||
'Concert One',
|
||||
'Cormorant',
|
||||
'Crimson Text',
|
||||
'Eczar',
|
||||
'Exo 2',
|
||||
'Fira Sans',
|
||||
'Fjalla One',
|
||||
'Frank Ruhl Libre',
|
||||
'Great Vibes',
|
||||
'Heebo',
|
||||
'IBM Plex',
|
||||
'Inconsolata',
|
||||
'Indie Flower',
|
||||
'Inknut Antiqua',
|
||||
'Inter',
|
||||
'Karla',
|
||||
'Libre Baskerville',
|
||||
'Libre Franklin',
|
||||
'Montserrat',
|
||||
'Neuton',
|
||||
'Notable',
|
||||
'Nothing You Could Do',
|
||||
'Noto Sans',
|
||||
'Nunito',
|
||||
'Old Standard TT',
|
||||
'Oxygen',
|
||||
'Pacifico',
|
||||
'Poppins',
|
||||
'Proza Libre',
|
||||
'PT Sans',
|
||||
'PT Serif',
|
||||
'Rakkas',
|
||||
'Reenie Beanie',
|
||||
'Roboto Slab',
|
||||
'Ropa Sans',
|
||||
'Rubik',
|
||||
'Shadows Into Light',
|
||||
'Space Mono',
|
||||
'Spectral',
|
||||
'Sue Ellen Francisco',
|
||||
'Titillium Web',
|
||||
'Ubuntu',
|
||||
'Varela',
|
||||
'Vollkorn',
|
||||
'Work Sans',
|
||||
'Yatra One',
|
||||
];
|
||||
|
||||
/** @var Functions */
|
||||
private $wp;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
public function __construct(
|
||||
Functions $wp,
|
||||
SettingsController $settings
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function displayCustomFonts(): bool {
|
||||
$display = $this->wp->applyFilters('mailpoet_display_custom_fonts', $this->settings->get('3rd_party_libs.enabled') === '1');
|
||||
return (bool)$display;
|
||||
}
|
||||
|
||||
public function enqueueStyle() {
|
||||
if (!$this->displayCustomFonts()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Due to a conflict with the WooCommerce Payments plugin, we need to load custom fonts in more requests.
|
||||
// When we load all custom fonts in one request, a form from WC Payments isn't displayed correctly.
|
||||
// It looks that the larger file size overloads the Stripe SDK.
|
||||
foreach (array_chunk(self::FONTS, self::FONT_CHUNK_SIZE) as $key => $fonts) {
|
||||
$this->wp->wpEnqueueStyle('mailpoet_custom_fonts_' . $key, $this->generateLink($fonts));
|
||||
}
|
||||
}
|
||||
|
||||
public function generateHtmlCustomFontLink(): string {
|
||||
if (!$this->displayCustomFonts()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$output = '';
|
||||
foreach (array_chunk(self::FONTS, self::FONT_CHUNK_SIZE) as $key => $fonts) {
|
||||
$output .= sprintf('<link href="%s" rel="stylesheet">', $this->generateLink($fonts));
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function generateLink(array $fonts): string {
|
||||
$fonts = array_map(function ($fontName) {
|
||||
return urlencode($fontName) . ':400,400i,700,700i';
|
||||
}, $fonts);
|
||||
$fonts = implode('|', $fonts);
|
||||
return 'https://fonts.googleapis.com/css?family=' . $fonts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Form\Util;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Form\Widget;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class Export {
|
||||
public static function getAll() {
|
||||
return [
|
||||
'html' => static::get('html'),
|
||||
'php' => static::get('php'),
|
||||
'iframe' => static::get('iframe'),
|
||||
'shortcode' => static::get('shortcode'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function get($type = 'html') {
|
||||
switch ($type) {
|
||||
case 'iframe':
|
||||
// generate url to load iframe's content
|
||||
$iframeUrl = WPFunctions::get()->addQueryArg([
|
||||
'mailpoet_form_iframe' => ':form_id:',
|
||||
], WPFunctions::get()->trailingslashit(WPFunctions::get()->siteUrl()));
|
||||
|
||||
$onload = "var _this = this; window.addEventListener('message', function(e) {if(e.data.MailPoetIframeHeight){_this.style.height = e.data.MailPoetIframeHeight;}})";
|
||||
// generate iframe
|
||||
return join(' ', [
|
||||
'<iframe',
|
||||
'width="100%"',
|
||||
'height="100%"',
|
||||
'scrolling="no"',
|
||||
'frameborder="0"',
|
||||
'src="' . WPFunctions::get()->escUrl($iframeUrl) . '"',
|
||||
'class="mailpoet_form_iframe"',
|
||||
'id="mailpoet_form_iframe"',
|
||||
'vspace="0"',
|
||||
'tabindex="0"',
|
||||
sprintf('onload="%s"', $onload),
|
||||
'marginwidth="0"',
|
||||
'marginheight="0"',
|
||||
'hspace="0"',
|
||||
'allowtransparency="true"></iframe>',
|
||||
]);
|
||||
|
||||
case 'php':
|
||||
$output = [
|
||||
'$form_widget = new \MailPoet\Form\Widget();',
|
||||
'echo $form_widget->widget(array(\'form\' => ' .
|
||||
':form_id:' .
|
||||
', \'form_type\' => \'php\'));',
|
||||
];
|
||||
return join("\n", $output);
|
||||
|
||||
case 'html':
|
||||
$output = [];
|
||||
|
||||
$output[] = '<!-- ' .
|
||||
__(
|
||||
'BEGIN Scripts: you should place them in the header of your theme',
|
||||
'mailpoet'
|
||||
) .
|
||||
' -->';
|
||||
|
||||
// CSS
|
||||
$output[] = '<link rel="stylesheet" type="text/css" href="' .
|
||||
Env::$assetsUrl . '/dist/css/mailpoet-public.css?mp_ver=' . MAILPOET_VERSION .
|
||||
'" />';
|
||||
|
||||
// jQuery
|
||||
$output[] = '<script type="text/javascript" src="' .
|
||||
WPFunctions::get()->includesUrl() . 'js/jquery/jquery.js?mp_ver' . MAILPOET_VERSION .
|
||||
'"></script>';
|
||||
|
||||
// JS
|
||||
$output[] = '<script type="text/javascript" src="' .
|
||||
Env::$assetsUrl . '/dist/js/vendor.js?mp_ver=' . MAILPOET_VERSION .
|
||||
'"></script>';
|
||||
$output[] = '<script type="text/javascript" src="' .
|
||||
Env::$assetsUrl . '/dist/js/public.js?mp_ver=' . MAILPOET_VERSION .
|
||||
'"></script>';
|
||||
|
||||
// (JS) variables...
|
||||
$output[] = '<script type="text/javascript">';
|
||||
$output[] = ' var MailPoetForm = MailPoetForm || {';
|
||||
$output[] = ' is_rtl: ' . ((int)is_rtl()) . ",";
|
||||
$output[] = ' ajax_url: "' . admin_url('admin-ajax.php') . '"';
|
||||
$output[] = ' };';
|
||||
$output[] = '</script>';
|
||||
$output[] = '<!-- ' .
|
||||
__('END Scripts', 'mailpoet') .
|
||||
'-->';
|
||||
|
||||
$formWidget = new Widget();
|
||||
$output[] = $formWidget->widget([
|
||||
'form' => ':form_id:',
|
||||
'form_type' => 'php',
|
||||
]);
|
||||
return join("\n", $output);
|
||||
|
||||
case 'shortcode':
|
||||
return '[mailpoet_form id=":form_id:"]';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Form\Util;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class FieldNameObfuscator {
|
||||
|
||||
const OBFUSCATED_FIELD_PREFIX = 'form_field_';
|
||||
const HASH_LENGTH = 12;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
public function obfuscate($name) {
|
||||
$authKey = defined('AUTH_KEY') ? AUTH_KEY : '';
|
||||
$hash = substr(md5($authKey . $this->wp->homeUrl() . $name), 0, self::HASH_LENGTH);
|
||||
return self::OBFUSCATED_FIELD_PREFIX . base64_encode($hash . '_' . $name);
|
||||
}
|
||||
|
||||
public function deobfuscate($name) {
|
||||
$decoded = base64_decode(substr($name, strlen(self::OBFUSCATED_FIELD_PREFIX)));
|
||||
return substr($decoded, self::HASH_LENGTH + 1);
|
||||
}
|
||||
|
||||
public function deobfuscateFormPayload($data) {
|
||||
$result = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$result[$this->deobfuscateField($key)] = $value;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function deobfuscateField($name) {
|
||||
if ($this->wasFieldObfuscated($name)) {
|
||||
return $this->deobfuscate($name);
|
||||
} else {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
private function wasFieldObfuscated($name) {
|
||||
return strpos($name, FieldNameObfuscator::OBFUSCATED_FIELD_PREFIX) === 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Form\Util;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoetVendor\Sabberworm\CSS\Parser as CSSParser;
|
||||
|
||||
class Styles {
|
||||
public function prefixStyles($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) {
|
||||
$stringSelector = is_string($selector) ? $selector : $selector->__toString();
|
||||
return sprintf('%s %s', $prefix, $stringSelector);
|
||||
}, $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);
|
||||
}
|
||||
|
||||
public function renderFormSettingsStyles(FormEntity $form, string $selector, string $displayType): string {
|
||||
if (!is_array($form->getSettings())) return '';
|
||||
$formSettings = $form->getSettings();
|
||||
// Wrapper styles
|
||||
$styles = [];
|
||||
|
||||
if (isset($formSettings['border_size']) && isset($formSettings['border_color'])) {
|
||||
$styles[] = 'border: ' . $formSettings['border_size'] . 'px solid ' . $formSettings['border_color'];
|
||||
}
|
||||
|
||||
if (isset($formSettings['border_radius'])) {
|
||||
$styles[] = 'border-radius: ' . $formSettings['border_radius'] . 'px';
|
||||
}
|
||||
|
||||
$backgrounds = [];
|
||||
$mobileBackgrounds = [];
|
||||
if (isset($formSettings['background_image_url']) && $formSettings['background_image_url']) {
|
||||
$backgroundPosition = 'center';
|
||||
$backgroundRepeat = 'no-repeat';
|
||||
$backgroundSize = 'cover';
|
||||
if (isset($formSettings['background_image_display']) && $formSettings['background_image_display'] === 'fit') {
|
||||
$backgroundPosition = 'center top';
|
||||
$backgroundSize = 'contain';
|
||||
}
|
||||
if (isset($formSettings['background_image_display']) && $formSettings['background_image_display'] === 'tile') {
|
||||
$backgroundRepeat = 'repeat';
|
||||
$backgroundSize = 'auto';
|
||||
}
|
||||
$backgrounds[] = "url(" . trim($formSettings['background_image_url']) . ") $backgroundPosition / $backgroundSize $backgroundRepeat";
|
||||
}
|
||||
|
||||
if (!empty($formSettings['gradient'])) {
|
||||
$backgrounds[] = trim($formSettings['gradient']);
|
||||
$mobileBackgrounds[] = trim($formSettings['gradient']);
|
||||
}
|
||||
|
||||
if (!empty($formSettings['backgroundColor'])) {
|
||||
$backgrounds[] = trim($formSettings['backgroundColor']);
|
||||
$mobileBackgrounds[] = trim($formSettings['backgroundColor']);
|
||||
}
|
||||
|
||||
if ($backgrounds) {
|
||||
$styles[] = 'background: ' . join(', ', $backgrounds);
|
||||
}
|
||||
|
||||
if (isset($formSettings['fontColor'])) {
|
||||
$styles[] = 'color: ' . trim($formSettings['fontColor']);
|
||||
}
|
||||
|
||||
if (isset($formSettings['alignment'])) {
|
||||
$styles[] = 'text-align: ' . $formSettings['alignment'];
|
||||
}
|
||||
$formWrapperStyles = $selector . '{' . join(';', $styles) . ';}';
|
||||
|
||||
// Media styles for mobile
|
||||
$media = $this->getMobileStyles($selector, $displayType, $mobileBackgrounds);
|
||||
|
||||
// Form element styles
|
||||
$formStyles = [];
|
||||
if (isset($formSettings['form_padding'])) {
|
||||
if (
|
||||
in_array(
|
||||
$displayType,
|
||||
[FormEntity::DISPLAY_TYPE_POPUP, FormEntity::DISPLAY_TYPE_FIXED_BAR, FormEntity::DISPLAY_TYPE_SLIDE_IN]
|
||||
)
|
||||
) {
|
||||
$padding = $formSettings['form_padding'];
|
||||
$media .= " @media (min-width: 500px) {{$selector} {padding: {$padding}px;}} ";
|
||||
} else {
|
||||
$formStyles[] = 'padding: ' . $formSettings['form_padding'] . 'px';
|
||||
}
|
||||
}
|
||||
$formElementStyles = '';
|
||||
if ($formStyles) {
|
||||
$formElementStyles = $selector . ' form.mailpoet_form {' . join(';', $formStyles) . ';}';
|
||||
}
|
||||
|
||||
// Width styles
|
||||
$widthStyles = $this->renderWidthStyles($formSettings, $selector, $displayType);
|
||||
|
||||
$typeSpecificStyles = $this->getFormTypeSpecificStyles($selector, $displayType);
|
||||
|
||||
$messagesStyles = $this->renderFormMessageStyles($form, $selector);
|
||||
|
||||
$additionalStyles = $selector . ' .mailpoet_paragraph.last {margin-bottom: 0} ';
|
||||
$media .= " @media (min-width: 500px) {{$selector} .last .mailpoet_paragraph:last-child {margin-bottom: 0}} ";
|
||||
$media .= " @media (max-width: 500px) {{$selector} .mailpoet_form_column:last-child .mailpoet_paragraph:last-child {margin-bottom: 0}} ";
|
||||
|
||||
return $formWrapperStyles
|
||||
. $formElementStyles
|
||||
. $widthStyles
|
||||
. $messagesStyles
|
||||
. $typeSpecificStyles
|
||||
. $additionalStyles
|
||||
. $media;
|
||||
}
|
||||
|
||||
private function renderWidthStyles(array $formSettings, string $selector, string $displayType): string {
|
||||
$styles = [];
|
||||
|
||||
if (isset($formSettings['form_placement'][$displayType]['styles']['width'])) {
|
||||
$width = $this->getWidthValue($formSettings['form_placement'][$displayType]['styles']['width']);
|
||||
}
|
||||
|
||||
if ($displayType === FormEntity::DISPLAY_TYPE_POPUP) {
|
||||
if (isset($width)) {
|
||||
$styles[] = "width: $width";
|
||||
$styles[] = "max-width: 100vw";
|
||||
} else { // BC compatibilty
|
||||
$styles[] = 'width: 560px';
|
||||
$styles[] = 'max-width: 560px';
|
||||
}
|
||||
} elseif ($displayType === FormEntity::DISPLAY_TYPE_SLIDE_IN) {
|
||||
if (isset($width)) {
|
||||
$styles[] = "width: $width";
|
||||
$styles[] = "max-width: 100vw";
|
||||
} else { // BC compatibilty
|
||||
$styles[] = 'max-width: 600px';
|
||||
$styles[] = 'min-width: 350px';
|
||||
}
|
||||
} elseif ($displayType === FormEntity::DISPLAY_TYPE_FIXED_BAR) {
|
||||
if (isset($width)) {
|
||||
$styles[] = "width: $width";
|
||||
$styles[] = "max-width: 100%";
|
||||
} else { // BC compatibilty
|
||||
$styles[] = 'max-width: 960px';
|
||||
}
|
||||
} elseif ($displayType === FormEntity::DISPLAY_TYPE_BELOW_POST) {
|
||||
if (isset($width)) {
|
||||
$styles[] = "width: $width";
|
||||
}
|
||||
} elseif ($displayType === FormEntity::DISPLAY_TYPE_OTHERS) {
|
||||
if (isset($width)) {
|
||||
$styles[] = "width: $width";
|
||||
}
|
||||
}
|
||||
|
||||
$widthSelector = $selector;
|
||||
$widthSelector .= $displayType === FormEntity::DISPLAY_TYPE_FIXED_BAR ? ' form.mailpoet_form' : '';
|
||||
|
||||
if (!$styles) {
|
||||
return '';
|
||||
}
|
||||
return $widthSelector . '{' . join(';', $styles) . ';}';
|
||||
}
|
||||
|
||||
private function getWidthValue(array $width) {
|
||||
return $width['value'] . ($width['unit'] === 'percent' ? '%' : 'px');
|
||||
}
|
||||
|
||||
public function renderFormMessageStyles(FormEntity $form, string $selector): string {
|
||||
$formSettings = $form->getSettings();
|
||||
if (!is_array($formSettings)) {
|
||||
return '';
|
||||
}
|
||||
return $this->renderMessagesStyles($formSettings, $selector);
|
||||
}
|
||||
|
||||
private function renderMessagesStyles(array $formSettings, string $selector): string {
|
||||
$styles = "$selector .mailpoet_message {margin: 0; padding: 0 20px;}";
|
||||
if (isset($formSettings['success_validation_color']) && $formSettings['success_validation_color']) {
|
||||
$success = $formSettings['success_validation_color'];
|
||||
$styles .= "
|
||||
$selector .mailpoet_validate_success {color: $success}
|
||||
$selector input.parsley-success {color: $success}
|
||||
$selector select.parsley-success {color: $success}
|
||||
$selector textarea.parsley-success {color: $success}
|
||||
";
|
||||
}
|
||||
if (isset($formSettings['error_validation_color']) && $formSettings['error_validation_color']) {
|
||||
$error = $formSettings['error_validation_color'];
|
||||
$styles .= "
|
||||
$selector .mailpoet_validate_error {color: $error}
|
||||
$selector input.parsley-error {color: $error}
|
||||
$selector select.parsley-error {color: $error}
|
||||
$selector textarea.textarea.parsley-error {color: $error}
|
||||
$selector .parsley-errors-list {color: $error}
|
||||
$selector .parsley-required {color: $error}
|
||||
$selector .parsley-custom-error-message {color: $error}
|
||||
";
|
||||
}
|
||||
return $styles;
|
||||
}
|
||||
|
||||
private function getFormTypeSpecificStyles(string $selector, string $displayType): string {
|
||||
$styles = [];
|
||||
if ($displayType === FormEntity::DISPLAY_TYPE_SLIDE_IN) {
|
||||
$styles[] = $selector . '.mailpoet_form_slide_in { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }';
|
||||
$styles[] = $selector . '.mailpoet_form_position_right { border-top-right-radius: 0; }';
|
||||
$styles[] = $selector . '.mailpoet_form_position_left { border-top-left-radius: 0; }';
|
||||
}
|
||||
return join('', $styles);
|
||||
}
|
||||
|
||||
private function getMobileStyles(string $selector, string $displayType, array $mobileBackgrounds): string {
|
||||
$wrapperStyles = [];
|
||||
if ($mobileBackgrounds) {
|
||||
$wrapperStyles[] = 'background: ' . join(', ', $mobileBackgrounds) . ';';
|
||||
} else {
|
||||
$wrapperStyles[] = 'background-image: none;';
|
||||
}
|
||||
if (
|
||||
in_array(
|
||||
$displayType,
|
||||
[FormEntity::DISPLAY_TYPE_POPUP, FormEntity::DISPLAY_TYPE_FIXED_BAR, FormEntity::DISPLAY_TYPE_SLIDE_IN]
|
||||
)
|
||||
) {
|
||||
$wrapperStyles = array_merge($wrapperStyles, [
|
||||
'animation: none;',
|
||||
'border: none;',
|
||||
'border-radius: 0;',
|
||||
'bottom: 0;',
|
||||
'left: 0;',
|
||||
'max-height: 40%;',
|
||||
'padding: 20px;',
|
||||
'right: 0;',
|
||||
'top: auto;',
|
||||
'transform: none;',
|
||||
'width: 100%;',
|
||||
'min-width: 100%;',
|
||||
]);
|
||||
}
|
||||
return "@media (max-width: 500px) {{$selector} {" . join('', $wrapperStyles) . "}}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user