MPMarketingChannel.php
2 years ago
MPMarketingChannelController.php
2 years ago
MPMarketingChannelDataController.php
2 years ago
index.php
2 years ago
MPMarketingChannel.php
213 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WooCommerce\MultichannelMarketing; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\Marketing\MarketingCampaign; |
| 9 | use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType; |
| 10 | use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface; |
| 11 | use Automattic\WooCommerce\Admin\Marketing\Price; |
| 12 | use MailPoet\Config\Menu; |
| 13 | |
| 14 | class MPMarketingChannel implements MarketingChannelInterface { |
| 15 | |
| 16 | /** |
| 17 | * @var MarketingCampaignType[] |
| 18 | */ |
| 19 | private $campaignTypes; |
| 20 | |
| 21 | /** |
| 22 | * @var MPMarketingChannelDataController |
| 23 | */ |
| 24 | private $channelDataController; |
| 25 | |
| 26 | const CAMPAIGN_TYPE_NEWSLETTERS = 'mailpoet-newsletters'; |
| 27 | const CAMPAIGN_TYPE_POST_NOTIFICATIONS = 'mailpoet-post-notifications'; |
| 28 | const CAMPAIGN_TYPE_AUTOMATIONS = 'mailpoet-automations'; |
| 29 | |
| 30 | public function __construct( |
| 31 | MPMarketingChannelDataController $channelDataController |
| 32 | ) { |
| 33 | $this->channelDataController = $channelDataController; |
| 34 | $this->campaignTypes = $this->generateCampaignTypes(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the unique identifier string for the marketing channel extension, also known as the plugin slug. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function get_slug(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 43 | return 'mailpoet'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the name of the marketing channel. |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function get_name(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 52 | return __('MailPoet', 'mailpoet'); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the description of the marketing channel. |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | public function get_description(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 61 | return __('Create and send newsletters, post notifications and welcome emails from your WordPress.', 'mailpoet'); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the path to the channel icon. |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public function get_icon_url(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 70 | return $this->channelDataController->getIconUrl(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the setup status of the marketing channel. |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function is_setup_completed(): bool { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 79 | return $this->channelDataController->isMPSetupComplete(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the URL to the settings page, or the link to complete the setup/onboarding if the channel has not been set up yet. |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | public function get_setup_url(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 88 | if ($this->channelDataController->isMPSetupComplete()) { |
| 89 | return admin_url('admin.php?page=' . Menu::MAIN_PAGE_SLUG); |
| 90 | } |
| 91 | |
| 92 | return admin_url('admin.php?page=' . Menu::WELCOME_WIZARD_PAGE_SLUG . '&mailpoet_wizard_loaded_via_woocommerce_marketing_dashboard'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the status of the marketing channel's product listings. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function get_product_listings_status(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 101 | if (!$this->channelDataController->isMailPoetSendingServiceEnabled()) { |
| 102 | return self::PRODUCT_LISTINGS_NOT_APPLICABLE; |
| 103 | } |
| 104 | |
| 105 | // Check for error status. It's null by default when there isn't an error |
| 106 | $sendingStatus = $this->channelDataController->getMailPoetSendingStatus(); |
| 107 | |
| 108 | if ($sendingStatus) { |
| 109 | return self::PRODUCT_LISTINGS_SYNC_FAILED; |
| 110 | } |
| 111 | |
| 112 | return self::PRODUCT_LISTINGS_SYNCED; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the number of channel issues/errors (e.g. account-related errors, product synchronization issues, etc.). |
| 117 | * |
| 118 | * @return int The number of issues to resolve, or 0 if there are no issues with the channel. |
| 119 | */ |
| 120 | public function get_errors_count(): int { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 121 | return $this->channelDataController->getErrorCount(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns an array of marketing campaign types that the channel supports. |
| 126 | * |
| 127 | * @return MarketingCampaignType[] Array of marketing campaign type objects. |
| 128 | */ |
| 129 | public function get_supported_campaign_types(): array { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 130 | return $this->campaignTypes; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns an array of the channel's marketing campaigns. |
| 135 | * |
| 136 | * @return MarketingCampaign[] |
| 137 | */ |
| 138 | public function get_campaigns(): array { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 139 | $allCampaigns = $this->generateCampaigns(); |
| 140 | |
| 141 | if (empty($allCampaigns)) { |
| 142 | return []; |
| 143 | } |
| 144 | |
| 145 | return $allCampaigns; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Generate the marketing channel campaign types |
| 150 | * |
| 151 | * @return MarketingCampaignType[] |
| 152 | */ |
| 153 | protected function generateCampaignTypes(): array { |
| 154 | return [ |
| 155 | self::CAMPAIGN_TYPE_NEWSLETTERS => new MarketingCampaignType( |
| 156 | 'mailpoet-newsletters', |
| 157 | $this, |
| 158 | __('MailPoet Newsletters', 'mailpoet'), |
| 159 | __( |
| 160 | 'Send a newsletter with images, buttons, dividers, and social bookmarks. Or, just send a basic text email.', |
| 161 | 'mailpoet', |
| 162 | ), |
| 163 | admin_url('admin.php?page=' . Menu::EMAILS_PAGE_SLUG . '&loadedvia=woo_multichannel_dashboard#/new/standard'), |
| 164 | $this->get_icon_url() |
| 165 | ), |
| 166 | self::CAMPAIGN_TYPE_POST_NOTIFICATIONS => new MarketingCampaignType( |
| 167 | 'mailpoet-post-notifications', |
| 168 | $this, |
| 169 | __('MailPoet Post Notifications', 'mailpoet'), |
| 170 | __( |
| 171 | 'Let MailPoet email your subscribers with your latest content. You can send daily, weekly, monthly, or even immediately after publication.', |
| 172 | 'mailpoet', |
| 173 | ), |
| 174 | admin_url('admin.php?page=' . Menu::EMAILS_PAGE_SLUG . '&loadedvia=woo_multichannel_dashboard#/new/notification'), |
| 175 | $this->get_icon_url() |
| 176 | ), |
| 177 | self::CAMPAIGN_TYPE_AUTOMATIONS => new MarketingCampaignType( |
| 178 | 'mailpoet-automations', |
| 179 | $this, |
| 180 | __('MailPoet Automations', 'mailpoet'), |
| 181 | __('Set up automations to send abandoned cart reminders, welcome new subscribers, celebrate first-time buyers, and much more.', 'mailpoet'), |
| 182 | admin_url('admin.php?page=' . Menu::AUTOMATION_TEMPLATES_PAGE_SLUG . '&loadedvia=woo_multichannel_dashboard'), |
| 183 | $this->get_icon_url() |
| 184 | ), |
| 185 | ]; |
| 186 | } |
| 187 | |
| 188 | protected function generateCampaigns(): array { |
| 189 | return array_map( |
| 190 | function (array $data) { |
| 191 | $cost = null; |
| 192 | |
| 193 | if (isset($data['price'])) { |
| 194 | $cost = new Price((string)$data['price']['amount'], $data['price']['currency']); |
| 195 | } |
| 196 | |
| 197 | return new MarketingCampaign( |
| 198 | $data['id'], |
| 199 | $data['campaignType'], |
| 200 | $data['name'], |
| 201 | $data['url'], |
| 202 | $cost, |
| 203 | ); |
| 204 | }, |
| 205 | array_merge( |
| 206 | $this->channelDataController->getAutomations($this->campaignTypes[self::CAMPAIGN_TYPE_AUTOMATIONS]), |
| 207 | $this->channelDataController->getPostNotificationNewsletters($this->campaignTypes[self::CAMPAIGN_TYPE_POST_NOTIFICATIONS]), |
| 208 | $this->channelDataController->getStandardNewsletterList($this->campaignTypes[self::CAMPAIGN_TYPE_NEWSLETTERS]) |
| 209 | ) |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 |