AbstractAutomationEmbed.php
5 months ago
Automation.php
2 years ago
AutomationAnalytics.php
2 months ago
AutomationEditor.php
2 months ago
AutomationFlowEmbed.php
5 months ago
AutomationPreviewEmbed.php
2 months ago
AutomationTemplates.php
2 years ago
CustomFields.php
2 months ago
DynamicSegments.php
2 months ago
ExperimentalFeatures.php
3 years ago
FormEditor.php
1 week ago
Forms.php
2 months ago
Help.php
2 months ago
Homepage.php
2 years ago
Landingpage.php
2 years ago
Logs.php
1 month ago
NewsletterEditor.php
2 months ago
Newsletters.php
2 months ago
Settings.php
5 months ago
StaticSegments.php
2 months ago
Subscribers.php
1 week ago
SubscribersExport.php
3 years ago
SubscribersImport.php
2 months ago
Tags.php
2 months ago
Upgrade.php
1 year ago
WelcomeWizard.php
2 months ago
WooCommerceSetup.php
2 months ago
index.php
3 years ago
AbstractAutomationEmbed.php
208 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\AdminPages\Pages; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AdminPages\AssetsController; |
| 9 | use MailPoet\Automation\Engine\Control\SubjectTransformerHandler; |
| 10 | use MailPoet\Automation\Engine\Data\Field; |
| 11 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 12 | use MailPoet\Automation\Engine\Registry; |
| 13 | use MailPoet\Config\Renderer; |
| 14 | use MailPoet\Config\ServicesChecker; |
| 15 | use MailPoet\Settings\TrackingConfig; |
| 16 | use MailPoet\Util\License\Features\CapabilitiesManager; |
| 17 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 18 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 19 | use MailPoet\WooCommerce\WooCommerceBookings\Helper as WooCommerceBookingsHelper; |
| 20 | use MailPoet\WooCommerce\WooCommerceSubscriptions\Helper as WooCommerceSubscriptionsHelper; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | |
| 23 | abstract class AbstractAutomationEmbed { |
| 24 | protected AssetsController $assetsController; |
| 25 | protected Registry $registry; |
| 26 | protected Renderer $renderer; |
| 27 | protected TrackingConfig $trackingConfig; |
| 28 | protected WPFunctions $wp; |
| 29 | protected SubscribersFeature $subscribersFeature; |
| 30 | protected CapabilitiesManager $capabilitiesManager; |
| 31 | protected ServicesChecker $servicesChecker; |
| 32 | protected WooCommerceHelper $wooCommerceHelper; |
| 33 | protected WooCommerceSubscriptionsHelper $wooCommerceSubscriptionsHelper; |
| 34 | protected WooCommerceBookingsHelper $wooCommerceBookingsHelper; |
| 35 | protected SubjectTransformerHandler $subjectTransformerHandler; |
| 36 | |
| 37 | public function __construct( |
| 38 | AssetsController $assetsController, |
| 39 | Registry $registry, |
| 40 | Renderer $renderer, |
| 41 | TrackingConfig $trackingConfig, |
| 42 | WPFunctions $wp, |
| 43 | SubscribersFeature $subscribersFeature, |
| 44 | CapabilitiesManager $capabilitiesManager, |
| 45 | ServicesChecker $servicesChecker, |
| 46 | WooCommerceHelper $wooCommerceHelper, |
| 47 | WooCommerceSubscriptionsHelper $wooCommerceSubscriptionsHelper, |
| 48 | WooCommerceBookingsHelper $wooCommerceBookingsHelper, |
| 49 | SubjectTransformerHandler $subjectTransformerHandler |
| 50 | ) { |
| 51 | $this->assetsController = $assetsController; |
| 52 | $this->registry = $registry; |
| 53 | $this->renderer = $renderer; |
| 54 | $this->trackingConfig = $trackingConfig; |
| 55 | $this->wp = $wp; |
| 56 | $this->subscribersFeature = $subscribersFeature; |
| 57 | $this->capabilitiesManager = $capabilitiesManager; |
| 58 | $this->servicesChecker = $servicesChecker; |
| 59 | $this->wooCommerceHelper = $wooCommerceHelper; |
| 60 | $this->wooCommerceSubscriptionsHelper = $wooCommerceSubscriptionsHelper; |
| 61 | $this->wooCommerceBookingsHelper = $wooCommerceBookingsHelper; |
| 62 | $this->subjectTransformerHandler = $subjectTransformerHandler; |
| 63 | } |
| 64 | |
| 65 | abstract public function render(): void; |
| 66 | |
| 67 | abstract protected function setupDependencies(): void; |
| 68 | |
| 69 | abstract protected function getTemplateName(): string; |
| 70 | |
| 71 | abstract protected function getCustomData(): array; |
| 72 | |
| 73 | protected function renderEmbed(): void { |
| 74 | $this->disableAdminBarAndEmojis(); |
| 75 | $this->setupDependencies(); |
| 76 | |
| 77 | $headContent = $this->captureHead(); |
| 78 | $footerContent = $this->captureFooter(); |
| 79 | |
| 80 | $data = array_merge( |
| 81 | $this->getBaseData(), |
| 82 | $this->getCustomData(), |
| 83 | [ |
| 84 | 'head_content' => $headContent, |
| 85 | 'footer_content' => $footerContent, |
| 86 | ] |
| 87 | ); |
| 88 | |
| 89 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 90 | echo $this->renderer->render($this->getTemplateName(), $data); |
| 91 | exit; |
| 92 | } |
| 93 | |
| 94 | protected function disableAdminBarAndEmojis(): void { |
| 95 | // Disable admin bar for embed (intentional for iframe display) |
| 96 | // phpcs:ignore WordPressVIPMinimum.UserExperience.AdminBarRemoval.RemovalDetected |
| 97 | add_filter('show_admin_bar', '__return_false'); |
| 98 | |
| 99 | // Disable WordPress emoji handling (prevents deprecation warning) |
| 100 | remove_action('wp_head', 'print_emoji_detection_script', 7); |
| 101 | remove_action('wp_print_styles', 'print_emoji_styles'); |
| 102 | remove_action('admin_print_scripts', 'print_emoji_detection_script'); |
| 103 | remove_action('admin_print_styles', 'print_emoji_styles'); |
| 104 | } |
| 105 | |
| 106 | protected function captureHead(): string { |
| 107 | ob_start(); |
| 108 | wp_head(); |
| 109 | return (string)ob_get_clean(); |
| 110 | } |
| 111 | |
| 112 | protected function captureFooter(): string { |
| 113 | ob_start(); |
| 114 | wp_footer(); |
| 115 | return (string)ob_get_clean(); |
| 116 | } |
| 117 | |
| 118 | protected function getBaseData(): array { |
| 119 | return [ |
| 120 | 'locale' => $this->wp->getLocale(), |
| 121 | 'api' => [ |
| 122 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 123 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 124 | ], |
| 125 | 'registry' => $this->buildRegistry(), |
| 126 | 'context' => $this->buildContext(), |
| 127 | 'tracking_config' => $this->trackingConfig->getConfig(), |
| 128 | 'has_valid_premium_key' => $this->subscribersFeature->hasValidPremiumKey(), |
| 129 | 'subscribers_limit_reached' => $this->subscribersFeature->check(), |
| 130 | 'premium_active' => $this->servicesChecker->isPremiumPluginActive(), |
| 131 | 'capabilities' => $this->capabilitiesManager->getCapabilities(), |
| 132 | 'woocommerce_active' => $this->wooCommerceHelper->isWooCommerceActive(), |
| 133 | 'woocommerce_subscriptions_active' => $this->wooCommerceSubscriptionsHelper->isWooCommerceSubscriptionsActive(), |
| 134 | 'woocommerce_bookings_active' => $this->wooCommerceBookingsHelper->isWooCommerceBookingsActive(), |
| 135 | 'woocommerce_store_config' => $this->wooCommerceHelper->isWooCommerceActive() ? $this->wooCommerceHelper->getWoocommerceStoreConfig() : null, |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | protected function buildRegistry(): array { |
| 140 | $steps = []; |
| 141 | foreach ($this->registry->getSteps() as $key => $step) { |
| 142 | $steps[$key] = [ |
| 143 | 'key' => $step->getKey(), |
| 144 | 'name' => $step->getName(), |
| 145 | 'subject_keys' => $step instanceof Trigger ? $this->subjectTransformerHandler->getSubjectKeysForTrigger($step) : $step->getSubjectKeys(), |
| 146 | 'args_schema' => $step->getArgsSchema()->toArray(), |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | $subjects = []; |
| 151 | foreach ($this->registry->getSubjects() as $key => $subject) { |
| 152 | $subjectFields = $subject->getFields(); |
| 153 | usort($subjectFields, function (Field $a, Field $b) { |
| 154 | return $a->getName() <=> $b->getName(); |
| 155 | }); |
| 156 | |
| 157 | $subjects[$key] = [ |
| 158 | 'key' => $subject->getKey(), |
| 159 | 'name' => $subject->getName(), |
| 160 | 'args_schema' => $subject->getArgsSchema()->toArray(), |
| 161 | 'field_keys' => array_map(function ($field) { |
| 162 | return $field->getKey(); |
| 163 | }, $subjectFields), |
| 164 | ]; |
| 165 | } |
| 166 | |
| 167 | $fields = []; |
| 168 | foreach ($this->registry->getFields() as $key => $field) { |
| 169 | $fields[$key] = [ |
| 170 | 'key' => $field->getKey(), |
| 171 | 'type' => $field->getType(), |
| 172 | 'name' => $field->getName(), |
| 173 | 'args' => $field->getArgs(), |
| 174 | ]; |
| 175 | } |
| 176 | |
| 177 | $filters = []; |
| 178 | foreach ($this->registry->getFilters() as $fieldType => $filter) { |
| 179 | $conditions = []; |
| 180 | foreach ($filter->getConditions() as $key => $label) { |
| 181 | $conditions[] = [ |
| 182 | 'key' => $key, |
| 183 | 'label' => $label, |
| 184 | ]; |
| 185 | } |
| 186 | $filters[$fieldType] = [ |
| 187 | 'field_type' => $filter->getFieldType(), |
| 188 | 'conditions' => $conditions, |
| 189 | ]; |
| 190 | } |
| 191 | |
| 192 | return [ |
| 193 | 'steps' => $steps, |
| 194 | 'subjects' => $subjects, |
| 195 | 'fields' => $fields, |
| 196 | 'filters' => $filters, |
| 197 | ]; |
| 198 | } |
| 199 | |
| 200 | protected function buildContext(): array { |
| 201 | $data = []; |
| 202 | foreach ($this->registry->getContextFactories() as $key => $factory) { |
| 203 | $data[$key] = $factory(); |
| 204 | } |
| 205 | return $data; |
| 206 | } |
| 207 | } |
| 208 |