mailpoet
/
lib
/
Automation
/
Integrations
/
WordPress
/
SubjectTransformers
/
CommentSubjectToPostSubjectTransformer.php
CommentSubjectToPostSubjectTransformer.php
52 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WordPress\SubjectTransformers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Subject; |
| 9 | use MailPoet\Automation\Engine\Integration\SubjectTransformer; |
| 10 | use MailPoet\Automation\Engine\WordPress; |
| 11 | use MailPoet\Automation\Integrations\WordPress\Subjects\CommentSubject; |
| 12 | use MailPoet\Automation\Integrations\WordPress\Subjects\PostSubject; |
| 13 | |
| 14 | class CommentSubjectToPostSubjectTransformer implements SubjectTransformer { |
| 15 | |
| 16 | /** @var WordPress */ |
| 17 | private $wp; |
| 18 | |
| 19 | public function __construct( |
| 20 | WordPress $wp |
| 21 | ) { |
| 22 | $this->wp = $wp; |
| 23 | } |
| 24 | |
| 25 | public function accepts(): string { |
| 26 | return CommentSubject::KEY; |
| 27 | } |
| 28 | |
| 29 | public function returns(): string { |
| 30 | return PostSubject::KEY; |
| 31 | } |
| 32 | |
| 33 | public function transform(Subject $data): ?Subject { |
| 34 | |
| 35 | if ($this->accepts() !== $data->getKey()) { |
| 36 | throw new \InvalidArgumentException('Invalid subject type'); |
| 37 | } |
| 38 | |
| 39 | $commentId = (int)$data->getArgs()['comment_id']; |
| 40 | $comment = $this->wp->getComment($commentId); |
| 41 | if (!$comment instanceof \WP_Comment) { |
| 42 | return null; |
| 43 | } |
| 44 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 45 | $post = $this->wp->getPost((int)$comment->comment_post_ID); |
| 46 | if (!$post instanceof \WP_Post) { |
| 47 | return null; |
| 48 | } |
| 49 | return new Subject(PostSubject::KEY, ['post_id' => (int)$post->ID]); |
| 50 | } |
| 51 | } |
| 52 |