Events
1 year ago
WooCommerce.php
2 years ago
WooCommerceEventFactory.php
3 years ago
index.php
3 years ago
WooCommerce.php
123 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\AutomaticEmails\WooCommerce; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AutomaticEmails\AutomaticEmails; |
| 9 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use MailPoet\WP\Notice; |
| 12 | |
| 13 | class WooCommerce { |
| 14 | const SLUG = 'woocommerce'; |
| 15 | const EVENTS_FILTER = 'mailpoet_woocommerce_events'; |
| 16 | |
| 17 | /** @var WooCommerceHelper */ |
| 18 | private $woocommerceHelper; |
| 19 | |
| 20 | /** @var string[] */ |
| 21 | public $availableEvents = [ |
| 22 | 'AbandonedCart', |
| 23 | 'FirstPurchase', |
| 24 | 'PurchasedInCategory', |
| 25 | 'PurchasedProduct', |
| 26 | ]; |
| 27 | |
| 28 | /** @var bool */ |
| 29 | private $woocommerceEnabled; |
| 30 | |
| 31 | /** @var WPFunctions */ |
| 32 | private $wp; |
| 33 | |
| 34 | /** @var WooCommerceEventFactory */ |
| 35 | private $eventFactory; |
| 36 | |
| 37 | public function __construct( |
| 38 | WPFunctions $wp, |
| 39 | WooCommerceHelper $woocommerceHelper, |
| 40 | WooCommerceEventFactory $eventFactory |
| 41 | ) { |
| 42 | $this->wp = $wp; |
| 43 | $this->woocommerceHelper = $woocommerceHelper; |
| 44 | $this->woocommerceEnabled = $this->isWoocommerceEnabled(); |
| 45 | $this->eventFactory = $eventFactory; |
| 46 | } |
| 47 | |
| 48 | public function init() { |
| 49 | $this->wp->addFilter( |
| 50 | AutomaticEmails::FILTER_PREFIX . self::SLUG, |
| 51 | [ |
| 52 | $this, |
| 53 | 'setupGroup', |
| 54 | ] |
| 55 | ); |
| 56 | $this->wp->addFilter( |
| 57 | self::EVENTS_FILTER, |
| 58 | [ |
| 59 | $this, |
| 60 | 'setupEvents', |
| 61 | ] |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | public function setupGroup() { |
| 66 | return [ |
| 67 | 'slug' => self::SLUG, |
| 68 | 'title' => __('WooCommerce', 'mailpoet'), |
| 69 | 'description' => __('Automatically send an email based on your customers’ purchase behavior. Enhance your customer service and start increasing sales with WooCommerce follow up emails.', 'mailpoet'), |
| 70 | 'events' => $this->wp->applyFilters(self::EVENTS_FILTER, []), |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | public function setupEvents($events) { |
| 75 | $customEventDetails = (!$this->woocommerceEnabled) ? [ |
| 76 | 'actionButtonTitle' => __('WooCommerce is required', 'mailpoet'), |
| 77 | 'actionButtonLink' => 'https://wordpress.org/plugins/woocommerce/', |
| 78 | ] : []; |
| 79 | |
| 80 | foreach ($this->availableEvents as $event) { |
| 81 | $eventInstance = in_array($event, $this->availableEvents, true) |
| 82 | ? $this->eventFactory->createEvent($event) |
| 83 | : null; |
| 84 | |
| 85 | if (!$eventInstance) { |
| 86 | $this->displayEventWarning($event); |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if (method_exists($eventInstance, 'init')) { |
| 91 | $eventInstance->init(); |
| 92 | } else { |
| 93 | $this->displayEventWarning($event); |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | if (method_exists($eventInstance, 'getEventDetails')) { |
| 98 | $eventDetails = array_merge($eventInstance->getEventDetails(), $customEventDetails); |
| 99 | } else { |
| 100 | $this->displayEventWarning($event); |
| 101 | continue; |
| 102 | } |
| 103 | $events[] = $eventDetails; |
| 104 | } |
| 105 | |
| 106 | return $events; |
| 107 | } |
| 108 | |
| 109 | public function isWoocommerceEnabled() { |
| 110 | return $this->woocommerceHelper->isWooCommerceActive(); |
| 111 | } |
| 112 | |
| 113 | private function displayEventWarning($event) { |
| 114 | $notice = sprintf( |
| 115 | '%s %s', |
| 116 | // translators: %s is the name of the event. |
| 117 | sprintf(__('WooCommerce %s event is misconfigured.', 'mailpoet'), $event), |
| 118 | __('Please contact our technical support for assistance.', 'mailpoet') |
| 119 | ); |
| 120 | Notice::displayWarning($notice); |
| 121 | } |
| 122 | } |
| 123 |