Ei kuvausta

AutomaticEmails.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace MailPoet\API\JSON\v1;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\API\JSON\Endpoint as APIEndpoint;
  5. use MailPoet\API\JSON\Error as APIError;
  6. use MailPoet\Config\AccessControl;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. class AutomaticEmails extends APIEndpoint {
  9. public $permissions = [
  10. 'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS,
  11. ];
  12. private $wp;
  13. public function __construct() {
  14. $this->wp = new WPFunctions;
  15. }
  16. public function getEventOptions($data) {
  17. $query = (!empty($data['query'])) ? $data['query'] : null;
  18. $filter = (!empty($data['filter'])) ? $data['filter'] : null;
  19. $emailSlug = (!empty($data['email_slug'])) ? $data['email_slug'] : null;
  20. $eventSlug = (!empty($data['event_slug'])) ? $data['event_slug'] : null;
  21. if (!$query || !$filter || !$emailSlug || !$eventSlug) {
  22. return $this->errorResponse(
  23. [
  24. APIError::BAD_REQUEST => WPFunctions::get()->__('Improperly formatted request.', 'mailpoet'),
  25. ]
  26. );
  27. }
  28. $automaticEmails = new \MailPoet\AutomaticEmails\AutomaticEmails();
  29. $event = $automaticEmails->getAutomaticEmailEventBySlug($emailSlug, $eventSlug);
  30. $eventFilter = (!empty($event['options']['remoteQueryFilter'])) ? $event['options']['remoteQueryFilter'] : null;
  31. return ($eventFilter === $filter && WPFunctions::get()->hasFilter($eventFilter)) ?
  32. $this->successResponse($this->wp->applyFilters($eventFilter, $query)) :
  33. $this->errorResponse(
  34. [
  35. APIError::BAD_REQUEST => WPFunctions::get()->__('Automatic email event filter does not exist.', 'mailpoet'),
  36. ]
  37. );
  38. }
  39. public function getEventShortcodes($data) {
  40. $emailSlug = (!empty($data['email_slug'])) ? $data['email_slug'] : null;
  41. $eventSlug = (!empty($data['event_slug'])) ? $data['event_slug'] : null;
  42. if (!$emailSlug || !$eventSlug) {
  43. return $this->errorResponse(
  44. [
  45. APIError::BAD_REQUEST => WPFunctions::get()->__('Improperly formatted request.', 'mailpoet'),
  46. ]
  47. );
  48. }
  49. $automaticEmails = new \MailPoet\AutomaticEmails\AutomaticEmails();
  50. $automaticEmail = $automaticEmails->getAutomaticEmailBySlug($emailSlug);
  51. $event = $automaticEmails->getAutomaticEmailEventBySlug($emailSlug, $eventSlug);
  52. if (!$event) {
  53. return $this->errorResponse(
  54. [
  55. APIError::BAD_REQUEST => WPFunctions::get()->__('Automatic email event does not exist.', 'mailpoet'),
  56. ]
  57. );
  58. }
  59. $eventShortcodes = (!empty($event['shortcodes']) && is_array($event['shortcodes'])) ?
  60. [
  61. $automaticEmail['title'] => $event['shortcodes'],
  62. ] :
  63. null;
  64. return $this->successResponse($eventShortcodes);
  65. }
  66. }