Нет описания

AssetsController.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace MailPoet\Form;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Config\Env;
  5. use MailPoet\Config\Renderer as BasicRenderer;
  6. use MailPoet\Settings\SettingsController;
  7. use MailPoet\Subscription\Captcha;
  8. use MailPoet\WP\Functions as WPFunctions;
  9. class AssetsController {
  10. /** @var WPFunctions */
  11. private $wp;
  12. /** @var BasicRenderer */
  13. private $renderer;
  14. /** @var SettingsController */
  15. private $settings;
  16. const RECAPTCHA_API_URL = 'https://www.google.com/recaptcha/api.js?render=explicit';
  17. public function __construct(WPFunctions $wp, BasicRenderer $renderer, SettingsController $settings) {
  18. $this->wp = $wp;
  19. $this->renderer = $renderer;
  20. $this->settings = $settings;
  21. }
  22. /**
  23. * Returns assets scripts tags as string
  24. * @return string
  25. */
  26. public function printScripts() {
  27. ob_start();
  28. $captcha = $this->settings->get('captcha');
  29. if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
  30. echo '<script src="' . self::RECAPTCHA_API_URL . '" async defer></script>';
  31. }
  32. $this->wp->wpPrintScripts('jquery');
  33. $this->wp->wpPrintScripts('mailpoet_vendor');
  34. $this->wp->wpPrintScripts('mailpoet_public');
  35. $scripts = ob_get_contents();
  36. ob_end_clean();
  37. if ($scripts === false) {
  38. return '';
  39. }
  40. return $scripts;
  41. }
  42. public function setupFormPreviewDependencies() {
  43. $this->setupFrontEndDependencies();
  44. $this->wp->wpEnqueueScript(
  45. 'mailpoet_form_preview',
  46. Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('form_preview.js'),
  47. ['jquery'],
  48. Env::$version,
  49. true
  50. );
  51. }
  52. public function setupFrontEndDependencies() {
  53. $captcha = $this->settings->get('captcha');
  54. if (!empty($captcha['type']) && $captcha['type'] === Captcha::TYPE_RECAPTCHA) {
  55. $this->wp->wpEnqueueScript(
  56. 'mailpoet_recaptcha',
  57. self::RECAPTCHA_API_URL
  58. );
  59. }
  60. $this->wp->wpEnqueueStyle(
  61. 'mailpoet_public',
  62. Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css')
  63. );
  64. $this->wp->wpEnqueueScript(
  65. 'mailpoet_public',
  66. Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'),
  67. ['jquery'],
  68. Env::$version,
  69. true
  70. );
  71. $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [
  72. 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'),
  73. 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false),
  74. ]);
  75. $ajaxFailedErrorMessage = $this->wp->__('An error has happened while performing a request, please try again later.');
  76. $inlineScript = <<<EOL
  77. function initMailpoetTranslation() {
  78. if (typeof MailPoet !== 'undefined') {
  79. MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
  80. } else {
  81. setTimeout(initMailpoetTranslation, 250);
  82. }
  83. }
  84. setTimeout(initMailpoetTranslation, 250);
  85. EOL;
  86. $this->wp->wpAddInlineScript(
  87. 'mailpoet_public',
  88. sprintf($inlineScript, $ajaxFailedErrorMessage),
  89. 'after'
  90. );
  91. }
  92. public function setupAdminWidgetPageDependencies() {
  93. $this->wp->wpEnqueueScript(
  94. 'mailpoet_vendor',
  95. Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('vendor.js'),
  96. [],
  97. Env::$version,
  98. true
  99. );
  100. $this->wp->wpEnqueueScript(
  101. 'mailpoet_admin',
  102. Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('mailpoet.js'),
  103. [],
  104. Env::$version,
  105. true
  106. );
  107. }
  108. }