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