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