Skip to content

Commit

Permalink
Merge pull request #113 from ConvertKit/form-uid
Browse files Browse the repository at this point in the history
Version 1.6.0
  • Loading branch information
growdev authored Jun 30, 2018
2 parents d521fc9 + e3d1d7b commit 79addd8
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 61 deletions.
5 changes: 2 additions & 3 deletions admin/class-convertkit-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 '<tr class="form-field term-description-wrap"><th scope="row"><label for="description">ConvertKit Form</label></th><td>';
Expand Down
2 changes: 1 addition & 1 deletion admin/section/class-convertkit-settings-contactform7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down
8 changes: 5 additions & 3 deletions admin/section/class-convertkit-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down Expand Up @@ -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' => '',
Expand Down
4 changes: 2 additions & 2 deletions admin/section/class-convertkit-settings-wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
50 changes: 29 additions & 21 deletions includes/class-ck-widget-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
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;
}
}
}

Expand Down Expand Up @@ -210,7 +218,7 @@ public function form( $instance ) {
name="<?php echo $this->get_field_name( $key ); ?>">
<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
<option
value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value['name'] ); ?></option>
<?php endforeach; ?>
</select>
</p>
Expand Down
63 changes: 62 additions & 1 deletion includes/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 ) {
Expand All @@ -142,18 +200,21 @@ 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'] ) {
continue;
}
$_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;
Expand Down
76 changes: 49 additions & 27 deletions includes/class-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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 = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
$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;
}
}
}

Expand Down Expand Up @@ -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 = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
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(
Expand Down Expand Up @@ -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().
}
}
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down

0 comments on commit 79addd8

Please sign in to comment.