CommentSubject.php
2 years ago
PostSubject.php
2 years ago
UserSubject.php
5 months ago
index.php
3 years ago
UserSubject.php
112 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WordPress\Subjects; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeImmutable; |
| 9 | use MailPoet\Automation\Engine\Data\Field; |
| 10 | use MailPoet\Automation\Engine\Data\Subject as SubjectData; |
| 11 | use MailPoet\Automation\Engine\Integration\Payload; |
| 12 | use MailPoet\Automation\Engine\Integration\Subject; |
| 13 | use MailPoet\Automation\Engine\WordPress; |
| 14 | use MailPoet\Automation\Integrations\WordPress\Payloads\UserPayload; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoet\Validator\Schema\ObjectSchema; |
| 17 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 18 | use WP_User; |
| 19 | |
| 20 | /** |
| 21 | * @implements Subject<UserPayload> |
| 22 | */ |
| 23 | class UserSubject implements Subject { |
| 24 | const KEY = 'wordpress:user'; |
| 25 | |
| 26 | /** @var WordPress */ |
| 27 | private $wordPress; |
| 28 | |
| 29 | /** @var DotcomHelperFunctions */ |
| 30 | private $dotcomHelperFunctions; |
| 31 | |
| 32 | public function __construct( |
| 33 | WordPress $wordPress, |
| 34 | DotcomHelperFunctions $dotcomHelperFunctions |
| 35 | ) { |
| 36 | $this->wordPress = $wordPress; |
| 37 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 38 | } |
| 39 | |
| 40 | public function getName(): string { |
| 41 | if ($this->dotcomHelperFunctions->isGarden()) { |
| 42 | // translators: automation subject (entity entering automation) title |
| 43 | return __('User', 'mailpoet'); |
| 44 | } |
| 45 | // translators: automation subject (entity entering automation) title |
| 46 | return __('WordPress user', 'mailpoet'); |
| 47 | } |
| 48 | |
| 49 | public function getKey(): string { |
| 50 | return self::KEY; |
| 51 | } |
| 52 | |
| 53 | public function getArgsSchema(): ObjectSchema { |
| 54 | return Builder::object([ |
| 55 | 'user_id' => Builder::integer()->required(), |
| 56 | ]); |
| 57 | } |
| 58 | |
| 59 | public function getPayload(SubjectData $subjectData): Payload { |
| 60 | $id = $subjectData->getArgs()['user_id']; |
| 61 | $user = new WP_User($id); |
| 62 | return new UserPayload($user); |
| 63 | } |
| 64 | |
| 65 | public function getFields(): array { |
| 66 | global $wp_roles; |
| 67 | $roles = []; |
| 68 | foreach ($wp_roles->role_names as $id => $name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 69 | $roles[] = ['id' => $id, 'name' => $name]; |
| 70 | } |
| 71 | |
| 72 | return [ |
| 73 | new Field( |
| 74 | 'wordpress:user:email', |
| 75 | Field::TYPE_STRING, |
| 76 | __('Email', 'mailpoet'), |
| 77 | function (UserPayload $payload) { |
| 78 | return $payload->getEmail(); |
| 79 | } |
| 80 | ), |
| 81 | new Field( |
| 82 | 'wordpress:user:is-guest', |
| 83 | Field::TYPE_BOOLEAN, |
| 84 | __('Is guest', 'mailpoet'), |
| 85 | function (UserPayload $payload) { |
| 86 | return !$payload->exists(); |
| 87 | } |
| 88 | ), |
| 89 | new Field( |
| 90 | 'wordpress:user:registered-date', |
| 91 | Field::TYPE_DATETIME, |
| 92 | __('Registered date', 'mailpoet'), |
| 93 | function (UserPayload $payload) { |
| 94 | $date = $payload->getUser()->user_registered; |
| 95 | return $date ? new DateTimeImmutable($date, $this->wordPress->wpTimezone()) : null; |
| 96 | } |
| 97 | ), |
| 98 | new Field( |
| 99 | 'wordpress:user:roles', |
| 100 | Field::TYPE_ENUM_ARRAY, |
| 101 | __('Roles', 'mailpoet'), |
| 102 | function (UserPayload $payload) { |
| 103 | return $payload->getRoles(); |
| 104 | }, |
| 105 | [ |
| 106 | 'options' => $roles, |
| 107 | ] |
| 108 | ), |
| 109 | ]; |
| 110 | } |
| 111 | } |
| 112 |