| 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(); |
| 27 |
$posts->schedule_cron_event(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Runs routines when the Plugin version has been updated. |
| 32 |
* |
| 33 |
* @since 1.9.7.4 |
| 34 |
*/ |
| 35 |
public function update() { |
| 36 |
|
| 37 |
// Get installed Plugin version. |
| 38 |
$current_version = get_option( 'convertkit_version' ); |
| 39 |
|
| 40 |
// If the version number matches the plugin version, no update routines |
| 41 |
// need to run. |
| 42 |
if ( $current_version === CONVERTKIT_PLUGIN_VERSION ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* 1.4.1: Change ID to form_id for API version 3.0. |
| 48 |
*/ |
| 49 |
if ( ! $current_version || version_compare( $current_version, '1.4.1', '<' ) ) { |
| 50 |
$this->change_id_to_form_id(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* 1.6.1+: Refresh Forms, Landing Pages and Tags data stored in settings, |
| 55 |
* to get new Forms Builder Settings. |
| 56 |
*/ |
| 57 |
if ( version_compare( $current_version, '1.6.1', '<' ) ) { |
| 58 |
$this->refresh_resources(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and |
| 63 |
* _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting |
| 64 |
* in Settings > ConvertKit > General. |
| 65 |
*/ |
| 66 |
if ( version_compare( $current_version, '1.9.6', '<' ) ) { |
| 67 |
$this->migrate_default_form_settings(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* 1.9.7.4+: Schedule Post Resources' Cron event to refresh Posts cache hourly, |
| 72 |
* as the activate() routine won't pick this up for existing active installations. |
| 73 |
*/ |
| 74 |
if ( version_compare( $current_version, '1.9.7.4', '<' ) ) { |
| 75 |
$posts = new ConvertKit_Resource_Posts(); |
| 76 |
$posts->schedule_cron_event(); |
| 77 |
} |
| 78 |
|
| 79 |
// Update the installed version number in the options table. |
| 80 |
update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION ); |
| 81 |
|
| 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 |
private function migrate_default_form_settings() { |
| 90 |
|
| 91 |
$convertkit_settings = new ConvertKit_Settings(); |
| 92 |
|
| 93 |
// Bail if no default_form setting exists. |
| 94 |
$settings = get_option( $convertkit_settings::SETTINGS_NAME ); |
| 95 |
if ( ! $settings ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
if ( ! array_key_exists( 'default_form', $settings ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Restructure settings. |
| 103 |
$settings['page_form'] = $settings['default_form']; |
| 104 |
$settings['post_form'] = $settings['default_form']; |
| 105 |
|
| 106 |
// Remove obsolete default_form setting. |
| 107 |
unset( $settings['default_form'] ); |
| 108 |
|
| 109 |
// Update. |
| 110 |
update_option( $convertkit_settings::SETTINGS_NAME, $settings ); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* 1.6.1: Refresh Forms, Landing Pages and Tags data stored in settings, |
| 116 |
* to get new Forms Builder Settings. |
| 117 |
*/ |
| 118 |
private function refresh_resources() { |
| 119 |
|
| 120 |
$forms = new ConvertKit_Resource_Forms(); |
| 121 |
$landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 122 |
$tags = new ConvertKit_Resource_Tags(); |
| 123 |
|
| 124 |
$forms->refresh(); |
| 125 |
$landing_pages->refresh(); |
| 126 |
$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( $convertkit_settings->get_api_key(), $convertkit_settings->get_api_secret(), $convertkit_settings->debug_enabled() ); |
| 158 |
|
| 159 |
// Get form mappings. |
| 160 |
$mappings = $api->get_subscription_forms(); |
| 161 |
|
| 162 |
// Bail if no form mappings exist. |
| 163 |
if ( ! $mappings ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
// 1. Update global form. |
| 168 |
$settings_data = $convertkit_settings->get(); |
| 169 |
$settings_data['default_form'] = isset( $mappings[ $convertkit_settings->get_default_form( 'post' ) ] ) ? $mappings[ $convertkit_settings->get_default_form( 'post' ) ] : 0; |
| 170 |
update_option( $convertkit_settings::SETTINGS_NAME, $settings_data ); |
| 171 |
|
| 172 |
// 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs |
| 173 |
// Scan content for shortcode and update |
| 174 |
// Remove page_id from posts array after page is updated. |
| 175 |
foreach ( $posts as $key => $post_id ) { |
| 176 |
$post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true ); |
| 177 |
|
| 178 |
if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) { |
| 179 |
$post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; |
| 180 |
} |
| 181 |
if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) { |
| 182 |
$post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0; |
| 183 |
} |
| 184 |
update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings ); |
| 185 |
unset( $posts[ $key ] ); |
| 186 |
update_option( '_wp_convertkit_upgrade_posts', $posts ); |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Runs routines when the Plugin is deactivated. |
| 193 |
* |
| 194 |
* @since 1.9.7.4 |
| 195 |
*/ |
| 196 |
public function deactivate() { |
| 197 |
|
| 198 |
// Call any functions to e.g. unschedule WordPress Cron events now. |
| 199 |
$posts = new ConvertKit_Resource_Posts(); |
| 200 |
$posts->unschedule_cron_event(); |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|