| @@ -24,8 +24,9 @@ | ||
| 24 | 24 | |
| 25 | 25 | // Call any functions to e.g. schedule WordPress Cron events now. |
| 26 | 26 | $posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 27 | 27 | $posts->schedule_cron_event(); |
| 28 | + | |
| 28 | 29 | } |
| 29 | 30 | |
| 30 | 31 | /** |
| 31 | 32 | * Runs routines when the Plugin version has been updated. |
| @@ -43,15 +44,37 @@ | ||
| 43 | 44 | return; |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | /** |
| 47 | - * 1.4.1: Change ID to form_id for API version 3.0. | |
| 48 | + * 2.5.2: Migrate Forminator to ConvertKit Form Mappings | |
| 48 | 49 | */ |
| 49 | - if ( ! $current_version || version_compare( $current_version, '1.4.1', '<' ) ) { | |
| 50 | - $this->change_id_to_form_id(); | |
| 50 | + if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) { | |
| 51 | + $this->migrate_forminator_form_mapping_settings(); | |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | /** |
| 55 | + * 2.5.2: Migrate Contact Form 7 to ConvertKit Form Mappings | |
| 56 | + */ | |
| 57 | + if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) { | |
| 58 | + $this->migrate_contact_form_7_form_mapping_settings(); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * 2.5.0: Get Access token for API version 4.0 using a v3 API Key and Secret. | |
| 63 | + */ | |
| 64 | + if ( ! $current_version || version_compare( $current_version, '2.5.0', '<' ) ) { | |
| 65 | + $this->maybe_get_access_token_by_api_key_and_secret(); | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * 2.4.9.1+: Migrate ck_default_form to _wp_convertkit_term_meta[form], as Term settings | |
| 70 | + * support multiple options (form, position etc). | |
| 71 | + */ | |
| 72 | + if ( version_compare( $current_version, '2.4.9.1', '<' ) ) { | |
| 73 | + $this->migrate_term_form_settings(); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 54 | 77 | * 1.6.1+: Refresh Forms, Landing Pages and Tags data stored in settings, |
| 55 | 78 | * to get new Forms Builder Settings. |
| 56 | 79 | */ |
| 57 | 80 | if ( version_compare( $current_version, '1.6.1', '<' ) ) { |
| @@ -81,8 +104,190 @@ | ||
| 81 | 104 | |
| 82 | 105 | } |
| 83 | 106 | |
| 84 | 107 | /** |
| 108 | + * 2.5.2: Prefix any Forminator to ConvertKit Form ID mappings with `form:`, now that | |
| 109 | + * the Plugin supports adding a subscriber to a Form, Tag or Sequence. | |
| 110 | + * | |
| 111 | + * @since 2.5.2 | |
| 112 | + */ | |
| 113 | + private function migrate_forminator_form_mapping_settings() { | |
| 114 | + | |
| 115 | + $convertkit_forminator_settings = new ConvertKit_Forminator_Settings(); | |
| 116 | + | |
| 117 | + // Bail if no settings exist. | |
| 118 | + if ( ! $convertkit_forminator_settings->has_settings() ) { | |
| 119 | + return; | |
| 120 | + } | |
| 121 | + | |
| 122 | + // Get settings. | |
| 123 | + $settings = $convertkit_forminator_settings->get(); | |
| 124 | + | |
| 125 | + // Iterate through settings. | |
| 126 | + foreach ( $settings as $forminator_form_id => $convertkit_form_id ) { | |
| 127 | + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`. | |
| 128 | + if ( ! is_numeric( $forminator_form_id ) ) { | |
| 129 | + continue; | |
| 130 | + } | |
| 131 | + | |
| 132 | + // Skip values that are blank i.e. no ConvertKit Form ID specified. | |
| 133 | + if ( empty( $convertkit_form_id ) ) { | |
| 134 | + continue; | |
| 135 | + } | |
| 136 | + | |
| 137 | + // Skip values that are non-numeric i.e. the `form_` prefix was already added. | |
| 138 | + // This should never happen as this routine runs once, but this is a sanity check. | |
| 139 | + if ( ! is_numeric( $convertkit_form_id ) ) { | |
| 140 | + continue; | |
| 141 | + } | |
| 142 | + | |
| 143 | + // Prefix the ConvertKit Form ID with `form_`. | |
| 144 | + $settings[ $forminator_form_id ] = 'form:' . $convertkit_form_id; | |
| 145 | + } | |
| 146 | + | |
| 147 | + // Update settings. | |
| 148 | + update_option( $convertkit_forminator_settings::SETTINGS_NAME, $settings ); | |
| 149 | + | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * 2.5.2: Prefix any Contact Form 7 to ConvertKit Form ID mappings with `form:`, now that | |
| 154 | + * the Plugin supports adding a subscriber to a Form, Tag or Sequence. | |
| 155 | + * | |
| 156 | + * @since 2.5.2 | |
| 157 | + */ | |
| 158 | + private function migrate_contact_form_7_form_mapping_settings() { | |
| 159 | + | |
| 160 | + $convertkit_contact_form_7_settings = new ConvertKit_ContactForm7_Settings(); | |
| 161 | + | |
| 162 | + // Bail if no settings exist. | |
| 163 | + if ( ! $convertkit_contact_form_7_settings->has_settings() ) { | |
| 164 | + return; | |
| 165 | + } | |
| 166 | + | |
| 167 | + // Get settings. | |
| 168 | + $settings = $convertkit_contact_form_7_settings->get(); | |
| 169 | + | |
| 170 | + // Iterate through settings. | |
| 171 | + foreach ( $settings as $contact_form_7_form_id => $convertkit_form_id ) { | |
| 172 | + // Skip keys that are non-numeric e.g. `creator_network_recommendations_*`. | |
| 173 | + if ( ! is_numeric( $contact_form_7_form_id ) ) { | |
| 174 | + continue; | |
| 175 | + } | |
| 176 | + | |
| 177 | + // Skip values that are blank i.e. no ConvertKit Form ID specified. | |
| 178 | + if ( empty( $convertkit_form_id ) ) { | |
| 179 | + continue; | |
| 180 | + } | |
| 181 | + | |
| 182 | + // Skip values that are non-numeric i.e. the `form_` prefix was already added. | |
| 183 | + // This should never happen as this routine runs once, but this is a sanity check. | |
| 184 | + if ( ! is_numeric( $convertkit_form_id ) ) { | |
| 185 | + continue; | |
| 186 | + } | |
| 187 | + | |
| 188 | + // Prefix the ConvertKit Form ID with `form_`. | |
| 189 | + $settings[ $contact_form_7_form_id ] = 'form:' . $convertkit_form_id; | |
| 190 | + } | |
| 191 | + | |
| 192 | + // Update settings. | |
| 193 | + update_option( $convertkit_contact_form_7_settings::SETTINGS_NAME, $settings ); | |
| 194 | + | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * 2.5.0: Fetch an Access Token, Refresh Token and Expiry for v4 API use | |
| 199 | + * based on the Plugin setting's v3 API Key and Secret. | |
| 200 | + * | |
| 201 | + * @since 2.5.0 | |
| 202 | + */ | |
| 203 | + private function maybe_get_access_token_by_api_key_and_secret() { | |
| 204 | + | |
| 205 | + $convertkit_settings = new ConvertKit_Settings(); | |
| 206 | + | |
| 207 | + // Bail if an Access Token exists; we don't need to fetch another one. | |
| 208 | + if ( $convertkit_settings->has_access_token() ) { | |
| 209 | + return; | |
| 210 | + } | |
| 211 | + | |
| 212 | + // Bail if no API Key or Secret. | |
| 213 | + if ( empty( $convertkit_settings->get_api_key() ) ) { | |
| 214 | + return; | |
| 215 | + } | |
| 216 | + if ( empty( $convertkit_settings->get_api_secret() ) ) { | |
| 217 | + return; | |
| 218 | + } | |
| 219 | + | |
| 220 | + // Get Access Token by API Key and Secret. | |
| 221 | + $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI ); | |
| 222 | + $result = $api->get_access_token_by_api_key_and_secret( | |
| 223 | + $convertkit_settings->get_api_key(), | |
| 224 | + $convertkit_settings->get_api_secret() | |
| 225 | + ); | |
| 226 | + | |
| 227 | + // Bail if an error occured. | |
| 228 | + if ( is_wp_error( $result ) ) { | |
| 229 | + return; | |
| 230 | + } | |
| 231 | + | |
| 232 | + // Store the new credentials. | |
| 233 | + // We don't use update_credentials(), because the response | |
| 234 | + // includes an `expires_at`, not a `created_at` and `expires_in`. | |
| 235 | + $convertkit_settings->save( | |
| 236 | + array( | |
| 237 | + 'access_token' => $result['oauth']['access_token'], | |
| 238 | + 'refresh_token' => $result['oauth']['refresh_token'], | |
| 239 | + 'token_expires' => $result['oauth']['expires_at'], | |
| 240 | + ) | |
| 241 | + ); | |
| 242 | + | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Migrate ck_default_form to _wp_convertkit_term_meta[form], as Term settings | |
| 247 | + * support multiple options (form, position etc). | |
| 248 | + * | |
| 249 | + * @since 2.4.9.1 | |
| 250 | + */ | |
| 251 | + private function migrate_term_form_settings() { | |
| 252 | + | |
| 253 | + // Get all Terms that have ConvertKit settings defined. | |
| 254 | + $query = new WP_Term_Query( | |
| 255 | + array( | |
| 256 | + 'taxonomy' => 'category', | |
| 257 | + 'hide_empty' => false, | |
| 258 | + 'fields' => 'ids', | |
| 259 | + 'meta_query' => array( | |
| 260 | + array( | |
| 261 | + 'key' => 'ck_default_form', | |
| 262 | + 'comparison' => 'EXISTS', | |
| 263 | + ), | |
| 264 | + ), | |
| 265 | + ) | |
| 266 | + ); | |
| 267 | + | |
| 268 | + // Bail if no Terms exist. | |
| 269 | + if ( ! $query->terms ) { | |
| 270 | + return; | |
| 271 | + } | |
| 272 | + | |
| 273 | + // Iterate through Terms, mapping settings. | |
| 274 | + foreach ( $query->terms as $term_id ) { | |
| 275 | + $term_settings = new ConvertKit_Term( $term_id ); | |
| 276 | + $term_settings->save( | |
| 277 | + array( | |
| 278 | + 'form' => get_term_meta( $term_id, 'ck_default_form', true ), // Fetch form setting from old meta key. | |
| 279 | + 'form_position' => '', // Default to no position. | |
| 280 | + ) | |
| 281 | + ); | |
| 282 | + | |
| 283 | + // Delete old Term meta. | |
| 284 | + delete_term_meta( $term_id, 'ck_default_form' ); | |
| 285 | + } | |
| 286 | + | |
| 287 | + } | |
| 288 | + | |
| 289 | + /** | |
| 85 | 290 | * 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and |
| 86 | 291 | * _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting |
| 87 | 292 | * in Settings > ConvertKit > General. |
| 88 | 293 | */ |
| @@ -123,74 +328,8 @@ | ||
| 123 | 328 | |
| 124 | 329 | $forms->refresh(); |
| 125 | 330 | $landing_pages->refresh(); |
| 126 | 331 | $tags->refresh(); |
| 127 | - | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * 1.4.1: Change ID to form_id for API version 3.0. | |
| 132 | - * | |
| 133 | - * @since 1.4.1 | |
| 134 | - */ | |
| 135 | - private function change_id_to_form_id() { | |
| 136 | - | |
| 137 | - // Bail if the API isn't configured. | |
| 138 | - $convertkit_settings = new ConvertKit_Settings(); | |
| 139 | - if ( ! $convertkit_settings->has_api_key_and_secret() ) { | |
| 140 | - return; | |
| 141 | - } | |
| 142 | - | |
| 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 ); | |
| 154 | - } | |
| 155 | - | |
| 156 | - // Initialize the API. | |
| 157 | - $api = new ConvertKit_API( | |
| 158 | - $convertkit_settings->get_api_key(), | |
| 159 | - $convertkit_settings->get_api_secret(), | |
| 160 | - $convertkit_settings->debug_enabled(), | |
| 161 | - 'setup' | |
| 162 | - ); | |
| 163 | - | |
| 164 | - // Get form mappings. | |
| 165 | - $mappings = $api->get_subscription_forms(); | |
| 166 | - | |
| 167 | - // Bail if no form mappings exist. | |
| 168 | - if ( ! $mappings ) { | |
| 169 | - return; | |
| 170 | - } | |
| 171 | - | |
| 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 ); | |
| 176 | - | |
| 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 ); | |
| 182 | - | |
| 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 ); | |
| 192 | - } | |
| 193 | 332 | |
| 194 | 333 | } |
| 195 | 334 | |
| 196 | 335 | /** |