AdminUserSubscription.php
2 months ago
Blacklist.php
1 year ago
Comment.php
2 months ago
Form.php
2 weeks ago
Manage.php
5 days ago
ManageSubscriptionFormRenderer.php
5 days ago
Pages.php
5 days ago
Registration.php
2 months ago
SubscriptionUrlFactory.php
5 days ago
Throttling.php
2 months ago
index.php
3 years ago
AdminUserSubscription.php
200 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscription; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Renderer as TemplateRenderer; |
| 9 | use MailPoet\Entities\SubscriberEntity; |
| 10 | use MailPoet\Logging\LoggerFactory; |
| 11 | use MailPoet\Settings\SettingsController; |
| 12 | use MailPoet\Subscribers\ConfirmationEmailMailer; |
| 13 | use MailPoet\Subscribers\SubscribersRepository; |
| 14 | use MailPoet\WP\Functions as WPFunctions; |
| 15 | |
| 16 | class AdminUserSubscription { |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var SettingsController */ |
| 21 | private $settings; |
| 22 | |
| 23 | /** @var SubscribersRepository */ |
| 24 | private $subscribersRepository; |
| 25 | |
| 26 | /** @var ConfirmationEmailMailer */ |
| 27 | private $confirmationEmailMailer; |
| 28 | |
| 29 | /** @var LoggerFactory */ |
| 30 | private $loggerFactory; |
| 31 | |
| 32 | /** @var TemplateRenderer */ |
| 33 | private $templateRenderer; |
| 34 | |
| 35 | public function __construct( |
| 36 | WPFunctions $wp, |
| 37 | SettingsController $settings, |
| 38 | SubscribersRepository $subscribersRepository, |
| 39 | ConfirmationEmailMailer $confirmationEmailMailer, |
| 40 | LoggerFactory $loggerFactory, |
| 41 | TemplateRenderer $templateRenderer |
| 42 | ) { |
| 43 | $this->wp = $wp; |
| 44 | $this->settings = $settings; |
| 45 | $this->subscribersRepository = $subscribersRepository; |
| 46 | $this->confirmationEmailMailer = $confirmationEmailMailer; |
| 47 | $this->loggerFactory = $loggerFactory; |
| 48 | $this->templateRenderer = $templateRenderer; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set up hooks for the Add New User form in WordPress admin |
| 53 | */ |
| 54 | public function setupHooks(): void { |
| 55 | // Set up hooks for the Add New User form |
| 56 | $this->wp->addAction('user_new_form', [$this, 'displaySubscriberStatusField']); |
| 57 | |
| 58 | // Use the lower priority filter instead of an action - this is simpler and more reliable |
| 59 | $this->wp->addFilter('mailpoet_subscriber_data_before_save', [$this, 'modifySubscriberData'], 10, 1); |
| 60 | |
| 61 | // Add action to send confirmation email after user registration is complete |
| 62 | $this->wp->addAction('user_register', [$this, 'maybeSendConfirmationEmail'], 30, 1); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Display the subscriber status field on the Add New User form |
| 67 | * |
| 68 | * @param string $type The form context, 'add-new-user' for single site and network admin |
| 69 | */ |
| 70 | public function displaySubscriberStatusField($type): void { |
| 71 | // According to WordPress docs, the parameter is 'add-new-user' for single site and network admin |
| 72 | if ($type !== 'add-new-user') { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $templateData = [ |
| 77 | 'confirmationEnabled' => (bool)$this->settings->get('signup_confirmation.enabled', false), |
| 78 | 'statuses' => [ |
| 79 | 'subscribed' => SubscriberEntity::STATUS_SUBSCRIBED, |
| 80 | 'unconfirmed' => SubscriberEntity::STATUS_UNCONFIRMED, |
| 81 | 'unsubscribed' => SubscriberEntity::STATUS_UNSUBSCRIBED, |
| 82 | ], |
| 83 | ]; |
| 84 | |
| 85 | $renderedTemplate = $this->templateRenderer->render( |
| 86 | 'subscription/admin_user_status_field.html', |
| 87 | $templateData |
| 88 | ); |
| 89 | |
| 90 | $allowedHtml = [ |
| 91 | 'table' => ['class' => true], |
| 92 | 'tr' => [], |
| 93 | 'th' => ['scope' => true], |
| 94 | 'td' => [], |
| 95 | 'label' => ['for' => true], |
| 96 | 'select' => ['name' => true, 'id' => true], |
| 97 | 'option' => ['value' => true, 'selected' => true], |
| 98 | ]; |
| 99 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 100 | echo $this->wp->wpKses($renderedTemplate, $allowedHtml); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Modify subscriber data before save during user creation |
| 105 | * |
| 106 | * @param array $data The subscriber data |
| 107 | * @return array Modified subscriber data |
| 108 | */ |
| 109 | public function modifySubscriberData($data): array { |
| 110 | // Only process during user creation in admin |
| 111 | if (!$this->isAdminUserCreation()) { |
| 112 | return $data; |
| 113 | } |
| 114 | |
| 115 | // Check if our field was submitted |
| 116 | if (!isset($_POST['mailpoet_subscriber_status']) || !is_string($_POST['mailpoet_subscriber_status'])) { |
| 117 | return $data; |
| 118 | } |
| 119 | |
| 120 | $status = sanitize_text_field($_POST['mailpoet_subscriber_status']); |
| 121 | |
| 122 | // Validate the status value |
| 123 | $validStatuses = [ |
| 124 | SubscriberEntity::STATUS_SUBSCRIBED, |
| 125 | SubscriberEntity::STATUS_UNCONFIRMED, |
| 126 | SubscriberEntity::STATUS_UNSUBSCRIBED, |
| 127 | ]; |
| 128 | |
| 129 | if (!in_array($status, $validStatuses)) { |
| 130 | return $data; |
| 131 | } |
| 132 | |
| 133 | // Update the subscriber data |
| 134 | $data['status'] = $status; |
| 135 | $data['source'] = 'administrator'; |
| 136 | |
| 137 | return $data; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Send confirmation email if needed after user registration |
| 142 | * |
| 143 | * @param int $userId The WordPress user ID |
| 144 | */ |
| 145 | public function maybeSendConfirmationEmail(int $userId): void { |
| 146 | // Only process during user creation in admin |
| 147 | if (!$this->isAdminUserCreation()) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Check if our field was submitted and is set to unconfirmed |
| 152 | if ( |
| 153 | !isset($_POST['mailpoet_subscriber_status']) || |
| 154 | $_POST['mailpoet_subscriber_status'] !== SubscriberEntity::STATUS_UNCONFIRMED |
| 155 | ) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Find the subscriber |
| 160 | $subscriber = $this->subscribersRepository->findOneBy(['wpUserId' => $userId]); |
| 161 | |
| 162 | if (!$subscriber || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNCONFIRMED) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Send confirmation email. Per-list confirmation settings are not resolved |
| 167 | // here because admin-created users are subscribed via the WP Users segment. |
| 168 | try { |
| 169 | $this->confirmationEmailMailer->sendConfirmationEmailOnce($subscriber); |
| 170 | } catch (\Exception $e) { |
| 171 | // Log the error |
| 172 | $logger = $this->loggerFactory->getLogger(); |
| 173 | $logger->error( |
| 174 | __('Failed to send confirmation email for admin-created user', 'mailpoet'), |
| 175 | ['user_id' => $userId, 'error' => $e->getMessage()] |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check if we're in the context of admin user creation |
| 182 | * |
| 183 | * @return bool |
| 184 | */ |
| 185 | private function isAdminUserCreation(): bool { |
| 186 | // Check if we're in admin |
| 187 | if (!$this->wp->isAdmin()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // Check for the WordPress new user admin page |
| 192 | global $pagenow; |
| 193 | if ($pagenow !== 'user-new.php') { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | } |
| 200 |