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