PopulatorData
3 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
3 weeks ago
Capabilities.php
3 months ago
Changelog.php
2 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
6 months ago
Hooks.php
1 month ago
HooksWooCommerce.php
1 month ago
Initializer.php
1 month ago
Installer.php
10 months ago
Localizer.php
3 years ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
3 weeks ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
2 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 month ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
2 months ago
TranslationUpdater.php
3 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
1 week ago
index.php
3 years ago
AssetsLoader.php
87 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class AssetsLoader { |
| 11 | private const FORM_EDITOR_WORDPRESS_STYLE_DEPENDENCIES = [ |
| 12 | 'wp-components', |
| 13 | 'wp-block-library', |
| 14 | 'wp-block-library-theme', |
| 15 | 'wp-block-editor', |
| 16 | 'wp-edit-blocks', |
| 17 | 'wp-editor', |
| 18 | 'wp-edit-post', |
| 19 | 'wp-format-library', |
| 20 | ]; |
| 21 | |
| 22 | // Pages that load @wordpress/components from WordPress core, so they depend on the |
| 23 | // core `wp-components` handle instead of enqueueing our bundled copy (which would |
| 24 | // load the same styles twice with a fragile, environment-dependent order). |
| 25 | private const WORDPRESS_COMPONENTS_FROM_CORE_PAGES = [ |
| 26 | 'mailpoet-form-editor', |
| 27 | 'mailpoet-form-editor-template-selection', |
| 28 | ]; |
| 29 | |
| 30 | /** @var Renderer */ |
| 31 | private $renderer; |
| 32 | |
| 33 | /** @var WPFunctions */ |
| 34 | private $wp; |
| 35 | |
| 36 | public function __construct( |
| 37 | RendererFactory $rendererFactory, |
| 38 | WPFunctions $wp |
| 39 | ) { |
| 40 | $this->renderer = $rendererFactory->getRenderer(); |
| 41 | $this->wp = $wp; |
| 42 | } |
| 43 | |
| 44 | public function loadStyles(): void { |
| 45 | // MailPoet plugin style should be loaded on all mailpoet sites |
| 46 | $page = isset($_GET['page']) && is_string($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : null; |
| 47 | if ($page && strpos($page, 'mailpoet-') === 0) { |
| 48 | $componentsStyle = $this->enqueueWordPressComponentsStyle($page); |
| 49 | $this->enqueueStyle('mailpoet-plugin', [ |
| 50 | 'forms', // To prevent conflict in CSS with WP forms we need to add dependency |
| 51 | 'buttons', |
| 52 | $componentsStyle, // @wordpress/components must load before MailPoet overrides |
| 53 | ]); |
| 54 | } |
| 55 | if ($page === 'mailpoet-form-editor') { |
| 56 | $this->enqueueStyle( |
| 57 | 'mailpoet-form-editor', |
| 58 | array_merge(['mailpoet-plugin'], self::FORM_EDITOR_WORDPRESS_STYLE_DEPENDENCIES) |
| 59 | ); |
| 60 | $this->enqueueStyle('mailpoet-public'); |
| 61 | } |
| 62 | if ($page === 'mailpoet-form-editor-template-selection') { |
| 63 | $this->enqueueStyle('mailpoet-form-editor', ['mailpoet-plugin', 'wp-components']); |
| 64 | } |
| 65 | // We reuse a part of CSS in the newsletter editor |
| 66 | if ($page === 'mailpoet-newsletter-editor') { |
| 67 | $this->enqueueStyle('mailpoet-form-editor', ['mailpoet-plugin']); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private function enqueueWordPressComponentsStyle(string $page): string { |
| 72 | if (in_array($page, self::WORDPRESS_COMPONENTS_FROM_CORE_PAGES, true)) { |
| 73 | return 'wp-components'; |
| 74 | } |
| 75 | $this->enqueueStyle('mailpoet-wp-components'); |
| 76 | return 'mailpoet-wp-components'; |
| 77 | } |
| 78 | |
| 79 | private function enqueueStyle(string $name, array $deps = []): void { |
| 80 | $this->wp->wpEnqueueStyle( |
| 81 | $name, |
| 82 | Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset("{$name}.css"), |
| 83 | $deps |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 |