PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.1
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-subscriber.php

class-convertkit-subscriber.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.0.1, at includes/class-convertkit-subscriber.php

175 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Subscriber class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class to confirm a ConvertKit Subscriber ID exists, writing/reading
11 * it from cookie storage.
12 *
13 * @since 2.0.0
14 */
15 class ConvertKit_Subscriber {
16
17 /**
18 * Holds the key to check on requests and store as a cookie.
19 *
20 * @since 2.0.0
21 *
22 * @var string
23 */
24 private $key = 'ck_subscriber_id';
25
26 /**
27 * Gets the subscriber ID from either the request's `ck_subscriber_id` parameter,
28 * or the existing `ck_subscriber_id` cookie.
29 *
30 * @since 2.0.0
31 *
32 * @return WP_Error|bool|int Error | false | Subscriber ID
33 */
34 public function get_subscriber_id() {
35
36 // If the subscriber ID is in the request URI, use it.
37 if ( isset( $_REQUEST[ $this->key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
38 return $this->validate_and_store_subscriber_id( absint( $_REQUEST[ $this->key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification
39 }
40
41 // If the subscriber ID is in a cookie, return it.
42 // For performance, we don't check that the subscriber ID exists every time, otherwise this would
43 // call the API on every page load.
44 if ( isset( $_COOKIE[ $this->key ] ) ) {
45 return $this->get_subscriber_id_from_cookie();
46 }
47
48 // If here, no subscriber ID exists.
49 return false;
50
51 }
52
53 /**
54 * Validates the given subscriber ID by querying the API to confirm
55 * the subscriber exists before storing their ID in a cookie.
56 *
57 * @since 2.0.0
58 *
59 * @param int $subscriber_id Possible Subscriber ID.
60 * @return WP_Error|int Error | Confirmed Subscriber ID
61 */
62 public function validate_and_store_subscriber_id( $subscriber_id ) {
63
64 // Bail if the API hasn't been configured.
65 $settings = new ConvertKit_Settings();
66 if ( ! $settings->has_api_key_and_secret() ) {
67 return new WP_Error(
68 'convertkit_subscriber_get_subscriber_id_from_request_error',
69 __( 'API Key and Secret not configured in Plugin Settings.', 'convertkit' )
70 );
71 }
72
73 // Initialize the API.
74 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() );
75
76 // Get subscriber by ID, to ensure they exist.
77 $subscriber = $api->get_subscriber_by_id( $subscriber_id );
78
79 // Bail if no subscriber exists with the given subscriber ID, or an error occured.
80 if ( is_wp_error( $subscriber ) ) {
81 // Delete the cookie.
82 $this->forget();
83
84 // Return error.
85 return $subscriber;
86 }
87
88 // Store the subscriber ID as a cookie.
89 $this->set( $subscriber['id'] );
90
91 // Return subscriber ID.
92 return absint( $subscriber['id'] );
93
94 }
95
96 /**
97 * Validates the given subscriber email by querying the API to confirm
98 * the subscriber exists before storing their ID in a cookie.
99 *
100 * @since 2.0.0
101 *
102 * @param string $subscriber_email Possible Subscriber Email.
103 * @return WP_Error|int Error | Confirmed Subscriber ID
104 */
105 public function validate_and_store_subscriber_email( $subscriber_email ) {
106
107 // Bail if the API hasn't been configured.
108 $settings = new ConvertKit_Settings();
109 if ( ! $settings->has_api_key_and_secret() ) {
110 return new WP_Error(
111 'convertkit_subscriber_get_subscriber_id_from_request_error',
112 __( 'API Key and Secret not configured in Plugin Settings.', 'convertkit' )
113 );
114 }
115
116 // Initialize the API.
117 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() );
118
119 // Get subscriber by email, to ensure they exist.
120 $subscriber = $api->get_subscriber_by_email( $subscriber_email );
121
122 // Bail if no subscriber exists with the given subscriber ID, or an error occured.
123 if ( is_wp_error( $subscriber ) ) {
124 // Delete the cookie.
125 $this->forget();
126
127 // Return error.
128 return $subscriber;
129 }
130
131 // Store the subscriber ID as a cookie.
132 $this->set( $subscriber['id'] );
133
134 // Return subscriber ID.
135 return absint( $subscriber['id'] );
136
137 }
138
139 /**
140 * Gets the subscriber ID from the `ck_subscriber_id` cookie.
141 *
142 * @since 2.0.0
143 */
144 private function get_subscriber_id_from_cookie() {
145
146 return absint( $_COOKIE[ $this->key ] );
147
148 }
149
150 /**
151 * Stores the given subscriber ID in the `ck_subscriber_id` cookie.
152 *
153 * @since 2.0.0
154 *
155 * @param int $subscriber_id Subscriber ID.
156 */
157 public function set( $subscriber_id ) {
158
159 setcookie( $this->key, (string) $subscriber_id, time() + ( 365 * DAY_IN_SECONDS ), '/' );
160
161 }
162
163 /**
164 * Deletes the `ck_subscriber_id` cookie.
165 *
166 * @since 2.0.0
167 */
168 public function forget() {
169
170 setcookie( $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' );
171
172 }
173
174 }
175