| @@ -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,51 +50,8 @@ | ||
| 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. | |
| 56 | - * | |
| 57 | - * @since 2.0.0 | |
| 58 | - * | |
| 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 | |
| 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 $subscriber['id']; | |
| 93 | - | |
| 94 | - } | |
| 95 | - | |
| 96 | - /** | |
| 97 | 54 | * Validates the given subscriber email by querying the API to confirm |
| 98 | 55 | * the subscriber exists before storing their ID in a cookie. |
| 99 | 56 | * |
| 100 | 57 | * @since 2.0.0 |
| @@ -105,35 +62,42 @@ | ||
| 105 | 62 | public function validate_and_store_subscriber_email( $subscriber_email ) { |
| 106 | 63 | |
| 107 | 64 | // Bail if the API hasn't been configured. |
| 108 | 65 | $settings = new ConvertKit_Settings(); |
| 109 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 66 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 110 | 67 | return new WP_Error( |
| 111 | 68 | 'convertkit_subscriber_get_subscriber_id_from_request_error', |
| 112 | - __( 'API Key and Secret not configured in Plugin Settings.', 'convertkit' ) | |
| 69 | + __( 'Access Token not configured in Plugin Settings.', 'convertkit' ) | |
| 113 | 70 | ); |
| 114 | 71 | } |
| 115 | 72 | |
| 116 | 73 | // Initialize the API. |
| 117 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); | |
| 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 | + ); | |
| 118 | 82 | |
| 119 | 83 | // Get subscriber by email, to ensure they exist. |
| 120 | - $subscriber = $api->get_subscriber_by_email( $subscriber_email ); | |
| 84 | + $subscriber_id = $api->get_subscriber_id( $subscriber_email ); | |
| 121 | 85 | |
| 122 | 86 | // Bail if no subscriber exists with the given subscriber ID, or an error occured. |
| 123 | - if ( is_wp_error( $subscriber ) ) { | |
| 87 | + if ( is_wp_error( $subscriber_id ) ) { | |
| 124 | 88 | // Delete the cookie. |
| 125 | 89 | $this->forget(); |
| 126 | 90 | |
| 127 | 91 | // Return error. |
| 128 | - return $subscriber; | |
| 92 | + return $subscriber_id; | |
| 129 | 93 | } |
| 130 | 94 | |
| 131 | 95 | // Store the subscriber ID as a cookie. |
| 132 | - $this->set( $subscriber['id'] ); | |
| 96 | + $this->set( $subscriber_id ); | |
| 133 | 97 | |
| 134 | 98 | // Return subscriber ID. |
| 135 | - return $subscriber['id']; | |
| 99 | + return $subscriber_id; | |
| 136 | 100 | |
| 137 | 101 | } |
| 138 | 102 | |
| 139 | 103 | /** |
| @@ -139,17 +103,24 @@ | ||
| 139 | 103 | /** |
| 140 | 104 | * Gets the subscriber ID from the `ck_subscriber_id` cookie. |
| 141 | 105 | * |
| 142 | 106 | * @since 2.0.0 |
| 107 | + * | |
| 108 | + * @return string | |
| 143 | 109 | */ |
| 144 | 110 | private function get_subscriber_id_from_cookie() { |
| 145 | 111 | |
| 146 | - return $_COOKIE[ $this->key ]; | |
| 112 | + if ( ! isset( $_COOKIE[ $this->key ] ) ) { | |
| 113 | + return ''; | |
| 114 | + } | |
| 147 | 115 | |
| 116 | + return sanitize_text_field( wp_unslash( $_COOKIE[ $this->key ] ) ); | |
| 117 | + | |
| 148 | 118 | } |
| 149 | 119 | |
| 150 | 120 | /** |
| 151 | - * Stores the given subscriber ID in the `ck_subscriber_id` cookie. | |
| 121 | + * Stores the given subscriber ID in the `ck_subscriber_id` cookie | |
| 122 | + * and a prefixed `wordpress_ck_subscriber_id` cookie. | |
| 152 | 123 | * |
| 153 | 124 | * @since 2.0.0 |
| 154 | 125 | * |
| 155 | 126 | * @param int|string $subscriber_id Subscriber ID. |
| @@ -156,8 +127,9 @@ | ||
| 156 | 127 | */ |
| 157 | 128 | public function set( $subscriber_id ) { |
| 158 | 129 | |
| 159 | 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 ), '/' ); | |
| 160 | 132 | |
| 161 | 133 | } |
| 162 | 134 | |
| 163 | 135 | /** |
| @@ -167,8 +139,9 @@ | ||
| 167 | 139 | */ |
| 168 | 140 | public function forget() { |
| 169 | 141 | |
| 170 | 142 | setcookie( $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); |
| 143 | + setcookie( 'wordpress_' . $this->key, '', time() - ( 365 * DAY_IN_SECONDS ), '/' ); | |
| 171 | 144 | |
| 172 | 145 | } |
| 173 | 146 | |
| 174 | 147 | } |