PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.89.3
MailPoet – Newsletters, Email Marketing, and Automation v3.89.3
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Subscription / Comment.php
Comment.php
122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
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, WordPressDotOrg.sniffs.OutputEscaping.UnescapedOutputParameter
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 WPFunctions::get()->__('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 />&nbsp;' . esc_html($label) . '
61 </label>
62 </p>';
63 }
64
65 public function onSubmit($commentId, $commentStatus) {
66 if ($commentStatus === Comment::SPAM) return;
67
68 if (
69 isset($_POST['mailpoet']['subscribe_on_comment'])
70 && (bool)$_POST['mailpoet']['subscribe_on_comment'] === true
71 ) {
72 if ($commentStatus === Comment::PENDING_APPROVAL) {
73 // add a comment meta to remember to subscribe the user
74 // once the comment gets approved
75 WPFunctions::get()->addCommentMeta(
76 $commentId,
77 'mailpoet',
78 'subscribe_on_comment',
79 true
80 );
81 } else if ($commentStatus === Comment::APPROVED) {
82 $this->subscribeAuthorOfComment($commentId);
83 }
84 }
85 }
86
87 public function onStatusUpdate($commentId, $action) {
88 if ($action === 'approve') {
89 // check if the comment's author wants to subscribe
90 $doSubscribe = (
91 WPFunctions::get()->getCommentMeta(
92 $commentId,
93 'mailpoet',
94 true
95 ) === 'subscribe_on_comment'
96 );
97
98 if ($doSubscribe === true) {
99 $this->subscribeAuthorOfComment($commentId);
100
101 WPFunctions::get()->deleteCommentMeta($commentId, 'mailpoet');
102 }
103 }
104 }
105
106 private function subscribeAuthorOfComment($commentId) {
107 $segmentIds = $this->settings->get('subscribe.on_comment.segments', []);
108
109 if (!empty($segmentIds)) {
110 $comment = WPFunctions::get()->getComment($commentId);
111
112 $result = $this->subscriberActions->subscribe(
113 [
114 'email' => $comment->comment_author_email, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
115 'first_name' => $comment->comment_author, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
116 ],
117 $segmentIds
118 );
119 }
120 }
121 }
122