PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.0.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.0.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 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.0.2, at includes/class-convertkit-settings.php

668 lines 14.7 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 }
53
54 /**
55 * Returns Plugin settings.
56 *
57 * @since 1.9.6
58 *
59 * @return array
60 */
61 public function get() {
62
63 return $this->settings;
64
65 }
66
67 /**
68 * Returns the API Key Plugin setting.
69 *
70 * @since 1.9.6
71 *
72 * @return string
73 */
74 public function get_api_key() {
75
76 // Return API Key from constant, if defined.
77 if ( defined( 'CONVERTKIT_API_KEY' ) ) {
78 return CONVERTKIT_API_KEY;
79 }
80
81 // Return API Key from settings.
82 return $this->settings['api_key'];
83
84 }
85
86 /**
87 * Returns whether the API Key has been set in the Plugin settings.
88 *
89 * @since 1.9.6
90 *
91 * @return bool
92 */
93 public function has_api_key() {
94
95 return ( ! empty( $this->get_api_key() ) ? true : false );
96
97 }
98
99 /**
100 * Returns whether the API Key is stored as a constant in the wp-config.php file.
101 *
102 * @since 1.9.6
103 *
104 * @return bool
105 */
106 public function is_api_key_a_constant() {
107
108 return defined( 'CONVERTKIT_API_KEY' );
109
110 }
111
112 /**
113 * Returns the API Secret Plugin setting.
114 *
115 * @since 1.9.6
116 *
117 * @return string
118 */
119 public function get_api_secret() {
120
121 // Return API Secret from constant, if defined.
122 if ( defined( 'CONVERTKIT_API_SECRET' ) ) {
123 return CONVERTKIT_API_SECRET;
124 }
125
126 // Return API Secret from settings.
127 return $this->settings['api_secret'];
128
129 }
130
131 /**
132 * Returns whether the API Secret has been set in the Plugin settings.
133 *
134 * @since 1.9.6
135 *
136 * @return bool
137 */
138 public function has_api_secret() {
139
140 return ( ! empty( $this->get_api_secret() ) ? true : false );
141
142 }
143
144 /**
145 * Returns whether the API Secret is stored as a constant in the wp-config.php file.
146 *
147 * @since 1.9.6
148 *
149 * @return bool
150 */
151 public function is_api_secret_a_constant() {
152
153 return defined( 'CONVERTKIT_API_SECRET' );
154
155 }
156
157 /**
158 * Returns whether the API Key and Secret have been set in the Plugin settings.
159 *
160 * @since 1.9.6
161 *
162 * @return bool
163 */
164 public function has_api_key_and_secret() {
165
166 _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' );
167
168 // Use check for access and refresh token.
169 return $this->has_access_and_refresh_token();
170
171 }
172
173 /**
174 * Returns the Access Token Plugin setting.
175 *
176 * @since 2.5.0
177 *
178 * @return string
179 */
180 public function get_access_token() {
181
182 // Return Access Token from settings.
183 return $this->settings['access_token'];
184
185 }
186
187 /**
188 * Returns whether the Access Token has been set in the Plugin settings.
189 *
190 * @since 2.5.0
191 *
192 * @return bool
193 */
194 public function has_access_token() {
195
196 return ( ! empty( $this->get_access_token() ) ? true : false );
197
198 }
199
200 /**
201 * Returns the Refresh Token Plugin setting.
202 *
203 * @since 2.5.0
204 *
205 * @return string
206 */
207 public function get_refresh_token() {
208
209 // Return Refresh Token from settings.
210 return $this->settings['refresh_token'];
211
212 }
213
214 /**
215 * Returns whether the Refresh Token has been set in the Plugin settings.
216 *
217 * @since 2.5.0
218 *
219 * @return bool
220 */
221 public function has_refresh_token() {
222
223 return ( ! empty( $this->get_refresh_token() ) ? true : false );
224
225 }
226
227 /**
228 * Returns whether to use Access and Refresh Tokens for API requests,
229 * based on whether an Access Token and Refresh Token have been saved
230 * in the Plugin settings.
231 *
232 * @since 2.5.0
233 *
234 * @return bool
235 */
236 public function has_access_and_refresh_token() {
237
238 return $this->has_access_token() && $this->has_refresh_token();
239
240 }
241
242 /**
243 * Returns the Access Token expiry timestamp.
244 *
245 * @since 2.5.0
246 *
247 * @return int
248 */
249 public function get_token_expiry() {
250
251 // Return Token Expiry from settings.
252 return $this->settings['token_expires'];
253
254 }
255
256 /**
257 * Returns the Default Form Plugin setting.
258 *
259 * @since 1.9.6
260 *
261 * @param string $post_type Post Type.
262 * @return string|int Default Form (default|form id)
263 */
264 public function get_default_form( $post_type ) {
265
266 // Return default if this Post Type doesn't exist as a setting.
267 if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) {
268 return 'default';
269 }
270
271 // Backward compat. where older Plugin versions would store API errors in the option value
272 // with id = -2 and name = 'Error contacting API'.
273 if ( is_array( $this->settings[ $post_type . '_form' ] ) ) {
274 return 'default';
275 }
276
277 return $this->settings[ $post_type . '_form' ];
278
279 }
280
281 /**
282 * Returns whether the Default Form has been set in the Plugin settings.
283 *
284 * @since 1.9.6
285 *
286 * @param string $post_type Post Type.
287 * @return bool Post Type has a Default Form setting specified in Plugin Settings.
288 */
289 public function has_default_form( $post_type ) {
290
291 return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false );
292
293 }
294
295 /**
296 * Returns the Default Form Position Plugin setting.
297 *
298 * @since 2.5.8
299 *
300 * @param string $post_type Post Type.
301 * @return string|int Default Form (default|form id)
302 */
303 public function get_default_form_position( $post_type ) {
304
305 // Return after_content if this Post Type's position doesn't exist as a setting.
306 if ( ! array_key_exists( $post_type . '_form_position', $this->settings ) ) {
307 return 'after_content';
308 }
309
310 return $this->settings[ $post_type . '_form_position' ];
311
312 }
313
314 /**
315 * Returns the Default Form Position Element Plugin setting.
316 *
317 * @since 2.6.1
318 *
319 * @param string $post_type Post Type.
320 * @return string Element to insert form after
321 */
322 public function get_default_form_position_element( $post_type ) {
323
324 // Return after_content if this Post Type's position doesn't exist as a setting.
325 if ( ! array_key_exists( $post_type . '_form_position_element', $this->settings ) ) {
326 return 'p';
327 }
328
329 return $this->settings[ $post_type . '_form_position_element' ];
330
331 }
332
333 /**
334 * Returns the Default Form Position Index Plugin setting.
335 *
336 * @since 2.6.1
337 *
338 * @param string $post_type Post Type.
339 * @return int Number of elements before inserting form
340 */
341 public function get_default_form_position_element_index( $post_type ) {
342
343 // Return 1 if this Post Type's position index doesn't exist as a setting.
344 if ( ! array_key_exists( $post_type . '_form_position_element_index', $this->settings ) ) {
345 return 1;
346 }
347
348 return (int) $this->settings[ $post_type . '_form_position_element_index' ];
349
350 }
351
352 /**
353 * Returns the Global non-inline Form Plugin setting.
354 *
355 * @since 2.3.3
356 *
357 * @return array
358 */
359 public function get_non_inline_form() {
360
361 // Return blank array if no inline form is specified.
362 if ( ! $this->has_non_inline_form() ) {
363 return array();
364 }
365
366 // 2.6.8 and earlier stored a single Form ID in a string.
367 if ( is_string( $this->settings['non_inline_form'] ) ) {
368 return array( (int) $this->settings['non_inline_form'] );
369 }
370
371 // Cast values to integers and return.
372 return array_map( 'intval', $this->settings['non_inline_form'] );
373
374 }
375
376 /**
377 * Returns whether the Global non-inline Form has been set in the Plugin settings.
378 *
379 * @since 2.3.3
380 *
381 * @return bool Global non-inline Form setting specified in Plugin Settings.
382 */
383 public function has_non_inline_form() {
384
385 // 2.6.8 and earlier stored a single Form ID in a string.
386 if ( is_string( $this->settings['non_inline_form'] ) ) {
387 if ( ! empty( $this->settings['non_inline_form'] ) ) {
388 return true;
389 }
390
391 return false;
392 }
393
394 return ( count( $this->settings['non_inline_form'] ) > 0 ? true : false );
395
396 }
397
398 /**
399 * Returns whether the Global non-inline Form setting should honor the Page / Post
400 * None setting.
401 *
402 * @since 2.7.3
403 *
404 * @return bool
405 */
406 public function non_inline_form_honor_none_setting() {
407
408 return ( $this->settings['non_inline_form_honor_none_setting'] === 'on' ? true : false );
409
410 }
411
412 /**
413 * Returns whether the Non-inline Form Limit per Session setting has been set in the Plugin settings.
414 *
415 * @since 3.0.0
416 *
417 * @return bool
418 */
419 public function non_inline_form_limit_per_session() {
420
421 return ( $this->settings['non_inline_form_limit_per_session'] === 'on' ? true : false );
422
423 }
424
425 /**
426 * Returns the reCAPTCHA Site Key Plugin setting.
427 *
428 * @since 3.0.0
429 *
430 * @return string
431 */
432 public function recaptcha_site_key() {
433
434 return $this->settings['recaptcha_site_key'];
435
436 }
437
438 /**
439 * Returns whether the reCAPTCHA Site Key has been set in the Plugin settings.
440 *
441 * @since 3.0.0
442 *
443 * @return bool
444 */
445 public function has_recaptcha_site_key() {
446
447 return ! empty( $this->recaptcha_site_key() );
448
449 }
450
451 /**
452 * Returns the reCAPTCHA Secret Key Plugin setting.
453 *
454 * @since 3.0.0
455 *
456 * @return string
457 */
458 public function recaptcha_secret_key() {
459
460 return $this->settings['recaptcha_secret_key'];
461
462 }
463
464 /**
465 * Returns whether the reCAPTCHA Secret Key has been set in the Plugin settings.
466 *
467 * @since 3.0.0
468 *
469 * @return bool
470 */
471 public function has_recaptcha_secret_key() {
472
473 return ! empty( $this->recaptcha_secret_key() );
474
475 }
476
477 /**
478 * Returns whether the reCAPTCH Site Key and Secret Key are defined
479 * in the Plugin settings.
480 *
481 * @since 3.0.0
482 *
483 * @return bool
484 */
485 public function has_recaptcha_site_and_secret_keys() {
486
487 return $this->has_recaptcha_site_key() && $this->has_recaptcha_secret_key();
488
489 }
490
491 /**
492 * Returns the reCAPTCHA minimum score Plugin setting.
493 *
494 * @since 3.0.0
495 *
496 * @return float
497 */
498 public function recaptcha_minimum_score() {
499
500 return (float) $this->settings['recaptcha_minimum_score'];
501
502 }
503
504 /**
505 * Returns whether debugging is enabled in the Plugin settings.
506 *
507 * @since 1.9.6
508 *
509 * @return bool
510 */
511 public function debug_enabled() {
512
513 return ( $this->settings['debug'] === 'on' ? true : false );
514
515 }
516
517 /**
518 * Returns whether scripts are disabled in the Plugin settings.
519 *
520 * @since 1.9.6
521 *
522 * @return bool
523 */
524 public function scripts_disabled() {
525
526 return ( $this->settings['no_scripts'] === 'on' ? true : false );
527
528 }
529
530 /**
531 * Returns whether stylesheets are disabled in the Plugin settings.
532 *
533 * @since 1.9.6.9
534 *
535 * @return bool
536 */
537 public function css_disabled() {
538
539 return ( $this->settings['no_css'] === 'on' ? true : false );
540
541 }
542
543 /**
544 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
545 * e.g. on a new installation.
546 *
547 * @since 1.9.6
548 *
549 * @return array
550 */
551 public function get_defaults() {
552
553 $defaults = array(
554 // OAuth.
555 'access_token' => '', // string.
556 'refresh_token' => '', // string.
557 'token_expires' => '', // integer.
558
559 // API Key. Retained if needed for backward compat.
560 'api_key' => '', // string.
561 'api_secret' => '', // string.
562
563 // Site Wide.
564 'non_inline_form' => array(), // array.
565 'non_inline_form_honor_none_setting' => '', // blank|on.
566 'non_inline_form_limit_per_session' => '', // blank|on.
567
568 // reCAPTCHA.
569 'recaptcha_site_key' => '', // string.
570 'recaptcha_secret_key' => '', // string.
571 'recaptcha_minimum_score' => 0.5, // float.
572
573 // Advanced.
574 'debug' => '', // blank|on.
575 'no_scripts' => '', // blank|on.
576 'no_css' => '', // blank|on.
577 );
578
579 // Add Post Type Default Forms.
580 foreach ( convertkit_get_supported_post_types() as $post_type ) {
581 $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID.
582 $defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content,before_after_content,element.
583 $defaults[ $post_type . '_form_position_element' ] = 'p';
584 $defaults[ $post_type . '_form_position_element_index' ] = 1;
585 }
586
587 /**
588 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
589 * e.g. on a new installation.
590 *
591 * @since 1.9.6
592 *
593 * @param array $defaults Default Settings.
594 */
595 $defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults );
596
597 return $defaults;
598
599 }
600
601 /**
602 * Saves the new access token, refresh token and its expiry, and schedules
603 * a WordPress Cron event to refresh the token on expiry.
604 *
605 * @since 2.8.3
606 *
607 * @param array $result New Access Token, Refresh Token and Expiry.
608 * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
609 */
610 public function update_credentials( $result, $client_id ) {
611
612 // Don't save these credentials if they're not for this Client ID.
613 // They're for another Kit Plugin that uses OAuth.
614 if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
615 return;
616 }
617
618 $this->save(
619 array(
620 'access_token' => $result['access_token'],
621 'refresh_token' => $result['refresh_token'],
622 'token_expires' => ( time() + $result['expires_in'] ),
623 )
624 );
625
626 // Clear any existing scheduled WordPress Cron event.
627 wp_clear_scheduled_hook( 'convertkit_refresh_token' );
628
629 // Schedule a WordPress Cron event to refresh the token on expiry.
630 wp_schedule_single_event( ( time() + $result['expires_in'] ), 'convertkit_refresh_token' );
631
632 }
633
634 /**
635 * Deletes any existing access token, refresh token and its expiry from the Plugin settings.
636 *
637 * @since 2.5.0
638 */
639 public function delete_credentials() {
640
641 $this->save(
642 array(
643 'access_token' => '',
644 'refresh_token' => '',
645 'token_expires' => '',
646 )
647 );
648
649 }
650
651 /**
652 * Saves the given array of settings to the WordPress options table.
653 *
654 * @since 1.9.8.4
655 *
656 * @param array $settings Settings.
657 */
658 public function save( $settings ) {
659
660 update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) );
661
662 // Reload settings in class, to reflect changes.
663 $this->settings = get_option( self::SETTINGS_NAME );
664
665 }
666
667 }
668