Block
1 week ago
Listing
1 month ago
RestApi
1 month ago
Templates
2 years ago
Util
2 months ago
ApiDataSanitizer.php
2 weeks ago
AssetsController.php
2 months ago
BlockStylesRenderer.php
3 years ago
BlockWrapperRenderer.php
3 years ago
BlocksRenderer.php
1 week ago
DisplayFormInWPContent.php
2 months ago
FormHtmlSanitizer.php
1 month ago
FormMessageController.php
4 years ago
FormSaveController.php
1 year ago
FormsRepository.php
1 year ago
PreviewPage.php
2 months ago
PreviewWidget.php
1 year ago
Renderer.php
2 months ago
Widget.php
2 months ago
index.php
3 years ago
AssetsController.php
140 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Captcha\CaptchaConstants; |
| 9 | use MailPoet\Config\Env; |
| 10 | use MailPoet\Config\Renderer as BasicRenderer; |
| 11 | use MailPoet\Settings\SettingsController; |
| 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 | const TURNSTILE_API_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'; |
| 26 | |
| 27 | public function __construct( |
| 28 | WPFunctions $wp, |
| 29 | BasicRenderer $renderer, |
| 30 | SettingsController $settings |
| 31 | ) { |
| 32 | $this->wp = $wp; |
| 33 | $this->renderer = $renderer; |
| 34 | $this->settings = $settings; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns assets scripts tags as string |
| 39 | * @return string |
| 40 | */ |
| 41 | public function printScripts() { |
| 42 | ob_start(); |
| 43 | $captcha = $this->settings->get('captcha'); |
| 44 | if (!empty($captcha['type']) && CaptchaConstants::isReCaptcha($captcha['type'])) { |
| 45 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WPFunctions::escUrl() wraps esc_url(). |
| 46 | echo '<script src="' . $this->wp->escUrl(self::RECAPTCHA_API_URL) . '" async defer></script>'; |
| 47 | } |
| 48 | if (!empty($captcha['type']) && CaptchaConstants::isTurnstile($captcha['type'])) { |
| 49 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WPFunctions::escUrl() wraps esc_url(). |
| 50 | echo '<script src="' . $this->wp->escUrl(self::TURNSTILE_API_URL) . '" async defer></script>'; |
| 51 | } |
| 52 | |
| 53 | $this->wp->wpPrintScripts('jquery'); |
| 54 | $this->wp->wpPrintScripts('mailpoet_vendor'); |
| 55 | $this->wp->wpPrintScripts('mailpoet_public'); |
| 56 | |
| 57 | $scripts = ob_get_contents(); |
| 58 | ob_end_clean(); |
| 59 | if ($scripts === false) { |
| 60 | return ''; |
| 61 | } |
| 62 | return $scripts; |
| 63 | } |
| 64 | |
| 65 | public function setupFormPreviewDependencies() { |
| 66 | $this->setupFrontEndDependencies(); |
| 67 | $this->wp->wpEnqueueScript( |
| 68 | 'mailpoet_form_preview', |
| 69 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('form_preview.js'), |
| 70 | ['jquery'], |
| 71 | Env::$version, |
| 72 | true |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function setupFrontEndDependencies() { |
| 77 | $captcha = $this->settings->get('captcha'); |
| 78 | if (!empty($captcha['type']) && CaptchaConstants::isRecaptcha($captcha['type'])) { |
| 79 | $this->wp->wpEnqueueScript( |
| 80 | 'mailpoet_recaptcha', |
| 81 | self::RECAPTCHA_API_URL |
| 82 | ); |
| 83 | } |
| 84 | if (!empty($captcha['type']) && CaptchaConstants::isTurnstile($captcha['type'])) { |
| 85 | $this->wp->wpEnqueueScript( |
| 86 | 'mailpoet_turnstile', |
| 87 | self::TURNSTILE_API_URL |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $this->wp->wpEnqueueStyle( |
| 92 | 'mailpoet_public', |
| 93 | Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css') |
| 94 | ); |
| 95 | |
| 96 | $enqueuePlacementParams = [ |
| 97 | 'in_footer' => true, |
| 98 | 'strategy' => 'defer', |
| 99 | ]; |
| 100 | |
| 101 | $this->wp->wpEnqueueScript( |
| 102 | 'mailpoet_public', |
| 103 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'), |
| 104 | ['jquery'], |
| 105 | Env::$version, |
| 106 | $enqueuePlacementParams |
| 107 | ); |
| 108 | |
| 109 | $ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet'); |
| 110 | $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ |
| 111 | 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), |
| 112 | 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false), |
| 113 | 'ajax_common_error_message' => $ajaxFailedErrorMessage, |
| 114 | 'captcha_input_label' => __('Type in the characters you see in the picture above:', 'mailpoet'), |
| 115 | 'captcha_reload_title' => __('Reload CAPTCHA', 'mailpoet'), |
| 116 | 'captcha_audio_title' => __('Play CAPTCHA', 'mailpoet'), |
| 117 | 'assets_url' => Env::$assetsUrl, |
| 118 | 'collect_subscriber_timezones' => $this->settings->isSettingEnabled('collect_subscriber_timezones.enabled'), |
| 119 | ]); |
| 120 | } |
| 121 | |
| 122 | public function setupAdminWidgetPageDependencies() { |
| 123 | $this->wp->wpEnqueueScript( |
| 124 | 'mailpoet_vendor', |
| 125 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'), |
| 126 | [], |
| 127 | Env::$version, |
| 128 | true |
| 129 | ); |
| 130 | |
| 131 | $this->wp->wpEnqueueScript( |
| 132 | 'mailpoet_admin', |
| 133 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'), |
| 134 | [], |
| 135 | Env::$version, |
| 136 | true |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 |