PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.6.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.6.4
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.php

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

622 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class WP_ConvertKit
5 */
6 class WP_ConvertKit {
7
8 const POST_META_KEY = '_wp_convertkit_post_meta';
9
10 const SETTINGS_NAME = '_wp_convertkit_settings';
11
12 const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings';
13
14 /**
15 * @var ConvertKit_API
16 */
17 private static $api;
18
19 /**
20 * @var int Data Caching
21 */
22 private static $cache_period = 0;
23
24 /**
25 * @var null
26 */
27 private static $meta_defaults = null;
28
29 /**
30 * @var array
31 */
32 private static $settings_defaults = array(
33 'api_key' => '',
34 'default_form' => 0,
35 );
36
37 /**
38 * @var array
39 */
40 private static $forms_markup = array();
41
42 /** @var array */
43 private static $landing_pages_markup = array();
44
45 /** @var string */
46 private static $forms_version = '6';
47
48 /**
49 * Initialize the class
50 */
51 public static function init() {
52 self::add_actions();
53 self::add_filters();
54 self::register_shortcodes();
55
56 self::$cache_period = MINUTE_IN_SECONDS * 10;
57
58 self::_api_connect();
59 }
60
61 /**
62 * Add WP Actions
63 */
64 private static function add_actions() {
65 add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
66 if ( is_admin() ) {
67 add_action( 'add_meta_boxes_page', array( __CLASS__, 'add_meta_boxes' ) );
68 add_action( 'add_meta_boxes_post', array( __CLASS__, 'add_meta_boxes' ) );
69 } else {
70 add_action( 'template_redirect', array( __CLASS__, 'page_takeover' ) );
71 add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
72 }
73
74 add_action( 'widgets_init', array( __CLASS__, 'ck_register_widgets' ) );
75
76 add_action( 'save_post', array( __CLASS__, 'save_post_meta' ), 10, 2 );
77
78 add_action( 'init', array( __CLASS__, 'upgrade' ) , 10 );
79 }
80
81 /**
82 * Load plugin textdomain
83 */
84 public static function load_textdomain() {
85 load_plugin_textdomain( 'convertkit', false, basename( dirname( __FILE__ ) ) . '/languages/' );
86 }
87
88 /**
89 * Add WP Filters
90 */
91 private static function add_filters() {
92 if ( ! is_admin() ) {
93 add_filter( 'the_content', array( __CLASS__, 'append_form' ) );
94 }
95
96 add_filter( 'plugin_action_links_' . CONVERTKIT_PLUGIN_FILE, array( __CLASS__, 'add_settings_page_link' ) );
97 }
98
99 /**
100 * Register ConvertKit shortcodes
101 */
102 private static function register_shortcodes() {
103 add_shortcode( 'convertkit', array( __CLASS__, 'shortcode' ) );
104 }
105
106 /**
107 * Plugin action links callback
108 *
109 * @param $links
110 * @return array
111 */
112 public static function add_settings_page_link( $links ) {
113 $settings_link = sprintf( '<a href="%s">%s</a>', self::_get_settings_page_link(), __( 'Settings', 'convertkit' ) );
114
115 return array(
116 'settings' => $settings_link,
117 ) + $links;
118 }
119
120 /**
121 * Add Meta Boxes callback
122 *
123 * @param WP_Post $post The current post.
124 */
125 public static function add_meta_boxes( $post ) {
126 add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( __CLASS__, 'display_meta_box' ), $post->post_type, 'normal' );
127 }
128
129 /**
130 * Metabox callback
131 *
132 * @param $post
133 */
134 public static function display_meta_box( $post ) {
135
136 $forms = get_option( 'convertkit_forms' );
137 $landing_pages = get_option( 'convertkit_landing_pages' );
138 $tags = get_option( 'convertkit_tags' );
139
140 $meta = self::_get_meta( $post->ID );
141 $settings_link = self::_get_settings_page_link();
142
143 include( CONVERTKIT_PLUGIN_PATH . '/views/backend/meta-boxes/meta-box.php' );
144 }
145
146 /**
147 * Save post meta callback
148 *
149 * @param int $post_id Post id.
150 * @param WP_Post $post The post.
151 */
152 public static function save_post_meta( $post_id, $post ) {
153 if ( wp_is_post_autosave( $post_id )
154 || wp_is_post_revision( $post_id )
155 || ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) // WPCS input var okay.
156 || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { // WPCS input var okay.
157 return;
158 }
159 if ( isset( $_POST['wp-convertkit'] ) ) { // WPCS input var okay.
160 $form = '';
161 if ( isset( $_POST['wp-convertkit']['form'] ) ) { // WPCS input var okay.
162 $form = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form'] ) ); // WPCS input var okay.
163 }
164 $landing_page = '';
165 if ( isset( $_POST['wp-convertkit']['landing_page'] ) ) { // WPCS input var okay.
166 $landing_page = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['landing_page'] ) ); // WPCS input var okay.
167 }
168 $tag = '';
169 if ( isset( $_POST['wp-convertkit']['tag'] ) ) { // WPCS input var okay.
170 $tag = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['tag'] ) ); // WPCS input var okay.
171 }
172
173 $meta = array(
174 'form' => $form,
175 'landing_page' => $landing_page,
176 'tag' => $tag,
177 );
178 update_post_meta( $post_id, self::POST_META_KEY, $meta );
179 }
180 }
181
182 /**
183 * Page/Post display callback
184 *
185 * @param string $content The post content.
186 * @return string
187 */
188 public static function append_form( $content ) {
189
190 if ( is_singular( array( 'post' ) ) || is_page() ) {
191
192 $attributes = self::_get_meta( get_the_ID() );
193
194 // Get post/page form setting
195 if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) {
196 $form_id = $attributes['form'];
197 } else {
198 // Get category form
199 $form_id = self::get_category_form( get_the_ID() );
200
201 if ( 0 === $form_id ) {
202 // Get global default form
203 if ( '-1' === $attributes['form'] ) {
204 $form_id = self::_get_settings( 'default_form' );
205 }
206 }
207 }
208
209 if ( 0 < $form_id ) {
210
211 $forms = get_option( 'convertkit_forms' );
212
213 if ( isset( $forms[ $form_id ]['uid'] ) ) {
214 // new form
215 $tag = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
216 $content .= $tag;
217
218 } else {
219 // old form
220 $url = add_query_arg(
221 array(
222 'api_key' => self::_get_settings( 'api_key' ),
223 'v' => self::$forms_version,
224 ),
225 'https://forms.convertkit.com/' . $form_id . '.html'
226 );
227
228 $form_markup = self::$api->get_resource( $url );
229 $content .= $form_markup;
230 }
231 }
232 }
233
234 return $content;
235 }
236
237 /**
238 * Replace page content if a landing_page is set
239 */
240 public static function page_takeover() {
241 $queried_object = get_queried_object();
242 if ( isset( $queried_object->post_type )
243 && 'page' === $queried_object->post_type ) {
244
245 $landing_page_id = self::_get_meta( $queried_object->ID, 'landing_page' );
246
247 if ( '0' === $landing_page_id || empty( $landing_page_id ) ) {
248 // Set to None
249 return;
250 }
251
252 if ( strstr( $landing_page_id, 'http') ) {
253 // Old landing page
254 $landing_page = self::$api->get_resource( $landing_page_id );
255 if ( ! empty( $landing_page ) ) {
256 $script = WP_ConvertKit::get_ck_script();
257 $script .= "</head>";
258 $landing_page = str_replace( '</head>', $script, $landing_page );
259 echo $landing_page; // WPCS: XSS ok.
260 exit;
261 }
262 } else {
263 // New landing page
264 $landing_pages = get_option( 'convertkit_landing_pages' );
265 $landing_page = self::$api->get_resource( $landing_pages[$landing_page_id]['embed_url'] );
266 if ( ! empty( $landing_page ) ) {
267 $script = WP_ConvertKit::get_ck_script( true );
268 $script .= "</head>";
269 $landing_page = str_replace( '</head>', $script, $landing_page );
270 echo $landing_page; // WPCS: XSS ok.
271 exit;
272 }
273 }
274 }
275 }
276
277 /**
278 * Generate HTML for including ConvertKit JS in a landing page.
279 *
280 * During the landing page takeover the normal WordPress enqueue routine is not run so we need to add these
281 * javascript if we want subscribers tagged correctly.
282 *
283 * @since 1.5.2
284 * @param bool $include_jquery
285 * @return string
286 */
287 public static function get_ck_script( $include_jquery = false) {
288 $script = '';
289 if ( $include_jquery ) {
290 $scripts = new WP_Scripts();
291 $script .= "<script type='text/javascript' src='" . trailingslashit( $scripts->base_url ) . "wp-includes/js/jquery/jquery.js?ver=1.4.0'></script>";
292 }
293 $script .= "<script type='text/javascript' src='" . CONVERTKIT_PLUGIN_URL . "resources/frontend/jquery.cookie.min.js?ver=1.4.0'></script>";
294 $script .= "<script type='text/javascript' src='" . CONVERTKIT_PLUGIN_URL . 'resources/frontend/wp-convertkit.js?ver=' . CONVERTKIT_PLUGIN_VERSION . "'></script>";
295 $script .= "<script type='text/javascript'>/* <![CDATA[ */var ck_data = {\"ajaxurl\":\"" . admin_url( 'admin-ajax.php' ) . '"};/* ]]> */</script>';
296 return $script;
297 }
298
299 /**
300 * Enqueue scripts
301 */
302 public static function enqueue_scripts() {
303 wp_enqueue_script( 'jquery-cookie', CONVERTKIT_PLUGIN_URL . 'resources/frontend/jquery.cookie.min.js', array( 'jquery' ), '1.4.0' );
304
305 wp_register_script( 'convertkit-js', CONVERTKIT_PLUGIN_URL . 'resources/frontend/wp-convertkit.js', array( 'jquery-cookie' ), CONVERTKIT_PLUGIN_VERSION );
306 wp_localize_script( 'convertkit-js', 'ck_data', array(
307 'ajaxurl' => admin_url( 'admin-ajax.php' ),
308 ) );
309 wp_enqueue_script( 'convertkit-js' );
310 }
311
312 /**
313 * Register widget.
314 */
315 public static function ck_register_widgets() {
316 register_widget( 'CK_Widget_Form' );
317 }
318
319 /**
320 * Shortcode callback
321 *
322 * @param array $attributes Shortcode attributes.
323 * @param null $content
324 * @return mixed|void
325 */
326 public static function shortcode( $attributes, $content = null ) {
327
328 if ( isset( $attributes['id'] ) ) {
329 $form_id = $attributes['id'];
330 $forms = get_option( 'convertkit_forms' );
331
332 if ( isset( $forms[ $form_id ]['uid'] ) ) {
333 // new form
334 $form_markup = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
335 return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes );
336 } else {
337 // old form
338 $url = add_query_arg(
339 array(
340 'api_key' => self::_get_settings( 'api_key' ),
341 'v' => self::$forms_version,
342 ),
343 'https://forms.convertkit.com/' . $form_id . '.html'
344 );
345 }
346 } elseif ( isset( $attributes['form'] ) ) {
347 $form_id = $attributes['form'];
348 $forms = get_option( 'convertkit_forms' );
349
350 if ( isset( $forms[ $form_id ]['uid'] ) ) {
351 // new form
352 $form_markup = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
353 return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes );
354 } else {
355 $url = add_query_arg(
356 array(
357 'k' => self::_get_settings( 'api_key' ),
358 'v' => '2',
359 ),
360 'https://api.convertkit.com/forms/' . $form_id . '/embed'
361 );
362 }
363 } else {
364 $form_id = self::_get_settings( 'default_form' );
365 $url = add_query_arg(
366 array(
367 'api_key' => self::_get_settings( 'api_key' ),
368 'v' => self::$forms_version,
369 ),
370 'https://forms.convertkit.com/' . $form_id . '.html'
371 );
372 }
373
374 if ( 0 < $form_id ) {
375 $form_markup = self::$api->get_resource( $url );
376 } else {
377 $form_markup = '';
378 }
379
380 return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes );
381 }
382
383 /**
384 * Retrieve meta
385 *
386 * @return array|null
387 */
388 private static function _get_meta_defaults() {
389 if ( is_null( self::$meta_defaults ) ) {
390 self::$meta_defaults = array(
391 'form' => '-1',
392 'landing_page' => '',
393 'tag' => '',
394 );
395 }
396
397 return self::$meta_defaults;
398 }
399
400 /**
401 * Get selected post meta
402 *
403 * @param int $post_id Post ID.
404 * @param null $meta_key Key string to get.
405 *
406 * @return array|bool
407 */
408 private static function _get_meta( $post_id, $meta_key = null ) {
409 $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
410
411 $meta = get_post_meta( $post_id, self::POST_META_KEY, true );
412 $meta_defaults = self::_get_meta_defaults();
413
414 if ( empty( $meta ) ) {
415 $meta = $meta_defaults;
416
417 $old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) );
418 if ( 0 !== $old_value ) {
419 $meta['form'] = $old_value;
420 }
421 }
422
423 $meta = shortcode_atts( $meta_defaults, $meta );
424
425 return is_null( $meta_key ) ? $meta : ( isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : false );
426 }
427
428 /**
429 * Get plugin settings
430 *
431 * @param null $settings_key
432 * @return mixed|null|void
433 */
434 public static function _get_settings( $settings_key = null ) {
435 $settings = get_option( self::SETTINGS_NAME, self::$settings_defaults );
436
437 return is_null( $settings_key ) ? $settings : ( isset( $settings[ $settings_key ] ) ? $settings[ $settings_key ] : null);
438 }
439
440 /**
441 * Get instance of ConvertKitAPI
442 */
443 private static function _api_connect() {
444 $api_key = self::_get_settings( 'api_key' );
445 $api_secret = self::_get_settings( 'api_secret' );
446 $debug = self::_get_settings( 'debug' );
447
448 self::$api = new ConvertKit_API( $api_key, $api_secret, $debug );
449 }
450
451 /**
452 * Get instance of the CK API
453 *
454 * @return ConvertKit_API
455 */
456 public static function get_api() {
457 return self::$api;
458 }
459
460 /**
461 * Get ConvertKit API key
462 *
463 * @return mixed|null|void
464 */
465 public static function get_api_key() {
466 return self::_get_settings( 'api_key' );
467 }
468
469 /**
470 * Get ConvertKit API secret
471 *
472 * @return mixed|null|void
473 */
474 public static function get_api_secret() {
475 return self::_get_settings( 'api_secret' );
476 }
477
478 /**
479 * @return string
480 */
481 public static function get_forms_version() {
482 return self::$forms_version;
483 }
484
485 /**
486 * Get link to the plugin settings page
487 *
488 * @param array $query_args Args to add to URL.
489 * @return string
490 */
491 private static function _get_settings_page_link( $query_args = array() ) {
492 $query_args = array(
493 'page' => self::SETTINGS_PAGE_SLUG,
494 ) + $query_args;
495
496 return add_query_arg( $query_args, admin_url( 'options-general.php' ) );
497 }
498
499 /**
500 * Get default form for the post's categories.
501 * If there are multiple set the last one found will be returned.
502 *
503 * @param int $id
504 * @return int $form_id
505 */
506 private static function get_category_form( $id ) {
507 $categories = wp_get_post_categories( $id );
508
509 $form_id = 0;
510 foreach ( $categories as $category ) {
511
512 $id = get_term_meta( $category, 'ck_default_form', true );
513 if ( $id ) {
514 $form_id = $id;
515 }
516 }
517
518 return $form_id;
519 }
520
521 /**
522 * Output data to log file
523 *
524 * @param string $message String to add to log.
525 */
526 static function log( $message ) {
527
528 if ( 'on' === self::_get_settings( 'debug' ) ) {
529 $dir = CONVERTKIT_PLUGIN_PATH;
530 $handle = fopen( trailingslashit( $dir ) . 'log.txt', 'a' );
531 if ( $handle ) {
532 $time = date_i18n( 'm-d-Y @ H:i:s -' );
533 fwrite( $handle, $time . ' ' . $message . "\n" );
534 fclose( $handle );
535 }
536 }
537
538 }
539
540 /**
541 * Run version specific upgrade.
542 */
543 public static function upgrade() {
544
545 $current_version = get_option( 'convertkit_version' );
546 $convertkit_user_history_table = get_option( 'convertkit_user_history_table' );
547
548 if ( ! $current_version ) {
549 // Run 1.4.1 upgrade.
550 $settings = self::_get_settings();
551
552 if ( isset( $settings['api_key'] ) ) {
553 // Get all posts and pages to track what has been updated.
554 $posts = get_option( '_wp_convertkit_upgrade_posts' );
555
556 if ( ! $posts ) {
557
558 $args = array(
559 'post_type' => array( 'post', 'page' ),
560 'fields' => 'ids',
561 );
562
563 $result = new WP_Query( $args );
564 if ( ! is_wp_error( $result ) ) {
565 $posts = $result->posts;
566 update_option( '_wp_convertkit_upgrade_posts', $posts );
567 }
568 }
569
570 // Get form mappings.
571 $mappings = self::$api->get_resources( 'subscription_forms' );
572
573 // 1. Update global form. Set 'api_version' so this is only done once.
574 if ( ! isset( $settings['api_version'] ) ) {
575 $old_form_id = $settings['default_form'];
576 $settings['default_form'] = isset( $mappings[ $old_form_id ] ) ? $mappings[ $old_form_id ] : 0;
577 $settings['api_version'] = 'v3';
578 update_option( self::SETTINGS_NAME, $settings );
579 }
580
581 // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs
582 // Scan content for shortcode and update
583 // Remove page_id from posts array after page is updated.
584 foreach ( $posts as $key => $post_id ) {
585 $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true );
586
587 if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) {
588 $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0;
589 }
590 if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) {
591 $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0;
592 }
593 update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings );
594 unset( $posts[ $key ] );
595 update_option( '_wp_convertkit_upgrade_posts', $posts );
596 }
597
598 // Done scanning posts, upgrade complete.
599 if ( empty( $posts ) ) {
600 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
601 delete_option( '_wp_convertkit_upgrade_posts' );
602 }
603 } else {
604 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
605 } // End if().
606 } elseif ( version_compare( $current_version, '1.5.0', '<' ) || ! $convertkit_user_history_table ) {
607
608 // Create User History table
609 ConvertKit_Custom_Content::create_table();
610 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
611
612 } elseif ( version_compare( $current_version, '1.6.1', '<' ) ) {
613 // Refresh the forms meta to get new forms builder settings
614 $api_key = self::_get_settings( 'api_key' );
615 if ( ! empty( $api_key ) ) {
616 self::$api->update_resources( $api_key );
617 }
618 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
619 }// End if().
620 }
621 }
622