PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.3
3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 All 195 releases
convertkit / includes / class-convertkit-setup.php

class-convertkit-setup.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.3, at includes/class-convertkit-setup.php

211 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 1.4.1: Change ID to form_id for API version 3.0.
49 */
50 if ( ! $current_version || version_compare( $current_version, '1.4.1', '<' ) ) {
51 $this->change_id_to_form_id();
52 }
53
54 /**
55 * 1.6.1+: Refresh Forms, Landing Pages and Tags data stored in settings,
56 * to get new Forms Builder Settings.
57 */
58 if ( version_compare( $current_version, '1.6.1', '<' ) ) {
59 $this->refresh_resources();
60 }
61
62 /**
63 * 1.9.6+: Migrate _wp_convertkit_settings[default_form] to _wp_convertkit_settings[page_form] and
64 * _wp_convertkit_settings[post_form], now that each Post Type has its own Default Form setting
65 * in Settings > ConvertKit > General.
66 */
67 if ( version_compare( $current_version, '1.9.6', '<' ) ) {
68 $this->migrate_default_form_settings();
69 }
70
71 /**
72 * 1.9.7.4+: Schedule Post Resources' Cron event to refresh Posts cache hourly,
73 * as the activate() routine won't pick this up for existing active installations.
74 */
75 if ( version_compare( $current_version, '1.9.7.4', '<' ) ) {
76 $posts = new ConvertKit_Resource_Posts( 'cron' );
77 $posts->schedule_cron_event();
78 }
79
80 // Update the installed version number in the options table.
81 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
82
83 }
84
85 /**
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.
89 */
90 private function migrate_default_form_settings() {
91
92 $convertkit_settings = new ConvertKit_Settings();
93
94 // Bail if no default_form setting exists.
95 $settings = get_option( $convertkit_settings::SETTINGS_NAME );
96 if ( ! $settings ) {
97 return;
98 }
99 if ( ! array_key_exists( 'default_form', $settings ) ) {
100 return;
101 }
102
103 // Restructure settings.
104 $settings['page_form'] = $settings['default_form'];
105 $settings['post_form'] = $settings['default_form'];
106
107 // Remove obsolete default_form setting.
108 unset( $settings['default_form'] );
109
110 // Update.
111 update_option( $convertkit_settings::SETTINGS_NAME, $settings );
112
113 }
114
115 /**
116 * 1.6.1: Refresh Forms, Landing Pages and Tags data stored in settings,
117 * to get new Forms Builder Settings.
118 */
119 private function refresh_resources() {
120
121 $forms = new ConvertKit_Resource_Forms( 'setup' );
122 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'setup' );
123 $tags = new ConvertKit_Resource_Tags( 'setup' );
124
125 $forms->refresh();
126 $landing_pages->refresh();
127 $tags->refresh();
128
129 }
130
131 /**
132 * 1.4.1: Change ID to form_id for API version 3.0.
133 *
134 * @since 1.4.1
135 */
136 private function change_id_to_form_id() {
137
138 // Bail if the API isn't configured.
139 $convertkit_settings = new ConvertKit_Settings();
140 if ( ! $convertkit_settings->has_api_key_and_secret() ) {
141 return;
142 }
143
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 );
155 }
156
157 // Initialize the API.
158 $api = new ConvertKit_API(
159 $convertkit_settings->get_api_key(),
160 $convertkit_settings->get_api_secret(),
161 $convertkit_settings->debug_enabled(),
162 'setup'
163 );
164
165 // Get form mappings.
166 $mappings = $api->get_subscription_forms();
167
168 // Bail if no form mappings exist.
169 if ( ! $mappings ) {
170 return;
171 }
172
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 );
177
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 );
183
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 );
193 }
194
195 }
196
197 /**
198 * Runs routines when the Plugin is deactivated.
199 *
200 * @since 1.9.7.4
201 */
202 public function deactivate() {
203
204 // Call any functions to e.g. unschedule WordPress Cron events now.
205 $posts = new ConvertKit_Resource_Posts( 'cron' );
206 $posts->unschedule_cron_event();
207
208 }
209
210 }
211