| 1 |
<?php |
| 2 |
namespace MailPoet\Subscription; |
| 3 |
use MailPoet\Models\Setting; |
| 4 |
use MailPoet\Models\Subscriber; |
| 5 |
|
| 6 |
class Comment { |
| 7 |
const SPAM = 'spam'; |
| 8 |
const APPROVED = 1; |
| 9 |
const PENDING_APPROVAL = 0; |
| 10 |
|
| 11 |
static function extendLoggedInForm($field) { |
| 12 |
$field .= self::getSubscriptionField(); |
| 13 |
return $field; |
| 14 |
} |
| 15 |
|
| 16 |
static function extendLoggedOutForm() { |
| 17 |
echo self::getSubscriptionField(); |
| 18 |
} |
| 19 |
|
| 20 |
static function getSubscriptionField() { |
| 21 |
$label = Setting::getValue( |
| 22 |
'subscribe.on_comment.label', |
| 23 |
__('Yes, please add me to your mailing list.', 'mailpoet') |
| 24 |
); |
| 25 |
|
| 26 |
return '<p class="comment-form-mailpoet"> |
| 27 |
<label for="mailpoet_subscribe_on_comment"> |
| 28 |
<input |
| 29 |
type="checkbox" |
| 30 |
id="mailpoet_subscribe_on_comment" |
| 31 |
value="1" |
| 32 |
name="mailpoet[subscribe_on_comment]" |
| 33 |
/> '.esc_attr($label).' |
| 34 |
</label> |
| 35 |
</p>'; |
| 36 |
} |
| 37 |
|
| 38 |
static function onSubmit($comment_id, $comment_status) { |
| 39 |
if($comment_status === Comment::SPAM) return; |
| 40 |
|
| 41 |
if( |
| 42 |
isset($_POST['mailpoet']['subscribe_on_comment']) |
| 43 |
&& (bool)$_POST['mailpoet']['subscribe_on_comment'] === true |
| 44 |
) { |
| 45 |
if($comment_status === Comment::PENDING_APPROVAL) { |
| 46 |
// add a comment meta to remember to subscribe the user |
| 47 |
// once the comment gets approved |
| 48 |
add_comment_meta( |
| 49 |
$comment_id, |
| 50 |
'mailpoet', |
| 51 |
'subscribe_on_comment', |
| 52 |
true |
| 53 |
); |
| 54 |
} else if($comment_status === Comment::APPROVED) { |
| 55 |
static::subscribeAuthorOfComment($comment_id); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static function onStatusUpdate($comment_id, $action) { |
| 61 |
if($action === 'approve') { |
| 62 |
// check if the comment's author wants to subscribe |
| 63 |
$do_subscribe = ( |
| 64 |
get_comment_meta( |
| 65 |
$comment_id, |
| 66 |
'mailpoet', |
| 67 |
true |
| 68 |
) === 'subscribe_on_comment' |
| 69 |
); |
| 70 |
|
| 71 |
if($do_subscribe === true) { |
| 72 |
static::subscribeAuthorOfComment($comment_id); |
| 73 |
|
| 74 |
delete_comment_meta($comment_id, 'mailpoet'); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private static function subscribeAuthorOfComment($comment_id) { |
| 80 |
$segment_ids = Setting::getValue('subscribe.on_comment.segments', array()); |
| 81 |
|
| 82 |
if(!empty($segment_ids)) { |
| 83 |
$comment = get_comment($comment_id); |
| 84 |
|
| 85 |
$result = Subscriber::subscribe( |
| 86 |
array( |
| 87 |
'email' => $comment->comment_author_email, |
| 88 |
'first_name' => $comment->comment_author |
| 89 |
), |
| 90 |
$segment_ids |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|