PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.6
3.4.6 3.4.5 3.4.4 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 All 199 releases
← All changes | includes/class-convertkit-setup.php +617 -77 2.2.7 → 3.4.6 View file →
@@ -22,15 +22,32 @@
22 22 */
23 23 public function activate() {
24 24
25 25 // Call any functions to e.g. schedule WordPress Cron events now.
26 - $posts = new ConvertKit_Resource_Posts( 'cron' );
27 - $posts->schedule_cron_event();
26 + $this->schedule_cron_events();
27 +
28 + // Install entries database table.
29 + $entries = new ConvertKit_Form_Entries();
30 + $entries->create_database_table();
31 +
28 32 }
29 33
30 34 /**
31 - * Runs routines when the Plugin version has been updated.
35 + * Runs routines on every Plugin request e.g.
36 + * ensuring WordPress Cron events are scheduled.
32 37 *
38 + * @since 2.6.6
39 + */
40 + public function initialize() {
41 +
42 + // Call any functions to e.g. schedule WordPress Cron events now.
43 + $this->schedule_cron_events();
44 +
45 + }
46 +
47 + /**
48 + * Runs routines if the Plugin version has been updated.
49 + *
33 50 * @since 1.9.7.4
34 51 */
35 52 public function update() {
36 53
@@ -43,26 +60,84 @@
43 60 return;
44 61 }
45 62
46 63 /**
47 - * 1.4.1: Change ID to form_id for API version 3.0.
64 + * 3.0.4: Add form_id to entries database table.
48 65 */
49 - if ( ! $current_version || version_compare( $current_version, '1.4.1', '<' ) ) {
50 - $this->change_id_to_form_id();
66 + if ( version_compare( $current_version, '3.0.4', '<' ) ) {
67 + $this->add_form_id_column_and_key_to_form_entries_database_table();
51 68 }
52 69
53 70 /**
54 - * 1.6.1+: Refresh Forms, Landing Pages and Tags data stored in settings,
55 - * to get new Forms Builder Settings.
71 + * 3.0.0: Migrate reCAPTCHA settings from Restrict Content to General settings.
72 + * Install entries database table.
56 73 */
57 - if ( version_compare( $current_version, '1.6.1', '<' ) ) {
58 - $this->refresh_resources();
74 + if ( version_compare( $current_version, '3.0.0', '<' ) ) {
75 + $this->migrate_recaptcha_settings();
76 +
77 + // Install entries database table.
78 + $entries = new ConvertKit_Form_Entries();
79 + $entries->create_database_table();
59 80 }
60 81
61 82 /**
83 + * 2.6.6: Migrate 'Default' Form value in _wp_convertkit_term_meta[form] from 0 to -1,
84 + * to match Posts.
85 + */
86 + if ( version_compare( $current_version, '2.6.6', '<' ) ) {
87 + $this->migrate_term_default_form_settings();
88 + }
89 +
90 + /**
91 + * 2.5.4: Migrate WishList Member to ConvertKit Form Mappings
92 + */
93 + if ( ! $current_version || version_compare( $current_version, '2.5.4', '<' ) ) {
94 + $this->migrate_wlm_none_setting();
95 + $this->migrate_wlm_form_tag_mapping_settings();
96 + }
97 +
98 + /**
99 + * 2.5.3: Migrate Third Party Form integrations' 'None' option values from `default` to blank.
100 + */
101 + if ( ! $current_version || version_compare( $current_version, '2.5.3', '<' ) ) {
102 + $this->migrate_contact_form_7_none_setting();
103 + $this->migrate_forminator_none_setting();
104 + $this->migrate_wlm_none_setting();
105 + }
106 +
107 + /**
108 + * 2.5.2: Migrate Forminator to ConvertKit Form Mappings
109 + */
110 + if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) {
111 + $this->migrate_forminator_form_mapping_settings();
112 + }
113 +
114 + /**
115 + * 2.5.2: Migrate Contact Form 7 to ConvertKit Form Mappings
116 + */
117 + if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) {
118 + $this->migrate_contact_form_7_form_mapping_settings();
119 + }
120 +
121 + /**
122 + * 2.5.0: Get Access token for API version 4.0 using a v3 API Key and Secret.
123 + */
124 + if ( ! $current_version || version_compare( $current_version, '2.5.0', '<' ) ) {
125 + $this->maybe_get_access_token_by_api_key_and_secret();
126 + }
127 +
128 + /**
129 + * 2.4.9.1+: Migrate ck_default_form to _wp_convertkit_term_meta[form], as Term settings
130 + * support multiple options (form, position etc).
131 + */
132 + if ( version_compare( $current_version, '2.4.9.1', '<' ) ) {
133 + $this->migrate_term_form_settings();
134 + }
135 +
136 + /**
62 137 * 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and
63 138 * _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting
64 - * in Settings > ConvertKit > General.
139 + * in Settings > Kit > General.
65 140 */
66 141 if ( version_compare( $current_version, '1.9.6', '<' ) ) {
67 142 $this->migrate_default_form_settings();
68 143 }
@@ -75,8 +150,13 @@
75 150 $posts = new ConvertKit_Resource_Posts( 'cron' );
76 151 $posts->schedule_cron_event();
77 152 }
78 153
154 + // Actions that should run regardless of the version number
155 + // whenever the Plugin is updated.
156 + $this->remove_v3_api_secret_from_settings();
157 + $this->maybe_delete_old_cached_resource_structure();
158 +
79 159 // Update the installed version number in the options table.
80 160 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
81 161
82 162 }
@@ -81,117 +161,554 @@
81 161
82 162 }
83 163
84 164 /**
85 - * 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and
86 - * _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting
87 - * in Settings > ConvertKit > General.
165 + * Remove v3 API Secret from settings.
166 + *
167 + * @since 3.2.4
88 168 */
89 - private function migrate_default_form_settings() {
169 + private function remove_v3_api_secret_from_settings() {
90 170
171 + $settings = new ConvertKit_Settings();
172 + $settings->save(
173 + array(
174 + 'api_secret' => '',
175 + )
176 + );
177 +
178 + }
179 +
180 + /**
181 + * Deletes any cached resources stored in the options table if they are a flat array
182 + * that originates from between 1.6.0 and 1.9.5.2 of the Plugin.
183 + * i.e. [id => name], rather than the expected [id => [id => '...', name => '...', ...]].
184 + *
185 + * This will permit the Resources classes to fetch and cache the Form resources correctly
186 + * from the API, and prevent fatal errors when a user activates the latest version of the Plugin
187 + * on a site that previously had a very old Plugin version installed.
188 + *
189 + * @since 3.2.5
190 + */
191 + private function maybe_delete_old_cached_resource_structure() {
192 +
193 + // Define the resource options to check.
194 + $resource_options = array(
195 + 'convertkit_forms',
196 + 'convertkit_landing_pages',
197 + 'convertkit_tags',
198 + );
199 +
200 + foreach ( $resource_options as $resource_option ) {
201 + // Get the resource option.
202 + $resource = get_option( $resource_option, false );
203 +
204 + // If the resource option does not exist, there's nothing to delete.
205 + if ( false === $resource ) {
206 + return;
207 + }
208 +
209 + // If the resource structure is correct e.g. [id => [name => '...']], there's nothing to delete.
210 + if ( is_array( reset( $resource ) ) ) {
211 + return;
212 + }
213 +
214 + // Delete the cached resource.
215 + delete_option( $resource_option );
216 + }
217 +
218 + }
219 +
220 + /**
221 + * Adds form_id column and key to form entries database table.
222 + *
223 + * @since 3.0.4
224 + */
225 + private function add_form_id_column_and_key_to_form_entries_database_table() {
226 +
227 + global $wpdb;
228 +
229 + // Check if the column exists.
230 + $exists = $wpdb->get_var(
231 + $wpdb->prepare(
232 + 'SELECT COLUMN_NAME
233 + FROM INFORMATION_SCHEMA.COLUMNS
234 + WHERE table_name = %s
235 + AND table_schema = %s
236 + AND column_name = %s',
237 + $wpdb->prefix . 'kit_form_entries',
238 + $wpdb->dbname,
239 + 'form_id'
240 + )
241 + );
242 +
243 + // If the column exists, don't add it again.
244 + if ( $exists === 'form_id' ) {
245 + return;
246 + }
247 +
248 + // Add the form_id column and key.
249 + // The table name isn't prepared, as the %i placeholder requires WordPress 6.2+.
250 + $wpdb->query( "ALTER TABLE `{$wpdb->prefix}kit_form_entries` ADD COLUMN `form_id` int(11) NOT NULL AFTER `custom_fields`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
251 + $wpdb->query( "ALTER TABLE `{$wpdb->prefix}kit_form_entries` ADD KEY `form_id` (`form_id`)" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
252 +
253 + }
254 +
255 + /**
256 + * 3.0.0: Migrate reCAPTCHA settings from Restrict Content settings to General settings.
257 + *
258 + * @since 3.0.0
259 + */
260 + private function migrate_recaptcha_settings() {
261 +
262 + // Read Restrict Content settings directly from options table,
263 + // as Convertkit_Settings_Restrict_Content won't have helper
264 + // methods to return the specific settings.
265 + $settings = get_option( '_wp_convertkit_settings_restrict_content' );
266 +
267 + // If no reCAPTCHA settings exist, bail.
268 + if ( ! isset( $settings['recaptcha_site_key'] ) || ! isset( $settings['recaptcha_secret_key'] ) || ! isset( $settings['recaptcha_minimum_score'] ) ) {
269 + return;
270 + }
271 +
272 + // Load settings class, saving the reCAPTCHA settings to the General settings.
91 273 $convertkit_settings = new ConvertKit_Settings();
274 + $convertkit_settings->save(
275 + array(
276 + 'recaptcha_site_key' => $settings['recaptcha_site_key'],
277 + 'recaptcha_secret_key' => $settings['recaptcha_secret_key'],
278 + 'recaptcha_minimum_score' => $settings['recaptcha_minimum_score'],
279 + )
280 + );
92 281
93 - // Bail if no default_form setting exists.
94 - $settings = get_option( $convertkit_settings::SETTINGS_NAME );
95 - if ( ! $settings ) {
282 + // Remove reCAPTCHA settings from Restrict Content settings.
283 + unset( $settings['recaptcha_site_key'] );
284 + unset( $settings['recaptcha_secret_key'] );
285 + unset( $settings['recaptcha_minimum_score'] );
286 + update_option( '_wp_convertkit_settings_restrict_content', $settings );
287 +
288 + }
289 +
290 + /**
291 + * Change the Default value of 0 to -1 in wp_convertkit_term_meta[form], to
292 + * match how the Default value is stored in Posts.
293 + *
294 + * @since 2.6.6
295 + */
296 + private function migrate_term_default_form_settings() {
297 +
298 + // Get all Terms that have ConvertKit settings defined.
299 + $query = new WP_Term_Query(
300 + array(
301 + 'taxonomy' => 'category',
302 + 'hide_empty' => false,
303 + 'fields' => 'ids',
304 + 'meta_query' => array(
305 + array(
306 + 'key' => '_wp_convertkit_term_meta',
307 + 'comparison' => 'EXISTS',
308 + ),
309 + ),
310 + )
311 + );
312 +
313 + // Bail if no Terms exist.
314 + if ( ! $query->terms ) {
96 315 return;
97 316 }
98 - if ( ! array_key_exists( 'default_form', $settings ) ) {
317 +
318 + // Iterate through Terms, mapping settings.
319 + foreach ( $query->terms as $term_id ) {
320 + $term_settings = new ConvertKit_Term( $term_id );
321 +
322 + // If the Form setting is Default i.e. it does not have a formchange it from 0 to -1.
323 + if ( ! $term_settings->has_form() ) {
324 + $term_settings->save(
325 + array(
326 + 'form' => -1,
327 + )
328 + );
329 + }
330 + }
331 +
332 + }
333 +
334 + /**
335 + * 2.5.4: Migrate WLM settings:
336 + * - Prefix any WishList Member to ConvertKit Form ID mappings with `form:`,
337 + * - Prefix any WishList Member to ConvertKit Tag ID mappings with `tag:`,
338 + * - Standardise the settings keys to the format {wlm_level_id}_subscribe
339 + * and {wlm_level_id}_unsubscribe.
340 + *
341 + * @since 2.5.4
342 + */
343 + private function migrate_wlm_form_tag_mapping_settings() {
344 +
345 + $convertkit_wlm_settings = new ConvertKit_Wishlist_Settings();
346 +
347 + // Bail if no settings exist.
348 + if ( ! $convertkit_wlm_settings->has_settings() ) {
99 349 return;
100 350 }
101 351
102 - // Restructure settings.
103 - $settings['page_form'] = $settings['default_form'];
104 - $settings['post_form'] = $settings['default_form'];
352 + // Define new array for settings.
353 + $settings = array();
105 354
106 - // Remove obsolete default_form setting.
107 - unset( $settings['default_form'] );
355 + // Iterate through settings.
356 + foreach ( $convertkit_wlm_settings->get() as $key => $convertkit_form_or_tag_id ) {
357 + // Split the settings key.
358 + list( $wlm_level_id, $type ) = explode( '_', $key );
108 359
109 - // Update.
110 - update_option( $convertkit_settings::SETTINGS_NAME, $settings );
360 + switch ( $type ) {
361 + case 'form':
362 + // This is the action to perform when the user is added to the WLM Level.
363 + // Use a new name for the setting key to reflect this.
364 + // < 2.5.4, forms were the only option here, so prefix the resource ID with `form:`.
365 + $settings[ $wlm_level_id . '_add' ] = ( empty( $convertkit_form_or_tag_id ) ? '' : 'form:' . $convertkit_form_or_tag_id );
366 + break;
111 367
368 + case 'unsubscribe':
369 + // This is the action to perform when the user is removed from the WLM Level.
370 + // Use a new name for the setting key to reflect this.
371 + // < 2.5.4, tags were the only option here, so prefix the resource ID with `tag:`.
372 + $settings[ $wlm_level_id . '_remove' ] = ( empty( $convertkit_form_or_tag_id ) ? '' : 'tag:' . $convertkit_form_or_tag_id );
373 + break;
374 + }
375 + }
376 +
377 + // Update settings.
378 + update_option( $convertkit_wlm_settings::SETTINGS_NAME, $settings );
379 +
112 380 }
113 381
114 382 /**
115 - * 1.6.1: Refresh Forms, Landing Pages and Tags data stored in settings,
116 - * to get new Forms Builder Settings.
383 + * 2.5.3: Migrate Third Party Form integrations' 'None' option values from `default` to blank.
384 + *
385 + * 2.4.9 changed the 'None' label's value from `default` to a blank string, as the v4 API's
386 + * `add_subscriber_to_form()` method introduces type declarations, which would result in
387 + * an uncaught TypeError when passing a non integer value.
388 + *
389 + * The PR for that (https://github.com/ConvertKit/convertkit-wordpress/pull/655) didn't include
390 + * any tests or upgrade/migration routines to change any existing saved settings where the 'None'
391 + * label's value was stored as `default`.
117 392 */
118 - private function refresh_resources() {
393 + private function migrate_contact_form_7_none_setting() {
119 394
120 - $forms = new ConvertKit_Resource_Forms( 'setup' );
121 - $landing_pages = new ConvertKit_Resource_Landing_Pages( 'setup' );
122 - $tags = new ConvertKit_Resource_Tags( 'setup' );
395 + $convertkit_contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
123 396
124 - $forms->refresh();
125 - $landing_pages->refresh();
126 - $tags->refresh();
397 + // Bail if no settings exist.
398 + if ( ! $convertkit_contact_form_7_settings->has_settings() ) {
399 + return;
400 + }
127 401
402 + // Get settings.
403 + $settings = $convertkit_contact_form_7_settings->get();
404 +
405 + // Iterate through settings.
406 + foreach ( $settings as $contact_form_7_form_id => $convertkit_form_id ) {
407 + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`.
408 + if ( ! is_numeric( $contact_form_7_form_id ) ) {
409 + continue;
410 + }
411 +
412 + // Change 'default' to a blank string.
413 + if ( $convertkit_form_id === 'default' ) {
414 + $settings[ $contact_form_7_form_id ] = '';
415 + }
416 + }
417 +
418 + // Update settings.
419 + update_option( $convertkit_contact_form_7_settings::SETTINGS_NAME, $settings );
420 +
128 421 }
129 422
130 423 /**
131 - * 1.4.1: Change ID to form_id for API version 3.0.
424 + * 2.5.3: Migrate Third Party Form integrations' 'None' option values from `default` to blank.
132 425 *
133 - * @since 1.4.1
426 + * 2.4.9 changed the 'None' label's value from `default` to a blank string, as the v4 API's
427 + * `add_subscriber_to_form()` method introduces type declarations, which would result in
428 + * an uncaught TypeError when passing a non integer value.
429 + *
430 + * The PR for that (https://github.com/ConvertKit/convertkit-wordpress/pull/655) didn't include
431 + * any tests or upgrade/migration routines to change any existing saved settings where the 'None'
432 + * label's value was stored as `default`.
134 433 */
135 - private function change_id_to_form_id() {
434 + private function migrate_forminator_none_setting() {
136 435
137 - // Bail if the API isn't configured.
436 + $convertkit_forminator_settings = new ConvertKit_Forminator_Settings();
437 +
438 + // Bail if no settings exist.
439 + if ( ! $convertkit_forminator_settings->has_settings() ) {
440 + return;
441 + }
442 +
443 + // Get settings.
444 + $settings = $convertkit_forminator_settings->get();
445 +
446 + // Iterate through settings.
447 + foreach ( $settings as $forminator_form_id => $convertkit_form_id ) {
448 + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`.
449 + if ( ! is_numeric( $forminator_form_id ) ) {
450 + continue;
451 + }
452 +
453 + // Change 'default' to a blank string.
454 + if ( $convertkit_form_id === 'default' ) {
455 + $settings[ $forminator_form_id ] = '';
456 + }
457 + }
458 +
459 + // Update settings.
460 + update_option( $convertkit_forminator_settings::SETTINGS_NAME, $settings );
461 +
462 + }
463 +
464 + /**
465 + * 2.5.3: Migrate Third Party Form integrations' 'None' option values from `default` to blank.
466 + *
467 + * 2.4.9 changed the 'None' label's value from `default` to a blank string, as the v4 API's
468 + * `add_subscriber_to_form()` method introduces type declarations, which would result in
469 + * an uncaught TypeError when passing a non integer value.
470 + *
471 + * The PR for that (https://github.com/ConvertKit/convertkit-wordpress/pull/655) didn't include
472 + * any tests or upgrade/migration routines to change any existing saved settings where the 'None'
473 + * label's value was stored as `default`.
474 + */
475 + private function migrate_wlm_none_setting() {
476 +
477 + $convertkit_wlm_settings = new ConvertKit_Wishlist_Settings();
478 +
479 + // Bail if no settings exist.
480 + if ( ! $convertkit_wlm_settings->has_settings() ) {
481 + return;
482 + }
483 +
484 + // Get settings.
485 + $settings = $convertkit_wlm_settings->get();
486 +
487 + // Iterate through settings.
488 + foreach ( $settings as $wlm_level_id => $value ) {
489 + // Change 'default' to a blank string.
490 + if ( $value === 'default' ) {
491 + $settings[ $wlm_level_id ] = '';
492 + }
493 + }
494 +
495 + // Update settings.
496 + update_option( $convertkit_wlm_settings::SETTINGS_NAME, $settings );
497 +
498 + }
499 +
500 + /**
501 + * 2.5.2: Prefix any Forminator to ConvertKit Form ID mappings with `form:`, now that
502 + * the Plugin supports adding a subscriber to a Form, Tag or Sequence.
503 + *
504 + * @since 2.5.2
505 + */
506 + private function migrate_forminator_form_mapping_settings() {
507 +
508 + $convertkit_forminator_settings = new ConvertKit_Forminator_Settings();
509 +
510 + // Bail if no settings exist.
511 + if ( ! $convertkit_forminator_settings->has_settings() ) {
512 + return;
513 + }
514 +
515 + // Get settings.
516 + $settings = $convertkit_forminator_settings->get();
517 +
518 + // Iterate through settings.
519 + foreach ( $settings as $forminator_form_id => $convertkit_form_id ) {
520 + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`.
521 + if ( ! is_numeric( $forminator_form_id ) ) {
522 + continue;
523 + }
524 +
525 + // Skip values that are blank i.e. no ConvertKit Form ID specified.
526 + if ( empty( $convertkit_form_id ) ) {
527 + continue;
528 + }
529 +
530 + // Skip values that are non-numeric i.e. the `form_` prefix was already added.
531 + // This should never happen as this routine runs once, but this is a sanity check.
532 + if ( ! is_numeric( $convertkit_form_id ) ) {
533 + continue;
534 + }
535 +
536 + // Prefix the ConvertKit Form ID with `form_`.
537 + $settings[ $forminator_form_id ] = 'form:' . $convertkit_form_id;
538 + }
539 +
540 + // Update settings.
541 + update_option( $convertkit_forminator_settings::SETTINGS_NAME, $settings );
542 +
543 + }
544 +
545 + /**
546 + * 2.5.2: Prefix any Contact Form 7 to ConvertKit Form ID mappings with `form:`, now that
547 + * the Plugin supports adding a subscriber to a Form, Tag or Sequence.
548 + *
549 + * @since 2.5.2
550 + */
551 + private function migrate_contact_form_7_form_mapping_settings() {
552 +
553 + $convertkit_contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
554 +
555 + // Bail if no settings exist.
556 + if ( ! $convertkit_contact_form_7_settings->has_settings() ) {
557 + return;
558 + }
559 +
560 + // Get settings.
561 + $settings = $convertkit_contact_form_7_settings->get();
562 +
563 + // Iterate through settings.
564 + foreach ( $settings as $contact_form_7_form_id => $convertkit_form_id ) {
565 + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`.
566 + if ( ! is_numeric( $contact_form_7_form_id ) ) {
567 + continue;
568 + }
569 +
570 + // Skip values that are blank i.e. no ConvertKit Form ID specified.
571 + if ( empty( $convertkit_form_id ) ) {
572 + continue;
573 + }
574 +
575 + // Skip values that are non-numeric i.e. the `form_` prefix was already added.
576 + // This should never happen as this routine runs once, but this is a sanity check.
577 + if ( ! is_numeric( $convertkit_form_id ) ) {
578 + continue;
579 + }
580 +
581 + // Prefix the ConvertKit Form ID with `form_`.
582 + $settings[ $contact_form_7_form_id ] = 'form:' . $convertkit_form_id;
583 + }
584 +
585 + // Update settings.
586 + update_option( $convertkit_contact_form_7_settings::SETTINGS_NAME, $settings );
587 +
588 + }
589 +
590 + /**
591 + * 2.5.0: Fetch an Access Token, Refresh Token and Expiry for v4 API use
592 + * based on the Plugin setting's v3 API Key and Secret.
593 + *
594 + * @since 2.5.0
595 + */
596 + private function maybe_get_access_token_by_api_key_and_secret() {
597 +
138 598 $convertkit_settings = new ConvertKit_Settings();
139 - if ( ! $convertkit_settings->has_api_key_and_secret() ) {
599 +
600 + // Bail if an Access Token exists; we don't need to fetch another one.
601 + if ( $convertkit_settings->has_access_token() ) {
140 602 return;
141 603 }
142 604
143 - // Get all posts and pages to track what has been updated.
144 - $posts = get_option( '_wp_convertkit_upgrade_posts' );
145 - if ( ! $posts ) {
146 - $args = array(
147 - 'post_type' => array( 'post', 'page' ),
148 - 'fields' => 'ids',
149 - );
150 -
151 - $result = new WP_Query( $args );
152 - $posts = $result->posts;
153 - update_option( '_wp_convertkit_upgrade_posts', $posts );
605 + // Bail if no API Key or Secret.
606 + if ( empty( $convertkit_settings->get_api_key() ) ) {
607 + return;
154 608 }
609 + if ( empty( $convertkit_settings->get_api_secret() ) ) {
610 + return;
611 + }
155 612
156 - // Initialize the API.
157 - $api = new ConvertKit_API(
613 + // Get Access Token by API Key and Secret.
614 + $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI );
615 + $result = $api->get_access_token_by_api_key_and_secret(
158 616 $convertkit_settings->get_api_key(),
159 617 $convertkit_settings->get_api_secret(),
160 - $convertkit_settings->debug_enabled(),
161 - 'setup'
618 + get_site_url()
162 619 );
163 620
164 - // Get form mappings.
165 - $mappings = $api->get_subscription_forms();
621 + // Bail if an error occurred.
622 + if ( is_wp_error( $result ) ) {
623 + return;
624 + }
166 625
167 - // Bail if no form mappings exist.
168 - if ( ! $mappings ) {
626 + // Store the new credentials.
627 + // We don't use update_credentials(), because the response
628 + // includes an `expires_at`, not a `created_at` and `expires_in`.
629 + $convertkit_settings->save(
630 + array(
631 + 'access_token' => $result['oauth']['access_token'],
632 + 'refresh_token' => $result['oauth']['refresh_token'],
633 + 'token_expires' => $result['oauth']['expires_at'],
634 + )
635 + );
636 +
637 + }
638 +
639 + /**
640 + * Migrate ck_default_form to _wp_convertkit_term_meta[form], as Term settings
641 + * support multiple options (form, position etc).
642 + *
643 + * @since 2.4.9.1
644 + */
645 + private function migrate_term_form_settings() {
646 +
647 + // Get all Terms that have ConvertKit settings defined.
648 + $query = new WP_Term_Query(
649 + array(
650 + 'taxonomy' => 'category',
651 + 'hide_empty' => false,
652 + 'fields' => 'ids',
653 + 'meta_query' => array(
654 + array(
655 + 'key' => 'ck_default_form',
656 + 'comparison' => 'EXISTS',
657 + ),
658 + ),
659 + )
660 + );
661 +
662 + // Bail if no Terms exist.
663 + if ( ! $query->terms ) {
169 664 return;
170 665 }
171 666
172 - // 1. Update global form.
173 - $settings_data = $convertkit_settings->get();
174 - $settings_data['default_form'] = isset( $mappings[ $convertkit_settings->get_default_form( 'post' ) ] ) ? $mappings[ $convertkit_settings->get_default_form( 'post' ) ] : 0;
175 - update_option( $convertkit_settings::SETTINGS_NAME, $settings_data );
667 + // Iterate through Terms, mapping settings.
668 + foreach ( $query->terms as $term_id ) {
669 + $term_settings = new ConvertKit_Term( $term_id );
670 + $term_settings->save(
671 + array(
672 + 'form' => get_term_meta( $term_id, 'ck_default_form', true ), // Fetch form setting from old meta key.
673 + 'form_position' => '', // Default to no position.
674 + )
675 + );
176 676
177 - // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs
178 - // Scan content for shortcode and update
179 - // Remove page_id from posts array after page is updated.
180 - foreach ( $posts as $key => $post_id ) {
181 - $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true );
677 + // Delete old Term meta.
678 + delete_term_meta( $term_id, 'ck_default_form' );
679 + }
182 680
183 - if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) {
184 - $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0;
185 - }
186 - if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) {
187 - $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0;
188 - }
189 - update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings );
190 - unset( $posts[ $key ] );
191 - update_option( '_wp_convertkit_upgrade_posts', $posts );
681 + }
682 +
683 + /**
684 + * 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and
685 + * _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting
686 + * in Settings > Kit > General.
687 + */
688 + private function migrate_default_form_settings() {
689 +
690 + $convertkit_settings = new ConvertKit_Settings();
691 +
692 + // Bail if no default_form setting exists.
693 + $settings = get_option( $convertkit_settings::SETTINGS_NAME );
694 + if ( ! $settings ) {
695 + return;
192 696 }
697 + if ( ! array_key_exists( 'default_form', $settings ) ) {
698 + return;
699 + }
193 700
701 + // Restructure settings.
702 + $settings['page_form'] = $settings['default_form'];
703 + $settings['post_form'] = $settings['default_form'];
704 +
705 + // Remove obsolete default_form setting.
706 + unset( $settings['default_form'] );
707 +
708 + // Update.
709 + update_option( $convertkit_settings::SETTINGS_NAME, $settings );
710 +
194 711 }
195 712
196 713 /**
197 714 * Runs routines when the Plugin is deactivated.
@@ -200,8 +717,31 @@
200 717 */
201 718 public function deactivate() {
202 719
203 720 // Call any functions to e.g. unschedule WordPress Cron events now.
721 + $this->unschedule_cron_events();
722 +
723 + }
724 +
725 + /**
726 + * Schedules any Plugin specific CRON events, if they do not already exist.
727 + *
728 + * @since 2.6.6
729 + */
730 + private function schedule_cron_events() {
731 +
732 + $posts = new ConvertKit_Resource_Posts( 'cron' );
733 + $posts->schedule_cron_event();
734 +
735 + }
736 +
737 + /**
738 + * Unschedules any Plugin specific CRON events, if they exist.
739 + *
740 + * @since 2.6.6
741 + */
742 + private function unschedule_cron_events() {
743 +
204 744 $posts = new ConvertKit_Resource_Posts( 'cron' );
205 745 $posts->unschedule_cron_event();
206 746
207 747 }