| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: ConvertKit |
| 4 |
* Plugin URI: https://convertkit.com/ |
| 5 |
* Description: Quickly and easily integrate ConvertKit forms into your site. |
| 6 |
* Version: 1.4.8 |
| 7 |
* Author: ConvertKit |
| 8 |
* Author URI: https://convertkit.com/ |
| 9 |
* Text Domain: convertkit |
| 10 |
*/ |
| 11 |
|
| 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'; |
| 16 |
|
| 17 |
if ( ! class_exists( 'WP_ConvertKit' ) ) { |
| 18 |
/** |
| 19 |
* Class WP_ConvertKit |
| 20 |
*/ |
| 21 |
class WP_ConvertKit { |
| 22 |
|
| 23 |
const VERSION = '1.4.8'; |
| 24 |
|
| 25 |
const POST_META_KEY = '_wp_convertkit_post_meta'; |
| 26 |
|
| 27 |
const SETTINGS_NAME = '_wp_convertkit_settings'; |
| 28 |
|
| 29 |
const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var ConvertKit_API |
| 33 |
*/ |
| 34 |
private static $api; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var int Data Caching |
| 38 |
*/ |
| 39 |
private static $cache_period = 0; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var null |
| 43 |
*/ |
| 44 |
private static $meta_defaults = null; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
private static $settings_defaults = array( |
| 50 |
'api_key' => '', |
| 51 |
'default_form' => 0, |
| 52 |
); |
| 53 |
|
| 54 |
/** |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
private static $forms_markup = array(); |
| 58 |
|
| 59 |
/** @var array */ |
| 60 |
private static $landing_pages_markup = array(); |
| 61 |
|
| 62 |
/** @var string */ |
| 63 |
private static $forms_version = '6'; |
| 64 |
|
| 65 |
/** |
| 66 |
* Initialize the class |
| 67 |
*/ |
| 68 |
public static function init() { |
| 69 |
self::add_actions(); |
| 70 |
self::add_filters(); |
| 71 |
self::register_shortcodes(); |
| 72 |
|
| 73 |
self::$cache_period = MINUTE_IN_SECONDS * 10; |
| 74 |
|
| 75 |
self::_api_connect(); |
| 76 |
} |
| 77 |
|
| 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'; |
| 483 |
|