| 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 |
* Gets the subscriber ID from the `ck_subscriber_id` cookie. |
| 55 |
* |
| 56 |
* @since 2.0.0 |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
private function get_subscriber_id_from_cookie() { |
| 61 |
|
| 62 |
if ( ! isset( $_COOKIE[ $this->key ] ) ) { |
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
return sanitize_text_field( wp_unslash( $_COOKIE[ $this->key ] ) ); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Stores the given subscriber ID in the `ck_subscriber_id` cookie |
| 72 |
* and a prefixed `wordpress_ck_subscriber_id` cookie. |
| 73 |
* |
| 74 |
* @since 2.0.0 |
| 75 |
* |
| 76 |
* @param int|string $subscriber_id Subscriber ID. |
| 77 |
*/ |
| 78 |
public function set( $subscriber_id ) { |
| 79 |
|
| 80 |
setcookie( $this->key, (string) $subscriber_id, time() + ( 365 * DAY_IN_SECONDS ), '/' ); |
| 81 |
setcookie( 'wordpress_' . $this->key, (string) $subscriber_id, time() + ( 365 * DAY_IN_SECONDS ), '/' ); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Deletes the `ck_subscriber_id` cookie. |
| 87 |
* |
| 88 |
* @since 2.0.0 |
| 89 |
*/ |
| 90 |
public function forget() { |
| 91 |
|
| 92 |
setcookie( $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); |
| 93 |
setcookie( 'wordpress_' . $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|