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