| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP ConvertKit |
| 4 |
Plugin URI: http://convertkit.com/ |
| 5 |
Description: Quickly and easily integrate ConvertKit forms into your site. |
| 6 |
Version: 1.3.8 |
| 7 |
Author: ConvertKit |
| 8 |
Author URI: http://convertkit.com/ |
| 9 |
*/ |
| 10 |
|
| 11 |
require_once plugin_dir_path( __FILE__ ) . "/lib/convertkit-api.php"; |
| 12 |
require_once plugin_dir_path( __FILE__ ) . "/lib/integration/wishlist_member.php"; |
| 13 |
|
| 14 |
if(!class_exists('WP_ConvertKit')) { |
| 15 |
class WP_ConvertKit { |
| 16 |
|
| 17 |
// Plugin Version |
| 18 |
const VERSION = '1.3.8'; |
| 19 |
|
| 20 |
// DB Keys |
| 21 |
const POST_META_KEY = '_wp_convertkit_post_meta'; |
| 22 |
const SETTINGS_NAME = '_wp_convertkit_settings'; |
| 23 |
|
| 24 |
// Page Slugs |
| 25 |
const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings'; |
| 26 |
|
| 27 |
private static $api; |
| 28 |
|
| 29 |
// Data Caching |
| 30 |
private static $cache_period = 0; |
| 31 |
private static $meta_defaults = null; |
| 32 |
private static $settings_defaults = array( |
| 33 |
'api_key' => '', |
| 34 |
'default_form' => 0, |
| 35 |
); |
| 36 |
|
| 37 |
private static $forms_markup = array(); |
| 38 |
private static $landing_pages_markup = array(); |
| 39 |
|
| 40 |
public static function init() { |
| 41 |
self::add_actions(); |
| 42 |
self::add_filters(); |
| 43 |
self::register_shortcodes(); |
| 44 |
|
| 45 |
self::$cache_period = MINUTE_IN_SECONDS * 10; |
| 46 |
|
| 47 |
self::_api_connect(); |
| 48 |
} |
| 49 |
|
| 50 |
private static function add_actions() { |
| 51 |
if(is_admin()) { |
| 52 |
add_action('add_meta_boxes_page', array(__CLASS__, 'add_meta_boxes')); |
| 53 |
add_action('add_meta_boxes_post', array(__CLASS__, 'add_meta_boxes')); |
| 54 |
} else { |
| 55 |
add_action('template_redirect', array(__CLASS__, 'page_takeover')); |
| 56 |
} |
| 57 |
|
| 58 |
add_action('save_post', array(__CLASS__, 'save_post_meta'), 10, 2); |
| 59 |
} |
| 60 |
|
| 61 |
private static function add_filters() { |
| 62 |
if(!is_admin()) { |
| 63 |
add_filter('the_content', array(__CLASS__, 'append_form')); |
| 64 |
} |
| 65 |
|
| 66 |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(__CLASS__, 'add_settings_page_link')); |
| 67 |
} |
| 68 |
|
| 69 |
private static function register_shortcodes() { |
| 70 |
add_shortcode('convertkit', array(__CLASS__, 'shortcode')); |
| 71 |
} |
| 72 |
|
| 73 |
// Callbacks |
| 74 |
|
| 75 |
/// Settings Related |
| 76 |
|
| 77 |
public static function add_settings_page_link($links) { |
| 78 |
$settings_link = sprintf('<a href="%s">%s</a>', self::_get_settings_page_link(), __('Settings')); |
| 79 |
|
| 80 |
return array('settings' => $settings_link) + $links; |
| 81 |
} |
| 82 |
|
| 83 |
/// Page / Post Editing |
| 84 |
|
| 85 |
public static function add_meta_boxes($post) { |
| 86 |
$forms = self::$api->get_resources('forms'); |
| 87 |
$landing_pages = self::$api->get_resources('landing_pages'); |
| 88 |
|
| 89 |
if(!empty($forms) || ('page' === $post->post_type && !empty($landing_pages))) { |
| 90 |
add_meta_box('wp-convertkit-meta-box', __('ConvertKit'), array(__CLASS__, 'display_meta_box'), $post->post_type, 'normal'); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public static function display_meta_box($post) { |
| 95 |
$forms = self::$api->get_resources('forms'); |
| 96 |
$landing_pages = self::$api->get_resources('landing_pages'); |
| 97 |
|
| 98 |
$meta = self::_get_meta($post->ID); |
| 99 |
$settings_link = self::_get_settings_page_link(); |
| 100 |
|
| 101 |
include('views/backend/meta-boxes/meta-box.php'); |
| 102 |
} |
| 103 |
|
| 104 |
public static function save_post_meta($post_id, $post) { |
| 105 |
$data = stripslashes_deep($_POST); |
| 106 |
if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id) || !isset($data['wp-convertkit-save-meta-nonce']) || !wp_verify_nonce($data['wp-convertkit-save-meta-nonce'], 'wp-convertkit-save-meta')) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
self::_set_meta($post_id, $data['wp-convertkit']); |
| 111 |
} |
| 112 |
|
| 113 |
/// Page / Post Display |
| 114 |
|
| 115 |
public static function append_form($content) { |
| 116 |
if(is_singular(array('post')) || is_page()) { |
| 117 |
$content .= wp_convertkit_get_form_embed(self::_get_meta(get_the_ID())); |
| 118 |
} |
| 119 |
|
| 120 |
return $content; |
| 121 |
} |
| 122 |
|
| 123 |
public static function page_takeover() { |
| 124 |
$queried_object = get_queried_object(); |
| 125 |
if(isset($queried_object->post_type) && 'page' === $queried_object->post_type && ($landing_page_url = self::_get_meta($queried_object->ID, 'landing_page'))) { |
| 126 |
$landing_page = self::$api->get_resource($landing_page_url); |
| 127 |
|
| 128 |
if(!empty($landing_page)) { |
| 129 |
echo $landing_page; |
| 130 |
exit; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/// Shortcodes |
| 137 |
|
| 138 |
public static function shortcode($attributes, $content = null) { |
| 139 |
return wp_convertkit_get_form_embed($attributes); |
| 140 |
} |
| 141 |
|
| 142 |
// Data Retrieval |
| 143 |
|
| 144 |
/// Page / Post Meta Data |
| 145 |
|
| 146 |
private static function _get_meta_defaults() { |
| 147 |
if(is_null(self::$meta_defaults)) { |
| 148 |
self::$meta_defaults = array( |
| 149 |
'form' => -1, |
| 150 |
'landing_page' => '', |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
return self::$meta_defaults; |
| 155 |
} |
| 156 |
|
| 157 |
private static function _get_meta($post_id, $meta_key = null) { |
| 158 |
$post_id = empty($post_id) ? get_the_ID() : $post_id; |
| 159 |
|
| 160 |
$meta = get_post_meta($post_id, self::POST_META_KEY, true); |
| 161 |
$meta_defaults = self::_get_meta_defaults(); |
| 162 |
|
| 163 |
if(empty($meta)) { |
| 164 |
$meta = $meta_defaults; |
| 165 |
|
| 166 |
$old_value = intval(get_post_meta($post_id, '_convertkit_convertkit_form', true)); |
| 167 |
if(0 !== $old_value) { |
| 168 |
$meta['form'] = $old_value; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$meta = shortcode_atts($meta_defaults, $meta); |
| 173 |
|
| 174 |
return is_null($meta_key) ? $meta : (isset($meta[$meta_key]) ? $meta[$meta_key] : false); |
| 175 |
} |
| 176 |
|
| 177 |
private static function _set_meta($post_id, $meta) { |
| 178 |
$post_id = empty($post_id) ? get_the_ID() : $post_id; |
| 179 |
|
| 180 |
update_post_meta($post_id, self::POST_META_KEY, $meta); |
| 181 |
|
| 182 |
return $meta; |
| 183 |
} |
| 184 |
|
| 185 |
/// Settings |
| 186 |
|
| 187 |
private static function _get_settings($settings_key = null) { |
| 188 |
$settings = get_option(self::SETTINGS_NAME, self::$settings_defaults); |
| 189 |
|
| 190 |
return is_null($settings_key) ? $settings : (isset($settings[$settings_key]) ? $settings[$settings_key] : null); |
| 191 |
} |
| 192 |
|
| 193 |
/// API |
| 194 |
|
| 195 |
private static function _api_connect() { |
| 196 |
$api_key = self::_get_settings('api_key'); |
| 197 |
|
| 198 |
self::$api = new ConvertKitAPI($api_key); |
| 199 |
} |
| 200 |
|
| 201 |
// Links |
| 202 |
|
| 203 |
private static function _get_settings_page_link($query_args = array()) { |
| 204 |
$query_args = array('page' => self::SETTINGS_PAGE_SLUG) + $query_args; |
| 205 |
|
| 206 |
return add_query_arg($query_args, admin_url('options-general.php')); |
| 207 |
} |
| 208 |
|
| 209 |
// Template Tags |
| 210 |
|
| 211 |
public static function get_form_embed($attributes) { |
| 212 |
$attributes = shortcode_atts(array( |
| 213 |
'form' => -1, |
| 214 |
), $attributes); |
| 215 |
|
| 216 |
extract($attributes); |
| 217 |
|
| 218 |
$form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); |
| 219 |
$form = false; |
| 220 |
|
| 221 |
if ($form_id == 0) { |
| 222 |
return ""; |
| 223 |
} |
| 224 |
|
| 225 |
$forms_available = self::$api->get_resources('forms'); |
| 226 |
foreach($forms_available as $form_available) { |
| 227 |
if($form_available['id'] == $form_id) { |
| 228 |
$form = $form_available; |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
$form_markup = self::$api->get_resource($form['embed']); |
| 234 |
|
| 235 |
return $form_markup; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
require_once('lib/template-tags.php'); |
| 240 |
WP_ConvertKit::init(); |
| 241 |
} |
| 242 |
|
| 243 |
include 'admin/settings.php'; |
| 244 |
|