| @@ -1,142 +1,454 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * ConvertKit WordPress Plugin. | |
| 4 | - * | |
| 5 | - * @package ConvertKit | |
| 6 | - * @author ConvertKit | |
| 7 | - * | |
| 8 | - * @wordpress-plugin | |
| 9 | - * Plugin Name: ConvertKit | |
| 10 | - * Plugin URI: https://convertkit.com/ | |
| 11 | - * Description: Quickly and easily integrate ConvertKit forms into your site. | |
| 12 | - * Version: 2.2.7 | |
| 13 | - * Author: ConvertKit | |
| 14 | - * Author URI: https://convertkit.com/ | |
| 15 | - * Text Domain: convertkit | |
| 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.4.3 | |
| 7 | + Author: ConvertKit | |
| 8 | + Author URI: http://convertkit.com/ | |
| 16 | 9 | */ |
| 17 | 10 | |
| 18 | -// Bail if ConvertKit is alread loaded. | |
| 19 | -if ( class_exists( 'WP_ConvertKit' ) ) { | |
| 20 | - return; | |
| 21 | -} | |
| 11 | +require_once plugin_dir_path( __FILE__ ) . "/lib/convertkit-api.php"; | |
| 12 | +require_once plugin_dir_path( __FILE__ ) . "/lib/class-ck-widget-form.php"; | |
| 13 | +require_once plugin_dir_path( __FILE__ ) . "/lib/integration/wishlist_member.php"; | |
| 22 | 14 | |
| 23 | -// Define ConverKit Plugin paths and version number. | |
| 24 | -define( 'CONVERTKIT_PLUGIN_NAME', 'ConvertKit' ); // Used for user-agent in API class. | |
| 25 | -define( 'CONVERTKIT_PLUGIN_FILE', plugin_basename( __FILE__ ) ); | |
| 26 | -define( 'CONVERTKIT_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
| 27 | -define( 'CONVERTKIT_PLUGIN_PATH', __DIR__ ); | |
| 28 | -define( 'CONVERTKIT_PLUGIN_VERSION', '2.2.7' ); | |
| 15 | +if(!class_exists('WP_ConvertKit')) { | |
| 16 | + /** | |
| 17 | + * Class WP_ConvertKit | |
| 18 | + */ | |
| 19 | + class WP_ConvertKit { | |
| 29 | 20 | |
| 30 | -// Load shared classes, if they have not been included by another ConvertKit Plugin. | |
| 31 | -if ( ! class_exists( 'ConvertKit_API' ) ) { | |
| 32 | - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-api.php'; | |
| 33 | -} | |
| 34 | -if ( ! class_exists( 'ConvertKit_Log' ) ) { | |
| 35 | - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-log.php'; | |
| 36 | -} | |
| 37 | -if ( ! class_exists( 'ConvertKit_Resource' ) ) { | |
| 38 | - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-resource.php'; | |
| 39 | -} | |
| 40 | -if ( ! class_exists( 'ConvertKit_Review_Request' ) ) { | |
| 41 | - require_once CONVERTKIT_PLUGIN_PATH . '/vendor/convertkit/convertkit-wordpress-libraries/src/class-convertkit-review-request.php'; | |
| 42 | -} | |
| 21 | + const VERSION = '1.4.3'; | |
| 43 | 22 | |
| 44 | -// Load plugin files that are always required. | |
| 45 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/cron-functions.php'; | |
| 46 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/functions.php'; | |
| 47 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-wp-convertkit.php'; | |
| 48 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-ajax.php'; | |
| 49 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-gutenberg.php'; | |
| 50 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-output.php'; | |
| 51 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-output-restrict-content.php'; | |
| 52 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-post.php'; | |
| 53 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-preview-output.php'; | |
| 54 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-creator-network-recommendations.php'; | |
| 55 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-forms.php'; | |
| 56 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-landing-pages.php'; | |
| 57 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-posts.php'; | |
| 58 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-products.php'; | |
| 59 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-resource-tags.php'; | |
| 60 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-settings.php'; | |
| 61 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-settings-restrict-content.php'; | |
| 62 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-setup.php'; | |
| 63 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-shortcodes.php'; | |
| 64 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-subscriber.php'; | |
| 65 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-term.php'; | |
| 66 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-user.php'; | |
| 67 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-widgets.php'; | |
| 68 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block.php'; | |
| 69 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-broadcasts.php'; | |
| 70 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-content.php'; | |
| 71 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-form-trigger.php'; | |
| 72 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-form.php'; | |
| 73 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/blocks/class-convertkit-block-product.php'; | |
| 74 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter.php'; | |
| 75 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter-form-link.php'; | |
| 76 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/block-formatters/class-convertkit-block-formatter-product-link.php'; | |
| 77 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/widgets/class-ck-widget-form.php'; | |
| 23 | + const POST_META_KEY = '_wp_convertkit_post_meta'; | |
| 78 | 24 | |
| 79 | -// Contact Form 7 Integration. | |
| 80 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7.php'; | |
| 81 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7-settings.php'; | |
| 25 | + const SETTINGS_NAME = '_wp_convertkit_settings'; | |
| 82 | 26 | |
| 83 | -// Elementor Integration. | |
| 84 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/elementor/class-convertkit-elementor.php'; | |
| 27 | + const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings'; | |
| 85 | 28 | |
| 86 | -// WishList Member Integration. | |
| 87 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist.php'; | |
| 88 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist-settings.php'; | |
| 29 | + /** @var ConvertKitAPI */ | |
| 30 | + private static $api; | |
| 89 | 31 | |
| 90 | -// WooCommerce Integration. | |
| 91 | -require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/woocommerce/class-convertkit-woocommerce-product-form.php'; | |
| 32 | + /** @var int Data Caching */ | |
| 33 | + private static $cache_period = 0; | |
| 92 | 34 | |
| 93 | -// Load files that are only used in the WordPress Administration interface. | |
| 94 | -if ( is_admin() ) { | |
| 95 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-bulk-edit.php'; | |
| 96 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-quick-edit.php'; | |
| 97 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-cache-plugins.php'; | |
| 98 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-category.php'; | |
| 99 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-notices.php'; | |
| 100 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-post.php'; | |
| 101 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-refresh-resources.php'; | |
| 102 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-settings.php'; | |
| 103 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-tinymce.php'; | |
| 104 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-user.php'; | |
| 105 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-setup-wizard.php'; | |
| 106 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-multi-value-field-table.php'; | |
| 107 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-base.php'; | |
| 108 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-general.php'; | |
| 109 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/section/class-convertkit-settings-tools.php'; | |
| 110 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/setup-wizard/class-convertkit-admin-setup-wizard-plugin.php'; | |
| 35 | + /** @var null */ | |
| 36 | + private static $meta_defaults = null; | |
| 111 | 37 | |
| 112 | - // Contact Form 7 Integration. | |
| 113 | - require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/contactform7/class-convertkit-contactform7-admin-settings.php'; | |
| 38 | + /** @var array */ | |
| 39 | + private static $settings_defaults = array( | |
| 40 | + 'api_key' => '', | |
| 41 | + 'default_form' => 0, | |
| 42 | + ); | |
| 114 | 43 | |
| 115 | - // WishList Member Integration. | |
| 116 | - require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/wishlist/class-convertkit-wishlist-admin-settings.php'; | |
| 44 | + /** @var array */ | |
| 45 | + private static $forms_markup = array(); | |
| 117 | 46 | |
| 118 | - // Restrict Content Integration. | |
| 119 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-restrict-content.php'; | |
| 120 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-settings-restrict-content.php'; | |
| 121 | - require_once CONVERTKIT_PLUGIN_PATH . '/admin/setup-wizard/class-convertkit-admin-setup-wizard-restrict-content.php'; | |
| 122 | -} | |
| 47 | + /** @var array */ | |
| 48 | + private static $landing_pages_markup = array(); | |
| 123 | 49 | |
| 124 | -// Register Plugin activation and deactivation functions. | |
| 125 | -register_activation_hook( __FILE__, 'convertkit_plugin_activate' ); | |
| 126 | -add_action( 'wp_insert_site', 'convertkit_plugin_activate_new_site' ); | |
| 127 | -add_action( 'activate_blog', 'convertkit_plugin_activate_new_site' ); | |
| 128 | -register_deactivation_hook( __FILE__, 'convertkit_plugin_deactivate' ); | |
| 50 | + /** @var string */ | |
| 51 | + private static $forms_version = '6'; | |
| 129 | 52 | |
| 130 | -/** | |
| 131 | - * Main function to return Plugin instance. | |
| 132 | - * | |
| 133 | - * @since 1.9.6 | |
| 134 | - */ | |
| 135 | -function WP_ConvertKit() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName | |
| 53 | + /** | |
| 54 | + * Initialize the class | |
| 55 | + */ | |
| 56 | + public static function init() { | |
| 57 | + self::add_actions(); | |
| 58 | + self::add_filters(); | |
| 59 | + self::register_shortcodes(); | |
| 136 | 60 | |
| 137 | - return WP_ConvertKit::get_instance(); | |
| 61 | + self::$cache_period = MINUTE_IN_SECONDS * 10; | |
| 138 | 62 | |
| 63 | + self::_api_connect(); | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Add WP Actions | |
| 68 | + */ | |
| 69 | + private static function add_actions() { | |
| 70 | + if(is_admin()) { | |
| 71 | + add_action('add_meta_boxes_page', array(__CLASS__, 'add_meta_boxes')); | |
| 72 | + add_action('add_meta_boxes_post', array(__CLASS__, 'add_meta_boxes')); | |
| 73 | + } else { | |
| 74 | + add_action('template_redirect', array(__CLASS__, 'page_takeover')); | |
| 75 | + } | |
| 76 | + | |
| 77 | + add_action('widgets_init', array(__CLASS__,'ck_register_widgets')); | |
| 78 | + | |
| 79 | + add_action('save_post', array(__CLASS__, 'save_post_meta'), 10, 2); | |
| 80 | + | |
| 81 | + add_action('init', array(__CLASS__, 'upgrade') , 10); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Add WP Filters | |
| 86 | + */ | |
| 87 | + private static function add_filters() { | |
| 88 | + if(!is_admin()) { | |
| 89 | + add_filter('the_content', array(__CLASS__, 'append_form')); | |
| 90 | + } | |
| 91 | + | |
| 92 | + add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(__CLASS__, 'add_settings_page_link')); | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Register ConvertKit shortcodes | |
| 97 | + */ | |
| 98 | + private static function register_shortcodes() { | |
| 99 | + add_shortcode('convertkit', array(__CLASS__, 'shortcode')); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Plugin action links callback | |
| 104 | + * | |
| 105 | + * @param $links | |
| 106 | + * @return array | |
| 107 | + */ | |
| 108 | + public static function add_settings_page_link($links) { | |
| 109 | + $settings_link = sprintf('<a href="%s">%s</a>', self::_get_settings_page_link(), __('Settings')); | |
| 110 | + | |
| 111 | + return array('settings' => $settings_link) + $links; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Add Meta Boxes callback | |
| 116 | + * | |
| 117 | + * @param $post | |
| 118 | + */ | |
| 119 | + public static function add_meta_boxes($post) { | |
| 120 | + $forms = self::$api->get_resources('forms'); | |
| 121 | + $landing_pages = self::$api->get_resources('landing_pages'); | |
| 122 | + | |
| 123 | + if(!empty($forms) || ('page' === $post->post_type && !empty($landing_pages))) { | |
| 124 | + add_meta_box('wp-convertkit-meta-box', __('ConvertKit'), array(__CLASS__, 'display_meta_box'), $post->post_type, 'normal'); | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Metabox callback | |
| 130 | + * | |
| 131 | + * @param $post | |
| 132 | + */ | |
| 133 | + public static function display_meta_box($post) { | |
| 134 | + $forms = self::$api->get_resources('forms'); | |
| 135 | + $landing_pages = self::$api->get_resources('landing_pages'); | |
| 136 | + | |
| 137 | + $meta = self::_get_meta($post->ID); | |
| 138 | + $settings_link = self::_get_settings_page_link(); | |
| 139 | + | |
| 140 | + include('views/backend/meta-boxes/meta-box.php'); | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Save post meta callback | |
| 145 | + * | |
| 146 | + * @param $post_id | |
| 147 | + * @param $post | |
| 148 | + */ | |
| 149 | + public static function save_post_meta($post_id, $post) { | |
| 150 | + $data = stripslashes_deep($_POST); | |
| 151 | + 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')) { | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + | |
| 155 | + self::_set_meta($post_id, $data['wp-convertkit']); | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Page/Post display callback | |
| 160 | + * | |
| 161 | + * @param $content | |
| 162 | + * @return string | |
| 163 | + */ | |
| 164 | + public static function append_form( $content ) { | |
| 165 | + | |
| 166 | + if(is_singular(array('post')) || is_page()) { | |
| 167 | + | |
| 168 | + $attributes = self::_get_meta( get_the_ID() ); | |
| 169 | + | |
| 170 | + $form_id = 0; | |
| 171 | + | |
| 172 | + if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) { | |
| 173 | + $form_id = $attributes['form']; | |
| 174 | + } else { | |
| 175 | + if ( -1 == $attributes['form'] ) | |
| 176 | + $form_id = self::_get_settings('default_form'); | |
| 177 | + } | |
| 178 | + | |
| 179 | + if ( 0 < $form_id ) { | |
| 180 | + $url = add_query_arg( array( | |
| 181 | + 'api_key' => self::_get_settings( 'api_key' ), | |
| 182 | + 'v' => self::$forms_version, | |
| 183 | + ), | |
| 184 | + 'https://forms.convertkit.com/' . $form_id . '.html' | |
| 185 | + ); | |
| 186 | + | |
| 187 | + $form_markup = self::$api->get_resource( $url ); | |
| 188 | + $content .= $form_markup; | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + return $content; | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Replace page content if a landing_page is set | |
| 197 | + */ | |
| 198 | + public static function page_takeover() { | |
| 199 | + $queried_object = get_queried_object(); | |
| 200 | + if(isset($queried_object->post_type) | |
| 201 | + && 'page' === $queried_object->post_type | |
| 202 | + && ($landing_page_url = self::_get_meta($queried_object->ID, 'landing_page'))) { | |
| 203 | + $landing_page = self::$api->get_resource($landing_page_url); | |
| 204 | + | |
| 205 | + if(!empty($landing_page)) { | |
| 206 | + echo $landing_page; | |
| 207 | + exit; | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Register widget. | |
| 215 | + */ | |
| 216 | + public static function ck_register_widgets(){ | |
| 217 | + register_widget( 'CK_Widget_Form' ); | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Shortcode callback | |
| 222 | + * | |
| 223 | + * @param $attributes | |
| 224 | + * @param null $content | |
| 225 | + * @return mixed|void | |
| 226 | + */ | |
| 227 | + public static function shortcode($attributes, $content = null) { | |
| 228 | + | |
| 229 | + if ( isset( $attributes['id'] ) ) { | |
| 230 | + $form_id = $attributes['id']; | |
| 231 | + $url = add_query_arg( array( | |
| 232 | + 'api_key' => self::_get_settings( 'api_key' ), | |
| 233 | + 'v' => self::$forms_version, | |
| 234 | + ), | |
| 235 | + 'https://forms.convertkit.com/' . $form_id . '.html' | |
| 236 | + ); | |
| 237 | + } elseif ( isset( $attributes['form'] ) ) { | |
| 238 | + $form_id = $attributes['form']; | |
| 239 | + $url = add_query_arg( array( | |
| 240 | + 'k' => self::_get_settings( 'api_key' ), | |
| 241 | + 'v' => '2', | |
| 242 | + ), | |
| 243 | + 'https://api.convertkit.com/forms/' . $form_id . '/embed' | |
| 244 | + ); | |
| 245 | + } else { | |
| 246 | + $form_id = self::_get_settings( 'default_form' ); | |
| 247 | + $url = add_query_arg( array( | |
| 248 | + 'api_key' => self::_get_settings( 'api_key' ), | |
| 249 | + 'v' => self::$forms_version, | |
| 250 | + ), | |
| 251 | + 'https://forms.convertkit.com/' . $form_id . '.html' | |
| 252 | + ); | |
| 253 | + } | |
| 254 | + | |
| 255 | + if ( 0 < $form_id ) { | |
| 256 | + $form_markup = self::$api->get_resource( $url ); | |
| 257 | + } else { | |
| 258 | + $form_markup = ''; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return apply_filters('wp_convertkit_get_form_embed', $form_markup, $attributes ); | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Retrieve meta | |
| 266 | + * | |
| 267 | + * @return array|null | |
| 268 | + */ | |
| 269 | + private static function _get_meta_defaults() { | |
| 270 | + if(is_null(self::$meta_defaults)) { | |
| 271 | + self::$meta_defaults = array( | |
| 272 | + 'form' => -1, | |
| 273 | + 'landing_page' => '', | |
| 274 | + ); | |
| 275 | + } | |
| 276 | + | |
| 277 | + return self::$meta_defaults; | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * @param $post_id | |
| 282 | + * @param null $meta_key | |
| 283 | + * | |
| 284 | + * @return array|bool | |
| 285 | + */ | |
| 286 | + private static function _get_meta($post_id, $meta_key = null) { | |
| 287 | + $post_id = empty($post_id) ? get_the_ID() : $post_id; | |
| 288 | + | |
| 289 | + $meta = get_post_meta($post_id, self::POST_META_KEY, true); | |
| 290 | + $meta_defaults = self::_get_meta_defaults(); | |
| 291 | + | |
| 292 | + if(empty($meta)) { | |
| 293 | + $meta = $meta_defaults; | |
| 294 | + | |
| 295 | + $old_value = intval(get_post_meta($post_id, '_convertkit_convertkit_form', true)); | |
| 296 | + if(0 !== $old_value) { | |
| 297 | + $meta['form'] = $old_value; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + $meta = shortcode_atts($meta_defaults, $meta); | |
| 302 | + | |
| 303 | + return is_null($meta_key) ? $meta : (isset($meta[$meta_key]) ? $meta[$meta_key] : false); | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * @param $post_id | |
| 308 | + * @param $meta | |
| 309 | + * | |
| 310 | + * @return mixed | |
| 311 | + */ | |
| 312 | + private static function _set_meta($post_id, $meta) { | |
| 313 | + $post_id = empty($post_id) ? get_the_ID() : $post_id; | |
| 314 | + | |
| 315 | + update_post_meta($post_id, self::POST_META_KEY, $meta); | |
| 316 | + | |
| 317 | + return $meta; | |
| 318 | + } | |
| 319 | + | |
| 320 | + /** | |
| 321 | + * Get plugin settings | |
| 322 | + * | |
| 323 | + * @param null $settings_key | |
| 324 | + * @return mixed|null|void | |
| 325 | + */ | |
| 326 | + private static function _get_settings($settings_key = null) { | |
| 327 | + $settings = get_option(self::SETTINGS_NAME, self::$settings_defaults); | |
| 328 | + | |
| 329 | + return is_null($settings_key) ? $settings : (isset($settings[$settings_key]) ? $settings[$settings_key] : null); | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** | |
| 333 | + * Get instance of ConvertKitAPI | |
| 334 | + */ | |
| 335 | + private static function _api_connect() { | |
| 336 | + $api_key = self::_get_settings('api_key'); | |
| 337 | + $api_secret = self::_get_settings('api_secret'); | |
| 338 | + $debug = self::_get_settings('debug'); | |
| 339 | + | |
| 340 | + self::$api = new ConvertKitAPI($api_key,$api_secret,$debug); | |
| 341 | + } | |
| 342 | + | |
| 343 | + /** | |
| 344 | + * Get instance of the CK API | |
| 345 | + * | |
| 346 | + * @return ConvertKitAPI | |
| 347 | + */ | |
| 348 | + public static function get_api(){ | |
| 349 | + return self::$api; | |
| 350 | + } | |
| 351 | + | |
| 352 | + public static function get_api_key(){ | |
| 353 | + return self::_get_settings( 'api_key' ); | |
| 354 | + } | |
| 355 | + | |
| 356 | + public static function get_forms_version(){ | |
| 357 | + return self::$forms_version; | |
| 358 | + } | |
| 359 | + | |
| 360 | + | |
| 361 | + /** | |
| 362 | + * Get link to the plugin settings page | |
| 363 | + * | |
| 364 | + * @param array $query_args | |
| 365 | + * @return string | |
| 366 | + */ | |
| 367 | + private static function _get_settings_page_link($query_args = array()) { | |
| 368 | + $query_args = array('page' => self::SETTINGS_PAGE_SLUG) + $query_args; | |
| 369 | + | |
| 370 | + return add_query_arg($query_args, admin_url('options-general.php')); | |
| 371 | + } | |
| 372 | + | |
| 373 | + | |
| 374 | + /** | |
| 375 | + * Run version specific upgrade. | |
| 376 | + */ | |
| 377 | + public static function upgrade() { | |
| 378 | + | |
| 379 | + $current_version = get_option( 'convertkit_version' ); | |
| 380 | + | |
| 381 | + if ( ! $current_version) { | |
| 382 | + | |
| 383 | + // Run 1.4.1 upgrade | |
| 384 | + $settings = self::_get_settings( ); | |
| 385 | + | |
| 386 | + if ( isset( $settings['api_key'] ) ) { | |
| 387 | + | |
| 388 | + // Get all posts and pages to track what has been updated | |
| 389 | + $posts = get_option( '_wp_convertkit_upgrade_posts' ); | |
| 390 | + | |
| 391 | + if ( ! $posts ) { | |
| 392 | + | |
| 393 | + $args = array( | |
| 394 | + 'post_type' => array( 'post', 'page' ), | |
| 395 | + 'fields' => 'ids', | |
| 396 | + 'posts_per_page' => -1, | |
| 397 | + ); | |
| 398 | + | |
| 399 | + $result = new WP_Query( $args ); | |
| 400 | + if (! is_wp_error( $result ) ) { | |
| 401 | + $posts = $result->posts; | |
| 402 | + update_option( '_wp_convertkit_upgrade_posts', $posts ); | |
| 403 | + } | |
| 404 | + } | |
| 405 | + | |
| 406 | + // Get form mappings | |
| 407 | + $mappings = self::$api->get_resources('subscription_forms');; | |
| 408 | + | |
| 409 | + // 1. Update global form. Set 'api_version' so this is only done once. | |
| 410 | + if ( ! isset( $settings['api_version'] ) ) { | |
| 411 | + $old_form_id = $settings['default_form']; | |
| 412 | + $settings['default_form'] = isset( $mappings[ $old_form_id ] ) ? $mappings[ $old_form_id ] : 0; | |
| 413 | + $settings['api_version'] = 'v3'; | |
| 414 | + update_option( self::SETTINGS_NAME, $settings ); | |
| 415 | + } | |
| 416 | + | |
| 417 | + // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs | |
| 418 | + // Scan content for shortcode and update | |
| 419 | + // Remove page_id from posts array after page is updated. | |
| 420 | + foreach ( $posts as $key => $post_id ) { | |
| 421 | + $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true ); | |
| 422 | + | |
| 423 | + if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) | |
| 424 | + $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; | |
| 425 | + if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) | |
| 426 | + $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0; | |
| 427 | + | |
| 428 | + update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings ); | |
| 429 | + | |
| 430 | + unset($posts[ $key ]); | |
| 431 | + update_option( '_wp_convertkit_upgrade_posts', $posts ); | |
| 432 | + } | |
| 433 | + | |
| 434 | + // Done scanning posts, upgrade complete. | |
| 435 | + if ( empty( $posts ) ) { | |
| 436 | + update_option( 'convertkit_version', self::VERSION ); | |
| 437 | + delete_option( '_wp_convertkit_upgrade_posts' ); | |
| 438 | + } | |
| 439 | + | |
| 440 | + } else { | |
| 441 | + | |
| 442 | + update_option( 'convertkit_version', self::VERSION ); | |
| 443 | + | |
| 444 | + } | |
| 445 | + | |
| 446 | + } | |
| 447 | + | |
| 448 | + } | |
| 449 | + } | |
| 450 | + | |
| 451 | + WP_ConvertKit::init(); | |
| 139 | 452 | } |
| 140 | 453 | |
| 141 | -// Finally, initialize the Plugin. | |
| 142 | -WP_ConvertKit(); | |
| 454 | +include 'admin/settings.php'; | |