| @@ -44,8 +44,11 @@ | ||
| 44 | 44 | } else { |
| 45 | 45 | $this->settings = array_merge( $this->get_defaults(), $settings ); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | + // Update Access Token when refreshed by the API class. | |
| 49 | + add_action( 'convertkit_api_refresh_token', array( $this, 'update_credentials' ), 10, 2 ); | |
| 50 | + | |
| 48 | 51 | } |
| 49 | 52 | |
| 50 | 53 | /** |
| 51 | 54 | * Returns Plugin settings. |
| @@ -163,8 +166,91 @@ | ||
| 163 | 166 | |
| 164 | 167 | } |
| 165 | 168 | |
| 166 | 169 | /** |
| 170 | + * Returns the Access Token Plugin setting. | |
| 171 | + * | |
| 172 | + * @since 2.5.0 | |
| 173 | + * | |
| 174 | + * @return string | |
| 175 | + */ | |
| 176 | + public function get_access_token() { | |
| 177 | + | |
| 178 | + // Return Access Token from settings. | |
| 179 | + return $this->settings['access_token']; | |
| 180 | + | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Returns whether the Access Token has been set in the Plugin settings. | |
| 185 | + * | |
| 186 | + * @since 2.5.0 | |
| 187 | + * | |
| 188 | + * @return bool | |
| 189 | + */ | |
| 190 | + public function has_access_token() { | |
| 191 | + | |
| 192 | + return ( ! empty( $this->get_access_token() ) ? true : false ); | |
| 193 | + | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Returns the Refresh Token Plugin setting. | |
| 198 | + * | |
| 199 | + * @since 2.5.0 | |
| 200 | + * | |
| 201 | + * @return string | |
| 202 | + */ | |
| 203 | + public function get_refresh_token() { | |
| 204 | + | |
| 205 | + // Return Refresh Token from settings. | |
| 206 | + return $this->settings['refresh_token']; | |
| 207 | + | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Returns whether the Refresh Token has been set in the Plugin settings. | |
| 212 | + * | |
| 213 | + * @since 2.5.0 | |
| 214 | + * | |
| 215 | + * @return bool | |
| 216 | + */ | |
| 217 | + public function has_refresh_token() { | |
| 218 | + | |
| 219 | + return ( ! empty( $this->get_refresh_token() ) ? true : false ); | |
| 220 | + | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Returns whether to use Access and Refresh Tokens for API requests, | |
| 225 | + * based on whether an Access Token and Refresh Token have been saved | |
| 226 | + * in the Plugin settings. | |
| 227 | + * | |
| 228 | + * @since 2.5.0 | |
| 229 | + * | |
| 230 | + * @return bool | |
| 231 | + */ | |
| 232 | + public function has_access_and_refresh_token() { | |
| 233 | + | |
| 234 | + return $this->has_access_token() && $this->has_refresh_token(); | |
| 235 | + | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Returns the Access Token expiry timestamp. | |
| 240 | + * | |
| 241 | + * @since 2.5.0 | |
| 242 | + * | |
| 243 | + * @return int | |
| 244 | + */ | |
| 245 | + public function get_token_expiry() { | |
| 246 | + | |
| 247 | + // Return Token Expiry from settings. | |
| 248 | + return $this->settings['token_expires']; | |
| 249 | + | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** | |
| 167 | 253 | * Returns the Default Form Plugin setting. |
| 168 | 254 | * |
| 169 | 255 | * @since 1.9.6 |
| 170 | 256 | * |
| @@ -202,8 +288,39 @@ | ||
| 202 | 288 | |
| 203 | 289 | } |
| 204 | 290 | |
| 205 | 291 | /** |
| 292 | + * Returns the Global non-inline Form Plugin setting. | |
| 293 | + * | |
| 294 | + * @since 2.3.3 | |
| 295 | + * | |
| 296 | + * @return string|int Non-inline Form (blank string|form id) | |
| 297 | + */ | |
| 298 | + public function get_non_inline_form() { | |
| 299 | + | |
| 300 | + // Return blank string if no inline form is specified. | |
| 301 | + if ( ! $this->has_non_inline_form() ) { | |
| 302 | + return ''; | |
| 303 | + } | |
| 304 | + | |
| 305 | + return $this->settings['non_inline_form']; | |
| 306 | + | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Returns whether the Global non-inline Form has been set in the Plugin settings. | |
| 311 | + * | |
| 312 | + * @since 2.3.3 | |
| 313 | + * | |
| 314 | + * @return bool Global non-inline Form setting specified in Plugin Settings. | |
| 315 | + */ | |
| 316 | + public function has_non_inline_form() { | |
| 317 | + | |
| 318 | + return ( ! empty( $this->settings['non_inline_form'] ) ? true : false ); | |
| 319 | + | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 206 | 323 | * Returns whether debugging is enabled in the Plugin settings. |
| 207 | 324 | * |
| 208 | 325 | * @since 1.9.6 |
| 209 | 326 | * |
| @@ -251,13 +368,22 @@ | ||
| 251 | 368 | */ |
| 252 | 369 | public function get_defaults() { |
| 253 | 370 | |
| 254 | 371 | $defaults = array( |
| 255 | - 'api_key' => '', // string. | |
| 256 | - 'api_secret' => '', // string. | |
| 257 | - 'debug' => '', // blank|on. | |
| 258 | - 'no_scripts' => '', // blank|on. | |
| 259 | - 'no_css' => '', // blank|on. | |
| 372 | + // OAuth. | |
| 373 | + 'access_token' => '', // string. | |
| 374 | + 'refresh_token' => '', // string. | |
| 375 | + 'token_expires' => '', // integer. | |
| 376 | + | |
| 377 | + // API Key. Retained if needed for backward compat. | |
| 378 | + 'api_key' => '', // string. | |
| 379 | + 'api_secret' => '', // string. | |
| 380 | + | |
| 381 | + // Settings. | |
| 382 | + 'non_inline_form' => '', // string. | |
| 383 | + 'debug' => '', // blank|on. | |
| 384 | + 'no_scripts' => '', // blank|on. | |
| 385 | + 'no_css' => '', // blank|on. | |
| 260 | 386 | ); |
| 261 | 387 | |
| 262 | 388 | // Add Post Type Default Forms. |
| 263 | 389 | foreach ( convertkit_get_supported_post_types() as $post_type ) { |
| @@ -278,8 +404,52 @@ | ||
| 278 | 404 | |
| 279 | 405 | } |
| 280 | 406 | |
| 281 | 407 | /** |
| 408 | + * Saves the new access token, refresh token and its expiry when the API | |
| 409 | + * class automatically refreshes an outdated access token. | |
| 410 | + * | |
| 411 | + * @since 2.5.0 | |
| 412 | + * | |
| 413 | + * @param array $result New Access Token, Refresh Token and Expiry. | |
| 414 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 415 | + */ | |
| 416 | + public function update_credentials( $result, $client_id ) { | |
| 417 | + | |
| 418 | + // Don't save these credentials if they're not for this Client ID. | |
| 419 | + // They're for another ConvertKit Plugin that uses OAuth. | |
| 420 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 421 | + return; | |
| 422 | + } | |
| 423 | + | |
| 424 | + $this->save( | |
| 425 | + array( | |
| 426 | + 'access_token' => $result['access_token'], | |
| 427 | + 'refresh_token' => $result['refresh_token'], | |
| 428 | + 'token_expires' => ( $result['created_at'] + $result['expires_in'] ), | |
| 429 | + ) | |
| 430 | + ); | |
| 431 | + | |
| 432 | + } | |
| 433 | + | |
| 434 | + /** | |
| 435 | + * Deletes any existing access token, refresh token and its expiry from the Plugin settings. | |
| 436 | + * | |
| 437 | + * @since 2.5.0 | |
| 438 | + */ | |
| 439 | + public function delete_credentials() { | |
| 440 | + | |
| 441 | + $this->save( | |
| 442 | + array( | |
| 443 | + 'access_token' => '', | |
| 444 | + 'refresh_token' => '', | |
| 445 | + 'token_expires' => '', | |
| 446 | + ) | |
| 447 | + ); | |
| 448 | + | |
| 449 | + } | |
| 450 | + | |
| 451 | + /** | |
| 282 | 452 | * Saves the given array of settings to the WordPress options table. |
| 283 | 453 | * |
| 284 | 454 | * @since 1.9.8.4 |
| 285 | 455 | * |
| @@ -287,8 +457,11 @@ | ||
| 287 | 457 | */ |
| 288 | 458 | public function save( $settings ) { |
| 289 | 459 | |
| 290 | 460 | update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 461 | + | |
| 462 | + // Reload settings in class, to reflect changes. | |
| 463 | + $this->settings = get_option( self::SETTINGS_NAME ); | |
| 291 | 464 | |
| 292 | 465 | } |
| 293 | 466 | |
| 294 | 467 | } |