Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
AutomaticEmails.php
91 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\Error as APIError; |
| 10 | use MailPoet\AutomaticEmails\AutomaticEmails as AutomaticEmailsController; |
| 11 | use MailPoet\Config\AccessControl; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class AutomaticEmails extends APIEndpoint { |
| 15 | public $permissions = [ |
| 16 | 'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS, |
| 17 | ]; |
| 18 | |
| 19 | /** @var AutomaticEmailsController */ |
| 20 | private $automaticEmails; |
| 21 | |
| 22 | /** @var WPFunctions */ |
| 23 | private $wp; |
| 24 | |
| 25 | public function __construct( |
| 26 | AutomaticEmailsController $automaticEmails, |
| 27 | WPFunctions $wp |
| 28 | ) { |
| 29 | $this->automaticEmails = $automaticEmails; |
| 30 | $this->wp = $wp; |
| 31 | } |
| 32 | |
| 33 | public function getEventOptions($data) { |
| 34 | $query = (!empty($data['query'])) ? $data['query'] : null; |
| 35 | $filter = (!empty($data['filter'])) ? $data['filter'] : null; |
| 36 | $emailSlug = (!empty($data['email_slug'])) ? $data['email_slug'] : null; |
| 37 | $eventSlug = (!empty($data['event_slug'])) ? $data['event_slug'] : null; |
| 38 | |
| 39 | if (!$query || !$filter || !$emailSlug || !$eventSlug) { |
| 40 | return $this->errorResponse( |
| 41 | [ |
| 42 | APIError::BAD_REQUEST => __('Improperly formatted request.', 'mailpoet'), |
| 43 | ] |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | $event = $this->automaticEmails->getAutomaticEmailEventBySlug($emailSlug, $eventSlug); |
| 48 | $eventFilter = (!empty($event['options']['remoteQueryFilter'])) ? $event['options']['remoteQueryFilter'] : null; |
| 49 | |
| 50 | return ($eventFilter === $filter && WPFunctions::get()->hasFilter($eventFilter)) ? |
| 51 | $this->successResponse($this->wp->applyFilters($eventFilter, $query)) : |
| 52 | $this->errorResponse( |
| 53 | [ |
| 54 | APIError::BAD_REQUEST => __('Automatic email event filter does not exist.', 'mailpoet'), |
| 55 | ] |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | public function getEventShortcodes($data) { |
| 60 | $emailSlug = (!empty($data['email_slug'])) ? $data['email_slug'] : null; |
| 61 | $eventSlug = (!empty($data['event_slug'])) ? $data['event_slug'] : null; |
| 62 | |
| 63 | if (!$emailSlug || !$eventSlug) { |
| 64 | return $this->errorResponse( |
| 65 | [ |
| 66 | APIError::BAD_REQUEST => __('Improperly formatted request.', 'mailpoet'), |
| 67 | ] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | $automaticEmail = $this->automaticEmails->getAutomaticEmailBySlug($emailSlug); |
| 72 | $event = $this->automaticEmails->getAutomaticEmailEventBySlug($emailSlug, $eventSlug); |
| 73 | |
| 74 | if (!$event) { |
| 75 | return $this->errorResponse( |
| 76 | [ |
| 77 | APIError::BAD_REQUEST => __('Automatic email event does not exist.', 'mailpoet'), |
| 78 | ] |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | $eventShortcodes = (!empty($event['shortcodes']) && is_array($event['shortcodes'])) ? |
| 83 | [ |
| 84 | $automaticEmail['title'] => $event['shortcodes'], |
| 85 | ] : |
| 86 | null; |
| 87 | |
| 88 | return $this->successResponse($eventShortcodes); |
| 89 | } |
| 90 | } |
| 91 |