AssetsController.php
| 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 |