PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.65.0
MailPoet – Newsletters, Email Marketing, and Automation v3.65.0
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Form / AssetsController.php
AssetsController.php
130 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailPoet\Form;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use MailPoet\Config\Env;
9 use MailPoet\Config\Renderer as BasicRenderer;
10 use MailPoet\Settings\SettingsController;
11 use MailPoet\Subscription\Captcha;
12 use MailPoet\WP\Functions as WPFunctions;
13
14 class AssetsController {
15 /** @var WPFunctions */
16 private $wp;
17
18 /** @var BasicRenderer */
19 private $renderer;
20
21 /** @var SettingsController */
22 private $settings;
23
24 const RECAPTCHA_API_URL = 'https://www.google.com/recaptcha/api.js?render=explicit';
25
26 public function __construct(WPFunctions $wp, BasicRenderer $renderer, SettingsController $settings) {
27 $this->wp = $wp;
28 $this->renderer = $renderer;
29 $this->settings = $settings;
30 }
31
32 /**
33 * Returns assets scripts tags as string
34 * @return string
35 */
36 public function printScripts() {
37 ob_start();
38 $captcha = $this->settings->get('captcha');
39 if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
40 echo '<script src="' . self::RECAPTCHA_API_URL . '" async defer></script>';
41 }
42
43 $this->wp->wpPrintScripts('jquery');
44 $this->wp->wpPrintScripts('mailpoet_vendor');
45 $this->wp->wpPrintScripts('mailpoet_public');
46
47 $scripts = ob_get_contents();
48 ob_end_clean();
49 if ($scripts === false) {
50 return '';
51 }
52 return $scripts;
53 }
54
55 public function setupFormPreviewDependencies() {
56 $this->setupFrontEndDependencies();
57 $this->wp->wpEnqueueScript(
58 'mailpoet_form_preview',
59 Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('form_preview.js'),
60 ['jquery'],
61 Env::$version,
62 true
63 );
64 }
65
66 public function setupFrontEndDependencies() {
67 $captcha = $this->settings->get('captcha');
68 if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
69 $this->wp->wpEnqueueScript(
70 'mailpoet_recaptcha',
71 self::RECAPTCHA_API_URL
72 );
73 }
74
75 $this->wp->wpEnqueueStyle(
76 'mailpoet_public',
77 Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css')
78 );
79
80 $this->wp->wpEnqueueScript(
81 'mailpoet_public',
82 Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'),
83 ['jquery'],
84 Env::$version,
85 true
86 );
87
88 $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [
89 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'),
90 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false),
91 ]);
92
93 $ajaxFailedErrorMessage = $this->wp->__('An error has happened while performing a request, please try again later.');
94
95 $inlineScript = <<<EOL
96 function initMailpoetTranslation() {
97 if (typeof MailPoet !== 'undefined') {
98 MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
99 } else {
100 setTimeout(initMailpoetTranslation, 250);
101 }
102 }
103 setTimeout(initMailpoetTranslation, 250);
104 EOL;
105 $this->wp->wpAddInlineScript(
106 'mailpoet_public',
107 sprintf($inlineScript, $ajaxFailedErrorMessage),
108 'after'
109 );
110 }
111
112 public function setupAdminWidgetPageDependencies() {
113 $this->wp->wpEnqueueScript(
114 'mailpoet_vendor',
115 Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
116 [],
117 Env::$version,
118 true
119 );
120
121 $this->wp->wpEnqueueScript(
122 'mailpoet_admin',
123 Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'),
124 [],
125 Env::$version,
126 true
127 );
128 }
129 }
130