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
Comment.php
124 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\Subscribers\SubscriberActions; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class Comment { |
| 13 | const SPAM = 'spam'; |
| 14 | const APPROVED = 1; |
| 15 | const PENDING_APPROVAL = 0; |
| 16 | |
| 17 | /** @var SettingsController */ |
| 18 | private $settings; |
| 19 | |
| 20 | /** @var SubscriberActions */ |
| 21 | private $subscriberActions; |
| 22 | |
| 23 | public function __construct( |
| 24 | SettingsController $settings, |
| 25 | SubscriberActions $subscriberActions |
| 26 | ) { |
| 27 | $this->settings = $settings; |
| 28 | $this->subscriberActions = $subscriberActions; |
| 29 | } |
| 30 | |
| 31 | public function extendLoggedInForm($field) { |
| 32 | $field .= $this->getSubscriptionField(); |
| 33 | return $field; |
| 34 | } |
| 35 | |
| 36 | public function extendLoggedOutForm() { |
| 37 | // The method returns escaped content |
| 38 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 39 | echo $this->getSubscriptionField(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns escaped HTML for the subscription field. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | private function getSubscriptionField(): string { |
| 48 | $label = $this->settings->get( |
| 49 | 'subscribe.on_comment.label', |
| 50 | __('Yes, please add me to your mailing list.', 'mailpoet') |
| 51 | ); |
| 52 | |
| 53 | return '<p class="comment-form-mailpoet"> |
| 54 | <label for="mailpoet_subscribe_on_comment"> |
| 55 | <input |
| 56 | type="checkbox" |
| 57 | id="mailpoet_subscribe_on_comment" |
| 58 | value="1" |
| 59 | name="mailpoet[subscribe_on_comment]" |
| 60 | /> ' . esc_html($label) . ' |
| 61 | </label> |
| 62 | </p>'; |
| 63 | } |
| 64 | |
| 65 | public function onSubmit($commentId, $commentStatus) { |
| 66 | if ($commentStatus === Comment::SPAM) return; |
| 67 | |
| 68 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 69 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 70 | if ( |
| 71 | isset($mailpoetPost['subscribe_on_comment']) |
| 72 | && (bool)$mailpoetPost['subscribe_on_comment'] === true |
| 73 | ) { |
| 74 | if ($commentStatus === Comment::PENDING_APPROVAL) { |
| 75 | // add a comment meta to remember to subscribe the user |
| 76 | // once the comment gets approved |
| 77 | WPFunctions::get()->addCommentMeta( |
| 78 | $commentId, |
| 79 | 'mailpoet', |
| 80 | 'subscribe_on_comment', |
| 81 | true |
| 82 | ); |
| 83 | } elseif ($commentStatus === Comment::APPROVED) { |
| 84 | $this->subscribeAuthorOfComment($commentId); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function onStatusUpdate($commentId, $action) { |
| 90 | if ($action === 'approve') { |
| 91 | // check if the comment's author wants to subscribe |
| 92 | $doSubscribe = ( |
| 93 | WPFunctions::get()->getCommentMeta( |
| 94 | $commentId, |
| 95 | 'mailpoet', |
| 96 | true |
| 97 | ) === 'subscribe_on_comment' |
| 98 | ); |
| 99 | |
| 100 | if ($doSubscribe === true) { |
| 101 | $this->subscribeAuthorOfComment($commentId); |
| 102 | |
| 103 | WPFunctions::get()->deleteCommentMeta($commentId, 'mailpoet'); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private function subscribeAuthorOfComment($commentId) { |
| 109 | $segmentIds = $this->settings->get('subscribe.on_comment.segments', []); |
| 110 | |
| 111 | if (!empty($segmentIds)) { |
| 112 | $comment = WPFunctions::get()->getComment($commentId); |
| 113 | |
| 114 | $this->subscriberActions->subscribe( |
| 115 | [ |
| 116 | 'email' => $comment->comment_author_email, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 117 | 'first_name' => $comment->comment_author, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 118 | ], |
| 119 | $segmentIds |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 |