| @@ -158,13 +158,105 @@ | ||
| 158 | 158 | * @return bool |
| 159 | 159 | */ |
| 160 | 160 | public function has_api_key_and_secret() { |
| 161 | 161 | |
| 162 | - return $this->has_api_key() && $this->has_api_secret(); | |
| 162 | + _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' ); | |
| 163 | 163 | |
| 164 | + // Use check for access and refresh token. | |
| 165 | + return $this->has_access_and_refresh_token(); | |
| 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 | + // Reload settings from options table, to ensure we have the latest tokens. | |
| 179 | + $this->refresh_settings(); | |
| 180 | + | |
| 181 | + // Return Access Token from settings. | |
| 182 | + return $this->settings['access_token']; | |
| 183 | + | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Returns whether the Access Token has been set in the Plugin settings. | |
| 188 | + * | |
| 189 | + * @since 2.5.0 | |
| 190 | + * | |
| 191 | + * @return bool | |
| 192 | + */ | |
| 193 | + public function has_access_token() { | |
| 194 | + | |
| 195 | + return ( ! empty( $this->get_access_token() ) ? true : false ); | |
| 196 | + | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Returns the Refresh Token Plugin setting. | |
| 201 | + * | |
| 202 | + * @since 2.5.0 | |
| 203 | + * | |
| 204 | + * @return string | |
| 205 | + */ | |
| 206 | + public function get_refresh_token() { | |
| 207 | + | |
| 208 | + // Reload settings from options table, to ensure we have the latest tokens. | |
| 209 | + $this->refresh_settings(); | |
| 210 | + | |
| 211 | + // Return Refresh Token from settings. | |
| 212 | + return $this->settings['refresh_token']; | |
| 213 | + | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * Returns whether the Refresh Token has been set in the Plugin settings. | |
| 218 | + * | |
| 219 | + * @since 2.5.0 | |
| 220 | + * | |
| 221 | + * @return bool | |
| 222 | + */ | |
| 223 | + public function has_refresh_token() { | |
| 224 | + | |
| 225 | + return ( ! empty( $this->get_refresh_token() ) ? true : false ); | |
| 226 | + | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Returns whether to use Access and Refresh Tokens for API requests, | |
| 231 | + * based on whether an Access Token and Refresh Token have been saved | |
| 232 | + * in the Plugin settings. | |
| 233 | + * | |
| 234 | + * @since 2.5.0 | |
| 235 | + * | |
| 236 | + * @return bool | |
| 237 | + */ | |
| 238 | + public function has_access_and_refresh_token() { | |
| 239 | + | |
| 240 | + return $this->has_access_token() && $this->has_refresh_token(); | |
| 241 | + | |
| 242 | + } | |
| 243 | + | |
| 244 | + /** | |
| 245 | + * Returns the Access Token expiry timestamp. | |
| 246 | + * | |
| 247 | + * @since 2.5.0 | |
| 248 | + * | |
| 249 | + * @return int | |
| 250 | + */ | |
| 251 | + public function get_token_expiry() { | |
| 252 | + | |
| 253 | + // Return Token Expiry from settings. | |
| 254 | + return $this->settings['token_expires']; | |
| 255 | + | |
| 256 | + } | |
| 257 | + | |
| 258 | + /** | |
| 167 | 259 | * Returns the Default Form Plugin setting. |
| 168 | 260 | * |
| 169 | 261 | * @since 1.9.6 |
| 170 | 262 | * |
| @@ -202,8 +294,217 @@ | ||
| 202 | 294 | |
| 203 | 295 | } |
| 204 | 296 | |
| 205 | 297 | /** |
| 298 | + * Returns the Default Form Position Plugin setting. | |
| 299 | + * | |
| 300 | + * @since 2.5.8 | |
| 301 | + * | |
| 302 | + * @param string $post_type Post Type. | |
| 303 | + * @return string|int Default Form (default|form id) | |
| 304 | + */ | |
| 305 | + public function get_default_form_position( $post_type ) { | |
| 306 | + | |
| 307 | + // Return after_content if this Post Type's position doesn't exist as a setting. | |
| 308 | + if ( ! array_key_exists( $post_type . '_form_position', $this->settings ) ) { | |
| 309 | + return 'after_content'; | |
| 310 | + } | |
| 311 | + | |
| 312 | + return $this->settings[ $post_type . '_form_position' ]; | |
| 313 | + | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * Returns the Default Form Position Element Plugin setting. | |
| 318 | + * | |
| 319 | + * @since 2.6.1 | |
| 320 | + * | |
| 321 | + * @param string $post_type Post Type. | |
| 322 | + * @return string Element to insert form after | |
| 323 | + */ | |
| 324 | + public function get_default_form_position_element( $post_type ) { | |
| 325 | + | |
| 326 | + // Return after_content if this Post Type's position doesn't exist as a setting. | |
| 327 | + if ( ! array_key_exists( $post_type . '_form_position_element', $this->settings ) ) { | |
| 328 | + return 'p'; | |
| 329 | + } | |
| 330 | + | |
| 331 | + return $this->settings[ $post_type . '_form_position_element' ]; | |
| 332 | + | |
| 333 | + } | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Returns the Default Form Position Index Plugin setting. | |
| 337 | + * | |
| 338 | + * @since 2.6.1 | |
| 339 | + * | |
| 340 | + * @param string $post_type Post Type. | |
| 341 | + * @return int Number of elements before inserting form | |
| 342 | + */ | |
| 343 | + public function get_default_form_position_element_index( $post_type ) { | |
| 344 | + | |
| 345 | + // Return 1 if this Post Type's position index doesn't exist as a setting. | |
| 346 | + if ( ! array_key_exists( $post_type . '_form_position_element_index', $this->settings ) ) { | |
| 347 | + return 1; | |
| 348 | + } | |
| 349 | + | |
| 350 | + return (int) $this->settings[ $post_type . '_form_position_element_index' ]; | |
| 351 | + | |
| 352 | + } | |
| 353 | + | |
| 354 | + /** | |
| 355 | + * Returns the Global non-inline Form Plugin setting. | |
| 356 | + * | |
| 357 | + * @since 2.3.3 | |
| 358 | + * | |
| 359 | + * @return array | |
| 360 | + */ | |
| 361 | + public function get_non_inline_form() { | |
| 362 | + | |
| 363 | + // Return blank array if no inline form is specified. | |
| 364 | + if ( ! $this->has_non_inline_form() ) { | |
| 365 | + return array(); | |
| 366 | + } | |
| 367 | + | |
| 368 | + // 2.6.8 and earlier stored a single Form ID in a string. | |
| 369 | + if ( is_string( $this->settings['non_inline_form'] ) ) { | |
| 370 | + return array( (int) $this->settings['non_inline_form'] ); | |
| 371 | + } | |
| 372 | + | |
| 373 | + // Cast values to integers and return. | |
| 374 | + return array_map( 'intval', $this->settings['non_inline_form'] ); | |
| 375 | + | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * Returns whether the Global non-inline Form has been set in the Plugin settings. | |
| 380 | + * | |
| 381 | + * @since 2.3.3 | |
| 382 | + * | |
| 383 | + * @return bool Global non-inline Form setting specified in Plugin Settings. | |
| 384 | + */ | |
| 385 | + public function has_non_inline_form() { | |
| 386 | + | |
| 387 | + // 2.6.8 and earlier stored a single Form ID in a string. | |
| 388 | + if ( is_string( $this->settings['non_inline_form'] ) ) { | |
| 389 | + if ( ! empty( $this->settings['non_inline_form'] ) ) { | |
| 390 | + return true; | |
| 391 | + } | |
| 392 | + | |
| 393 | + return false; | |
| 394 | + } | |
| 395 | + | |
| 396 | + return ( count( $this->settings['non_inline_form'] ) > 0 ? true : false ); | |
| 397 | + | |
| 398 | + } | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Returns whether the Global non-inline Form setting should honor the Page / Post | |
| 402 | + * None setting. | |
| 403 | + * | |
| 404 | + * @since 2.7.3 | |
| 405 | + * | |
| 406 | + * @return bool | |
| 407 | + */ | |
| 408 | + public function non_inline_form_honor_none_setting() { | |
| 409 | + | |
| 410 | + return ( $this->settings['non_inline_form_honor_none_setting'] === 'on' ? true : false ); | |
| 411 | + | |
| 412 | + } | |
| 413 | + | |
| 414 | + /** | |
| 415 | + * Returns whether the Non-inline Form Limit per Session setting has been set in the Plugin settings. | |
| 416 | + * | |
| 417 | + * @since 3.0.0 | |
| 418 | + * | |
| 419 | + * @return bool | |
| 420 | + */ | |
| 421 | + public function non_inline_form_limit_per_session() { | |
| 422 | + | |
| 423 | + return ( $this->settings['non_inline_form_limit_per_session'] === 'on' ? true : false ); | |
| 424 | + | |
| 425 | + } | |
| 426 | + | |
| 427 | + /** | |
| 428 | + * Returns the reCAPTCHA Site Key Plugin setting. | |
| 429 | + * | |
| 430 | + * @since 3.0.0 | |
| 431 | + * | |
| 432 | + * @return string | |
| 433 | + */ | |
| 434 | + public function recaptcha_site_key() { | |
| 435 | + | |
| 436 | + return $this->settings['recaptcha_site_key']; | |
| 437 | + | |
| 438 | + } | |
| 439 | + | |
| 440 | + /** | |
| 441 | + * Returns whether the reCAPTCHA Site Key has been set in the Plugin settings. | |
| 442 | + * | |
| 443 | + * @since 3.0.0 | |
| 444 | + * | |
| 445 | + * @return bool | |
| 446 | + */ | |
| 447 | + public function has_recaptcha_site_key() { | |
| 448 | + | |
| 449 | + return ! empty( $this->recaptcha_site_key() ); | |
| 450 | + | |
| 451 | + } | |
| 452 | + | |
| 453 | + /** | |
| 454 | + * Returns the reCAPTCHA Secret Key Plugin setting. | |
| 455 | + * | |
| 456 | + * @since 3.0.0 | |
| 457 | + * | |
| 458 | + * @return string | |
| 459 | + */ | |
| 460 | + public function recaptcha_secret_key() { | |
| 461 | + | |
| 462 | + return $this->settings['recaptcha_secret_key']; | |
| 463 | + | |
| 464 | + } | |
| 465 | + | |
| 466 | + /** | |
| 467 | + * Returns whether the reCAPTCHA Secret Key has been set in the Plugin settings. | |
| 468 | + * | |
| 469 | + * @since 3.0.0 | |
| 470 | + * | |
| 471 | + * @return bool | |
| 472 | + */ | |
| 473 | + public function has_recaptcha_secret_key() { | |
| 474 | + | |
| 475 | + return ! empty( $this->recaptcha_secret_key() ); | |
| 476 | + | |
| 477 | + } | |
| 478 | + | |
| 479 | + /** | |
| 480 | + * Returns whether the reCAPTCH Site Key and Secret Key are defined | |
| 481 | + * in the Plugin settings. | |
| 482 | + * | |
| 483 | + * @since 3.0.0 | |
| 484 | + * | |
| 485 | + * @return bool | |
| 486 | + */ | |
| 487 | + public function has_recaptcha_site_and_secret_keys() { | |
| 488 | + | |
| 489 | + return $this->has_recaptcha_site_key() && $this->has_recaptcha_secret_key(); | |
| 490 | + | |
| 491 | + } | |
| 492 | + | |
| 493 | + /** | |
| 494 | + * Returns the reCAPTCHA minimum score Plugin setting. | |
| 495 | + * | |
| 496 | + * @since 3.0.0 | |
| 497 | + * | |
| 498 | + * @return float | |
| 499 | + */ | |
| 500 | + public function recaptcha_minimum_score() { | |
| 501 | + | |
| 502 | + return (float) $this->settings['recaptcha_minimum_score']; | |
| 503 | + | |
| 504 | + } | |
| 505 | + | |
| 506 | + /** | |
| 206 | 507 | * Returns whether debugging is enabled in the Plugin settings. |
| 207 | 508 | * |
| 208 | 509 | * @since 1.9.6 |
| 209 | 510 | * |
| @@ -241,8 +542,34 @@ | ||
| 241 | 542 | |
| 242 | 543 | } |
| 243 | 544 | |
| 244 | 545 | /** |
| 546 | + * Returns whether the Add New Landing Page / Member Content button is disabled in the Plugin settings. | |
| 547 | + * | |
| 548 | + * @since 3.2.0 | |
| 549 | + * | |
| 550 | + * @return bool | |
| 551 | + */ | |
| 552 | + public function add_new_button_disabled() { | |
| 553 | + | |
| 554 | + return ( $this->settings['no_add_new_button'] === 'on' ? true : false ); | |
| 555 | + | |
| 556 | + } | |
| 557 | + | |
| 558 | + /** | |
| 559 | + * Returns whether usage tracking is enabled in the Plugin settings. | |
| 560 | + * | |
| 561 | + * @since 3.0.4 | |
| 562 | + * | |
| 563 | + * @return bool | |
| 564 | + */ | |
| 565 | + public function usage_tracking() { | |
| 566 | + | |
| 567 | + return ( $this->settings['usage_tracking'] === 'on' ? true : false ); | |
| 568 | + | |
| 569 | + } | |
| 570 | + | |
| 571 | + /** | |
| 245 | 572 | * The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| 246 | 573 | * e.g. on a new installation. |
| 247 | 574 | * |
| 248 | 575 | * @since 1.9.6 |
| @@ -251,18 +578,41 @@ | ||
| 251 | 578 | */ |
| 252 | 579 | public function get_defaults() { |
| 253 | 580 | |
| 254 | 581 | $defaults = array( |
| 255 | - 'api_key' => '', // string. | |
| 256 | - 'api_secret' => '', // string. | |
| 257 | - 'debug' => '', // blank|on. | |
| 258 | - 'no_scripts' => '', // blank|on. | |
| 259 | - 'no_css' => '', // blank|on. | |
| 582 | + // OAuth. | |
| 583 | + 'access_token' => '', // string. | |
| 584 | + 'refresh_token' => '', // string. | |
| 585 | + 'token_expires' => '', // integer. | |
| 586 | + | |
| 587 | + // API Key. Retained if needed for backward compat. | |
| 588 | + 'api_key' => '', // string. | |
| 589 | + 'api_secret' => '', // string. | |
| 590 | + | |
| 591 | + // Site Wide. | |
| 592 | + 'non_inline_form' => array(), // array. | |
| 593 | + 'non_inline_form_honor_none_setting' => '', // blank|on. | |
| 594 | + 'non_inline_form_limit_per_session' => '', // blank|on. | |
| 595 | + | |
| 596 | + // reCAPTCHA. | |
| 597 | + 'recaptcha_site_key' => '', // string. | |
| 598 | + 'recaptcha_secret_key' => '', // string. | |
| 599 | + 'recaptcha_minimum_score' => 0.5, // float. | |
| 600 | + | |
| 601 | + // Advanced. | |
| 602 | + 'debug' => '', // blank|on. | |
| 603 | + 'no_scripts' => '', // blank|on. | |
| 604 | + 'no_css' => '', // blank|on. | |
| 605 | + 'no_add_new_button' => '', // blank|on. | |
| 606 | + 'usage_tracking' => '', // blank|on. | |
| 260 | 607 | ); |
| 261 | 608 | |
| 262 | 609 | // Add Post Type Default Forms. |
| 263 | 610 | foreach ( convertkit_get_supported_post_types() as $post_type ) { |
| 264 | - $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. | |
| 611 | + $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. | |
| 612 | + $defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content,before_after_content,element. | |
| 613 | + $defaults[ $post_type . '_form_position_element' ] = 'p'; | |
| 614 | + $defaults[ $post_type . '_form_position_element_index' ] = 1; | |
| 265 | 615 | } |
| 266 | 616 | |
| 267 | 617 | /** |
| 268 | 618 | * The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| @@ -278,8 +628,63 @@ | ||
| 278 | 628 | |
| 279 | 629 | } |
| 280 | 630 | |
| 281 | 631 | /** |
| 632 | + * Saves the new access token, refresh token and its expiry, and schedules | |
| 633 | + * a WordPress Cron event to refresh the token on expiry. | |
| 634 | + * | |
| 635 | + * @since 2.8.3 | |
| 636 | + * | |
| 637 | + * @param array $result New Access Token, Refresh Token and Expiry. | |
| 638 | + */ | |
| 639 | + public function update_credentials( $result ) { | |
| 640 | + | |
| 641 | + // Remove any existing persistent notice. | |
| 642 | + WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' ); | |
| 643 | + | |
| 644 | + $this->save( | |
| 645 | + array( | |
| 646 | + 'access_token' => $result['access_token'], | |
| 647 | + 'refresh_token' => $result['refresh_token'], | |
| 648 | + 'token_expires' => ( time() + $result['expires_in'] ), | |
| 649 | + ) | |
| 650 | + ); | |
| 651 | + | |
| 652 | + // Clear any existing scheduled WordPress Cron event. | |
| 653 | + wp_clear_scheduled_hook( 'convertkit_refresh_token' ); | |
| 654 | + | |
| 655 | + // Schedule a WordPress Cron event to refresh the token on expiry. | |
| 656 | + wp_schedule_single_event( ( time() + $result['expires_in'] ), 'convertkit_refresh_token' ); | |
| 657 | + | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * Deletes any existing access token, refresh token and its expiry from the Plugin settings, | |
| 662 | + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry. | |
| 663 | + * | |
| 664 | + * @since 2.5.0 | |
| 665 | + */ | |
| 666 | + public function delete_credentials() { | |
| 667 | + | |
| 668 | + $this->save( | |
| 669 | + array( | |
| 670 | + // OAuth. | |
| 671 | + 'access_token' => '', | |
| 672 | + 'refresh_token' => '', | |
| 673 | + 'token_expires' => '', | |
| 674 | + | |
| 675 | + // API Key. | |
| 676 | + 'api_key' => '', | |
| 677 | + 'api_secret' => '', | |
| 678 | + ) | |
| 679 | + ); | |
| 680 | + | |
| 681 | + // Clear any existing scheduled WordPress Cron event. | |
| 682 | + wp_clear_scheduled_hook( 'convertkit_refresh_token' ); | |
| 683 | + | |
| 684 | + } | |
| 685 | + | |
| 686 | + /** | |
| 282 | 687 | * Saves the given array of settings to the WordPress options table. |
| 283 | 688 | * |
| 284 | 689 | * @since 1.9.8.4 |
| 285 | 690 | * |
| @@ -287,8 +692,29 @@ | ||
| 287 | 692 | */ |
| 288 | 693 | public function save( $settings ) { |
| 289 | 694 | |
| 290 | 695 | update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 696 | + | |
| 697 | + // Reload settings in class, to reflect changes. | |
| 698 | + $this->refresh_settings(); | |
| 699 | + | |
| 700 | + } | |
| 701 | + | |
| 702 | + /** | |
| 703 | + * Reloads settings from the options table so this instance has the latest values. | |
| 704 | + * | |
| 705 | + * @since 3.1.1 | |
| 706 | + */ | |
| 707 | + private function refresh_settings() { | |
| 708 | + | |
| 709 | + $settings = get_option( self::SETTINGS_NAME ); | |
| 710 | + | |
| 711 | + if ( ! $settings ) { | |
| 712 | + $this->settings = $this->get_defaults(); | |
| 713 | + return; | |
| 714 | + } | |
| 715 | + | |
| 716 | + $this->settings = array_merge( $this->get_defaults(), $settings ); | |
| 291 | 717 | |
| 292 | 718 | } |
| 293 | 719 | |
| 294 | 720 | } |