PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.5.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.5.4
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 2.3.3 All 194 releases
convertkit / includes / class-convertkit.php

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

567 lines 15.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 $forms = self::$api->get_resources( 'forms' );
127 $landing_pages = self::$api->get_resources( 'landing_pages' );
128
129 if ( ! empty( $forms ) || ( 'page' === $post->post_type && ! empty( $landing_pages ) ) ) {
130 add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( __CLASS__, 'display_meta_box' ), $post->post_type, 'normal' );
131 }
132 }
133
134 /**
135 * Metabox callback
136 *
137 * @param $post
138 */
139 public static function display_meta_box( $post ) {
140 $forms = self::$api->get_resources( 'forms' );
141 $landing_pages = self::$api->get_resources( 'landing_pages' );
142 $tags = self::$api->get_resources( 'tags' );
143 $tags = self::$api->get_resources( 'tags' );
144
145 $meta = self::_get_meta( $post->ID );
146 $settings_link = self::_get_settings_page_link();
147
148 include( CONVERTKIT_PLUGIN_PATH . '/views/backend/meta-boxes/meta-box.php' );
149 }
150
151 /**
152 * Save post meta callback
153 *
154 * @param int $post_id Post id.
155 * @param WP_Post $post The post.
156 */
157 public static function save_post_meta( $post_id, $post ) {
158 if ( wp_is_post_autosave( $post_id )
159 || wp_is_post_revision( $post_id )
160 || ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) // WPCS input var okay.
161 || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { // WPCS input var okay.
162 return;
163 }
164 if ( isset( $_POST['wp-convertkit'] ) ) { // WPCS input var okay.
165 $form = '';
166 if ( isset( $_POST['wp-convertkit']['form'] ) ) { // WPCS input var okay.
167 $form = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form'] ) ); // WPCS input var okay.
168 }
169 $landing_page = '';
170 if ( isset( $_POST['wp-convertkit']['landing_page'] ) ) { // WPCS input var okay.
171 $landing_page = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['landing_page'] ) ); // WPCS input var okay.
172 }
173 $tag = '';
174 if ( isset( $_POST['wp-convertkit']['tag'] ) ) { // WPCS input var okay.
175 $tag = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['tag'] ) ); // WPCS input var okay.
176 }
177
178 $meta = array(
179 'form' => $form,
180 'landing_page' => $landing_page,
181 'tag' => $tag,
182 );
183 update_post_meta( $post_id, self::POST_META_KEY, $meta );
184 }
185 }
186
187 /**
188 * Page/Post display callback
189 *
190 * @param string $content The post content.
191 * @return string
192 */
193 public static function append_form( $content ) {
194
195 if ( is_singular( array( 'post' ) ) || is_page() ) {
196
197 $attributes = self::_get_meta( get_the_ID() );
198
199 // Get post/page form setting
200 if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) {
201 $form_id = $attributes['form'];
202 } else {
203 // Get category form
204 $form_id = self::get_category_form( get_the_ID() );
205
206 if ( 0 === $form_id ) {
207 // Get global default form
208 if ( '-1' === $attributes['form'] ) {
209 $form_id = self::_get_settings( 'default_form' );
210 }
211 }
212 }
213
214 if ( 0 < $form_id ) {
215 $url = add_query_arg(
216 array(
217 'api_key' => self::_get_settings( 'api_key' ),
218 'v' => self::$forms_version,
219 ),
220 'https://forms.convertkit.com/' . $form_id . '.html'
221 );
222
223 $form_markup = self::$api->get_resource( $url );
224 $content .= $form_markup;
225 }
226 }
227
228 return $content;
229 }
230
231 /**
232 * Replace page content if a landing_page is set
233 */
234 public static function page_takeover() {
235 $queried_object = get_queried_object();
236 if ( isset( $queried_object->post_type )
237 && 'page' === $queried_object->post_type ) {
238
239 $landing_page_url = self::_get_meta( $queried_object->ID, 'landing_page' );
240
241 $landing_page = self::$api->get_resource( $landing_page_url );
242
243 if ( ! empty( $landing_page ) ) {
244 $script = WP_ConvertKit::get_ck_script();
245 $script .= "\n</head>";
246 $landing_page = str_replace( '</head>', $script, $landing_page );
247 echo $landing_page; // WPCS: XSS ok.
248 exit;
249 }
250 }
251 }
252
253 /**
254 * Generate HTML for including ConvertKit JS in a landing page.
255 *
256 * During the landing page takeover the normal WordPress enqueue routine is not run so we need to add these
257 * javascript if we want subscribers tagged correctly.
258 *
259 * @since 1.5.2
260 */
261 public static function get_ck_script() {
262 $script = "<script type='text/javascript' src='" . CONVERTKIT_PLUGIN_URL . "resources/frontend/jquery.cookie.min.js?ver=1.4.0'></script>";
263 $script .= "<script type='text/javascript' src='" . CONVERTKIT_PLUGIN_URL . 'resources/frontend/wp-convertkit.js?ver=' . CONVERTKIT_PLUGIN_VERSION . "'></script>";
264 $script .= "<script type='text/javascript'>/* <![CDATA[ */var ck_data = {\"ajaxurl\":\"" . admin_url( 'admin-ajax.php' ) . '\"};/* ]]> */</script>';
265 return $script;
266 }
267
268 /**
269 * Enqueue scripts
270 */
271 public static function enqueue_scripts() {
272 wp_enqueue_script( 'jquery-cookie', CONVERTKIT_PLUGIN_URL . 'resources/frontend/jquery.cookie.min.js', array( 'jquery' ), '1.4.0' );
273
274 wp_register_script( 'convertkit-js', CONVERTKIT_PLUGIN_URL . 'resources/frontend/wp-convertkit.js', array( 'jquery-cookie' ), CONVERTKIT_PLUGIN_VERSION );
275 wp_localize_script( 'convertkit-js', 'ck_data', array(
276 'ajaxurl' => admin_url( 'admin-ajax.php' ),
277 ) );
278 wp_enqueue_script( 'convertkit-js' );
279 }
280
281 /**
282 * Register widget.
283 */
284 public static function ck_register_widgets() {
285 register_widget( 'CK_Widget_Form' );
286 }
287
288 /**
289 * Shortcode callback
290 *
291 * @param array $attributes Shortcode attributes.
292 * @param null $content
293 * @return mixed|void
294 */
295 public static function shortcode( $attributes, $content = null ) {
296
297 if ( isset( $attributes['id'] ) ) {
298 $form_id = $attributes['id'];
299 $url = add_query_arg(
300 array(
301 'api_key' => self::_get_settings( 'api_key' ),
302 'v' => self::$forms_version,
303 ),
304 'https://forms.convertkit.com/' . $form_id . '.html'
305 );
306 } elseif ( isset( $attributes['form'] ) ) {
307 $form_id = $attributes['form'];
308 $url = add_query_arg(
309 array(
310 'k' => self::_get_settings( 'api_key' ),
311 'v' => '2',
312 ),
313 'https://api.convertkit.com/forms/' . $form_id . '/embed'
314 );
315 } else {
316 $form_id = self::_get_settings( 'default_form' );
317 $url = add_query_arg(
318 array(
319 'api_key' => self::_get_settings( 'api_key' ),
320 'v' => self::$forms_version,
321 ),
322 'https://forms.convertkit.com/' . $form_id . '.html'
323 );
324 }
325
326 if ( 0 < $form_id ) {
327 $form_markup = self::$api->get_resource( $url );
328 } else {
329 $form_markup = '';
330 }
331
332 return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes );
333 }
334
335 /**
336 * Retrieve meta
337 *
338 * @return array|null
339 */
340 private static function _get_meta_defaults() {
341 if ( is_null( self::$meta_defaults ) ) {
342 self::$meta_defaults = array(
343 'form' => '-1',
344 'landing_page' => '',
345 'tag' => '',
346 );
347 }
348
349 return self::$meta_defaults;
350 }
351
352 /**
353 * Get selected post meta
354 *
355 * @param int $post_id Post ID.
356 * @param null $meta_key Key string to get.
357 *
358 * @return array|bool
359 */
360 private static function _get_meta( $post_id, $meta_key = null ) {
361 $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
362
363 $meta = get_post_meta( $post_id, self::POST_META_KEY, true );
364 $meta_defaults = self::_get_meta_defaults();
365
366 if ( empty( $meta ) ) {
367 $meta = $meta_defaults;
368
369 $old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) );
370 if ( 0 !== $old_value ) {
371 $meta['form'] = $old_value;
372 }
373 }
374
375 $meta = shortcode_atts( $meta_defaults, $meta );
376
377 return is_null( $meta_key ) ? $meta : ( isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : false );
378 }
379
380 /**
381 * Get plugin settings
382 *
383 * @param null $settings_key
384 * @return mixed|null|void
385 */
386 public static function _get_settings( $settings_key = null ) {
387 $settings = get_option( self::SETTINGS_NAME, self::$settings_defaults );
388
389 return is_null( $settings_key ) ? $settings : ( isset( $settings[ $settings_key ] ) ? $settings[ $settings_key ] : null);
390 }
391
392 /**
393 * Get instance of ConvertKitAPI
394 */
395 private static function _api_connect() {
396 $api_key = self::_get_settings( 'api_key' );
397 $api_secret = self::_get_settings( 'api_secret' );
398 $debug = self::_get_settings( 'debug' );
399
400 self::$api = new ConvertKit_API( $api_key, $api_secret, $debug );
401 }
402
403 /**
404 * Get instance of the CK API
405 *
406 * @return ConvertKit_API
407 */
408 public static function get_api() {
409 return self::$api;
410 }
411
412 /**
413 * Get ConvertKit API key
414 *
415 * @return mixed|null|void
416 */
417 public static function get_api_key() {
418 return self::_get_settings( 'api_key' );
419 }
420
421 /**
422 * Get ConvertKit API secret
423 *
424 * @return mixed|null|void
425 */
426 public static function get_api_secret() {
427 return self::_get_settings( 'api_secret' );
428 }
429
430 /**
431 * @return string
432 */
433 public static function get_forms_version() {
434 return self::$forms_version;
435 }
436
437 /**
438 * Get link to the plugin settings page
439 *
440 * @param array $query_args Args to add to URL.
441 * @return string
442 */
443 private static function _get_settings_page_link( $query_args = array() ) {
444 $query_args = array(
445 'page' => self::SETTINGS_PAGE_SLUG,
446 ) + $query_args;
447
448 return add_query_arg( $query_args, admin_url( 'options-general.php' ) );
449 }
450
451 /**
452 * Get default form for the post's categories.
453 * If there are multiple set the last one found will be returned.
454 *
455 * @param int $id
456 * @return int $form_id
457 */
458 private static function get_category_form( $id ) {
459 $categories = wp_get_post_categories( $id );
460
461 $form_id = 0;
462 foreach ( $categories as $category ) {
463
464 $id = get_term_meta( $category, 'ck_default_form', true );
465 if ( $id ) {
466 $form_id = $id;
467 }
468 }
469
470 return $form_id;
471 }
472
473 /**
474 * Output data to log file
475 *
476 * @param string $message String to add to log.
477 */
478 static function log( $message ) {
479
480 if ( 'on' === self::_get_settings( 'debug' ) ) {
481 $dir = CONVERTKIT_PLUGIN_PATH;
482 $handle = fopen( trailingslashit( $dir ) . 'log.txt', 'a' );
483 if ( $handle ) {
484 $time = date_i18n( 'm-d-Y @ H:i:s -' );
485 fwrite( $handle, $time . ' ' . $message . "\n" );
486 fclose( $handle );
487 }
488 }
489
490 }
491
492 /**
493 * Run version specific upgrade.
494 */
495 public static function upgrade() {
496
497 $current_version = get_option( 'convertkit_version' );
498 $convertkit_user_history_table = get_option( 'convertkit_user_history_table' );
499
500 if ( ! $current_version ) {
501 // Run 1.4.1 upgrade.
502 $settings = self::_get_settings();
503
504 if ( isset( $settings['api_key'] ) ) {
505 // Get all posts and pages to track what has been updated.
506 $posts = get_option( '_wp_convertkit_upgrade_posts' );
507
508 if ( ! $posts ) {
509
510 $args = array(
511 'post_type' => array( 'post', 'page' ),
512 'fields' => 'ids',
513 );
514
515 $result = new WP_Query( $args );
516 if ( ! is_wp_error( $result ) ) {
517 $posts = $result->posts;
518 update_option( '_wp_convertkit_upgrade_posts', $posts );
519 }
520 }
521
522 // Get form mappings.
523 $mappings = self::$api->get_resources( 'subscription_forms' );
524
525 // 1. Update global form. Set 'api_version' so this is only done once.
526 if ( ! isset( $settings['api_version'] ) ) {
527 $old_form_id = $settings['default_form'];
528 $settings['default_form'] = isset( $mappings[ $old_form_id ] ) ? $mappings[ $old_form_id ] : 0;
529 $settings['api_version'] = 'v3';
530 update_option( self::SETTINGS_NAME, $settings );
531 }
532
533 // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs
534 // Scan content for shortcode and update
535 // Remove page_id from posts array after page is updated.
536 foreach ( $posts as $key => $post_id ) {
537 $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true );
538
539 if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) {
540 $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0;
541 }
542 if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) {
543 $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0;
544 }
545 update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings );
546 unset( $posts[ $key ] );
547 update_option( '_wp_convertkit_upgrade_posts', $posts );
548 }
549
550 // Done scanning posts, upgrade complete.
551 if ( empty( $posts ) ) {
552 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
553 delete_option( '_wp_convertkit_upgrade_posts' );
554 }
555 } else {
556 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
557 } // End if().
558 } elseif ( version_compare( $current_version, '1.5.0', '<' ) || ! $convertkit_user_history_table ) {
559
560 // Create User History table
561 ConvertKit_Custom_Content::create_table();
562 update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION );
563
564 } // End if().
565 }
566 }
567