PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.2.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.2.3
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 3.2.3, at includes/class-convertkit-subscriber.php

148 lines 3.6 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|string Error | false | Subscriber ID | Signed Subscriber ID
33 */
34 public function get_subscriber_id() {
35
36 // If the subscriber ID is in the request URI, use it.
37 if ( filter_has_var( INPUT_GET, $this->key ) ) {
38 $subscriber_id = filter_input( INPUT_GET, $this->key, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
39 $this->set( $subscriber_id );
40 return $subscriber_id;
41 }
42
43 // If the subscriber ID is in a cookie, return it.
44 if ( isset( $_COOKIE[ $this->key ] ) && ! empty( $_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 email 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 string $subscriber_email Possible Subscriber Email.
60 * @return WP_Error|int|string Error | Confirmed Subscriber ID or Signed Subscriber ID
61 */
62 public function validate_and_store_subscriber_email( $subscriber_email ) {
63
64 // Bail if the API hasn't been configured.
65 $settings = new ConvertKit_Settings();
66 if ( ! $settings->has_access_and_refresh_token() ) {
67 return new WP_Error(
68 'convertkit_subscriber_get_subscriber_id_from_request_error',
69 __( 'Access Token not configured in Plugin Settings.', 'convertkit' )
70 );
71 }
72
73 // Initialize the API.
74 $api = new ConvertKit_API_V4(
75 CONVERTKIT_OAUTH_CLIENT_ID,
76 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
77 $settings->get_access_token(),
78 $settings->get_refresh_token(),
79 $settings->debug_enabled(),
80 'subscriber'
81 );
82
83 // Get subscriber by email, to ensure they exist.
84 $subscriber_id = $api->get_subscriber_id( $subscriber_email );
85
86 // Bail if no subscriber exists with the given subscriber ID, or an error occured.
87 if ( is_wp_error( $subscriber_id ) ) {
88 // Delete the cookie.
89 $this->forget();
90
91 // Return error.
92 return $subscriber_id;
93 }
94
95 // Store the subscriber ID as a cookie.
96 $this->set( $subscriber_id );
97
98 // Return subscriber ID.
99 return $subscriber_id;
100
101 }
102
103 /**
104 * Gets the subscriber ID from the `ck_subscriber_id` cookie.
105 *
106 * @since 2.0.0
107 *
108 * @return string
109 */
110 private function get_subscriber_id_from_cookie() {
111
112 if ( ! isset( $_COOKIE[ $this->key ] ) ) {
113 return '';
114 }
115
116 return sanitize_text_field( wp_unslash( $_COOKIE[ $this->key ] ) );
117
118 }
119
120 /**
121 * Stores the given subscriber ID in the `ck_subscriber_id` cookie
122 * and a prefixed `wordpress_ck_subscriber_id` cookie.
123 *
124 * @since 2.0.0
125 *
126 * @param int|string $subscriber_id Subscriber ID.
127 */
128 public function set( $subscriber_id ) {
129
130 setcookie( $this->key, (string) $subscriber_id, time() + ( 365 * DAY_IN_SECONDS ), '/' );
131 setcookie( 'wordpress_' . $this->key, (string) $subscriber_id, time() + ( 365 * DAY_IN_SECONDS ), '/' );
132
133 }
134
135 /**
136 * Deletes the `ck_subscriber_id` cookie.
137 *
138 * @since 2.0.0
139 */
140 public function forget() {
141
142 setcookie( $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' );
143 setcookie( 'wordpress_' . $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' );
144
145 }
146
147 }
148