diff --git a/admin/class-convertkit-settings.php b/admin/class-convertkit-settings.php index e59b01d88..e317faf44 100644 --- a/admin/class-convertkit-settings.php +++ b/admin/class-convertkit-settings.php @@ -179,7 +179,7 @@ public function register_sections() { public function get_tags() { check_ajax_referer( 'convertkit-tinymce', 'security' ); - $tags = $this->api->get_resources( 'tags' ); + $tags = get_option( 'convertkit_tags' ); $values = array(); foreach ( $tags as $tag ) { $values[] = array( @@ -264,9 +264,8 @@ public function add_customer_meta_fields( $user ) { * @param WP_Term $tag */ public function category_form_fields( $tag ) { - global $convertkit_settings; - $forms = $convertkit_settings->api->get_resources( 'forms' ); + $forms = get_option( 'convertkit_forms' ); $default_form = get_term_meta( $tag->term_id, 'ck_default_form', true ); echo ''; diff --git a/admin/section/class-convertkit-settings-contactform7.php b/admin/section/class-convertkit-settings-contactform7.php index bca5b4069..4e5917e51 100644 --- a/admin/section/class-convertkit-settings-contactform7.php +++ b/admin/section/class-convertkit-settings-contactform7.php @@ -72,7 +72,7 @@ public function get_cf7_forms() { */ public function register_fields() { - $forms = $this->api->get_resources( 'forms' ); + $forms = get_option( 'convertkit_forms' ); foreach ( $this->forms as $form ) { diff --git a/admin/section/class-convertkit-settings-general.php b/admin/section/class-convertkit-settings-general.php index 4868f07b5..61d83df8f 100644 --- a/admin/section/class-convertkit-settings-general.php +++ b/admin/section/class-convertkit-settings-general.php @@ -27,6 +27,7 @@ public function __construct() { * Register and add settings */ public function register_fields() { + $forms = get_option( 'convertkit_forms' ); add_settings_field( 'api_key', 'API Key', @@ -49,7 +50,7 @@ public function register_fields() { array( $this, 'default_form_callback' ), $this->settings_key, $this->name, - $this->api->get_resources( 'forms' ) + $forms ); add_settings_field( @@ -167,8 +168,9 @@ public function debug_callback() { */ public function sanitize_settings( $settings ) { - // Clear the api transient. - delete_transient( 'convertkit_get_api_response' ); + if ( isset( $settings['api_key'] ) ) { + $this->api->update_resources( $settings['api_key'] ); + } return shortcode_atts( array( 'api_key' => '', 'api_secret' => '', diff --git a/admin/section/class-convertkit-settings-wishlist.php b/admin/section/class-convertkit-settings-wishlist.php index dcd857420..40d1b1f67 100644 --- a/admin/section/class-convertkit-settings-wishlist.php +++ b/admin/section/class-convertkit-settings-wishlist.php @@ -57,8 +57,8 @@ public function get_wlm_levels() { */ public function register_fields() { - $forms = $this->api->get_resources( 'forms' ); - $tags = $this->api->get_resources( 'tags' ); + $forms = get_option( 'convertkit_forms' ); + $tags = get_option( 'convertkit_tags' ); foreach ( $this->wlm_levels as $wlm_level ) { add_settings_field( diff --git a/includes/class-ck-widget-form.php b/includes/class-ck-widget-form.php index 84df3ba9b..7a89e7a6b 100644 --- a/includes/class-ck-widget-form.php +++ b/includes/class-ck-widget-form.php @@ -129,26 +129,34 @@ public function widget( $args, $instance ) { $api = WP_ConvertKit::get_api(); $form_id = $instance['form']; - - $url = add_query_arg( array( - 'api_key' => WP_ConvertKit::get_api_key(), - 'v' => WP_ConvertKit::get_forms_version(), - ), - 'https://forms.convertkit.com/' . $form_id . '.html' - ); - - $form_markup = $api->get_resource( $url ); - - if ( $api && ! is_wp_error( $api ) ) { - ob_start(); - - $this->widget_start( $args, $instance ); - echo $form_markup; - $this->widget_end( $args ); - - $content = ob_get_clean(); - - echo $content; + $forms = get_option( 'convertkit_forms' ); + + if ( isset( $forms[ $form_id ]['uid'] ) ) { + // new form + $tag = ''; + echo $tag; + } else { + // old form + $url = add_query_arg( array( + 'api_key' => WP_ConvertKit::get_api_key(), + 'v' => WP_ConvertKit::get_forms_version(), + ), + 'https://forms.convertkit.com/' . $form_id . '.html' + ); + + $form_markup = $api->get_resource( $url ); + + if ( $api && ! is_wp_error( $api ) ) { + ob_start(); + + $this->widget_start( $args, $instance ); + echo $form_markup; + $this->widget_end( $args ); + + $content = ob_get_clean(); + + echo $content; + } } } @@ -210,7 +218,7 @@ public function form( $instance ) { name="get_field_name( $key ); ?>"> $option_value ) : ?> + value="" >

diff --git a/includes/class-convertkit-api.php b/includes/class-convertkit-api.php index 20ac64aba..10c546675 100644 --- a/includes/class-convertkit-api.php +++ b/includes/class-convertkit-api.php @@ -92,6 +92,63 @@ public function add_tag( $tag, $options ) { return $this->make_request( $request, 'POST', $args ); } + /** + * Update resources in local options table. + * + * @param string $api_key + */ + public function update_resources( $api_key ) { + + $this->api_key = $api_key; + + $forms = array(); + $landing_pages = array(); + $tags = array(); + + WP_ConvertKit::log( 'Updating resource with API key: ' . $api_key ); + // Forms and Landing Pages + $api_response = $this->_get_api_response( 'forms' ); + + if ( is_null( $api_response ) || is_wp_error( $api_response ) || isset( $api_response['error'] ) || isset( $api_response['error_message'] ) ) { + $error_message = isset( $api_response['error'] ) ? $api_response['error'] : 'unknown error'; + $error_message .= is_wp_error( $api_response ) ? $api_response->get_error_message() : ''; + WP_ConvertKit::log( 'Error contacting API: ' . $error_message ); + + $forms[0] = array( + 'id' => '-2', + 'name' => 'Error contacting API', + ); + update_option( 'convertkit_forms', $forms ); + update_option( 'convertkit_landing_pages', $landing_pages ); + update_option( 'convertkit_tags', $tags ); + + } else { + + $response = isset( $api_response['forms'] ) ? $api_response['forms'] : array(); + foreach ( $response as $form ) { + if ( isset( $form['archived'] ) && $form['archived'] ) { + continue; + } + + if ( 'hosted' === $form['type'] ) { + $landing_pages[ $form['id'] ] = $form; + } else { + $forms[ $form['id'] ] = $form; + } + } + update_option( 'convertkit_forms', $forms ); + update_option( 'convertkit_landing_pages', $landing_pages ); + + // Tags + $api_response = $this->_get_api_response( 'tags' ); + $response = isset( $api_response['tags'] ) ? $api_response['tags'] : array(); + foreach ( $response as $tag ) { + $tags[] = $tag; + } + update_option( 'convertkit_tags', $tags ); + } + } + /** * Gets a resource index * @@ -128,7 +185,8 @@ public function get_resources( $resource ) { continue; } $_resource[] = $form; - $forms[ $form['id'] ] = $form['name']; + $forms[ $form['id'] ] = $form; + } update_option( 'convertkit_forms', $forms ); } elseif ( 'landing_pages' === $resource ) { @@ -142,6 +200,7 @@ public function get_resources( $resource ) { $_resource[] = $landing_page; } } + update_option( 'convertkit_landing_pages', $_resource ); } elseif ( 'subscription_forms' === $resource ) { foreach ( $api_response as $mapping ) { if ( isset( $mapping['archived'] ) && $mapping['archived'] ) { @@ -149,11 +208,13 @@ public function get_resources( $resource ) { } $_resource[ $mapping['id'] ] = $mapping['form_id']; } + update_option( 'convertkit_subscription_forms', $_resource ); } elseif ( 'tags' === $resource ) { $response = isset( $api_response['tags'] ) ? $api_response['tags'] : array(); foreach ( $response as $tag ) { $_resource[] = $tag; } + update_option( 'convertkit_tags', $_resource ); } $this->resources[ $resource ] = $_resource; diff --git a/includes/class-convertkit.php b/includes/class-convertkit.php index ea9c169a8..6589e31f6 100644 --- a/includes/class-convertkit.php +++ b/includes/class-convertkit.php @@ -123,12 +123,7 @@ public static function add_settings_page_link( $links ) { * @param WP_Post $post The current post. */ 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', 'convertkit' ), array( __CLASS__, 'display_meta_box' ), $post->post_type, 'normal' ); - } + add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( __CLASS__, 'display_meta_box' ), $post->post_type, 'normal' ); } /** @@ -137,10 +132,10 @@ public static function add_meta_boxes( $post ) { * @param $post */ public static function display_meta_box( $post ) { - $forms = self::$api->get_resources( 'forms' ); - $landing_pages = self::$api->get_resources( 'landing_pages' ); - $tags = self::$api->get_resources( 'tags' ); - $tags = self::$api->get_resources( 'tags' ); + + $forms = get_option( 'convertkit_forms' ); + $landing_pages = get_option( 'convertkit_landing_pages' ); + $tags = get_option( 'convertkit_tags' ); $meta = self::_get_meta( $post->ID ); $settings_link = self::_get_settings_page_link(); @@ -212,16 +207,27 @@ public static function append_form( $content ) { } if ( 0 < $form_id ) { - $url = add_query_arg( - array( - 'api_key' => self::_get_settings( 'api_key' ), - 'v' => self::$forms_version, - ), - 'https://forms.convertkit.com/' . $form_id . '.html' - ); - $form_markup = self::$api->get_resource( $url ); - $content .= $form_markup; + $forms = get_option( 'convertkit_forms' ); + + if ( isset( $forms[ $form_id ]['uid'] ) ) { + // new form + $tag = ''; + $content .= $tag; + + } else { + // old form + $url = add_query_arg( + array( + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), + 'https://forms.convertkit.com/' . $form_id . '.html' + ); + + $form_markup = self::$api->get_resource( $url ); + $content .= $form_markup; + } } } @@ -296,13 +302,22 @@ public static function shortcode( $attributes, $content = null ) { if ( isset( $attributes['id'] ) ) { $form_id = $attributes['id']; - $url = add_query_arg( - array( - 'api_key' => self::_get_settings( 'api_key' ), - 'v' => self::$forms_version, - ), - 'https://forms.convertkit.com/' . $form_id . '.html' - ); + $forms = get_option( 'convertkit_forms' ); + + if ( isset( $forms[ $form_id ]['uid'] ) ) { + // new form + $form_markup = ''; + return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes ); + } else { + // old form + $url = add_query_arg( + array( + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), + 'https://forms.convertkit.com/' . $form_id . '.html' + ); + } } elseif ( isset( $attributes['form'] ) ) { $form_id = $attributes['form']; $url = add_query_arg( @@ -561,6 +576,13 @@ public static function upgrade() { ConvertKit_Custom_Content::create_table(); update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION ); - } // End if(). + } elseif ( version_compare( $current_version, '1.6.0', '<' ) ) { + // Refresh the forms meta to get new forms builder settings + $api_key = self::_get_settings( 'api_key' ); + if ( ! empty( $api_key ) ) { + self::$api->update_resources( $api_key ); + } + update_option( 'convertkit_version', CONVERTKIT_PLUGIN_VERSION ); + }// End if(). } } diff --git a/readme.txt b/readme.txt index 3372d16fc..52b3157eb 100755 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://convertkit.com Tags: email, marketing, embed form, convertkit, capture Requires at least: 3.6 Tested up to: 4.9.6 -Stable tag: 1.5.5 +Stable tag: 1.6.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -46,6 +46,12 @@ Yes, for it to work you must first have an account on ConvertKit.com == Changelog == +### 1.6.0 2018-06-30 +* Add support for new form builder +* Remove unnecessary API calls +* Store form/landing page/tag data in WP Options +* Add update routine for refreshing local convertkit data + ### 1.5.5 2018-06-01 * Fix for error in javascript added to landing pages. * Fix for applying tags based on page views on initial visit. This adds an ajax call diff --git a/wp-convertkit.php b/wp-convertkit.php index 1fd73e739..f3b52e94b 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -3,7 +3,7 @@ * Plugin Name: ConvertKit * Plugin URI: https://convertkit.com/ * Description: Quickly and easily integrate ConvertKit forms into your site. - * Version: 1.5.5 + * Version: 1.6.0 * Author: ConvertKit * Author URI: https://convertkit.com/ * Text Domain: convertkit @@ -16,7 +16,7 @@ define( 'CONVERTKIT_PLUGIN_FILE', plugin_basename( __FILE__ ) ); define( 'CONVERTKIT_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'CONVERTKIT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); -define( 'CONVERTKIT_PLUGIN_VERSION', '1.5.5' ); +define( 'CONVERTKIT_PLUGIN_VERSION', '1.6.0' ); require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit.php'; require_once CONVERTKIT_PLUGIN_PATH . '/includes/class-convertkit-api.php';