PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.8.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.8.6
3.4.3 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 All 196 releases
← All changes | includes/class-convertkit-settings.php +282 -7 2.2.62.8.6 View file →
@@ -44,8 +44,12 @@
44 44 } else {
45 45 $this->settings = array_merge( $this->get_defaults(), $settings );
46 46 }
47 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 +
48 52 }
49 53
50 54 /**
51 55 * Returns Plugin settings.
@@ -158,13 +162,99 @@
158 162 * @return bool
159 163 */
160 164 public function has_api_key_and_secret() {
161 165
162 - return $this->has_api_key() && $this->has_api_secret();
166 + _deprecated_function( __FUNCTION__, '2.6.3', 'has_access_and_refresh_token()' );
163 167
168 + // Use check for access and refresh token.
169 + return $this->has_access_and_refresh_token();
170 +
164 171 }
165 172
166 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 + /**
167 257 * Returns the Default Form Plugin setting.
168 258 *
169 259 * @since 1.9.6
170 260 *
@@ -202,8 +292,125 @@
202 292
203 293 }
204 294
205 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 + /**
206 413 * Returns whether debugging is enabled in the Plugin settings.
207 414 *
208 415 * @since 1.9.6
209 416 *
@@ -251,18 +458,33 @@
251 458 */
252 459 public function get_defaults() {
253 460
254 461 $defaults = array(
255 - 'api_key' => '', // string.
256 - 'api_secret' => '', // string.
257 - 'debug' => '', // blank|on.
258 - 'no_scripts' => '', // blank|on.
259 - 'no_css' => '', // blank|on.
462 + // OAuth.
463 + 'access_token' => '', // string.
464 + 'refresh_token' => '', // string.
465 + 'token_expires' => '', // integer.
466 +
467 + // API Key. Retained if needed for backward compat.
468 + 'api_key' => '', // string.
469 + 'api_secret' => '', // string.
470 +
471 + // Site Wide.
472 + 'non_inline_form' => array(), // array.
473 + 'non_inline_form_honor_none_setting' => '', // blank|on.
474 +
475 + // Advanced.
476 + 'debug' => '', // blank|on.
477 + 'no_scripts' => '', // blank|on.
478 + 'no_css' => '', // blank|on.
260 479 );
261 480
262 481 // Add Post Type Default Forms.
263 482 foreach ( convertkit_get_supported_post_types() as $post_type ) {
264 - $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID.
483 + $defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID.
484 + $defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content,before_after_content,element.
485 + $defaults[ $post_type . '_form_position_element' ] = 'p';
486 + $defaults[ $post_type . '_form_position_element_index' ] = 1;
265 487 }
266 488
267 489 /**
268 490 * The default settings, used when the ConvertKit Plugin Settings haven't been saved
@@ -278,8 +500,58 @@
278 500
279 501 }
280 502
281 503 /**
504 + * Saves the new access token, refresh token and its expiry, and schedules
505 + * a WordPress Cron event to refresh the token on expiry.
506 + *
507 + * @since 2.8.3
508 + *
509 + * @param array $result New Access Token, Refresh Token and Expiry.
510 + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
511 + */
512 + public function update_credentials( $result, $client_id ) {
513 +
514 + // Don't save these credentials if they're not for this Client ID.
515 + // They're for another Kit Plugin that uses OAuth.
516 + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
517 + return;
518 + }
519 +
520 + $this->save(
521 + array(
522 + 'access_token' => $result['access_token'],
523 + 'refresh_token' => $result['refresh_token'],
524 + 'token_expires' => ( time() + $result['expires_in'] ),
525 + )
526 + );
527 +
528 + // Clear any existing scheduled WordPress Cron event.
529 + wp_clear_scheduled_hook( 'convertkit_refresh_token' );
530 +
531 + // Schedule a WordPress Cron event to refresh the token on expiry.
532 + wp_schedule_single_event( ( time() + $result['expires_in'] ), 'convertkit_refresh_token' );
533 +
534 + }
535 +
536 + /**
537 + * Deletes any existing access token, refresh token and its expiry from the Plugin settings.
538 + *
539 + * @since 2.5.0
540 + */
541 + public function delete_credentials() {
542 +
543 + $this->save(
544 + array(
545 + 'access_token' => '',
546 + 'refresh_token' => '',
547 + 'token_expires' => '',
548 + )
549 + );
550 +
551 + }
552 +
553 + /**
282 554 * Saves the given array of settings to the WordPress options table.
283 555 *
284 556 * @since 1.9.8.4
285 557 *
@@ -287,8 +559,11 @@
287 559 */
288 560 public function save( $settings ) {
289 561
290 562 update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) );
563 +
564 + // Reload settings in class, to reflect changes.
565 + $this->settings = get_option( self::SETTINGS_NAME );
291 566
292 567 }
293 568
294 569 }