PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.4.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.4.8
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
← All changes | wp-convertkit.php +456 -115 2.2.21.4.8 View file →
@@ -1,141 +1,482 @@
1 1 <?php
2 2 /**
3 - * ConvertKit WordPress Plugin.
4 - *
5 - * @package ConvertKit
6 - * @author ConvertKit
7 - *
8 - * @wordpress-plugin
9 3 * Plugin Name: ConvertKit
10 4 * Plugin URI: https://convertkit.com/
11 5 * Description: Quickly and easily integrate ConvertKit forms into your site.
12 - * Version: 2.2.2
6 + * Version: 1.4.8
13 7 * Author: ConvertKit
14 8 * Author URI: https://convertkit.com/
15 9 * Text Domain: convertkit
16 10 */
17 11
18 -// Bail if ConvertKit is alread loaded.
19 -if ( class_exists( 'WP_ConvertKit' ) ) {
20 - return;
21 -}
12 +require_once plugin_dir_path( __FILE__ ) . '/lib/class-convertkit-api.php';
13 +require_once plugin_dir_path( __FILE__ ) . '/lib/class-ck-widget-form.php';
14 +require_once plugin_dir_path( __FILE__ ) . '/lib/integration/class-convertkit-wishlist-integration.php';
15 +require_once plugin_dir_path( __FILE__ ) . '/lib/integration/class-convertkit-contactform7-integration.php';
22 16
23 -// Define ConverKit Plugin paths and version number.
24 -define( 'CONVERTKIT_PLUGIN_NAME', 'ConvertKit' ); // Used for user-agent in API class.
25 -define( 'CONVERTKIT_PLUGIN_FILE', plugin_basename( __FILE__ ) );
26 -define( 'CONVERTKIT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
27 -define( 'CONVERTKIT_PLUGIN_PATH', __DIR__ );
28 -define( 'CONVERTKIT_PLUGIN_VERSION', '2.2.2' );
17 +if ( ! class_exists( 'WP_ConvertKit' ) ) {
18 + /**
19 + * Class WP_ConvertKit
20 + */
21 + class WP_ConvertKit {
29 22
30 -// Load shared classes, if they have not been included by another ConvertKit Plugin.
31 -if ( ! class_exists( 'ConvertKit_API' ) ) {
32 - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-api.php';
33 -}
34 -if ( ! class_exists( 'ConvertKit_Log' ) ) {
35 - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-log.php';
36 -}
37 -if ( ! class_exists( 'ConvertKit_Resource' ) ) {
38 - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-resource.php';
39 -}
40 -if ( ! class_exists( 'ConvertKit_Review_Request' ) ) {
41 - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-review-request.php';
42 -}
23 + const VERSION = '1.4.8';
43 24
44 -// Load plugin files that are always required.
45 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/cron-functions.php';
46 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/functions.php';
47 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-wp-convertkit.php';
48 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-ajax.php';
49 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-gutenberg.php';
50 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-output.php';
51 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-output-restrict-content.php';
52 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-post.php';
53 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-preview-output.php';
54 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-forms.php';
55 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-landing-pages.php';
56 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-posts.php';
57 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-products.php';
58 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-tags.php';
59 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-settings.php';
60 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-settings-restrict-content.php';
61 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-setup.php';
62 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-shortcodes.php';
63 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-subscriber.php';
64 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-term.php';
65 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-user.php';
66 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-widgets.php';
67 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block.php';
68 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-broadcasts.php';
69 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-content.php';
70 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-form-trigger.php';
71 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-form.php';
72 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-product.php';
73 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter.php';
74 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter-form-link.php';
75 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter-product-link.php';
76 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/widgets/class-ck-widget-form.php';
25 + const POST_META_KEY = '_wp_convertkit_post_meta';
77 26
78 -// Contact Form 7 Integration.
79 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7.php';
80 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7-settings.php';
27 + const SETTINGS_NAME = '_wp_convertkit_settings';
81 28
82 -// Elementor Integration.
83 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/elementor/class-convertkit-elementor.php';
29 + const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings';
84 30
85 -// WishList Member Integration.
86 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist.php';
87 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist-settings.php';
31 + /**
32 + * @var ConvertKit_API
33 + */
34 + private static $api;
88 35
89 -// WooCommerce Integration.
90 -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/woocommerce/class-convertkit-woocommerce-product-form.php';
36 + /**
37 + * @var int Data Caching
38 + */
39 + private static $cache_period = 0;
91 40
92 -// Load files that are only used in the WordPress Administration interface.
93 -if ( is_admin() ) {
94 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-bulk-edit.php';
95 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-quick-edit.php';
96 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-cache-plugins.php';
97 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-category.php';
98 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-notices.php';
99 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-post.php';
100 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-refresh-resources.php';
101 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-settings.php';
102 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-tinymce.php';
103 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-user.php';
104 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-setup-wizard.php';
105 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-multi-value-field-table.php';
106 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-base.php';
107 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-general.php';
108 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-tools.php';
109 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/setup-wizard/class-convertkit-admin-setup-wizard-plugin.php';
41 + /**
42 + * @var null
43 + */
44 + private static $meta_defaults = null;
110 45
111 - // Contact Form 7 Integration.
112 - require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7-admin-settings.php';
46 + /**
47 + * @var array
48 + */
49 + private static $settings_defaults = array(
50 + 'api_key' => '',
51 + 'default_form' => 0,
52 + );
113 53
114 - // WishList Member Integration.
115 - require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist-admin-settings.php';
54 + /**
55 + * @var array
56 + */
57 + private static $forms_markup = array();
116 58
117 - // Restrict Content Integration.
118 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-restrict-content.php';
119 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-settings-restrict-content.php';
120 - require_once CONVERTKIT_PLUGIN_PATH . '/admin/setup-wizard/class-convertkit-admin-setup-wizard-restrict-content.php';
121 -}
59 + /** @var array */
60 + private static $landing_pages_markup = array();
122 61
123 -// Register Plugin activation and deactivation functions.
124 -register_activation_hook( __FILE__, 'convertkit_plugin_activate' );
125 -add_action( 'wp_insert_site', 'convertkit_plugin_activate_new_site' );
126 -add_action( 'activate_blog', 'convertkit_plugin_activate_new_site' );
127 -register_deactivation_hook( __FILE__, 'convertkit_plugin_deactivate' );
62 + /** @var string */
63 + private static $forms_version = '6';
128 64
129 -/**
130 - * Main function to return Plugin instance.
131 - *
132 - * @since 1.9.6
133 - */
134 -function WP_ConvertKit() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName
65 + /**
66 + * Initialize the class
67 + */
68 + public static function init() {
69 + self::add_actions();
70 + self::add_filters();
71 + self::register_shortcodes();
135 72
136 - return WP_ConvertKit::get_instance();
73 + self::$cache_period = MINUTE_IN_SECONDS * 10;
137 74
138 -}
75 + self::_api_connect();
76 + }
139 77
140 -// Finally, initialize the Plugin.
141 -WP_ConvertKit();
78 + /**
79 + * Add WP Actions
80 + */
81 + private static function add_actions() {
82 + add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
83 + if ( is_admin() ) {
84 + add_action( 'add_meta_boxes_page', array( __CLASS__, 'add_meta_boxes' ) );
85 + add_action( 'add_meta_boxes_post', array( __CLASS__, 'add_meta_boxes' ) );
86 + } else {
87 + add_action( 'template_redirect', array( __CLASS__, 'page_takeover' ) );
88 + }
89 +
90 + add_action( 'widgets_init', array( __CLASS__, 'ck_register_widgets' ) );
91 +
92 + add_action( 'save_post', array( __CLASS__, 'save_post_meta' ), 10, 2 );
93 +
94 + add_action( 'init', array( __CLASS__, 'upgrade' ) , 10 );
95 + }
96 +
97 + /**
98 + * Load plugin textdomain
99 + */
100 + public static function load_textdomain() {
101 + load_plugin_textdomain( 'convertkit', false, basename( dirname( __FILE__ ) ) . '/languages/' );
102 + }
103 +
104 + /**
105 + * Add WP Filters
106 + */
107 + private static function add_filters() {
108 + if ( ! is_admin() ) {
109 + add_filter( 'the_content', array( __CLASS__, 'append_form' ) );
110 + }
111 +
112 + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, 'add_settings_page_link' ) );
113 + }
114 +
115 + /**
116 + * Register ConvertKit shortcodes
117 + */
118 + private static function register_shortcodes() {
119 + add_shortcode( 'convertkit', array( __CLASS__, 'shortcode' ) );
120 + }
121 +
122 + /**
123 + * Plugin action links callback
124 + *
125 + * @param $links
126 + * @return array
127 + */
128 + public static function add_settings_page_link( $links ) {
129 + $settings_link = sprintf( '<a href="%s">%s</a>', self::_get_settings_page_link(), __( 'Settings', 'convertkit' ) );
130 +
131 + return array(
132 + 'settings' => $settings_link,
133 + ) + $links;
134 + }
135 +
136 + /**
137 + * Add Meta Boxes callback
138 + *
139 + * @param WP_Post $post The current post.
140 + */
141 + public static function add_meta_boxes( $post ) {
142 + $forms = self::$api->get_resources( 'forms' );
143 + $landing_pages = self::$api->get_resources( 'landing_pages' );
144 +
145 + if ( ! empty( $forms ) || ( 'page' === $post->post_type && ! empty( $landing_pages ) ) ) {
146 + add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( __CLASS__, 'display_meta_box' ), $post->post_type, 'normal' );
147 + }
148 + }
149 +
150 + /**
151 + * Metabox callback
152 + *
153 + * @param $post
154 + */
155 + public static function display_meta_box( $post ) {
156 + $forms = self::$api->get_resources( 'forms' );
157 + $landing_pages = self::$api->get_resources( 'landing_pages' );
158 +
159 + $meta = self::_get_meta( $post->ID );
160 + $settings_link = self::_get_settings_page_link();
161 +
162 + include( 'views/backend/meta-boxes/meta-box.php' );
163 + }
164 +
165 + /**
166 + * Save post meta callback
167 + *
168 + * @param int $post_id Post id.
169 + * @param WP_Post $post The post.
170 + */
171 + public static function save_post_meta( $post_id, $post ) {
172 + if ( wp_is_post_autosave( $post_id )
173 + || wp_is_post_revision( $post_id )
174 + || ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) // WPCS input var okay.
175 + || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { // WPCS input var okay.
176 + return;
177 + }
178 + if ( isset( $_POST['wp-convertkit'] ) ) { // WPCS input var okay.
179 + $form = '';
180 + if ( isset( $_POST['wp-convertkit']['form'] ) ) { // WPCS input var okay.
181 + $form = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form'] ) ); // WPCS input var okay.
182 + }
183 + $landing_page = '';
184 + if ( isset( $_POST['wp-convertkit']['landing_page'] ) ) { // WPCS input var okay.
185 + $landing_page = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['landing_page'] ) ); // WPCS input var okay.
186 + }
187 + $meta = array(
188 + 'form' => $form,
189 + 'landing_page' => $landing_page,
190 + );
191 + update_post_meta( $post_id, self::POST_META_KEY, $meta );
192 + }
193 + }
194 +
195 + /**
196 + * Page/Post display callback
197 + *
198 + * @param string $content The post content.
199 + * @return string
200 + */
201 + public static function append_form( $content ) {
202 +
203 + if ( is_singular( array( 'post' ) ) || is_page() ) {
204 +
205 + $attributes = self::_get_meta( get_the_ID() );
206 +
207 + $form_id = 0;
208 +
209 + if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) {
210 + $form_id = $attributes['form'];
211 + } else {
212 + if ( '-1' === $attributes['form'] ) {
213 + $form_id = self::_get_settings( 'default_form' );
214 + }
215 + }
216 +
217 + if ( 0 < $form_id ) {
218 + $url = add_query_arg( array(
219 + 'api_key' => self::_get_settings( 'api_key' ),
220 + 'v' => self::$forms_version,
221 + ),
222 + 'https://forms.convertkit.com/' . $form_id . '.html'
223 + );
224 +
225 + $form_markup = self::$api->get_resource( $url );
226 + $content .= $form_markup;
227 + }
228 + }
229 +
230 + return $content;
231 + }
232 +
233 + /**
234 + * Replace page content if a landing_page is set
235 + */
236 + public static function page_takeover() {
237 + $queried_object = get_queried_object();
238 + if ( isset( $queried_object->post_type )
239 + && 'page' === $queried_object->post_type ) {
240 +
241 + $landing_page_url = self::_get_meta( $queried_object->ID, 'landing_page' );
242 +
243 + $landing_page = self::$api->get_resource( $landing_page_url );
244 +
245 + if ( ! empty( $landing_page ) ) {
246 + echo $landing_page; // WPCS: XSS ok.
247 + exit;
248 + }
249 + }
250 +
251 + }
252 +
253 + /**
254 + * Register widget.
255 + */
256 + public static function ck_register_widgets() {
257 + register_widget( 'CK_Widget_Form' );
258 + }
259 +
260 + /**
261 + * Shortcode callback
262 + *
263 + * @param array $attributes Shortcode attributes.
264 + * @param null $content
265 + * @return mixed|void
266 + */
267 + public static function shortcode( $attributes, $content = null ) {
268 +
269 + if ( isset( $attributes['id'] ) ) {
270 + $form_id = $attributes['id'];
271 + $url = add_query_arg( array(
272 + 'api_key' => self::_get_settings( 'api_key' ),
273 + 'v' => self::$forms_version,
274 + ),
275 + 'https://forms.convertkit.com/' . $form_id . '.html'
276 + );
277 + } elseif ( isset( $attributes['form'] ) ) {
278 + $form_id = $attributes['form'];
279 + $url = add_query_arg( array(
280 + 'k' => self::_get_settings( 'api_key' ),
281 + 'v' => '2',
282 + ),
283 + 'https://api.convertkit.com/forms/' . $form_id . '/embed'
284 + );
285 + } else {
286 + $form_id = self::_get_settings( 'default_form' );
287 + $url = add_query_arg( array(
288 + 'api_key' => self::_get_settings( 'api_key' ),
289 + 'v' => self::$forms_version,
290 + ),
291 + 'https://forms.convertkit.com/' . $form_id . '.html'
292 + );
293 + }
294 +
295 + if ( 0 < $form_id ) {
296 + $form_markup = self::$api->get_resource( $url );
297 + } else {
298 + $form_markup = '';
299 + }
300 +
301 + return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes );
302 + }
303 +
304 + /**
305 + * Retrieve meta
306 + *
307 + * @return array|null
308 + */
309 + private static function _get_meta_defaults() {
310 + if ( is_null( self::$meta_defaults ) ) {
311 + self::$meta_defaults = array(
312 + 'form' => -1,
313 + 'landing_page' => '',
314 + );
315 + }
316 +
317 + return self::$meta_defaults;
318 + }
319 +
320 + /**
321 + * Get selected post meta
322 + *
323 + * @param int $post_id Post ID.
324 + * @param null $meta_key Key string to get.
325 + *
326 + * @return array|bool
327 + */
328 + private static function _get_meta( $post_id, $meta_key = null ) {
329 + $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
330 +
331 + $meta = get_post_meta( $post_id, self::POST_META_KEY, true );
332 + $meta_defaults = self::_get_meta_defaults();
333 +
334 + if ( empty( $meta ) ) {
335 + $meta = $meta_defaults;
336 +
337 + $old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) );
338 + if ( 0 !== $old_value ) {
339 + $meta['form'] = $old_value;
340 + }
341 + }
342 +
343 + $meta = shortcode_atts( $meta_defaults, $meta );
344 +
345 + return is_null( $meta_key ) ? $meta : ( isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : false );
346 + }
347 +
348 + /**
349 + * Get plugin settings
350 + *
351 + * @param null $settings_key
352 + * @return mixed|null|void
353 + */
354 + private static function _get_settings( $settings_key = null ) {
355 + $settings = get_option( self::SETTINGS_NAME, self::$settings_defaults );
356 +
357 + return is_null( $settings_key ) ? $settings : ( isset( $settings[ $settings_key ] ) ? $settings[ $settings_key ] : null);
358 + }
359 +
360 + /**
361 + * Get instance of ConvertKitAPI
362 + */
363 + private static function _api_connect() {
364 + $api_key = self::_get_settings( 'api_key' );
365 + $api_secret = self::_get_settings( 'api_secret' );
366 + $debug = self::_get_settings( 'debug' );
367 +
368 + self::$api = new ConvertKit_API( $api_key, $api_secret, $debug );
369 + }
370 +
371 + /**
372 + * Get instance of the CK API
373 + *
374 + * @return ConvertKit_API
375 + */
376 + public static function get_api() {
377 + return self::$api;
378 + }
379 +
380 + /**
381 + * Get ConvertKit API key
382 + *
383 + * @return mixed|null|void
384 + */
385 + public static function get_api_key() {
386 + return self::_get_settings( 'api_key' );
387 + }
388 +
389 + /**
390 + * @return string
391 + */
392 + public static function get_forms_version() {
393 + return self::$forms_version;
394 + }
395 +
396 + /**
397 + * Get link to the plugin settings page
398 + *
399 + * @param array $query_args Args to add to URL.
400 + * @return string
401 + */
402 + private static function _get_settings_page_link( $query_args = array() ) {
403 + $query_args = array(
404 + 'page' => self::SETTINGS_PAGE_SLUG,
405 + ) + $query_args;
406 +
407 + return add_query_arg( $query_args, admin_url( 'options-general.php' ) );
408 + }
409 +
410 + /**
411 + * Run version specific upgrade.
412 + */
413 + public static function upgrade() {
414 +
415 + $current_version = get_option( 'convertkit_version' );
416 +
417 + if ( ! $current_version ) {
418 + // Run 1.4.1 upgrade.
419 + $settings = self::_get_settings();
420 +
421 + if ( isset( $settings['api_key'] ) ) {
422 + // Get all posts and pages to track what has been updated.
423 + $posts = get_option( '_wp_convertkit_upgrade_posts' );
424 +
425 + if ( ! $posts ) {
426 +
427 + $args = array(
428 + 'post_type' => array( 'post', 'page' ),
429 + 'fields' => 'ids',
430 + );
431 +
432 + $result = new WP_Query( $args );
433 + if ( ! is_wp_error( $result ) ) {
434 + $posts = $result->posts;
435 + update_option( '_wp_convertkit_upgrade_posts', $posts );
436 + }
437 + }
438 +
439 + // Get form mappings.
440 + $mappings = self::$api->get_resources( 'subscription_forms' );
441 +
442 + // 1. Update global form. Set 'api_version' so this is only done once.
443 + if ( ! isset( $settings['api_version'] ) ) {
444 + $old_form_id = $settings['default_form'];
445 + $settings['default_form'] = isset( $mappings[ $old_form_id ] ) ? $mappings[ $old_form_id ] : 0;
446 + $settings['api_version'] = 'v3';
447 + update_option( self::SETTINGS_NAME, $settings );
448 + }
449 +
450 + // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs
451 + // Scan content for shortcode and update
452 + // Remove page_id from posts array after page is updated.
453 + foreach ( $posts as $key => $post_id ) {
454 + $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true );
455 +
456 + if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) {
457 + $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0;
458 + }
459 + if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) {
460 + $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0;
461 + }
462 + update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings );
463 + unset( $posts[ $key ] );
464 + update_option( '_wp_convertkit_upgrade_posts', $posts );
465 + }
466 +
467 + // Done scanning posts, upgrade complete.
468 + if ( empty( $posts ) ) {
469 + update_option( 'convertkit_version', self::VERSION );
470 + delete_option( '_wp_convertkit_upgrade_posts' );
471 + }
472 + } else {
473 + update_option( 'convertkit_version', self::VERSION );
474 + } // End if().
475 + } // End if().
476 + }
477 + }
478 +
479 + WP_ConvertKit::init();
480 +} // End if().
481 +
482 +include 'admin/class-convertkit-settings.php';