PostFieldsFactory.php
247 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\PostPayload; |
| 11 | |
| 12 | class PostFieldsFactory { |
| 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:post:id', |
| 30 | Field::TYPE_INTEGER, |
| 31 | __('ID', 'mailpoet'), |
| 32 | function (PostPayload $payload) { |
| 33 | return $payload->getPostId(); |
| 34 | } |
| 35 | ), |
| 36 | new Field( |
| 37 | 'wordpress:post:type', |
| 38 | Field::TYPE_ENUM, |
| 39 | __('Post type', 'mailpoet'), |
| 40 | function (PostPayload $payload) { |
| 41 | $post = $payload->getPost(); |
| 42 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 43 | return $post ? $post->post_type : null; |
| 44 | }, |
| 45 | [ |
| 46 | 'options' => $this->getPostTypes(), |
| 47 | ] |
| 48 | ), |
| 49 | new Field( |
| 50 | 'wordpress:post:status', |
| 51 | Field::TYPE_ENUM, |
| 52 | __('Post status', 'mailpoet'), |
| 53 | function (PostPayload $payload) { |
| 54 | $post = $payload->getPost(); |
| 55 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 56 | return $post ? $post->post_status : null; |
| 57 | }, |
| 58 | [ |
| 59 | 'options' => $this->getPostStatuses(), |
| 60 | ] |
| 61 | ), |
| 62 | new Field( |
| 63 | 'wordpress:post:content', |
| 64 | Field::TYPE_STRING, |
| 65 | __('Post Content', 'mailpoet'), |
| 66 | function (PostPayload $payload) { |
| 67 | $post = $payload->getPost(); |
| 68 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 69 | return $post ? $post->post_content : null; |
| 70 | } |
| 71 | ), |
| 72 | new Field( |
| 73 | 'wordpress:post:title', |
| 74 | Field::TYPE_STRING, |
| 75 | __('Post title', 'mailpoet'), |
| 76 | function (PostPayload $payload) { |
| 77 | $post = $payload->getPost(); |
| 78 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 79 | return $post ? $post->post_title : null; |
| 80 | } |
| 81 | ), |
| 82 | new Field( |
| 83 | 'wordpress:post:date', |
| 84 | Field::TYPE_DATETIME, |
| 85 | __('Post date', 'mailpoet'), |
| 86 | function (PostPayload $payload) { |
| 87 | $post = $payload->getPost(); |
| 88 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 89 | return $post ? $post->post_date_gmt : null; |
| 90 | } |
| 91 | ), |
| 92 | new Field( |
| 93 | 'wordpress:post:modified', |
| 94 | Field::TYPE_DATETIME, |
| 95 | __('Post last modified', 'mailpoet'), |
| 96 | function (PostPayload $payload) { |
| 97 | $post = $payload->getPost(); |
| 98 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 99 | return $post ? $post->post_modified_gmt : null; |
| 100 | } |
| 101 | ), |
| 102 | new Field( |
| 103 | 'wordpress:post:author', |
| 104 | Field::TYPE_INTEGER, |
| 105 | __('Post author ID', 'mailpoet'), |
| 106 | function (PostPayload $payload) { |
| 107 | $post = $payload->getPost(); |
| 108 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 109 | return $post ? (int)$post->post_author : null; |
| 110 | } |
| 111 | ), |
| 112 | new Field( |
| 113 | 'wordpress:post:excerpt', |
| 114 | Field::TYPE_STRING, |
| 115 | __('Post excerpt', 'mailpoet'), |
| 116 | function (PostPayload $payload) { |
| 117 | $post = $payload->getPost(); |
| 118 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 119 | return $post ? $post->post_excerpt : null; |
| 120 | } |
| 121 | ), |
| 122 | new Field( |
| 123 | 'wordpress:post:comment-status', |
| 124 | Field::TYPE_BOOLEAN, |
| 125 | __('Post open for comments', 'mailpoet'), |
| 126 | function (PostPayload $payload) { |
| 127 | $post = $payload->getPost(); |
| 128 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 129 | return $post ? $post->comment_status === 'open' : false; |
| 130 | } |
| 131 | ), |
| 132 | new Field( |
| 133 | 'wordpress:post:ping-status', |
| 134 | Field::TYPE_BOOLEAN, |
| 135 | __('Post open for pings', 'mailpoet'), |
| 136 | function (PostPayload $payload) { |
| 137 | $post = $payload->getPost(); |
| 138 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 139 | return $post ? $post->ping_status === 'open' : false; |
| 140 | } |
| 141 | ), |
| 142 | new Field( |
| 143 | 'wordpress:post:password', |
| 144 | Field::TYPE_STRING, |
| 145 | __('Post password', 'mailpoet'), |
| 146 | function (PostPayload $payload) { |
| 147 | $post = $payload->getPost(); |
| 148 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 149 | return $post ? $post->post_password : null; |
| 150 | } |
| 151 | ), |
| 152 | new Field( |
| 153 | 'wordpress:post:slug', |
| 154 | Field::TYPE_STRING, |
| 155 | __('Post slug', 'mailpoet'), |
| 156 | function (PostPayload $payload) { |
| 157 | $post = $payload->getPost(); |
| 158 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 159 | return $post ? $post->post_name : null; |
| 160 | } |
| 161 | ), |
| 162 | new Field( |
| 163 | 'wordpress:post:parent', |
| 164 | Field::TYPE_INTEGER, |
| 165 | __('Post parent ID', 'mailpoet'), |
| 166 | function (PostPayload $payload) { |
| 167 | $post = $payload->getPost(); |
| 168 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 169 | return $post ? $post->post_parent : null; |
| 170 | } |
| 171 | ), |
| 172 | new Field( |
| 173 | 'wordpress:post:has-parent', |
| 174 | Field::TYPE_BOOLEAN, |
| 175 | __('Post has parent', 'mailpoet'), |
| 176 | function (PostPayload $payload) { |
| 177 | $post = $payload->getPost(); |
| 178 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 179 | return $post ? $post->post_parent > 0 : false; |
| 180 | } |
| 181 | ), |
| 182 | new Field( |
| 183 | 'wordpress:post:guid', |
| 184 | Field::TYPE_STRING, |
| 185 | __('Post guid', 'mailpoet'), |
| 186 | function (PostPayload $payload) { |
| 187 | $post = $payload->getPost(); |
| 188 | return $post ? $post->guid : null; |
| 189 | } |
| 190 | ), |
| 191 | new Field( |
| 192 | 'wordpress:post:menu-order', |
| 193 | Field::TYPE_INTEGER, |
| 194 | __('Post menu order', 'mailpoet'), |
| 195 | function (PostPayload $payload) { |
| 196 | $post = $payload->getPost(); |
| 197 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 198 | return $post ? $post->menu_order : null; |
| 199 | } |
| 200 | ), |
| 201 | new Field( |
| 202 | 'wordpress:post:comment-count', |
| 203 | Field::TYPE_INTEGER, |
| 204 | __('Number of post comments', 'mailpoet'), |
| 205 | function (PostPayload $payload) { |
| 206 | $post = $payload->getPost(); |
| 207 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 208 | return $post ? $post->comment_count : 0; |
| 209 | } |
| 210 | ), |
| 211 | ]; |
| 212 | } |
| 213 | |
| 214 | private function getPostStatuses(): array { |
| 215 | $statuses = $this->wp->getPostStatuses(); |
| 216 | return array_values(array_map( |
| 217 | function($status, $index): array { |
| 218 | return [ |
| 219 | 'id' => $index, |
| 220 | 'name' => $status, |
| 221 | ]; |
| 222 | }, |
| 223 | $statuses, |
| 224 | array_keys($statuses) |
| 225 | )); |
| 226 | } |
| 227 | |
| 228 | private function getPostTypes(): array { |
| 229 | /** @var \WP_Post_Type[] $postTypes */ |
| 230 | $postTypes = $this->wp->getPostTypes([], 'objects'); |
| 231 | return array_values(array_map( |
| 232 | function(\WP_Post_Type $type): array { |
| 233 | return [ |
| 234 | 'id' => $type->name, |
| 235 | 'name' => $type->label, |
| 236 | ]; |
| 237 | }, |
| 238 | array_filter( |
| 239 | $postTypes, |
| 240 | function(\WP_Post_Type $type): bool { |
| 241 | return (bool)$type->public; |
| 242 | } |
| 243 | ) |
| 244 | )); |
| 245 | } |
| 246 | } |
| 247 |