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

563 lines 12.4 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_refresh_token', array( $this, 'update_credentials' ), 10, 2 );
50
51 }
52
53 /**
54 * Returns Plugin settings.
55 *
56 * @since 1.9.6
57 *
58 * @return array
59 */
60 public function get() {
61
62 return $this->settings;
63
64 }
65
66 /**
67 * Returns the API Key Plugin setting.
68 *
69 * @since 1.9.6
70 *
71 * @return string
72 */
73 public function get_api_key() {
74
75 // Return API Key from constant, if defined.
76 if ( defined( 'CONVERTKIT_API_KEY' ) ) {
77 return CONVERTKIT_API_KEY;
78 }
79
80 // Return API Key from settings.
81 return $this->settings['api_key'];
82
83 }
84
85 /**
86 * Returns whether the API Key has been set in the Plugin settings.
87 *
88 * @since 1.9.6
89 *
90 * @return bool
91 */
92 public function has_api_key() {
93
94 return ( ! empty( $this->get_api_key() ) ? true : false );
95
96 }
97
98 /**
99 * Returns whether the API Key is stored as a constant in the wp-config.php file.
100 *
101 * @since 1.9.6
102 *
103 * @return bool
104 */
105 public function is_api_key_a_constant() {
106
107 return defined( 'CONVERTKIT_API_KEY' );
108
109 }
110
111 /**
112 * Returns the API Secret Plugin setting.
113 *
114 * @since 1.9.6
115 *
116 * @return string
117 */
118 public function get_api_secret() {
119
120 // Return API Secret from constant, if defined.
121 if ( defined( 'CONVERTKIT_API_SECRET' ) ) {
122 return CONVERTKIT_API_SECRET;
123 }
124
125 // Return API Secret from settings.
126 return $this->settings['api_secret'];
127
128 }
129
130 /**
131 * Returns whether the API Secret has been set in the Plugin settings.
132 *
133 * @since 1.9.6
134 *
135 * @return bool
136 */
137 public function has_api_secret() {
138
139 return ( ! empty( $this->get_api_secret() ) ? true : false );
140
141 }
142
143 /**
144 * Returns whether the API Secret is stored as a constant in the wp-config.php file.
145 *
146 * @since 1.9.6
147 *
148 * @return bool
149 */
150 public function is_api_secret_a_constant() {
151
152 return defined( 'CONVERTKIT_API_SECRET' );
153
154 }
155
156 /**
157 * Returns whether the API Key and Secret have been set in the Plugin settings.
158 *
159 * @since 1.9.6
160 *
161 * @return bool
162 */
163 public function has_api_key_and_secret() {
164
165 _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' );
166
167 // Use check for access and refresh token.
168 return $this->has_access_and_refresh_token();
169
170 }
171
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 /**
256 * Returns the Default Form Plugin setting.
257 *
258 * @since 1.9.6
259 *
260 * @param string $post_type Post Type.
261 * @return string|int Default Form (default|form id)
262 */
263 public function get_default_form( $post_type ) {
264
265 // Return default if this Post Type doesn't exist as a setting.
266 if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) {
267 return 'default';
268 }
269
270 // Backward compat. where older Plugin versions would store API errors in the option value
271 // with id = -2 and name = 'Error contacting API'.
272 if ( is_array( $this->settings[ $post_type . '_form' ] ) ) {
273 return 'default';
274 }
275
276 return $this->settings[ $post_type . '_form' ];
277
278 }
279
280 /**
281 * Returns whether the Default Form has been set in the Plugin settings.
282 *
283 * @since 1.9.6
284 *
285 * @param string $post_type Post Type.
286 * @return bool Post Type has a Default Form setting specified in Plugin Settings.
287 */
288 public function has_default_form( $post_type ) {
289
290 return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false );
291
292 }
293
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 /**
412 * Returns whether debugging is enabled in the Plugin settings.
413 *
414 * @since 1.9.6
415 *
416 * @return bool
417 */
418 public function debug_enabled() {
419
420 return ( $this->settings['debug'] === 'on' ? true : false );
421
422 }
423
424 /**
425 * Returns whether scripts are disabled in the Plugin settings.
426 *
427 * @since 1.9.6
428 *
429 * @return bool
430 */
431 public function scripts_disabled() {
432
433 return ( $this->settings['no_scripts'] === 'on' ? true : false );
434
435 }
436
437 /**
438 * Returns whether stylesheets are disabled in the Plugin settings.
439 *
440 * @since 1.9.6.9
441 *
442 * @return bool
443 */
444 public function css_disabled() {
445
446 return ( $this->settings['no_css'] === 'on' ? true : false );
447
448 }
449
450 /**
451 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
452 * e.g. on a new installation.
453 *
454 * @since 1.9.6
455 *
456 * @return array
457 */
458 public function get_defaults() {
459
460 $defaults = array(
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.
478 );
479
480 // Add Post Type Default Forms.
481 foreach ( convertkit_get_supported_post_types() as $post_type ) {
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;
486 }
487
488 /**
489 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
490 * e.g. on a new installation.
491 *
492 * @since 1.9.6
493 *
494 * @param array $defaults Default Settings.
495 */
496 $defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults );
497
498 return $defaults;
499
500 }
501
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 /**
547 * Saves the given array of settings to the WordPress options table.
548 *
549 * @since 1.9.8.4
550 *
551 * @param array $settings Settings.
552 */
553 public function save( $settings ) {
554
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 );
559
560 }
561
562 }
563