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

468 lines 9.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 return $this->has_api_key() && $this->has_api_secret();
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 // Return Access Token from settings.
179 return $this->settings['access_token'];
180
181 }
182
183 /**
184 * Returns whether the Access Token has been set in the Plugin settings.
185 *
186 * @since 2.5.0
187 *
188 * @return bool
189 */
190 public function has_access_token() {
191
192 return ( ! empty( $this->get_access_token() ) ? true : false );
193
194 }
195
196 /**
197 * Returns the Refresh Token Plugin setting.
198 *
199 * @since 2.5.0
200 *
201 * @return string
202 */
203 public function get_refresh_token() {
204
205 // Return Refresh Token from settings.
206 return $this->settings['refresh_token'];
207
208 }
209
210 /**
211 * Returns whether the Refresh Token has been set in the Plugin settings.
212 *
213 * @since 2.5.0
214 *
215 * @return bool
216 */
217 public function has_refresh_token() {
218
219 return ( ! empty( $this->get_refresh_token() ) ? true : false );
220
221 }
222
223 /**
224 * Returns whether to use Access and Refresh Tokens for API requests,
225 * based on whether an Access Token and Refresh Token have been saved
226 * in the Plugin settings.
227 *
228 * @since 2.5.0
229 *
230 * @return bool
231 */
232 public function has_access_and_refresh_token() {
233
234 return $this->has_access_token() && $this->has_refresh_token();
235
236 }
237
238 /**
239 * Returns the Access Token expiry timestamp.
240 *
241 * @since 2.5.0
242 *
243 * @return int
244 */
245 public function get_token_expiry() {
246
247 // Return Token Expiry from settings.
248 return $this->settings['token_expires'];
249
250 }
251
252 /**
253 * Returns the Default Form Plugin setting.
254 *
255 * @since 1.9.6
256 *
257 * @param string $post_type Post Type.
258 * @return string|int Default Form (default|form id)
259 */
260 public function get_default_form( $post_type ) {
261
262 // Return default if this Post Type doesn't exist as a setting.
263 if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) {
264 return 'default';
265 }
266
267 // Backward compat. where older Plugin versions would store API errors in the option value
268 // with id = -2 and name = 'Error contacting API'.
269 if ( is_array( $this->settings[ $post_type . '_form' ] ) ) {
270 return 'default';
271 }
272
273 return $this->settings[ $post_type . '_form' ];
274
275 }
276
277 /**
278 * Returns whether the Default Form has been set in the Plugin settings.
279 *
280 * @since 1.9.6
281 *
282 * @param string $post_type Post Type.
283 * @return bool Post Type has a Default Form setting specified in Plugin Settings.
284 */
285 public function has_default_form( $post_type ) {
286
287 return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false );
288
289 }
290
291 /**
292 * Returns the Global non-inline Form Plugin setting.
293 *
294 * @since 2.3.3
295 *
296 * @return string|int Non-inline Form (blank string|form id)
297 */
298 public function get_non_inline_form() {
299
300 // Return blank string if no inline form is specified.
301 if ( ! $this->has_non_inline_form() ) {
302 return '';
303 }
304
305 return $this->settings['non_inline_form'];
306
307 }
308
309 /**
310 * Returns whether the Global non-inline Form has been set in the Plugin settings.
311 *
312 * @since 2.3.3
313 *
314 * @return bool Global non-inline Form setting specified in Plugin Settings.
315 */
316 public function has_non_inline_form() {
317
318 return ( ! empty( $this->settings['non_inline_form'] ) ? true : false );
319
320 }
321
322 /**
323 * Returns whether debugging is enabled in the Plugin settings.
324 *
325 * @since 1.9.6
326 *
327 * @return bool
328 */
329 public function debug_enabled() {
330
331 return ( $this->settings['debug'] === 'on' ? true : false );
332
333 }
334
335 /**
336 * Returns whether scripts are disabled in the Plugin settings.
337 *
338 * @since 1.9.6
339 *
340 * @return bool
341 */
342 public function scripts_disabled() {
343
344 return ( $this->settings['no_scripts'] === 'on' ? true : false );
345
346 }
347
348 /**
349 * Returns whether stylesheets are disabled in the Plugin settings.
350 *
351 * @since 1.9.6.9
352 *
353 * @return bool
354 */
355 public function css_disabled() {
356
357 return ( $this->settings['no_css'] === 'on' ? true : false );
358
359 }
360
361 /**
362 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
363 * e.g. on a new installation.
364 *
365 * @since 1.9.6
366 *
367 * @return array
368 */
369 public function get_defaults() {
370
371 $defaults = array(
372 // OAuth.
373 'access_token' => '', // string.
374 'refresh_token' => '', // string.
375 'token_expires' => '', // integer.
376
377 // API Key. Retained if needed for backward compat.
378 'api_key' => '', // string.
379 'api_secret' => '', // string.
380
381 // Settings.
382 'non_inline_form' => '', // string.
383 'debug' => '', // blank|on.
384 'no_scripts' => '', // blank|on.
385 'no_css' => '', // blank|on.
386 );
387
388 // Add Post Type Default Forms.
389 foreach ( convertkit_get_supported_post_types() as $post_type ) {
390 $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID.
391 }
392
393 /**
394 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
395 * e.g. on a new installation.
396 *
397 * @since 1.9.6
398 *
399 * @param array $defaults Default Settings.
400 */
401 $defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults );
402
403 return $defaults;
404
405 }
406
407 /**
408 * Saves the new access token, refresh token and its expiry when the API
409 * class automatically refreshes an outdated access token.
410 *
411 * @since 2.5.0
412 *
413 * @param array $result New Access Token, Refresh Token and Expiry.
414 * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
415 */
416 public function update_credentials( $result, $client_id ) {
417
418 // Don't save these credentials if they're not for this Client ID.
419 // They're for another ConvertKit Plugin that uses OAuth.
420 if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
421 return;
422 }
423
424 $this->save(
425 array(
426 'access_token' => $result['access_token'],
427 'refresh_token' => $result['refresh_token'],
428 'token_expires' => ( $result['created_at'] + $result['expires_in'] ),
429 )
430 );
431
432 }
433
434 /**
435 * Deletes any existing access token, refresh token and its expiry from the Plugin settings.
436 *
437 * @since 2.5.0
438 */
439 public function delete_credentials() {
440
441 $this->save(
442 array(
443 'access_token' => '',
444 'refresh_token' => '',
445 'token_expires' => '',
446 )
447 );
448
449 }
450
451 /**
452 * Saves the given array of settings to the WordPress options table.
453 *
454 * @since 1.9.8.4
455 *
456 * @param array $settings Settings.
457 */
458 public function save( $settings ) {
459
460 update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) );
461
462 // Reload settings in class, to reflect changes.
463 $this->settings = get_option( self::SETTINGS_NAME );
464
465 }
466
467 }
468