| 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?onload=reCaptchaCallback&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 |
$this->wp->wpPrintScripts('jquery'); |
| 39 |
$this->wp->wpPrintScripts('mailpoet_vendor'); |
| 40 |
$this->wp->wpPrintScripts('mailpoet_public'); |
| 41 |
echo '<script src="' . self::RECAPTCHA_API_URL . '" async defer></script>'; |
| 42 |
$scripts = ob_get_contents(); |
| 43 |
ob_end_clean(); |
| 44 |
return $scripts; |
| 45 |
} |
| 46 |
|
| 47 |
public function setupFrontEndDependencies() { |
| 48 |
$this->wp->wpEnqueueStyle( |
| 49 |
'mailpoet_public', |
| 50 |
Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css') |
| 51 |
); |
| 52 |
|
| 53 |
$this->wp->wpEnqueueScript( |
| 54 |
'mailpoet_vendor', |
| 55 |
Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'), |
| 56 |
[], |
| 57 |
Env::$version, |
| 58 |
true |
| 59 |
); |
| 60 |
|
| 61 |
$this->wp->wpEnqueueScript( |
| 62 |
'mailpoet_public', |
| 63 |
Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'), |
| 64 |
['jquery'], |
| 65 |
Env::$version, |
| 66 |
true |
| 67 |
); |
| 68 |
|
| 69 |
$captcha = $this->settings->get('captcha'); |
| 70 |
if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) { |
| 71 |
$this->wp->wpEnqueueScript( |
| 72 |
'mailpoet_recaptcha', |
| 73 |
self::RECAPTCHA_API_URL, |
| 74 |
['mailpoet_public'] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
$this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ |
| 79 |
'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), |
| 80 |
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false), |
| 81 |
]); |
| 82 |
|
| 83 |
$ajaxFailedErrorMessage = $this->wp->__('An error has happened while performing a request, please try again later.'); |
| 84 |
|
| 85 |
$inlineScript = <<<EOL |
| 86 |
function initMailpoetTranslation() { |
| 87 |
if (typeof MailPoet !== 'undefined') { |
| 88 |
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s') |
| 89 |
} else { |
| 90 |
setTimeout(initMailpoetTranslation, 250); |
| 91 |
} |
| 92 |
} |
| 93 |
setTimeout(initMailpoetTranslation, 250); |
| 94 |
EOL; |
| 95 |
$this->wp->wpAddInlineScript( |
| 96 |
'mailpoet_public', |
| 97 |
sprintf($inlineScript, $ajaxFailedErrorMessage), |
| 98 |
'after' |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public function setupAdminWidgetPageDependencies() { |
| 103 |
$this->wp->wpEnqueueScript( |
| 104 |
'mailpoet_vendor', |
| 105 |
Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'), |
| 106 |
[], |
| 107 |
Env::$version, |
| 108 |
true |
| 109 |
); |
| 110 |
|
| 111 |
$this->wp->wpEnqueueScript( |
| 112 |
'mailpoet_admin', |
| 113 |
Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'), |
| 114 |
[], |
| 115 |
Env::$version, |
| 116 |
true |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|