Geen omschrijving

WooCommerce.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace MailPoet\AutomaticEmails\WooCommerce;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\AutomaticEmails\AutomaticEmails;
  5. use MailPoet\WooCommerce\Helper as WooCommerceHelper;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. use MailPoet\WP\Notice;
  8. class WooCommerce {
  9. const SLUG = 'woocommerce';
  10. const EVENTS_FILTER = 'mailpoet_woocommerce_events';
  11. /** @var WooCommerceHelper */
  12. private $woocommerceHelper;
  13. public $availableEvents = [
  14. 'AbandonedCart',
  15. 'FirstPurchase',
  16. 'PurchasedInCategory',
  17. 'PurchasedProduct',
  18. ];
  19. private $woocommerceEnabled;
  20. private $wp;
  21. public function __construct() {
  22. $this->wp = new WPFunctions;
  23. $this->woocommerceHelper = new WooCommerceHelper();
  24. $this->woocommerceEnabled = $this->isWoocommerceEnabled();
  25. }
  26. public function init() {
  27. $this->wp->addFilter(
  28. AutomaticEmails::FILTER_PREFIX . self::SLUG,
  29. [
  30. $this,
  31. 'setupGroup',
  32. ]
  33. );
  34. $this->wp->addFilter(
  35. self::EVENTS_FILTER,
  36. [
  37. $this,
  38. 'setupEvents',
  39. ]
  40. );
  41. }
  42. public function setupGroup() {
  43. return [
  44. 'slug' => self::SLUG,
  45. 'title' => WPFunctions::get()->__('WooCommerce', 'mailpoet'),
  46. 'description' => WPFunctions::get()->__('Automatically send an email based on your customers’ purchase behavior. Enhance your customer service and start increasing sales with WooCommerce follow up emails.', 'mailpoet'),
  47. 'events' => $this->wp->applyFilters(self::EVENTS_FILTER, []),
  48. ];
  49. }
  50. public function setupEvents($events) {
  51. $customEventDetails = (!$this->woocommerceEnabled) ? [
  52. 'actionButtonTitle' => WPFunctions::get()->__('WooCommerce is required', 'mailpoet'),
  53. 'actionButtonLink' => 'https://wordpress.org/plugins/woocommerce/',
  54. ] : [];
  55. foreach ($this->availableEvents as $event) {
  56. $eventClass = sprintf(
  57. '%s\Events\%s',
  58. __NAMESPACE__,
  59. $event
  60. );
  61. if (!class_exists($eventClass)) {
  62. $this->displayEventWarning($event);
  63. continue;
  64. }
  65. $eventInstance = new $eventClass();
  66. if (method_exists($eventInstance, 'init')) {
  67. $eventInstance->init();
  68. } else {
  69. $this->displayEventWarning($event);
  70. continue;
  71. }
  72. if (method_exists($eventInstance, 'getEventDetails')) {
  73. $eventDetails = array_merge($eventInstance->getEventDetails(), $customEventDetails);
  74. } else {
  75. $this->displayEventWarning($event);
  76. continue;
  77. }
  78. $events[] = $eventDetails;
  79. }
  80. return $events;
  81. }
  82. public function isWoocommerceEnabled() {
  83. return $this->woocommerceHelper->isWooCommerceActive();
  84. }
  85. private function displayEventWarning($event) {
  86. $notice = sprintf('%s %s',
  87. sprintf(__('WooCommerce %s event is misconfigured.', 'mailpoet'), $event),
  88. WPFunctions::get()->__('Please contact our technical support for assistance.', 'mailpoet')
  89. );
  90. Notice::displayWarning($notice);
  91. }
  92. }