Нет описания

Comment.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace MailPoet\Subscription;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Settings\SettingsController;
  5. use MailPoet\Subscribers\SubscriberActions;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class Comment {
  8. const SPAM = 'spam';
  9. const APPROVED = 1;
  10. const PENDING_APPROVAL = 0;
  11. /** @var SettingsController */
  12. private $settings;
  13. /** @var SubscriberActions */
  14. private $subscriberActions;
  15. public function __construct(
  16. SettingsController $settings,
  17. SubscriberActions $subscriberActions
  18. ) {
  19. $this->settings = $settings;
  20. $this->subscriberActions = $subscriberActions;
  21. }
  22. public function extendLoggedInForm($field) {
  23. $field .= $this->getSubscriptionField();
  24. return $field;
  25. }
  26. public function extendLoggedOutForm() {
  27. echo $this->getSubscriptionField();
  28. }
  29. private function getSubscriptionField() {
  30. $label = $this->settings->get(
  31. 'subscribe.on_comment.label',
  32. WPFunctions::get()->__('Yes, please add me to your mailing list.', 'mailpoet')
  33. );
  34. return '<p class="comment-form-mailpoet">
  35. <label for="mailpoet_subscribe_on_comment">
  36. <input
  37. type="checkbox"
  38. id="mailpoet_subscribe_on_comment"
  39. value="1"
  40. name="mailpoet[subscribe_on_comment]"
  41. />&nbsp;' . esc_attr($label) . '
  42. </label>
  43. </p>';
  44. }
  45. public function onSubmit($commentId, $commentStatus) {
  46. if ($commentStatus === Comment::SPAM) return;
  47. if (
  48. isset($_POST['mailpoet']['subscribe_on_comment'])
  49. && (bool)$_POST['mailpoet']['subscribe_on_comment'] === true
  50. ) {
  51. if ($commentStatus === Comment::PENDING_APPROVAL) {
  52. // add a comment meta to remember to subscribe the user
  53. // once the comment gets approved
  54. WPFunctions::get()->addCommentMeta(
  55. $commentId,
  56. 'mailpoet',
  57. 'subscribe_on_comment',
  58. true
  59. );
  60. } else if ($commentStatus === Comment::APPROVED) {
  61. $this->subscribeAuthorOfComment($commentId);
  62. }
  63. }
  64. }
  65. public function onStatusUpdate($commentId, $action) {
  66. if ($action === 'approve') {
  67. // check if the comment's author wants to subscribe
  68. $doSubscribe = (
  69. WPFunctions::get()->getCommentMeta(
  70. $commentId,
  71. 'mailpoet',
  72. true
  73. ) === 'subscribe_on_comment'
  74. );
  75. if ($doSubscribe === true) {
  76. $this->subscribeAuthorOfComment($commentId);
  77. WPFunctions::get()->deleteCommentMeta($commentId, 'mailpoet');
  78. }
  79. }
  80. }
  81. private function subscribeAuthorOfComment($commentId) {
  82. $segmentIds = $this->settings->get('subscribe.on_comment.segments', []);
  83. if (!empty($segmentIds)) {
  84. $comment = WPFunctions::get()->getComment($commentId);
  85. $result = $this->subscriberActions->subscribe(
  86. [
  87. 'email' => $comment->comment_author_email, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  88. 'first_name' => $comment->comment_author, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  89. ],
  90. $segmentIds
  91. );
  92. }
  93. }
  94. }