settings = $settings; $this->subscriberActions = $subscriberActions; } public function extendLoggedInForm($field) { $field .= $this->getSubscriptionField(); return $field; } public function extendLoggedOutForm() { echo $this->getSubscriptionField(); } private function getSubscriptionField() { $label = $this->settings->get( 'subscribe.on_comment.label', WPFunctions::get()->__('Yes, please add me to your mailing list.', 'mailpoet') ); return '
'; } public function onSubmit($commentId, $commentStatus) { if ($commentStatus === Comment::SPAM) return; if ( isset($_POST['mailpoet']['subscribe_on_comment']) && (bool)$_POST['mailpoet']['subscribe_on_comment'] === true ) { if ($commentStatus === Comment::PENDING_APPROVAL) { // add a comment meta to remember to subscribe the user // once the comment gets approved WPFunctions::get()->addCommentMeta( $commentId, 'mailpoet', 'subscribe_on_comment', true ); } else if ($commentStatus === Comment::APPROVED) { $this->subscribeAuthorOfComment($commentId); } } } public function onStatusUpdate($commentId, $action) { if ($action === 'approve') { // check if the comment's author wants to subscribe $doSubscribe = ( WPFunctions::get()->getCommentMeta( $commentId, 'mailpoet', true ) === 'subscribe_on_comment' ); if ($doSubscribe === true) { $this->subscribeAuthorOfComment($commentId); WPFunctions::get()->deleteCommentMeta($commentId, 'mailpoet'); } } } private function subscribeAuthorOfComment($commentId) { $segmentIds = $this->settings->get('subscribe.on_comment.segments', []); if (!empty($segmentIds)) { $comment = WPFunctions::get()->getComment($commentId); $result = $this->subscriberActions->subscribe( [ 'email' => $comment->commentAuthorEmail, 'first_name' => $comment->commentAuthor, ], $segmentIds ); } } }