| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handle newsletter signup |
| 7 |
*/ |
| 8 |
class Newsletter { |
| 9 |
|
| 10 |
/** |
| 11 |
* Signup URL |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private const SIGNUP_URL = 'https://imagesourcecontrol.com/remote/subscribe.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Subscribe to the newsletter |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
public function subscribe(): array { |
| 23 |
$return = [ |
| 24 |
'success' => false, |
| 25 |
]; |
| 26 |
|
| 27 |
$email = User::get_email(); |
| 28 |
if ( ! $email ) { |
| 29 |
$return['message'] = 'Email invalid'; |
| 30 |
return $return; |
| 31 |
} |
| 32 |
|
| 33 |
$data = [ |
| 34 |
'email' => $email, |
| 35 |
'lang' => User::has_german_backend() ? 'de' : 'en', |
| 36 |
]; |
| 37 |
|
| 38 |
$result = wp_remote_post( |
| 39 |
self::SIGNUP_URL, |
| 40 |
[ |
| 41 |
'method' => 'POST', |
| 42 |
'timeout' => 20, |
| 43 |
'redirection' => 5, |
| 44 |
'httpversion' => '1.1', |
| 45 |
'blocking' => true, |
| 46 |
'body' => $data, |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
$status_code = wp_remote_retrieve_response_code( $result ); |
| 51 |
|
| 52 |
if ( is_wp_error( $result ) ) { |
| 53 |
$return['message'] = esc_html__( 'Something went wrong. Please sign up manually.', 'image-source-control-isc' ); |
| 54 |
} elseif ( $status_code === 201 ) { |
| 55 |
$return['message'] = esc_html__( 'Please check your email for the confirmation message.', 'image-source-control-isc' ); |
| 56 |
$return['success'] = true; |
| 57 |
// mark as subscribed |
| 58 |
$this->mark_current_users_as_subscribed(); |
| 59 |
} elseif ( $status_code === 204 ) { |
| 60 |
$return['message'] = esc_html__( 'The email address is invalid.', 'image-source-control-isc' ); |
| 61 |
} elseif ( $status_code === 304 ) { |
| 62 |
// the email is already subscribed to any list; if they weren’t subscribed to Marketing, they are now |
| 63 |
$return['message'] = esc_html__( 'The email address is already subscribed.', 'image-source-control-isc' ); |
| 64 |
$return['success'] = true; |
| 65 |
// if users see this, their WordPress probably doesn’t know that they are already subscribed |
| 66 |
$this->mark_current_users_as_subscribed(); |
| 67 |
} else { |
| 68 |
$return['message'] = esc_html__( 'Something went wrong. Please sign up manually.', 'image-source-control-isc' ); |
| 69 |
} |
| 70 |
|
| 71 |
return $return; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Close the newsletter box |
| 76 |
*/ |
| 77 |
public function close() { |
| 78 |
update_user_meta( get_current_user_id(), 'isc_newsletter_closed', true ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Check if the user is subscribed to the newsletter |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public function current_user_is_subscribed() { |
| 87 |
$user_id = get_current_user_id(); |
| 88 |
|
| 89 |
return ! $user_id || get_user_meta( $user_id, 'isc_newsletter_subscribed', true ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Check if the user closed the newsletter box |
| 94 |
* |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
public function current_user_closed_signup(): bool { |
| 98 |
$user_id = get_current_user_id(); |
| 99 |
|
| 100 |
return ! $user_id || get_user_meta( $user_id, 'isc_newsletter_closed', true ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Update information that the current user is subscribed |
| 105 |
*/ |
| 106 |
private function mark_current_users_as_subscribed() { |
| 107 |
if ( ! $this->current_user_is_subscribed() ) { |
| 108 |
update_user_meta( get_current_user_id(), 'isc_newsletter_subscribed', true ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|