mailpoet
/
lib
/
Automation
/
Integrations
/
WooCommerce
/
Triggers
/
Orders
/
OrderNoteAddedTrigger.php
OrderCancelledTrigger.php
2 years ago
OrderCompletedTrigger.php
2 years ago
OrderCreatedTrigger.php
3 months ago
OrderNoteAddedTrigger.php
1 year ago
OrderStatusChangedTrigger.php
2 years ago
index.php
2 years ago
OrderNoteAddedTrigger.php
141 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Triggers\Orders; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 9 | use MailPoet\Automation\Engine\Data\StepValidationArgs; |
| 10 | use MailPoet\Automation\Engine\Data\Subject; |
| 11 | use MailPoet\Automation\Engine\Hooks; |
| 12 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 13 | use MailPoet\Automation\Engine\WordPress; |
| 14 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\CustomerSubject; |
| 15 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 16 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; |
| 17 | use MailPoet\Automation\Integrations\WordPress\Subjects\CommentSubject; |
| 18 | use MailPoet\Validator\Builder; |
| 19 | use MailPoet\Validator\Schema\ObjectSchema; |
| 20 | |
| 21 | class OrderNoteAddedTrigger implements Trigger { |
| 22 | |
| 23 | /** @var WordPress */ |
| 24 | protected $wp; |
| 25 | |
| 26 | /** @var WooCommerce */ |
| 27 | protected $woocommerce; |
| 28 | |
| 29 | public function __construct( |
| 30 | WordPress $wp, |
| 31 | WooCommerce $woocommerceHelper |
| 32 | ) { |
| 33 | $this->wp = $wp; |
| 34 | $this->woocommerce = $woocommerceHelper; |
| 35 | } |
| 36 | |
| 37 | public function getKey(): string { |
| 38 | return 'woocommerce:order-note-added'; |
| 39 | } |
| 40 | |
| 41 | public function getName(): string { |
| 42 | // translators: automation trigger title |
| 43 | return __('Order note added', 'mailpoet'); |
| 44 | } |
| 45 | |
| 46 | public function getArgsSchema(): ObjectSchema { |
| 47 | return Builder::object([ |
| 48 | 'note_contains' => Builder::string()->default(''), |
| 49 | 'note_type' => Builder::string()->default('all'), |
| 50 | ]); |
| 51 | } |
| 52 | |
| 53 | public function getSubjectKeys(): array { |
| 54 | return [ |
| 55 | OrderSubject::KEY, |
| 56 | CustomerSubject::KEY, |
| 57 | CommentSubject::KEY, |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | public function validate(StepValidationArgs $args): void { |
| 62 | $triggerArgs = $args->getStep()->getArgs(); |
| 63 | $noteType = $triggerArgs['note_type'] ?? 'all'; |
| 64 | |
| 65 | if (!in_array($noteType, ['all', 'customer', 'private'], true)) { |
| 66 | throw new \InvalidArgumentException( |
| 67 | sprintf('Invalid note_type "%s". Allowed values: all, customer, private', $noteType) |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public function registerHooks(): void { |
| 73 | $this->wp->addAction( |
| 74 | 'woocommerce_order_note_added', |
| 75 | [ |
| 76 | $this, |
| 77 | 'handle', |
| 78 | ], |
| 79 | 10, |
| 80 | 2 |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function handle(int $commentId, \WC_Order $order): void { |
| 85 | $comment = get_comment($commentId); |
| 86 | if (!$comment) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | $isCustomerNote = (bool)get_comment_meta($commentId, 'is_customer_note', true); |
| 91 | $noteType = $isCustomerNote ? 'customer' : 'private'; |
| 92 | |
| 93 | $this->wp->doAction(Hooks::TRIGGER, $this, [ |
| 94 | new Subject(OrderSubject::KEY, ['order_id' => $order->get_id()]), |
| 95 | new Subject(CustomerSubject::KEY, ['customer_id' => $order->get_customer_id(), 'order_id' => $order->get_id()]), |
| 96 | new Subject(CommentSubject::KEY, [ |
| 97 | 'comment_id' => $commentId, |
| 98 | // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 99 | 'comment_content' => $comment->comment_content, |
| 100 | 'note_type' => $noteType, |
| 101 | ]), |
| 102 | ]); |
| 103 | } |
| 104 | |
| 105 | public function isTriggeredBy(StepRunArgs $args): bool { |
| 106 | $triggerArgs = $args->getStep()->getArgs(); |
| 107 | $configuredNoteContains = $triggerArgs['note_contains'] ?? ''; |
| 108 | $configuredNoteType = $triggerArgs['note_type'] ?? 'all'; |
| 109 | |
| 110 | // Get the comment from the CommentPayload |
| 111 | try { |
| 112 | $commentPayload = $args->getSinglePayloadByClass(\MailPoet\Automation\Integrations\WordPress\Payloads\CommentPayload::class); |
| 113 | } catch (\Exception $e) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | $comment = $commentPayload->getComment(); |
| 118 | if (!$comment) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | // Check note content filter |
| 123 | // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 124 | if (($configuredNoteContains !== '') && (stripos($comment->comment_content, $configuredNoteContains) === false)) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Check note type filter |
| 129 | if ($configuredNoteType !== 'all') { |
| 130 | $commentSubjectEntry = $args->getSingleSubjectEntry(CommentSubject::KEY); |
| 131 | $subjectArgs = $commentSubjectEntry->getSubjectData()->getArgs(); |
| 132 | $actualNoteType = $subjectArgs['note_type'] ?? null; |
| 133 | if ($actualNoteType !== $configuredNoteType) { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 |