PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.1.9
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.1.9
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 2.3.3 All 194 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.9, at includes/class-convertkit-settings.php

702 lines 15.3 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 }
49
50 /**
51 * Returns Plugin settings.
52 *
53 * @since 1.9.6
54 *
55 * @return array
56 */
57 public function get() {
58
59 return $this->settings;
60
61 }
62
63 /**
64 * Returns the API Key Plugin setting.
65 *
66 * @since 1.9.6
67 *
68 * @return string
69 */
70 public function get_api_key() {
71
72 // Return API Key from constant, if defined.
73 if ( defined( 'CONVERTKIT_API_KEY' ) ) {
74 return CONVERTKIT_API_KEY;
75 }
76
77 // Return API Key from settings.
78 return $this->settings['api_key'];
79
80 }
81
82 /**
83 * Returns whether the API Key has been set in the Plugin settings.
84 *
85 * @since 1.9.6
86 *
87 * @return bool
88 */
89 public function has_api_key() {
90
91 return ( ! empty( $this->get_api_key() ) ? true : false );
92
93 }
94
95 /**
96 * Returns whether the API Key is stored as a constant in the wp-config.php file.
97 *
98 * @since 1.9.6
99 *
100 * @return bool
101 */
102 public function is_api_key_a_constant() {
103
104 return defined( 'CONVERTKIT_API_KEY' );
105
106 }
107
108 /**
109 * Returns the API Secret Plugin setting.
110 *
111 * @since 1.9.6
112 *
113 * @return string
114 */
115 public function get_api_secret() {
116
117 // Return API Secret from constant, if defined.
118 if ( defined( 'CONVERTKIT_API_SECRET' ) ) {
119 return CONVERTKIT_API_SECRET;
120 }
121
122 // Return API Secret from settings.
123 return $this->settings['api_secret'];
124
125 }
126
127 /**
128 * Returns whether the API Secret has been set in the Plugin settings.
129 *
130 * @since 1.9.6
131 *
132 * @return bool
133 */
134 public function has_api_secret() {
135
136 return ( ! empty( $this->get_api_secret() ) ? true : false );
137
138 }
139
140 /**
141 * Returns whether the API Secret is stored as a constant in the wp-config.php file.
142 *
143 * @since 1.9.6
144 *
145 * @return bool
146 */
147 public function is_api_secret_a_constant() {
148
149 return defined( 'CONVERTKIT_API_SECRET' );
150
151 }
152
153 /**
154 * Returns whether the API Key and Secret have been set in the Plugin settings.
155 *
156 * @since 1.9.6
157 *
158 * @return bool
159 */
160 public function has_api_key_and_secret() {
161
162 _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' );
163
164 // Use check for access and refresh token.
165 return $this->has_access_and_refresh_token();
166
167 }
168
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 /**
259 * Returns the Default Form Plugin setting.
260 *
261 * @since 1.9.6
262 *
263 * @param string $post_type Post Type.
264 * @return string|int Default Form (default|form id)
265 */
266 public function get_default_form( $post_type ) {
267
268 // Return default if this Post Type doesn't exist as a setting.
269 if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) {
270 return 'default';
271 }
272
273 // Backward compat. where older Plugin versions would store API errors in the option value
274 // with id = -2 and name = 'Error contacting API'.
275 if ( is_array( $this->settings[ $post_type . '_form' ] ) ) {
276 return 'default';
277 }
278
279 return $this->settings[ $post_type . '_form' ];
280
281 }
282
283 /**
284 * Returns whether the Default Form has been set in the Plugin settings.
285 *
286 * @since 1.9.6
287 *
288 * @param string $post_type Post Type.
289 * @return bool Post Type has a Default Form setting specified in Plugin Settings.
290 */
291 public function has_default_form( $post_type ) {
292
293 return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false );
294
295 }
296
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 /**
507 * Returns whether debugging is enabled in the Plugin settings.
508 *
509 * @since 1.9.6
510 *
511 * @return bool
512 */
513 public function debug_enabled() {
514
515 return ( $this->settings['debug'] === 'on' ? true : false );
516
517 }
518
519 /**
520 * Returns whether scripts are disabled in the Plugin settings.
521 *
522 * @since 1.9.6
523 *
524 * @return bool
525 */
526 public function scripts_disabled() {
527
528 return ( $this->settings['no_scripts'] === 'on' ? true : false );
529
530 }
531
532 /**
533 * Returns whether stylesheets are disabled in the Plugin settings.
534 *
535 * @since 1.9.6.9
536 *
537 * @return bool
538 */
539 public function css_disabled() {
540
541 return ( $this->settings['no_css'] === 'on' ? true : false );
542
543 }
544
545 /**
546 * Returns whether usage tracking is enabled in the Plugin settings.
547 *
548 * @since 3.0.4
549 *
550 * @return bool
551 */
552 public function usage_tracking() {
553
554 return ( $this->settings['usage_tracking'] === 'on' ? true : false );
555
556 }
557
558 /**
559 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
560 * e.g. on a new installation.
561 *
562 * @since 1.9.6
563 *
564 * @return array
565 */
566 public function get_defaults() {
567
568 $defaults = array(
569 // OAuth.
570 'access_token' => '', // string.
571 'refresh_token' => '', // string.
572 'token_expires' => '', // integer.
573
574 // API Key. Retained if needed for backward compat.
575 'api_key' => '', // string.
576 'api_secret' => '', // string.
577
578 // Site Wide.
579 'non_inline_form' => array(), // array.
580 'non_inline_form_honor_none_setting' => '', // blank|on.
581 'non_inline_form_limit_per_session' => '', // blank|on.
582
583 // reCAPTCHA.
584 'recaptcha_site_key' => '', // string.
585 'recaptcha_secret_key' => '', // string.
586 'recaptcha_minimum_score' => 0.5, // float.
587
588 // Advanced.
589 'debug' => '', // blank|on.
590 'no_scripts' => '', // blank|on.
591 'no_css' => '', // blank|on.
592 'usage_tracking' => '', // blank|on.
593 );
594
595 // Add Post Type Default Forms.
596 foreach ( convertkit_get_supported_post_types() as $post_type ) {
597 $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID.
598 $defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content,before_after_content,element.
599 $defaults[ $post_type . '_form_position_element' ] = 'p';
600 $defaults[ $post_type . '_form_position_element_index' ] = 1;
601 }
602
603 /**
604 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
605 * e.g. on a new installation.
606 *
607 * @since 1.9.6
608 *
609 * @param array $defaults Default Settings.
610 */
611 $defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults );
612
613 return $defaults;
614
615 }
616
617 /**
618 * Saves the new access token, refresh token and its expiry, and schedules
619 * a WordPress Cron event to refresh the token on expiry.
620 *
621 * @since 2.8.3
622 *
623 * @param array $result New Access Token, Refresh Token and Expiry.
624 */
625 public function update_credentials( $result ) {
626
627 // Remove any existing persistent notice.
628 WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' );
629
630 $this->save(
631 array(
632 'access_token' => $result['access_token'],
633 'refresh_token' => $result['refresh_token'],
634 'token_expires' => ( time() + $result['expires_in'] ),
635 )
636 );
637
638 // Clear any existing scheduled WordPress Cron event.
639 wp_clear_scheduled_hook( 'convertkit_refresh_token' );
640
641 // Schedule a WordPress Cron event to refresh the token on expiry.
642 wp_schedule_single_event( ( time() + $result['expires_in'] ), 'convertkit_refresh_token' );
643
644 }
645
646 /**
647 * Deletes any existing access token, refresh token and its expiry from the Plugin settings,
648 * and clears any existing scheduled WordPress Cron event to refresh the token on expiry.
649 *
650 * @since 2.5.0
651 */
652 public function delete_credentials() {
653
654 $this->save(
655 array(
656 'access_token' => '',
657 'refresh_token' => '',
658 'token_expires' => '',
659 )
660 );
661
662 // Clear any existing scheduled WordPress Cron event.
663 wp_clear_scheduled_hook( 'convertkit_refresh_token' );
664
665 }
666
667 /**
668 * Saves the given array of settings to the WordPress options table.
669 *
670 * @since 1.9.8.4
671 *
672 * @param array $settings Settings.
673 */
674 public function save( $settings ) {
675
676 update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) );
677
678 // Reload settings in class, to reflect changes.
679 $this->refresh_settings();
680
681 }
682
683 /**
684 * Reloads settings from the options table so this instance has the latest values.
685 *
686 * @since 3.1.1
687 */
688 private function refresh_settings() {
689
690 $settings = get_option( self::SETTINGS_NAME );
691
692 if ( ! $settings ) {
693 $this->settings = $this->get_defaults();
694 return;
695 }
696
697 $this->settings = array_merge( $this->get_defaults(), $settings );
698
699 }
700
701 }
702