PluginProbe
Image Source Control Lite – Show Image Credits and Captions / trunk
Image Source Control Lite – Show Image Credits and Captions vtrunk
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / newsletter.php

newsletter.php in Image Source Control Lite – Show Image Credits and Captions trunk, at includes/newsletter.php

112 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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