diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index 6d1640fec2..c184105335 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -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); diff --git a/lib/Form/AssetsController.php b/lib/Form/AssetsController.php new file mode 100644 index 0000000000..5a9106e6a7 --- /dev/null +++ b/lib/Form/AssetsController.php @@ -0,0 +1,116 @@ +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 ''; + $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 = <<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 + ); + } +} diff --git a/lib/Form/Widget.php b/lib/Form/Widget.php index bc16793828..2b8bd915e6 100644 --- a/lib/Form/Widget.php +++ b/lib/Form/Widget.php @@ -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 ''; - $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 = <<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);