| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin activation, update and deactivation class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Runs any steps required on plugin activation, update and deactivation. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
* @version 1.9.7.4 |
| 15 |
*/ |
| 16 |
class ConvertKit_Setup { |
| 17 |
|
| 18 |
/** |
| 19 |
* Runs routines when the Plugin is activated. |
| 20 |
* |
| 21 |
* @since 1.9.7.4 |
| 22 |
*/ |
| 23 |
public function activate() { |
| 24 |
|
| 25 |
// Call any functions to e.g. schedule WordPress Cron events now. |
| 26 |
$posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 27 |
$posts->schedule_cron_event(); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Runs routines when the Plugin version has been updated. |
| 33 |
* |
| 34 |
* @since 1.9.7.4 |
| 35 |
*/ |
| 36 |
public function update() { |
| 37 |
|
| 38 |
// Get installed Plugin version. |
| 39 |
$current_version = get_option( 'convertkit_version' ); |
| 40 |
|
| 41 |
// If the version number matches the plugin version, no update routines |
| 42 |
// need to run. |
| 43 |
if ( $current_version === CONVERTKIT_PLUGIN_VERSION ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* 2.5.2: Migrate Forminator to ConvertKit Form Mappings |
| 49 |
*/ |
| 50 |
if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) { |
| 51 |
$this->migrate_forminator_form_mapping_settings(); |
| 52 |
} |
| 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 |
/** |
| 77 |
* 1.6.1+: Refresh Forms, Landing Pages and Tags data stored in settings, |
| 78 |
* to get new Forms Builder Settings. |
| 79 |
*/ |
| 80 |
if ( version_compare( $current_version, '1.6.1', '<' ) ) { |
| 81 |
$this->refresh_resources(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 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. |
| 88 |
*/ |
| 89 |
if ( version_compare( $current_version, '1.9.6', '<' ) ) { |
| 90 |
$this->migrate_default_form_settings(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* 1.9.7.4+: Schedule Post Resources' Cron event to refresh Posts cache hourly, |
| 95 |
* as the activate() routine won't pick this up for existing active installations. |
| 96 |
*/ |
| 97 |
if ( version_compare( $current_version, '1.9.7.4', '<' ) ) { |
| 98 |
$posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 99 |
$posts->schedule_cron_event(); |
| 100 |
} |
| 101 |
|
| 102 |
// Update the installed version number in the options table. |
| 103 |
update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 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 |
/** |
| 290 |
* 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and |
| 291 |
* _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting |
| 292 |
* in Settings > ConvertKit > General. |
| 293 |
*/ |
| 294 |
private function migrate_default_form_settings() { |
| 295 |
|
| 296 |
$convertkit_settings = new ConvertKit_Settings(); |
| 297 |
|
| 298 |
// Bail if no default_form setting exists. |
| 299 |
$settings = get_option( $convertkit_settings::SETTINGS_NAME ); |
| 300 |
if ( ! $settings ) { |
| 301 |
return; |
| 302 |
} |
| 303 |
if ( ! array_key_exists( 'default_form', $settings ) ) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
// Restructure settings. |
| 308 |
$settings['page_form'] = $settings['default_form']; |
| 309 |
$settings['post_form'] = $settings['default_form']; |
| 310 |
|
| 311 |
// Remove obsolete default_form setting. |
| 312 |
unset( $settings['default_form'] ); |
| 313 |
|
| 314 |
// Update. |
| 315 |
update_option( $convertkit_settings::SETTINGS_NAME, $settings ); |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* 1.6.1: Refresh Forms, Landing Pages and Tags data stored in settings, |
| 321 |
* to get new Forms Builder Settings. |
| 322 |
*/ |
| 323 |
private function refresh_resources() { |
| 324 |
|
| 325 |
$forms = new ConvertKit_Resource_Forms( 'setup' ); |
| 326 |
$landing_pages = new ConvertKit_Resource_Landing_Pages( 'setup' ); |
| 327 |
$tags = new ConvertKit_Resource_Tags( 'setup' ); |
| 328 |
|
| 329 |
$forms->refresh(); |
| 330 |
$landing_pages->refresh(); |
| 331 |
$tags->refresh(); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Runs routines when the Plugin is deactivated. |
| 337 |
* |
| 338 |
* @since 1.9.7.4 |
| 339 |
*/ |
| 340 |
public function deactivate() { |
| 341 |
|
| 342 |
// Call any functions to e.g. unschedule WordPress Cron events now. |
| 343 |
$posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 344 |
$posts->unschedule_cron_event(); |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
} |
| 349 |
|