Нет описания

Registration.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Registration {
  8. /** @var SettingsController */
  9. private $settings;
  10. /** @var SubscriberActions */
  11. private $subscriberActions;
  12. /** @var WPFunctions */
  13. private $wp;
  14. public function __construct(
  15. SettingsController $settings,
  16. WPFunctions $wp,
  17. SubscriberActions $subscriberActions
  18. ) {
  19. $this->settings = $settings;
  20. $this->subscriberActions = $subscriberActions;
  21. $this->wp = $wp;
  22. }
  23. public function extendForm() {
  24. $label = $this->settings->get(
  25. 'subscribe.on_register.label',
  26. WPFunctions::get()->__('Yes, please add me to your mailing list.', 'mailpoet')
  27. );
  28. $form = '<p class="registration-form-mailpoet">
  29. <label for="mailpoet_subscribe_on_register">
  30. <input
  31. type="hidden"
  32. id="mailpoet_subscribe_on_register_active"
  33. value="1"
  34. name="mailpoet[subscribe_on_register_active]"
  35. />
  36. <input
  37. type="checkbox"
  38. id="mailpoet_subscribe_on_register"
  39. value="1"
  40. name="mailpoet[subscribe_on_register]"
  41. />&nbsp;' . esc_attr($label) . '
  42. </label>
  43. </p>';
  44. $this->wp->applyFilters('mailpoet_register_form_extend', $form);
  45. print $form;
  46. }
  47. public function onMultiSiteRegister($result) {
  48. if (empty($result['errors']->errors)) {
  49. if (
  50. isset($_POST['mailpoet']['subscribe_on_register'])
  51. && (bool)$_POST['mailpoet']['subscribe_on_register'] === true
  52. ) {
  53. $this->subscribeNewUser(
  54. $result['user_name'],
  55. $result['user_email']
  56. );
  57. }
  58. }
  59. return $result;
  60. }
  61. public function onRegister(
  62. $errors,
  63. $userLogin,
  64. $userEmail = null
  65. ) {
  66. if (
  67. empty($errors->errors)
  68. && isset($_POST['mailpoet']['subscribe_on_register'])
  69. && (bool)$_POST['mailpoet']['subscribe_on_register'] === true
  70. ) {
  71. $this->subscribeNewUser(
  72. $userLogin,
  73. $userEmail
  74. );
  75. }
  76. return $errors;
  77. }
  78. private function subscribeNewUser($name, $email) {
  79. $segmentIds = $this->settings->get(
  80. 'subscribe.on_register.segments',
  81. []
  82. );
  83. $this->subscriberActions->subscribe(
  84. [
  85. 'email' => $email,
  86. 'first_name' => $name,
  87. ],
  88. $segmentIds
  89. );
  90. }
  91. }