Refactor form assets handling to separate class
[MAILPOET-2250]
This commit is contained in:
committed by
Jack Kitterhing
parent
6956a25c51
commit
bb4bc76e78
@ -127,6 +127,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\Features\FeatureFlagsRepository::class);
|
||||
// Form
|
||||
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Form\AssetsController::class);
|
||||
// Listing
|
||||
$container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Listing\BulkActionFactory::class)->setPublic(true);
|
||||
|
116
lib/Form/AssetsController.php
Normal file
116
lib/Form/AssetsController.php
Normal file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Config\Renderer as BasicRenderer;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Subscription\Captcha;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class AssetsController {
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var BasicRenderer */
|
||||
private $renderer;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
const RECAPTCHA_API_URL = 'https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit';
|
||||
|
||||
function __construct(WPFunctions $wp, BasicRenderer $renderer, SettingsController $settings) {
|
||||
$this->wp = $wp;
|
||||
$this->renderer = $renderer;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns assets scripts tags as string
|
||||
* @return string
|
||||
*/
|
||||
function printScripts() {
|
||||
ob_start();
|
||||
$this->wp->wpPrintScripts('jquery');
|
||||
$this->wp->wpPrintScripts('mailpoet_vendor');
|
||||
$this->wp->wpPrintScripts('mailpoet_public');
|
||||
echo '<script src="' . self::RECAPTCHA_API_URL . '" async defer></script>';
|
||||
$scripts = ob_get_contents();
|
||||
ob_end_clean();
|
||||
return $scripts;
|
||||
}
|
||||
|
||||
function setupFrontEndDependencies() {
|
||||
$this->wp->wpEnqueueStyle(
|
||||
'mailpoet_public',
|
||||
Env::$assets_url . '/dist/css/' . $this->renderer->getCssAsset('public.css')
|
||||
);
|
||||
|
||||
$this->wp->wpEnqueueScript(
|
||||
'mailpoet_vendor',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
$this->wp->wpEnqueueScript(
|
||||
'mailpoet_public',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('public.js'),
|
||||
['jquery'],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
$captcha = $this->settings->get('captcha');
|
||||
if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
|
||||
$this->wp->wpEnqueueScript(
|
||||
'mailpoet_recaptcha',
|
||||
self::RECAPTCHA_API_URL,
|
||||
['mailpoet_public']
|
||||
);
|
||||
}
|
||||
|
||||
$this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [
|
||||
'ajax_url' => $this->wp->adminUrl('admin-ajax.php'),
|
||||
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false),
|
||||
]);
|
||||
|
||||
$ajax_failed_error_message = $this->wp->__('An error has happened while performing a request, please try again later.');
|
||||
|
||||
$inline_script = <<<EOL
|
||||
function initMailpoetTranslation() {
|
||||
if (typeof MailPoet !== 'undefined') {
|
||||
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
|
||||
} else {
|
||||
setTimeout(initMailpoetTranslation, 250);
|
||||
}
|
||||
}
|
||||
setTimeout(initMailpoetTranslation, 250);
|
||||
EOL;
|
||||
$this->wp->wpAddInlineScript(
|
||||
'mailpoet_public',
|
||||
sprintf($inline_script, $ajax_failed_error_message),
|
||||
'after'
|
||||
);
|
||||
}
|
||||
|
||||
function setupAdminWidgetPageDependencies() {
|
||||
$this->wp->wpEnqueueScript(
|
||||
'mailpoet_vendor',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
$this->wp->wpEnqueueScript(
|
||||
'mailpoet_admin',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
@ -3,12 +3,10 @@
|
||||
namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\API\JSON\API;
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Config\Renderer as ConfigRenderer;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Subscription\Captcha;
|
||||
use MailPoet\Util\Security;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
@ -16,10 +14,8 @@ class Widget extends \WP_Widget {
|
||||
private $renderer;
|
||||
private $wp;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
const RECAPTCHA_API_URL = 'https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit';
|
||||
/** @var AssetsController */
|
||||
private $assets_controller;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct(
|
||||
@ -29,12 +25,12 @@ class Widget extends \WP_Widget {
|
||||
);
|
||||
$this->wp = new WPFunctions;
|
||||
$this->renderer = new \MailPoet\Config\Renderer(!WP_DEBUG, !WP_DEBUG);
|
||||
$this->settings = new SettingsController();
|
||||
$this->assets_controller = new AssetsController($this->wp, $this->renderer, new SettingsController());
|
||||
if (!is_admin()) {
|
||||
$this->setupIframe();
|
||||
} else {
|
||||
WPFunctions::get()->addAction('widgets_admin_page', [
|
||||
$this,
|
||||
$this->assets_controller,
|
||||
'setupAdminWidgetPageDependencies',
|
||||
]);
|
||||
}
|
||||
@ -51,13 +47,7 @@ class Widget extends \WP_Widget {
|
||||
]
|
||||
);
|
||||
|
||||
ob_start();
|
||||
WPFunctions::get()->wpPrintScripts('jquery');
|
||||
WPFunctions::get()->wpPrintScripts('mailpoet_vendor');
|
||||
WPFunctions::get()->wpPrintScripts('mailpoet_public');
|
||||
echo '<script src="' . self::RECAPTCHA_API_URL . '" async defer></script>';
|
||||
$scripts = ob_get_contents();
|
||||
ob_end_clean();
|
||||
$scripts = $this->assets_controller->printScripts();
|
||||
|
||||
// language attributes
|
||||
$language_attributes = [];
|
||||
@ -94,79 +84,6 @@ class Widget extends \WP_Widget {
|
||||
exit();
|
||||
}
|
||||
|
||||
function setupDependencies() {
|
||||
WPFunctions::get()->wpEnqueueStyle(
|
||||
'mailpoet_public',
|
||||
Env::$assets_url . '/dist/css/' . $this->renderer->getCssAsset('public.css')
|
||||
);
|
||||
|
||||
WPFunctions::get()->wpEnqueueScript(
|
||||
'mailpoet_vendor',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
WPFunctions::get()->wpEnqueueScript(
|
||||
'mailpoet_public',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('public.js'),
|
||||
['jquery'],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
$captcha = $this->settings->get('captcha');
|
||||
if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
|
||||
WPFunctions::get()->wpEnqueueScript(
|
||||
'mailpoet_recaptcha',
|
||||
self::RECAPTCHA_API_URL,
|
||||
['mailpoet_public']
|
||||
);
|
||||
}
|
||||
|
||||
WPFunctions::get()->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [
|
||||
'ajax_url' => WPFunctions::get()->adminUrl('admin-ajax.php'),
|
||||
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false),
|
||||
]);
|
||||
|
||||
$ajax_failed_error_message = WPFunctions::get()->__('An error has happened while performing a request, please try again later.');
|
||||
|
||||
$inline_script = <<<EOL
|
||||
function initMailpoetTranslation() {
|
||||
if (typeof MailPoet !== 'undefined') {
|
||||
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
|
||||
} else {
|
||||
setTimeout(initMailpoetTranslation, 250);
|
||||
}
|
||||
}
|
||||
setTimeout(initMailpoetTranslation, 250);
|
||||
EOL;
|
||||
WPFunctions::get()->wpAddInlineScript(
|
||||
'mailpoet_public',
|
||||
sprintf($inline_script, $ajax_failed_error_message),
|
||||
'after'
|
||||
);
|
||||
}
|
||||
|
||||
function setupAdminWidgetPageDependencies() {
|
||||
WPFunctions::get()->wpEnqueueScript(
|
||||
'mailpoet_vendor',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
|
||||
WPFunctions::get()->wpEnqueueScript(
|
||||
'mailpoet_admin',
|
||||
Env::$assets_url . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'),
|
||||
[],
|
||||
Env::$version,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the new widget's title.
|
||||
*/
|
||||
@ -250,7 +167,7 @@ EOL;
|
||||
* Output the widget itself.
|
||||
*/
|
||||
function widget($args, $instance = null) {
|
||||
$this->setupDependencies();
|
||||
$this->assets_controller->setupFrontEndDependencies();
|
||||
|
||||
// turn $args into variables
|
||||
extract($args);
|
||||
|
Reference in New Issue
Block a user