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
DynamicSegments.php
263 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\AdminPages\Pages; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AdminPages\AssetsController; |
| 9 | use MailPoet\AdminPages\PageRenderer; |
| 10 | use MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder; |
| 11 | use MailPoet\Automation\Engine\Data\Automation; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 13 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 14 | use MailPoet\Entities\DynamicSegmentFilterData; |
| 15 | use MailPoet\Entities\FormEntity; |
| 16 | use MailPoet\Entities\SegmentEntity; |
| 17 | use MailPoet\Form\FormsRepository; |
| 18 | use MailPoet\Newsletter\NewslettersRepository; |
| 19 | use MailPoet\Segments\SegmentDependencyValidator; |
| 20 | use MailPoet\Segments\SegmentsRepository; |
| 21 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 22 | use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader; |
| 23 | use MailPoet\WP\Functions as WPFunctions; |
| 24 | use MailPoetVendor\Doctrine\Common\Collections\Criteria; |
| 25 | |
| 26 | class DynamicSegments { |
| 27 | /** @var AssetsController */ |
| 28 | private $assetsController; |
| 29 | |
| 30 | /** @var PageRenderer */ |
| 31 | private $pageRenderer; |
| 32 | |
| 33 | /** @var WPFunctions */ |
| 34 | private $wp; |
| 35 | |
| 36 | /** @var WooCommerceHelper */ |
| 37 | private $woocommerceHelper; |
| 38 | |
| 39 | /** @var WPPostListLoader */ |
| 40 | private $wpPostListLoader; |
| 41 | |
| 42 | /** @var SegmentDependencyValidator */ |
| 43 | private $segmentDependencyValidator; |
| 44 | |
| 45 | /** @var CustomFieldsRepository */ |
| 46 | private $customFieldsRepository; |
| 47 | |
| 48 | /** @var CustomFieldsResponseBuilder */ |
| 49 | private $customFieldsResponseBuilder; |
| 50 | |
| 51 | /** @var SegmentsRepository */ |
| 52 | private $segmentsRepository; |
| 53 | |
| 54 | /** @var NewslettersRepository */ |
| 55 | private $newslettersRepository; |
| 56 | |
| 57 | /** @var FormsRepository */ |
| 58 | private $formsRepository; |
| 59 | |
| 60 | /** @var AutomationStorage */ |
| 61 | private $automationStorage; |
| 62 | |
| 63 | public function __construct( |
| 64 | AssetsController $assetsController, |
| 65 | PageRenderer $pageRenderer, |
| 66 | WPFunctions $wp, |
| 67 | WooCommerceHelper $woocommerceHelper, |
| 68 | WPPostListLoader $wpPostListLoader, |
| 69 | CustomFieldsRepository $customFieldsRepository, |
| 70 | CustomFieldsResponseBuilder $customFieldsResponseBuilder, |
| 71 | SegmentDependencyValidator $segmentDependencyValidator, |
| 72 | SegmentsRepository $segmentsRepository, |
| 73 | NewslettersRepository $newslettersRepository, |
| 74 | FormsRepository $formsRepository, |
| 75 | AutomationStorage $automationStorage |
| 76 | ) { |
| 77 | $this->assetsController = $assetsController; |
| 78 | $this->pageRenderer = $pageRenderer; |
| 79 | $this->wp = $wp; |
| 80 | $this->woocommerceHelper = $woocommerceHelper; |
| 81 | $this->wpPostListLoader = $wpPostListLoader; |
| 82 | $this->segmentDependencyValidator = $segmentDependencyValidator; |
| 83 | $this->customFieldsRepository = $customFieldsRepository; |
| 84 | $this->customFieldsResponseBuilder = $customFieldsResponseBuilder; |
| 85 | $this->segmentsRepository = $segmentsRepository; |
| 86 | $this->newslettersRepository = $newslettersRepository; |
| 87 | $this->formsRepository = $formsRepository; |
| 88 | $this->automationStorage = $automationStorage; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return void |
| 93 | */ |
| 94 | public function render() { |
| 95 | $data = []; |
| 96 | $customFields = $this->customFieldsRepository->findBy(['deletedAt' => null], ['name' => 'asc']); |
| 97 | $data['custom_fields'] = $this->customFieldsResponseBuilder->buildBatch($customFields); |
| 98 | |
| 99 | $wpRoles = $this->wp->getEditableRoles(); |
| 100 | $data['wordpress_editable_roles_list'] = array_map(function($roleId, $role) { |
| 101 | return [ |
| 102 | 'role_id' => $roleId, |
| 103 | 'role_name' => $role['name'], |
| 104 | ]; |
| 105 | }, array_keys($wpRoles), $wpRoles); |
| 106 | |
| 107 | $data['newsletters_list'] = $this->getNewslettersList(); |
| 108 | |
| 109 | $data['static_segments_list'] = []; |
| 110 | $criteria = new Criteria(); |
| 111 | $criteria->where(Criteria::expr()->isNull('deletedAt')); |
| 112 | $criteria->andWhere(Criteria::expr()->neq('type', SegmentEntity::TYPE_DYNAMIC)); |
| 113 | $criteria->orderBy(['name' => 'ASC']); |
| 114 | $segments = $this->segmentsRepository->matching($criteria); |
| 115 | foreach ($segments as $segment) { |
| 116 | $data['static_segments_list'][] = [ |
| 117 | 'id' => $segment->getId(), |
| 118 | 'name' => $segment->getName(), |
| 119 | 'type' => $segment->getType(), |
| 120 | 'description' => $segment->getDescription(), |
| 121 | ]; |
| 122 | } |
| 123 | |
| 124 | $data['product_attributes'] = []; |
| 125 | if ($this->woocommerceHelper->isWooCommerceActive()) { |
| 126 | $productAttributes = $this->woocommerceHelper->wcGetAttributeTaxonomies(); |
| 127 | |
| 128 | foreach ($productAttributes as $attribute) { |
| 129 | $taxonomy = 'pa_' . $attribute->attribute_name;// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 130 | $attributeTerms = $this->wp->getTerms( |
| 131 | [ |
| 132 | 'taxonomy' => $taxonomy, |
| 133 | 'hide_empty' => false, |
| 134 | ] |
| 135 | ); |
| 136 | |
| 137 | if ((!$attributeTerms instanceof \WP_Error) && !isset($attributeTerms['errors'])) { |
| 138 | $data['product_attributes'][$taxonomy] = [ // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 139 | 'id' => $attribute->attribute_id, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 140 | 'label' => $attribute->attribute_label, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 141 | 'terms' => $attributeTerms, |
| 142 | 'taxonomy' => $taxonomy, |
| 143 | ]; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Fetch local attributes used for product variations |
| 148 | $data['local_product_attributes'] = []; |
| 149 | $localAttributes = $this->getLocalAttributesUsedInProductVariations(); |
| 150 | foreach ($localAttributes as $localAttribute => $values) { |
| 151 | $data['local_product_attributes'][$localAttribute] = [ |
| 152 | 'name' => $localAttribute, |
| 153 | 'values' => $values, |
| 154 | ]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $data['product_categories'] = $this->wpPostListLoader->getWooCommerceCategories(); |
| 159 | $data['product_tags'] = $this->wpPostListLoader->getWooCommerceTags(); |
| 160 | |
| 161 | $data['products'] = $this->wpPostListLoader->getProducts(); |
| 162 | $data['variable_products'] = $this->woocommerceHelper->isWooCommerceActive() |
| 163 | ? $this->wpPostListLoader->getVariableProducts() |
| 164 | : []; |
| 165 | $data['membership_plans'] = $this->wpPostListLoader->getMembershipPlans(); |
| 166 | $data['subscription_products'] = $this->wpPostListLoader->getSubscriptionProducts(); |
| 167 | $wcCountries = $this->woocommerceHelper->isWooCommerceActive() ? $this->woocommerceHelper->getAllowedCountries() : []; |
| 168 | $data['woocommerce_countries'] = array_map(function ($code, $name) { |
| 169 | return [ |
| 170 | 'name' => $name, |
| 171 | 'code' => $code, |
| 172 | ]; |
| 173 | }, array_keys($wcCountries), $wcCountries); |
| 174 | $data['can_use_woocommerce_memberships'] = $this->segmentDependencyValidator->canUseDynamicFilterType( |
| 175 | DynamicSegmentFilterData::TYPE_WOOCOMMERCE_MEMBERSHIP |
| 176 | ); |
| 177 | $data['can_use_woocommerce_subscriptions'] = $this->segmentDependencyValidator->canUseDynamicFilterType( |
| 178 | DynamicSegmentFilterData::TYPE_WOOCOMMERCE_SUBSCRIPTION |
| 179 | ); |
| 180 | $wooCurrencySymbol = $this->woocommerceHelper->isWooCommerceActive() ? $this->woocommerceHelper->getWoocommerceCurrencySymbol() : ''; |
| 181 | $data['woocommerce_currency_symbol'] = html_entity_decode($wooCurrencySymbol, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); |
| 182 | $data['signup_forms'] = array_map(function(FormEntity $form) { |
| 183 | return [ |
| 184 | 'id' => $form->getId(), |
| 185 | 'name' => $form->getName(), |
| 186 | ]; |
| 187 | }, $this->formsRepository->findAll()); |
| 188 | |
| 189 | $data['woocommerce_payment_methods'] = []; |
| 190 | $data['woocommerce_shipping_methods'] = []; |
| 191 | |
| 192 | if ($this->woocommerceHelper->isWooCommerceActive()) { |
| 193 | $allGateways = $this->woocommerceHelper->getPaymentGateways()->payment_gateways(); |
| 194 | $paymentMethods = []; |
| 195 | foreach ($allGateways as $gatewayId => $gateway) { |
| 196 | $paymentMethods[] = [ |
| 197 | 'id' => $gatewayId, |
| 198 | 'name' => $gateway->get_method_title(), |
| 199 | ]; |
| 200 | } |
| 201 | $data['woocommerce_payment_methods'] = $paymentMethods; |
| 202 | |
| 203 | $data['woocommerce_shipping_methods'] = array_values($this->woocommerceHelper->getShippingMethodInstancesData()); |
| 204 | } |
| 205 | $data['automations'] = array_map(function(Automation $automation) { |
| 206 | return [ |
| 207 | 'id' => (string)$automation->getId(), |
| 208 | 'name' => $automation->getName(), |
| 209 | ]; |
| 210 | }, $this->automationStorage->getAutomations()); |
| 211 | |
| 212 | $this->assetsController->setupDynamicSegmentsDependencies(); |
| 213 | $this->assetsController->setupDataViewsDependencies(); |
| 214 | $data['api'] = [ |
| 215 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 216 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 217 | ]; |
| 218 | $this->pageRenderer->displayPage('segments/dynamic.html', $data); |
| 219 | } |
| 220 | |
| 221 | private function getLocalAttributesUsedInProductVariations(): array { |
| 222 | $attributes = []; |
| 223 | |
| 224 | if (!$this->woocommerceHelper->isWooCommerceActive()) { |
| 225 | return $attributes; |
| 226 | } |
| 227 | global $wpdb; |
| 228 | |
| 229 | $results = $wpdb->get_results($wpdb->prepare(" |
| 230 | SELECT DISTINCT pm.meta_key, pm.meta_value |
| 231 | FROM %i pm |
| 232 | INNER JOIN %i p ON pm.post_id = p.ID |
| 233 | WHERE pm.meta_key LIKE %s |
| 234 | AND p.post_type = 'product_variation' |
| 235 | GROUP BY pm.meta_key, pm.meta_value |
| 236 | ", $wpdb->postmeta, $wpdb->posts, 'attribute_%'), ARRAY_A); |
| 237 | |
| 238 | foreach ($results as $result) { |
| 239 | $attribute = substr($result['meta_key'], 10); |
| 240 | if (!isset($attributes[$attribute])) { |
| 241 | $attributes[$attribute] = []; |
| 242 | } |
| 243 | $attributes[$attribute][] = $result['meta_value']; |
| 244 | } |
| 245 | |
| 246 | return $attributes; |
| 247 | } |
| 248 | |
| 249 | private function getNewslettersList(): array { |
| 250 | $result = []; |
| 251 | foreach ($this->newslettersRepository->getStandardAndAutomationNewsletterList() as $newsletter) { |
| 252 | $result[] = [ |
| 253 | 'id' => (string)$newsletter->getId(), |
| 254 | 'subject' => $newsletter->getSubject(), |
| 255 | 'name' => $newsletter->getCampaignNameOrSubject(), |
| 256 | 'type' => $newsletter->getType(), |
| 257 | 'sent_at' => ($sentAt = $newsletter->getSentAt()) ? $sentAt->format('Y-m-d H:i:s') : null, |
| 258 | ]; |
| 259 | } |
| 260 | return $result; |
| 261 | } |
| 262 | } |
| 263 |