WooCommerce
2 months ago
AutomaticEmailFactory.php
3 years ago
AutomaticEmails.php
2 months ago
index.php
3 years ago
AutomaticEmails.php
144 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\AutomaticEmails; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class AutomaticEmails { |
| 11 | const FILTER_PREFIX = 'mailpoet_automatic_email_'; |
| 12 | |
| 13 | /** @var WPFunctions */ |
| 14 | private $wp; |
| 15 | |
| 16 | /** @var array|null */ |
| 17 | private $automaticEmails; |
| 18 | |
| 19 | /** @var AutomaticEmailFactory */ |
| 20 | private $automaticEmailFactory; |
| 21 | |
| 22 | public function __construct( |
| 23 | WPFunctions $wp, |
| 24 | AutomaticEmailFactory $automaticEmailFactory |
| 25 | ) { |
| 26 | $this->wp = $wp; |
| 27 | $this->automaticEmailFactory = $automaticEmailFactory; |
| 28 | } |
| 29 | |
| 30 | public function init() { |
| 31 | $instance = $this->automaticEmailFactory->createWooCommerceEmail(); |
| 32 | $instance->init(); |
| 33 | } |
| 34 | |
| 35 | public function getAutomaticEmails() { |
| 36 | global $wp_filter; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 37 | |
| 38 | if ($this->automaticEmails) { |
| 39 | return $this->automaticEmails; |
| 40 | } |
| 41 | |
| 42 | // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 43 | $registeredGroups = preg_grep('!^' . self::FILTER_PREFIX . '(.*?)$!', array_keys($wp_filter)); |
| 44 | |
| 45 | if (empty($registeredGroups)) return null; |
| 46 | |
| 47 | $automaticEmails = []; |
| 48 | foreach ($registeredGroups as $group) { |
| 49 | $automaticEmail = $this->wp->applyFilters($group, []); |
| 50 | |
| 51 | if ( |
| 52 | !is_array($automaticEmail) || |
| 53 | !$this->validateAutomaticEmailDataFields($automaticEmail) || |
| 54 | !is_array($automaticEmail['events']) || |
| 55 | !$this->validateAutomaticEmailEventsDataFields($automaticEmail['events']) |
| 56 | ) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | // keys associative events array by slug |
| 61 | $automaticEmail['events'] = array_column($automaticEmail['events'], null, 'slug'); |
| 62 | // keys associative automatic email array by slug |
| 63 | $slug = is_string($automaticEmail['slug']) ? $automaticEmail['slug'] : ''; |
| 64 | $automaticEmails[$slug] = $automaticEmail; |
| 65 | } |
| 66 | |
| 67 | $this->automaticEmails = $automaticEmails; |
| 68 | |
| 69 | return $automaticEmails; |
| 70 | } |
| 71 | |
| 72 | public function getAutomaticEmailBySlug($emailSlug) { |
| 73 | $automaticEmails = $this->getAutomaticEmails(); |
| 74 | |
| 75 | if (empty($automaticEmails)) return null; |
| 76 | |
| 77 | foreach ($automaticEmails as $email) { |
| 78 | if (!empty($email['slug']) && $email['slug'] === $emailSlug) return $email; |
| 79 | } |
| 80 | |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | public function getAutomaticEmailEventBySlug($emailSlug, $eventSlug) { |
| 85 | $automaticEmail = $this->getAutomaticEmailBySlug($emailSlug); |
| 86 | |
| 87 | if (empty($automaticEmail)) return null; |
| 88 | |
| 89 | foreach ($automaticEmail['events'] as $event) { |
| 90 | if (!empty($event['slug']) && $event['slug'] === $eventSlug) return $event; |
| 91 | } |
| 92 | |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | public function validateAutomaticEmailDataFields(array $automaticEmail) { |
| 97 | $requiredFields = [ |
| 98 | 'slug', |
| 99 | 'title', |
| 100 | 'description', |
| 101 | 'events', |
| 102 | ]; |
| 103 | |
| 104 | foreach ($requiredFields as $field) { |
| 105 | if (empty($automaticEmail[$field])) return false; |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | public function validateAutomaticEmailEventsDataFields(array $automaticEmailEvents) { |
| 112 | $requiredFields = [ |
| 113 | 'slug', |
| 114 | 'title', |
| 115 | 'description', |
| 116 | 'listingScheduleDisplayText', |
| 117 | ]; |
| 118 | |
| 119 | foreach ($automaticEmailEvents as $event) { |
| 120 | if (!is_array($event)) return false; |
| 121 | $validEvent = array_diff($requiredFields, array_keys($event)); |
| 122 | if (!empty($validEvent)) return false; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | public function unregisterAutomaticEmails() { |
| 129 | global $wp_filter; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 130 | |
| 131 | // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 132 | $registeredGroups = preg_grep('!^' . self::FILTER_PREFIX . '(.*?)$!', array_keys($wp_filter)); |
| 133 | |
| 134 | if (empty($registeredGroups)) return null; |
| 135 | |
| 136 | $self = $this; |
| 137 | array_map(function($group) use($self) { |
| 138 | $self->wp->removeAllFilters($group); |
| 139 | }, $registeredGroups); |
| 140 | |
| 141 | $this->automaticEmails = null; |
| 142 | } |
| 143 | } |
| 144 |