| @@ -33,16 +33,16 @@ | ||
| 33 | 33 | */ |
| 34 | 34 | public function get_subscriber_id() { |
| 35 | 35 | |
| 36 | 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( sanitize_text_field( $_REQUEST[ $this->key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 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; | |
| 39 | 41 | } |
| 40 | 42 | |
| 41 | 43 | // 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 ] ) ) { | |
| 44 | + if ( isset( $_COOKIE[ $this->key ] ) && ! empty( $_COOKIE[ $this->key ] ) ) { | |
| 45 | 45 | return $this->get_subscriber_id_from_cookie(); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | // If here, no subscriber ID exists. |
| @@ -50,114 +50,36 @@ | ||
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * Validates the given subscriber ID by querying the API to confirm | |
| 55 | - * the subscriber exists before storing their ID in a cookie. | |
| 54 | + * Gets the subscriber ID from the `ck_subscriber_id` cookie. | |
| 56 | 55 | * |
| 57 | 56 | * @since 2.0.0 |
| 58 | 57 | * |
| 59 | - * @param int|string $subscriber_id Possible Subscriber ID or Signed Subscriber ID. | |
| 60 | - * @return WP_Error|int|string Error | Confirmed Subscriber ID or Signed Subscriber ID | |
| 58 | + * @return string | |
| 61 | 59 | */ |
| 62 | - public function validate_and_store_subscriber_id( $subscriber_id ) { | |
| 60 | + private function get_subscriber_id_from_cookie() { | |
| 63 | 61 | |
| 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 | - ); | |
| 62 | + if ( ! isset( $_COOKIE[ $this->key ] ) ) { | |
| 63 | + return ''; | |
| 71 | 64 | } |
| 72 | 65 | |
| 73 | - // Initialize the API. | |
| 74 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); | |
| 66 | + return sanitize_text_field( wp_unslash( $_COOKIE[ $this->key ] ) ); | |
| 75 | 67 | |
| 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 $subscriber['id']; | |
| 93 | - | |
| 94 | 68 | } |
| 95 | 69 | |
| 96 | 70 | /** |
| 97 | - * Validates the given subscriber email by querying the API to confirm | |
| 98 | - * the subscriber exists before storing their ID in a cookie. | |
| 71 | + * Stores the given subscriber ID in the `ck_subscriber_id` cookie | |
| 72 | + * and a prefixed `wordpress_ck_subscriber_id` cookie. | |
| 99 | 73 | * |
| 100 | 74 | * @since 2.0.0 |
| 101 | 75 | * |
| 102 | - * @param string $subscriber_email Possible Subscriber Email. | |
| 103 | - * @return WP_Error|int|string Error | Confirmed Subscriber ID or Signed 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 $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 $_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 | 76 | * @param int|string $subscriber_id Subscriber ID. |
| 156 | 77 | */ |
| 157 | 78 | public function set( $subscriber_id ) { |
| 158 | 79 | |
| 159 | 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 ), '/' ); | |
| 160 | 82 | |
| 161 | 83 | } |
| 162 | 84 | |
| 163 | 85 | /** |
| @@ -167,8 +89,9 @@ | ||
| 167 | 89 | */ |
| 168 | 90 | public function forget() { |
| 169 | 91 | |
| 170 | 92 | setcookie( $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); |
| 93 | + setcookie( 'wordpress_' . $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); | |
| 171 | 94 | |
| 172 | 95 | } |
| 173 | 96 | |
| 174 | 97 | } |