CommentFieldsFactory.php
189 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WordPress\Fields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Field; |
| 9 | use MailPoet\Automation\Engine\WordPress; |
| 10 | use MailPoet\Automation\Integrations\WordPress\Payloads\CommentPayload; |
| 11 | |
| 12 | class CommentFieldsFactory { |
| 13 | |
| 14 | /** @var WordPress */ |
| 15 | private $wp; |
| 16 | |
| 17 | public function __construct( |
| 18 | WordPress $wp |
| 19 | ) { |
| 20 | $this->wp = $wp; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return Field[] |
| 25 | */ |
| 26 | public function getFields(): array { |
| 27 | return [ |
| 28 | new Field( |
| 29 | 'wordpress:comment:id', |
| 30 | Field::TYPE_INTEGER, |
| 31 | __('Comment ID', 'mailpoet'), |
| 32 | function (CommentPayload $payload) { |
| 33 | return $payload->getCommentId(); |
| 34 | } |
| 35 | ), |
| 36 | new Field( |
| 37 | 'wordpress:comment:author-name', |
| 38 | Field::TYPE_STRING, |
| 39 | __('Comment author name', 'mailpoet'), |
| 40 | function (CommentPayload $payload) { |
| 41 | $comment = $payload->getComment(); |
| 42 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 43 | return $comment ? $comment->comment_author : null; |
| 44 | } |
| 45 | ), |
| 46 | new Field( |
| 47 | 'wordpress:comment:author-email', |
| 48 | Field::TYPE_STRING, |
| 49 | __('Comment author email', 'mailpoet'), |
| 50 | function (CommentPayload $payload) { |
| 51 | $comment = $payload->getComment(); |
| 52 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 53 | return $comment ? $comment->comment_author_email : null; |
| 54 | } |
| 55 | ), |
| 56 | new Field( |
| 57 | 'wordpress:comment:author-url', |
| 58 | Field::TYPE_STRING, |
| 59 | __('Comment author URL', 'mailpoet'), |
| 60 | function (CommentPayload $payload) { |
| 61 | $comment = $payload->getComment(); |
| 62 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 63 | return $comment ? $comment->comment_author_url : null; |
| 64 | } |
| 65 | ), |
| 66 | new Field( |
| 67 | 'wordpress:comment:author-ip', |
| 68 | Field::TYPE_STRING, |
| 69 | __('Comment author IP', 'mailpoet'), |
| 70 | function (CommentPayload $payload) { |
| 71 | $comment = $payload->getComment(); |
| 72 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 73 | return $comment ? $comment->comment_author_IP : null; |
| 74 | } |
| 75 | ), |
| 76 | new Field( |
| 77 | 'wordpress:comment:date', |
| 78 | Field::TYPE_DATETIME, |
| 79 | __('Comment date', 'mailpoet'), |
| 80 | function (CommentPayload $payload) { |
| 81 | $comment = $payload->getComment(); |
| 82 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 83 | return $comment ? $comment->comment_date_gmt : null; |
| 84 | } |
| 85 | ), |
| 86 | new Field( |
| 87 | 'wordpress:comment:content', |
| 88 | Field::TYPE_STRING, |
| 89 | __('Comment content', 'mailpoet'), |
| 90 | function (CommentPayload $payload) { |
| 91 | $comment = $payload->getComment(); |
| 92 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 93 | return $comment ? $comment->comment_content : null; |
| 94 | } |
| 95 | ), |
| 96 | new Field( |
| 97 | 'wordpress:comment:karma', |
| 98 | Field::TYPE_INTEGER, |
| 99 | __('Comment karma', 'mailpoet'), |
| 100 | function (CommentPayload $payload) { |
| 101 | $comment = $payload->getComment(); |
| 102 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 103 | return $comment ? (int)$comment->comment_karma : null; |
| 104 | } |
| 105 | ), |
| 106 | new Field( |
| 107 | 'wordpress:comment:status', |
| 108 | Field::TYPE_ENUM, |
| 109 | __('Comment status', 'mailpoet'), |
| 110 | function (CommentPayload $payload) { |
| 111 | $status = $this->wp->wpGetCommentStatus($payload->getCommentId()); |
| 112 | if (!is_string($status)) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * wp_get_comment_status returns 'unapproved' and 'approved' where get_comment_statuses returns 'hold' and 'approve' |
| 118 | * We need to normalize the status for matches. |
| 119 | */ |
| 120 | if ($status === 'approved') { |
| 121 | $status = 'approve'; |
| 122 | } |
| 123 | if ($status === 'unapproved') { |
| 124 | $status = 'hold'; |
| 125 | } |
| 126 | return $status; |
| 127 | }, |
| 128 | [ |
| 129 | 'options' => $this->getCommentStatuses(), |
| 130 | ] |
| 131 | ), |
| 132 | new Field( |
| 133 | 'wordpress:comment:comment-agent', |
| 134 | Field::TYPE_STRING, |
| 135 | __('Comment user agent', 'mailpoet'), |
| 136 | function (CommentPayload $payload) { |
| 137 | $comment = $payload->getComment(); |
| 138 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 139 | return $comment ? $comment->comment_agent : null; |
| 140 | } |
| 141 | ), |
| 142 | new Field( |
| 143 | 'wordpress:comment:comment-type', |
| 144 | Field::TYPE_STRING, |
| 145 | __('Comment type', 'mailpoet'), |
| 146 | function (CommentPayload $payload) { |
| 147 | $comment = $payload->getComment(); |
| 148 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 149 | return $comment ? $comment->comment_type : null; |
| 150 | } |
| 151 | ), |
| 152 | new Field( |
| 153 | 'wordpress:comment:comment-parent', |
| 154 | Field::TYPE_INTEGER, |
| 155 | __('Comment parent ID', 'mailpoet'), |
| 156 | function (CommentPayload $payload) { |
| 157 | $comment = $payload->getComment(); |
| 158 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 159 | return $comment ? (int)$comment->comment_parent : null; |
| 160 | } |
| 161 | ), |
| 162 | new Field( |
| 163 | 'wordpress:comment:has-children', |
| 164 | Field::TYPE_BOOLEAN, |
| 165 | __('Comment has replies', 'mailpoet'), |
| 166 | function (CommentPayload $payload) { |
| 167 | $comment = $payload->getComment(); |
| 168 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 169 | return $comment ? count($comment->get_children()) > 0 : false; |
| 170 | } |
| 171 | ), |
| 172 | ]; |
| 173 | } |
| 174 | |
| 175 | private function getCommentStatuses(): array { |
| 176 | $statuses = $this->wp->getCommentStatuses(); |
| 177 | return array_values(array_map( |
| 178 | function($name, $id): array { |
| 179 | return [ |
| 180 | 'id' => $id, |
| 181 | 'name' => $name, |
| 182 | ]; |
| 183 | }, |
| 184 | $statuses, |
| 185 | array_keys($statuses) |
| 186 | )); |
| 187 | } |
| 188 | } |
| 189 |