unsubscription.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. defined('ABSPATH') || exit;
  3. require_once NEWSLETTER_INCLUDES_DIR . '/module.php';
  4. class NewsletterUnsubscription extends NewsletterModule {
  5. static $instance;
  6. /**
  7. * @return NewsletterUnsubscription
  8. */
  9. static function instance() {
  10. if (self::$instance == null) {
  11. self::$instance = new NewsletterUnsubscription();
  12. }
  13. return self::$instance;
  14. }
  15. function __construct() {
  16. parent::__construct('unsubscription', '1.0.0');
  17. add_action('init', array($this, 'hook_init'), 1);
  18. add_action('wp_loaded', array($this, 'hook_wp_loaded'));
  19. }
  20. function hook_init() {
  21. add_filter('newsletter_replace', array($this, 'hook_newsletter_replace'), 10, 3);
  22. add_filter('newsletter_page_text', array($this, 'hook_newsletter_page_text'), 10, 3);
  23. }
  24. function hook_wp_loaded() {
  25. global $wpdb;
  26. switch (Newsletter::instance()->action) {
  27. case 'u':
  28. $user = $this->get_user_from_request();
  29. $email = $this->get_email_from_request();
  30. if ($user == null) {
  31. $url = $this->build_message_url(null, 'unsubscription_error', $user);
  32. } else {
  33. $url = $this->build_message_url(null, 'unsubscribe', $user);
  34. }
  35. wp_redirect($url);
  36. die();
  37. break;
  38. case 'uc':
  39. if ($this->antibot_form_check()) {
  40. $user = $this->unsubscribe();
  41. if ($user->status == 'E') {
  42. $url = $this->build_message_url(null, 'unsubscription_error', $user);
  43. } else {
  44. $url = $this->build_message_url(null, 'unsubscribed', $user);
  45. }
  46. wp_redirect($url);
  47. } else {
  48. $this->request_to_antibot_form('Unsubscribe');
  49. }
  50. die();
  51. break;
  52. case 'reactivate':
  53. if ($this->antibot_form_check()) {
  54. $user = $this->reactivate();
  55. $url = $this->build_message_url(null, 'reactivated', $user);
  56. wp_redirect($url);
  57. } else {
  58. $this->request_to_antibot_form('Reactivate');
  59. }
  60. die();
  61. break;
  62. }
  63. }
  64. /**
  65. * Unsubscribes the subscriber from the request. Die on subscriber extraction failure.
  66. *
  67. * @return TNP_User
  68. */
  69. function unsubscribe() {
  70. $user = $this->get_user_from_request(true);
  71. if ($user->status == TNP_User::STATUS_UNSUBSCRIBED) {
  72. return $user;
  73. }
  74. $user = $this->refresh_user_token($user);
  75. $user = $this->set_user_status($user, TNP_User::STATUS_UNSUBSCRIBED);
  76. $this->add_user_log($user, 'unsubscribe');
  77. do_action('newsletter_unsubscribed', $user);
  78. global $wpdb;
  79. $email = $this->get_email_from_request();
  80. if ($email) {
  81. $wpdb->update(NEWSLETTER_USERS_TABLE, array('unsub_email_id' => (int) $email_id, 'unsub_time' => time()), array('id' => $user->id));
  82. }
  83. $this->send_unsubscribed_email($user);
  84. NewsletterSubscription::instance()->notify_admin($user, 'Newsletter unsubscription');
  85. return $user;
  86. }
  87. function send_unsubscribed_email($user, $force = false) {
  88. $options = $this->get_options('', $this->get_user_language($user));
  89. if (!$force && !empty($options['unsubscribed_disabled'])) {
  90. return true;
  91. }
  92. $message = $options['unsubscribed_message'];
  93. $subject = $options['unsubscribed_subject'];
  94. return NewsletterSubscription::instance()->mail($user, $subject, $message);
  95. }
  96. /**
  97. * Reactivate the subscriber extracted from the request setting his status
  98. * to confirmed and logging. No email are sent. Dies on subscriber extraction failure.
  99. *
  100. * @return TNP_User
  101. */
  102. function reactivate() {
  103. $user = $this->get_user_from_request(true);
  104. $user = $this->set_user_status($user, TNP_User::STATUS_CONFIRMED);
  105. $this->add_user_log($user, 'reactivate');
  106. return $user;
  107. }
  108. function hook_newsletter_replace($text, $user, $email) {
  109. if (!$user) {
  110. return $text;
  111. }
  112. $text = $this->replace_url($text, 'UNSUBSCRIPTION_CONFIRM_URL', $this->build_action_url('uc', $user, $email));
  113. $text = $this->replace_url($text, 'UNSUBSCRIPTION_URL', $this->build_action_url('u', $user, $email));
  114. $text = $this->replace_url($text, 'REACTIVATE_URL', $this->build_action_url('reactivate', $user, $email));
  115. return $text;
  116. }
  117. function hook_newsletter_page_text($text, $key, $user = null) {
  118. $options = $this->get_options('', $this->get_current_language($user));
  119. if ($key == 'unsubscribe') {
  120. if (!$user) {
  121. return 'Subscriber not found.';
  122. }
  123. return $options['unsubscribe_text'];
  124. }
  125. if ($key == 'unsubscribed') {
  126. if (!$user) {
  127. return $options['error_text'];
  128. }
  129. return $options['unsubscribed_text'];
  130. }
  131. if ($key == 'reactivated') {
  132. if (!$user) {
  133. return $options['error_text'];
  134. }
  135. return $options['reactivated_text'];
  136. }
  137. if ($key == 'unsubscription_error') {
  138. return $options['error_text'];
  139. }
  140. return $text;
  141. }
  142. function upgrade() {
  143. global $wpdb, $charset_collate;
  144. parent::upgrade();
  145. // Migration code
  146. if (empty($this->options) || empty($this->options['unsubscribe_text'])) {
  147. // Options of the subscription module (worng name, I know)
  148. $options = get_option('newsletter');
  149. $this->options['unsubscribe_text'] = $options['unsubscription_text'];
  150. $this->options['reactivated_text'] = $options['reactivated_text'];
  151. $this->options['unsubscribed_text'] = $options['unsubscribed_text'];
  152. $this->options['unsubscribed_message'] = $options['unsubscribed_message'];
  153. $this->options['unsubscribed_subject'] = $options['unsubscribed_subject'];
  154. $this->save_options($this->options);
  155. }
  156. }
  157. function admin_menu() {
  158. $this->add_admin_page('index', 'Unsubscribe');
  159. }
  160. }
  161. NewsletterUnsubscription::instance();