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
Registration.php
133 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\Settings\SettingsController; |
| 9 | use MailPoet\Statistics\Track\SubscriberHandler; |
| 10 | use MailPoet\Subscribers\SubscriberActions; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class Registration { |
| 14 | |
| 15 | /** @var SettingsController */ |
| 16 | private $settings; |
| 17 | |
| 18 | /** @var SubscriberActions */ |
| 19 | private $subscriberActions; |
| 20 | |
| 21 | /** @var WPFunctions */ |
| 22 | private $wp; |
| 23 | |
| 24 | /** @var SubscriberHandler */ |
| 25 | private $subscriberHandler; |
| 26 | |
| 27 | public function __construct( |
| 28 | SettingsController $settings, |
| 29 | WPFunctions $wp, |
| 30 | SubscriberActions $subscriberActions, |
| 31 | SubscriberHandler $subscriberHandler |
| 32 | ) { |
| 33 | $this->settings = $settings; |
| 34 | $this->subscriberActions = $subscriberActions; |
| 35 | $this->wp = $wp; |
| 36 | $this->subscriberHandler = $subscriberHandler; |
| 37 | } |
| 38 | |
| 39 | public function extendForm() { |
| 40 | $label = $this->settings->get( |
| 41 | 'subscribe.on_register.label', |
| 42 | __('Yes, please add me to your mailing list.', 'mailpoet') |
| 43 | ); |
| 44 | |
| 45 | $form = '<p class="registration-form-mailpoet"> |
| 46 | <label for="mailpoet_subscribe_on_register"> |
| 47 | <input |
| 48 | type="hidden" |
| 49 | id="mailpoet_subscribe_on_register_active" |
| 50 | value="1" |
| 51 | name="mailpoet[subscribe_on_register_active]" |
| 52 | /> |
| 53 | <input |
| 54 | type="checkbox" |
| 55 | id="mailpoet_subscribe_on_register" |
| 56 | value="1" |
| 57 | name="mailpoet[subscribe_on_register]" |
| 58 | /> ' . esc_html($label) . ' |
| 59 | </label> |
| 60 | </p>'; |
| 61 | |
| 62 | $filtered = $this->wp->applyFilters('mailpoet_register_form_extend', $form); |
| 63 | $form = is_string($filtered) ? $filtered : $form; |
| 64 | |
| 65 | // We control the template and $form can be considered safe. |
| 66 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 67 | echo $form; |
| 68 | } |
| 69 | |
| 70 | public function onMultiSiteRegister($result) { |
| 71 | if (empty($result['errors']->errors)) { |
| 72 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 73 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 74 | if ( |
| 75 | isset($mailpoetPost['subscribe_on_register']) |
| 76 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 77 | && !empty($result['user_email']) |
| 78 | ) { |
| 79 | $this->subscribeNewUser( |
| 80 | $result['user_name'], |
| 81 | $result['user_email'] |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | return $result; |
| 86 | } |
| 87 | |
| 88 | public function onRegister( |
| 89 | $errors, |
| 90 | $userLogin, |
| 91 | $userEmail = null |
| 92 | ) { |
| 93 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 94 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 95 | if ( |
| 96 | empty($errors->errors) |
| 97 | && isset($mailpoetPost['subscribe_on_register']) |
| 98 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 99 | && !empty($userEmail) |
| 100 | ) { |
| 101 | $this->subscribeNewUser( |
| 102 | $userLogin, |
| 103 | $userEmail |
| 104 | ); |
| 105 | } |
| 106 | return $errors; |
| 107 | } |
| 108 | |
| 109 | private function subscribeNewUser($name, $email) { |
| 110 | $segmentIds = $this->settings->get( |
| 111 | 'subscribe.on_register.segments', |
| 112 | [] |
| 113 | ); |
| 114 | $this->subscriberActions->subscribe( |
| 115 | [ |
| 116 | 'email' => $email, |
| 117 | 'first_name' => $name, |
| 118 | ], |
| 119 | $segmentIds |
| 120 | ); |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * On multisite headers are already sent at this point, tracking will start |
| 125 | * once the user has activated his account at a later stage. |
| 126 | **/ |
| 127 | if (!headers_sent()) { |
| 128 | // start subscriber tracking (by email, we don't have WP user ID yet) |
| 129 | $this->subscriberHandler->identifyByEmail($email); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 |