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
convertkit / includes / class-convertkit-settings.php

class-convertkit-settings.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.1.0, at includes/class-convertkit-settings.php

721 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Plugin Settings class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class to read ConvertKit Plugin Settings.
11 *
12 * @since 1.9.6
13 */
14 class ConvertKit_Settings {
15
16 /**
17 * Holds the Settings Key that stores site wide ConvertKit settings
18 *
19 * @var string
20 */
21 const SETTINGS_NAME = '_wp_convertkit_settings';
22
23 /**
24 * Holds the Settings
25 *
26 * @var array
27 */
28 private $settings = array();
29
30 /**
31 * Constructor. Reads settings from options table, falling back to defaults
32 * if no settings exist.
33 *
34 * @since 1.9.6
35 */
36 public function __construct() {
37
38 // Get Settings.
39 $settings = get_option( self::SETTINGS_NAME );
40
41 // If no Settings exist, falback to default settings.
42 if ( ! $settings ) {
43 $this->settings = $this->get_defaults();
44 } else {
45 $this->settings = array_merge( $this->get_defaults(), $settings );
46 }
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
56 }
57
58 /**
59 * Returns Plugin settings.
60 *
61 * @since 1.9.6
62 *
63 * @return array
64 */
65 public function get() {
66
67 return $this->settings;
68
69 }
70
71 /**
72 * Returns the API Key Plugin setting.
73 *
74 * @since 1.9.6
75 *
76 * @return string
77 */
78 public function get_api_key() {
79
80 // Return API Key from constant, if defined.
81 if ( defined( 'CONVERTKIT_API_KEY' ) ) {
82 return CONVERTKIT_API_KEY;
83 }
84
85 // Return API Key from settings.
86 return $this->settings['api_key'];
87
88 }
89
90 /**
91 * Returns whether the API Key has been set in the Plugin settings.
92 *
93 * @since 1.9.6
94 *
95 * @return bool
96 */
97 public function has_api_key() {
98
99 return ( ! empty( $this->get_api_key() ) ? true : false );
100
101 }
102
103 /**
104 * Returns whether the API Key is stored as a constant in the wp-config.php file.
105 *
106 * @since 1.9.6
107 *
108 * @return bool
109 */
110 public function is_api_key_a_constant() {
111
112 return defined( 'CONVERTKIT_API_KEY' );
113
114 }
115
116 /**
117 * Returns the API Secret Plugin setting.
118 *
119 * @since 1.9.6
120 *
121 * @return string
122 */
123 public function get_api_secret() {
124
125 // Return API Secret from constant, if defined.
126 if ( defined( 'CONVERTKIT_API_SECRET' ) ) {
127 return CONVERTKIT_API_SECRET;
128 }
129
130 // Return API Secret from settings.
131 return $this->settings['api_secret'];
132
133 }
134
135 /**
136 * Returns whether the API Secret has been set in the Plugin settings.
137 *
138 * @since 1.9.6
139 *
140 * @return bool
141 */
142 public function has_api_secret() {
143
144 return ( ! empty( $this->get_api_secret() ) ? true : false );
145
146 }
147
148 /**
149 * Returns whether the API Secret is stored as a constant in the wp-config.php file.
150 *
151 * @since 1.9.6
152 *
153 * @return bool
154 */
155 public function is_api_secret_a_constant() {
156
157 return defined( 'CONVERTKIT_API_SECRET' );
158
159 }
160
161 /**
162 * Returns whether the API Key and Secret have been set in the Plugin settings.
163 *
164 * @since 1.9.6
165 *
166 * @return bool
167 */
168 public function has_api_key_and_secret() {
169
170 _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' );
171
172 // Use check for access and refresh token.
173 return $this->has_access_and_refresh_token();
174
175 }
176
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 /**
261 * Returns the Default Form Plugin setting.
262 *
263 * @since 1.9.6
264 *
265 * @param string $post_type Post Type.
266 * @return string|int Default Form (default|form id)
267 */
268 public function get_default_form( $post_type ) {
269
270 // Return default if this Post Type doesn't exist as a setting.
271 if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) {
272 return 'default';
273 }
274
275 // Backward compat. where older Plugin versions would store API errors in the option value
276 // with id = -2 and name = 'Error contacting API'.
277 if ( is_array( $this->settings[ $post_type . '_form' ] ) ) {
278 return 'default';
279 }
280
281 return $this->settings[ $post_type . '_form' ];
282
283 }
284
285 /**
286 * Returns whether the Default Form has been set in the Plugin settings.
287 *
288 * @since 1.9.6
289 *
290 * @param string $post_type Post Type.
291 * @return bool Post Type has a Default Form setting specified in Plugin Settings.
292 */
293 public function has_default_form( $post_type ) {
294
295 return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false );
296
297 }
298
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 /**
509 * Returns whether debugging is enabled in the Plugin settings.
510 *
511 * @since 1.9.6
512 *
513 * @return bool
514 */
515 public function debug_enabled() {
516
517 return ( $this->settings['debug'] === 'on' ? true : false );
518
519 }
520
521 /**
522 * Returns whether scripts are disabled in the Plugin settings.
523 *
524 * @since 1.9.6
525 *
526 * @return bool
527 */
528 public function scripts_disabled() {
529
530 return ( $this->settings['no_scripts'] === 'on' ? true : false );
531
532 }
533
534 /**
535 * Returns whether stylesheets are disabled in the Plugin settings.
536 *
537 * @since 1.9.6.9
538 *
539 * @return bool
540 */
541 public function css_disabled() {
542
543 return ( $this->settings['no_css'] === 'on' ? true : false );
544
545 }
546
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 /**
561 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
562 * e.g. on a new installation.
563 *
564 * @since 1.9.6
565 *
566 * @return array
567 */
568 public function get_defaults() {
569
570 $defaults = array(
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.
595 );
596
597 // Add Post Type Default Forms.
598 foreach ( convertkit_get_supported_post_types() as $post_type ) {
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;
603 }
604
605 /**
606 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
607 * e.g. on a new installation.
608 *
609 * @since 1.9.6
610 *
611 * @param array $defaults Default Settings.
612 */
613 $defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults );
614
615 return $defaults;
616
617 }
618
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 /**
705 * Saves the given array of settings to the WordPress options table.
706 *
707 * @since 1.9.8.4
708 *
709 * @param array $settings Settings.
710 */
711 public function save( $settings ) {
712
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 );
717
718 }
719
720 }
721