| @@ -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. |
| @@ -158,13 +161,99 @@ | ||
| 158 | 161 | * @return bool |
| 159 | 162 | */ |
| 160 | 163 | public function has_api_key_and_secret() { |
| 161 | 164 | |
| 162 | - return $this->has_api_key() && $this->has_api_secret(); | |
| 165 | + _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' ); | |
| 163 | 166 | |
| 167 | + // Use check for access and refresh token. | |
| 168 | + return $this->has_access_and_refresh_token(); | |
| 169 | + | |
| 164 | 170 | } |
| 165 | 171 | |
| 166 | 172 | /** |
| 173 | + * Returns the Access Token Plugin setting. | |
| 174 | + * | |
| 175 | + * @since 2.5.0 | |
| 176 | + * | |
| 177 | + * @return string | |
| 178 | + */ | |
| 179 | + public function get_access_token() { | |
| 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 | + // Return Refresh Token from settings. | |
| 209 | + return $this->settings['refresh_token']; | |
| 210 | + | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Returns whether the Refresh Token has been set in the Plugin settings. | |
| 215 | + * | |
| 216 | + * @since 2.5.0 | |
| 217 | + * | |
| 218 | + * @return bool | |
| 219 | + */ | |
| 220 | + public function has_refresh_token() { | |
| 221 | + | |
| 222 | + return ( ! empty( $this->get_refresh_token() ) ? true : false ); | |
| 223 | + | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Returns whether to use Access and Refresh Tokens for API requests, | |
| 228 | + * based on whether an Access Token and Refresh Token have been saved | |
| 229 | + * in the Plugin settings. | |
| 230 | + * | |
| 231 | + * @since 2.5.0 | |
| 232 | + * | |
| 233 | + * @return bool | |
| 234 | + */ | |
| 235 | + public function has_access_and_refresh_token() { | |
| 236 | + | |
| 237 | + return $this->has_access_token() && $this->has_refresh_token(); | |
| 238 | + | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Returns the Access Token expiry timestamp. | |
| 243 | + * | |
| 244 | + * @since 2.5.0 | |
| 245 | + * | |
| 246 | + * @return int | |
| 247 | + */ | |
| 248 | + public function get_token_expiry() { | |
| 249 | + | |
| 250 | + // Return Token Expiry from settings. | |
| 251 | + return $this->settings['token_expires']; | |
| 252 | + | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 167 | 256 | * Returns the Default Form Plugin setting. |
| 168 | 257 | * |
| 169 | 258 | * @since 1.9.6 |
| 170 | 259 | * |
| @@ -202,8 +291,125 @@ | ||
| 202 | 291 | |
| 203 | 292 | } |
| 204 | 293 | |
| 205 | 294 | /** |
| 295 | + * Returns the Default Form Position Plugin setting. | |
| 296 | + * | |
| 297 | + * @since 2.5.8 | |
| 298 | + * | |
| 299 | + * @param string $post_type Post Type. | |
| 300 | + * @return string|int Default Form (default|form id) | |
| 301 | + */ | |
| 302 | + public function get_default_form_position( $post_type ) { | |
| 303 | + | |
| 304 | + // Return after_content if this Post Type's position doesn't exist as a setting. | |
| 305 | + if ( ! array_key_exists( $post_type . '_form_position', $this->settings ) ) { | |
| 306 | + return 'after_content'; | |
| 307 | + } | |
| 308 | + | |
| 309 | + return $this->settings[ $post_type . '_form_position' ]; | |
| 310 | + | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Returns the Default Form Position Element Plugin setting. | |
| 315 | + * | |
| 316 | + * @since 2.6.1 | |
| 317 | + * | |
| 318 | + * @param string $post_type Post Type. | |
| 319 | + * @return string Element to insert form after | |
| 320 | + */ | |
| 321 | + public function get_default_form_position_element( $post_type ) { | |
| 322 | + | |
| 323 | + // Return after_content if this Post Type's position doesn't exist as a setting. | |
| 324 | + if ( ! array_key_exists( $post_type . '_form_position_element', $this->settings ) ) { | |
| 325 | + return 'p'; | |
| 326 | + } | |
| 327 | + | |
| 328 | + return $this->settings[ $post_type . '_form_position_element' ]; | |
| 329 | + | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** | |
| 333 | + * Returns the Default Form Position Index Plugin setting. | |
| 334 | + * | |
| 335 | + * @since 2.6.1 | |
| 336 | + * | |
| 337 | + * @param string $post_type Post Type. | |
| 338 | + * @return int Number of elements before inserting form | |
| 339 | + */ | |
| 340 | + public function get_default_form_position_element_index( $post_type ) { | |
| 341 | + | |
| 342 | + // Return 1 if this Post Type's position index doesn't exist as a setting. | |
| 343 | + if ( ! array_key_exists( $post_type . '_form_position_element_index', $this->settings ) ) { | |
| 344 | + return 1; | |
| 345 | + } | |
| 346 | + | |
| 347 | + return (int) $this->settings[ $post_type . '_form_position_element_index' ]; | |
| 348 | + | |
| 349 | + } | |
| 350 | + | |
| 351 | + /** | |
| 352 | + * Returns the Global non-inline Form Plugin setting. | |
| 353 | + * | |
| 354 | + * @since 2.3.3 | |
| 355 | + * | |
| 356 | + * @return array | |
| 357 | + */ | |
| 358 | + public function get_non_inline_form() { | |
| 359 | + | |
| 360 | + // Return blank array if no inline form is specified. | |
| 361 | + if ( ! $this->has_non_inline_form() ) { | |
| 362 | + return array(); | |
| 363 | + } | |
| 364 | + | |
| 365 | + // 2.6.8 and earlier stored a single Form ID in a string. | |
| 366 | + if ( is_string( $this->settings['non_inline_form'] ) ) { | |
| 367 | + return array( (int) $this->settings['non_inline_form'] ); | |
| 368 | + } | |
| 369 | + | |
| 370 | + // Cast values to integers and return. | |
| 371 | + return array_map( 'intval', $this->settings['non_inline_form'] ); | |
| 372 | + | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 376 | + * Returns whether the Global non-inline Form has been set in the Plugin settings. | |
| 377 | + * | |
| 378 | + * @since 2.3.3 | |
| 379 | + * | |
| 380 | + * @return bool Global non-inline Form setting specified in Plugin Settings. | |
| 381 | + */ | |
| 382 | + public function has_non_inline_form() { | |
| 383 | + | |
| 384 | + // 2.6.8 and earlier stored a single Form ID in a string. | |
| 385 | + if ( is_string( $this->settings['non_inline_form'] ) ) { | |
| 386 | + if ( ! empty( $this->settings['non_inline_form'] ) ) { | |
| 387 | + return true; | |
| 388 | + } | |
| 389 | + | |
| 390 | + return false; | |
| 391 | + } | |
| 392 | + | |
| 393 | + return ( count( $this->settings['non_inline_form'] ) > 0 ? true : false ); | |
| 394 | + | |
| 395 | + } | |
| 396 | + | |
| 397 | + /** | |
| 398 | + * Returns whether the Global non-inline Form setting should honor the Page / Post | |
| 399 | + * None setting. | |
| 400 | + * | |
| 401 | + * @since 2.7.3 | |
| 402 | + * | |
| 403 | + * @return bool | |
| 404 | + */ | |
| 405 | + public function non_inline_form_honor_none_setting() { | |
| 406 | + | |
| 407 | + return ( $this->settings['non_inline_form_honor_none_setting'] === 'on' ? true : false ); | |
| 408 | + | |
| 409 | + } | |
| 410 | + | |
| 411 | + /** | |
| 206 | 412 | * Returns whether debugging is enabled in the Plugin settings. |
| 207 | 413 | * |
| 208 | 414 | * @since 1.9.6 |
| 209 | 415 | * |
| @@ -251,18 +457,33 @@ | ||
| 251 | 457 | */ |
| 252 | 458 | public function get_defaults() { |
| 253 | 459 | |
| 254 | 460 | $defaults = array( |
| 255 | - 'api_key' => '', // string. | |
| 256 | - 'api_secret' => '', // string. | |
| 257 | - 'debug' => '', // blank|on. | |
| 258 | - 'no_scripts' => '', // blank|on. | |
| 259 | - 'no_css' => '', // blank|on. | |
| 461 | + // OAuth. | |
| 462 | + 'access_token' => '', // string. | |
| 463 | + 'refresh_token' => '', // string. | |
| 464 | + 'token_expires' => '', // integer. | |
| 465 | + | |
| 466 | + // API Key. Retained if needed for backward compat. | |
| 467 | + 'api_key' => '', // string. | |
| 468 | + 'api_secret' => '', // string. | |
| 469 | + | |
| 470 | + // Site Wide. | |
| 471 | + 'non_inline_form' => array(), // array. | |
| 472 | + 'non_inline_form_honor_none_setting' => '', // blank|on. | |
| 473 | + | |
| 474 | + // Advanced. | |
| 475 | + 'debug' => '', // blank|on. | |
| 476 | + 'no_scripts' => '', // blank|on. | |
| 477 | + 'no_css' => '', // blank|on. | |
| 260 | 478 | ); |
| 261 | 479 | |
| 262 | 480 | // Add Post Type Default Forms. |
| 263 | 481 | foreach ( convertkit_get_supported_post_types() as $post_type ) { |
| 264 | - $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. | |
| 482 | + $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. | |
| 483 | + $defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content,before_after_content,element. | |
| 484 | + $defaults[ $post_type . '_form_position_element' ] = 'p'; | |
| 485 | + $defaults[ $post_type . '_form_position_element_index' ] = 1; | |
| 265 | 486 | } |
| 266 | 487 | |
| 267 | 488 | /** |
| 268 | 489 | * The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| @@ -278,8 +499,52 @@ | ||
| 278 | 499 | |
| 279 | 500 | } |
| 280 | 501 | |
| 281 | 502 | /** |
| 503 | + * Saves the new access token, refresh token and its expiry when the API | |
| 504 | + * class automatically refreshes an outdated access token. | |
| 505 | + * | |
| 506 | + * @since 2.5.0 | |
| 507 | + * | |
| 508 | + * @param array $result New Access Token, Refresh Token and Expiry. | |
| 509 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 510 | + */ | |
| 511 | + public function update_credentials( $result, $client_id ) { | |
| 512 | + | |
| 513 | + // Don't save these credentials if they're not for this Client ID. | |
| 514 | + // They're for another ConvertKit Plugin that uses OAuth. | |
| 515 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 516 | + return; | |
| 517 | + } | |
| 518 | + | |
| 519 | + $this->save( | |
| 520 | + array( | |
| 521 | + 'access_token' => $result['access_token'], | |
| 522 | + 'refresh_token' => $result['refresh_token'], | |
| 523 | + 'token_expires' => ( $result['created_at'] + $result['expires_in'] ), | |
| 524 | + ) | |
| 525 | + ); | |
| 526 | + | |
| 527 | + } | |
| 528 | + | |
| 529 | + /** | |
| 530 | + * Deletes any existing access token, refresh token and its expiry from the Plugin settings. | |
| 531 | + * | |
| 532 | + * @since 2.5.0 | |
| 533 | + */ | |
| 534 | + public function delete_credentials() { | |
| 535 | + | |
| 536 | + $this->save( | |
| 537 | + array( | |
| 538 | + 'access_token' => '', | |
| 539 | + 'refresh_token' => '', | |
| 540 | + 'token_expires' => '', | |
| 541 | + ) | |
| 542 | + ); | |
| 543 | + | |
| 544 | + } | |
| 545 | + | |
| 546 | + /** | |
| 282 | 547 | * Saves the given array of settings to the WordPress options table. |
| 283 | 548 | * |
| 284 | 549 | * @since 1.9.8.4 |
| 285 | 550 | * |
| @@ -287,8 +552,11 @@ | ||
| 287 | 552 | */ |
| 288 | 553 | public function save( $settings ) { |
| 289 | 554 | |
| 290 | 555 | update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 556 | + | |
| 557 | + // Reload settings in class, to reflect changes. | |
| 558 | + $this->settings = get_option( self::SETTINGS_NAME ); | |
| 291 | 559 | |
| 292 | 560 | } |
| 293 | 561 | |
| 294 | 562 | } |