From 1af4d458e4c9ab0bf1dc2915ac6874b30599c34d Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 17 May 2016 10:55:47 -0500 Subject: [PATCH 01/17] Adding upgrade function. --- .gitignore | 1 + wp-convertkit.php | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7c9386602..3c9581cc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ convertkit-svn node_modules/ .env +.DS_Store diff --git a/wp-convertkit.php b/wp-convertkit.php index ed97516ee..df361f4a7 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -3,7 +3,7 @@ Plugin Name: WP ConvertKit Plugin URI: http://convertkit.com/ Description: Quickly and easily integrate ConvertKit forms into your site. - Version: 1.4.0 + Version: 1.4.1 Author: ConvertKit Author URI: http://convertkit.com/ */ @@ -17,7 +17,7 @@ */ class WP_ConvertKit { - const VERSION = '1.4.0'; + const VERSION = '1.4.1'; const POST_META_KEY = '_wp_convertkit_post_meta'; @@ -74,6 +74,8 @@ private static function add_actions() { } add_action('save_post', array(__CLASS__, 'save_post_meta'), 10, 2); + + add_action('init', array(__CLASS__, 'upgrade') , 10); } /** @@ -320,6 +322,24 @@ public static function get_form_embed($attributes) { return $form_markup; } + + /** + * Run version specific upgrade. + */ + public static function upgrade() { + + $current_version = get_option( 'convertkit_version' ); + + if ( ! $current_version) { + + $settings = self::_get_settings( ); + $settings['default_form'] = ''; + update_option( self::SETTINGS_NAME, $settings ); + update_option( 'convertkit_version', self::VERSION ); + error_log( "AGAIN???" ); + } + + } } require_once('lib/template-tags.php'); From 80bffe910d2f798d1d65acf11b80b5c5eb045ff8 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 17 May 2016 10:56:21 -0500 Subject: [PATCH 02/17] Remove debug statement :) --- wp-convertkit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index df361f4a7..e6b8ebc5c 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -336,7 +336,7 @@ public static function upgrade() { $settings['default_form'] = ''; update_option( self::SETTINGS_NAME, $settings ); update_option( 'convertkit_version', self::VERSION ); - error_log( "AGAIN???" ); + } } From 705f2afc348957de39f4156a5717fa12a8d08492 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 17 May 2016 11:40:43 -0500 Subject: [PATCH 03/17] If form_id is not found at api return. This removes the error:notfound message from content --- wp-convertkit.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wp-convertkit.php b/wp-convertkit.php index e6b8ebc5c..e0ccdaa6e 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -311,6 +311,10 @@ public static function get_form_embed($attributes) { } } + // The Form ID is not found + if ( ! $form ) + return; + $url = add_query_arg( array( 'api_key' => self::_get_settings('api_key'), 'v' => self::$forms_version, From bf8056f14e29a0aaddab573ce4d6a6bab215bcdd Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 24 May 2016 13:28:31 -0500 Subject: [PATCH 04/17] Adding to 1.4.1 upgrade routine. --- lib/convertkit-api.php | 27 +++++++++++++----- wp-convertkit.php | 64 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index dcbc99b68..2c7931da4 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -15,7 +15,7 @@ class ConvertKitAPI { protected $api_version = 'v3'; /** @var string */ - protected $api_url_base = 'https://api.convertkit.com/'; + protected $api_url_base = 'http://api.blue.convertkit.com/'; /** @var array */ protected $resources = array(); @@ -45,26 +45,39 @@ public function __construct($api_key, $api_secret) { public function get_resources($resource) { if(!array_key_exists($resource, $this->resources)) { - // v3 only has 'forms' resource. - $api_response = $this->_get_api_response('forms'); + + if ( $resource == 'landing_pages' ) { + $api_response = $this->_get_api_response( 'forms' ); + } else { + $api_response = $this->_get_api_response( $resource ); + } if (is_null($api_response) || is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) { $this->resources[$resource] = array(); } else { $_resource = array(); + // v3 doesn't have landing_pages resource. Instead check 'type' for 'hosted' if ( 'forms' == $resource ) { - foreach ( $api_response as $form ){ + + $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); + foreach( $response as $form ){ if ( 'embed' == $form['type'] ){ $_resource[] = $form; } } } elseif ( 'landing_pages' == $resource ) { - foreach ( $api_response as $landing_page ){ + + $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); + foreach( $response as $landing_page ){ if ( 'hosted' == $landing_page['type'] ){ $_resource[] = $landing_page; } } + } elseif ( 'subscription_forms' == $resource ) { + foreach( $api_response as $mapping ){ + $_resource[ $mapping['id'] ] = $mapping['form_id']; + } } $this->resources[$resource] = $_resource; @@ -173,7 +186,7 @@ private function _get_api_response($path = '') { $args = array('api_key' => $this->api_key); $api_path = $this->api_url_base . $this->api_version; $url = add_query_arg($args, path_join($api_path, $path)); - $response = wp_remote_get($url, array( 'timeout' => 2 )); + $response = wp_remote_get($url, array( 'timeout' => 2, 'sslverify' => false)); if(is_wp_error($response)) { return array(); @@ -181,7 +194,7 @@ private function _get_api_response($path = '') { $data = json_decode(wp_remote_retrieve_body($response), true); } - return isset($data[$path]) ? $data[$path] : array(); + return $data; } /** diff --git a/wp-convertkit.php b/wp-convertkit.php index e0ccdaa6e..23892e81a 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -336,11 +336,67 @@ public static function upgrade() { if ( ! $current_version) { + // Run 1.4.1 upgrade $settings = self::_get_settings( ); - $settings['default_form'] = ''; - update_option( self::SETTINGS_NAME, $settings ); - update_option( 'convertkit_version', self::VERSION ); - + + if ( isset( $settings['api_key'] ) ) { + + // Get all posts and pages to track what has been updated + $posts = get_option( '_wp_convertkit_upgrade_posts' ); + + if ( ! $posts ) { + error_log( " no posts, so getting a list"); + + $args = array( + 'post_type' => array( 'post', 'page' ), + 'fields' => 'ids', + ); + + $result = new WP_Query( $args ); + if (! is_wp_error( $result ) ) { + $posts = $result->posts; + update_option( '_wp_convertkit_upgrade_posts', $posts ); + } + } + + // Get form mappings + $mappings = self::$api->get_resources('subscription_forms');; + + // 1. Update global form. Set 'api_version' so this is only done once. + if ( ! isset( $settings['api_version'] ) ) { + $old_form_id = $settings['default_form']; + $settings['default_form'] = isset( $mappings[ $old_form_id ] ) ? $mappings[ $old_form_id ] : 0; + $settings['api_version'] = 'v3'; + update_option( self::SETTINGS_NAME, $settings ); + } + + // 2. Scan posts/pages for _wp_convertkit_post_meta and update IDs + // Scan content for shortcode and update + // Remove page_id from posts array after page is updated. + foreach ( $posts as $key => $post_id ) { + $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true ); + + if ( isset( $post_settings['form'] ) ) + $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; + if ( isset( $post_settings['landing_page'] ) ) + $post_settings['landing_page'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; + + update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings ); + + unset($posts[ $key ]); + update_option( '_wp_convertkit_upgrade_posts', $posts ); + } + + // Done scanning posts, upgrade complete. + if ( empty( $posts ) ) + update_option( 'convertkit_version', self::VERSION ); + + } else { + + update_option( 'convertkit_version', self::VERSION ); + + } + } } From 417a6aaf25921f0b3ecbd5725f1757d701204e5a Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 24 May 2016 13:53:03 -0500 Subject: [PATCH 05/17] Fix landing page mapping. --- wp-convertkit.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index 23892e81a..9977a7961 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -345,7 +345,6 @@ public static function upgrade() { $posts = get_option( '_wp_convertkit_upgrade_posts' ); if ( ! $posts ) { - error_log( " no posts, so getting a list"); $args = array( 'post_type' => array( 'post', 'page' ), @@ -376,10 +375,10 @@ public static function upgrade() { foreach ( $posts as $key => $post_id ) { $post_settings = get_post_meta( $post_id, '_wp_convertkit_post_meta', true ); - if ( isset( $post_settings['form'] ) ) + if ( isset( $post_settings['form'] ) && ( 0 < $post_settings['form'] ) ) $post_settings['form'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; - if ( isset( $post_settings['landing_page'] ) ) - $post_settings['landing_page'] = isset( $mappings[ $post_settings['form'] ] ) ? $mappings[ $post_settings['form'] ] : 0; + if ( isset( $post_settings['landing_page'] ) && ( 0 < $post_settings['landing_page'] ) ) + $post_settings['landing_page'] = isset( $mappings[ $post_settings['landing_page'] ] ) ? $mappings[ $post_settings['landing_page'] ] : 0; update_post_meta( $post_id, '_wp_convertkit_post_meta', $post_settings ); From 66b6e5b2640e79c8aa83615cdebeace7f0452a90 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 24 May 2016 14:51:13 -0500 Subject: [PATCH 06/17] Updating Forms droopdown to show all forms including hosted --- lib/convertkit-api.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index 2c7931da4..6efa3424a 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -57,15 +57,10 @@ public function get_resources($resource) { } else { $_resource = array(); - // v3 doesn't have landing_pages resource. Instead check 'type' for 'hosted' if ( 'forms' == $resource ) { - $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); - foreach( $response as $form ){ - if ( 'embed' == $form['type'] ){ - $_resource[] = $form; - } - } + $_resource[] = $response; + } elseif ( 'landing_pages' == $resource ) { $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); From 326a7f90d9b8460240c368433709b5eb3501e0f6 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Wed, 25 May 2016 10:39:18 -0500 Subject: [PATCH 07/17] updating api url back to live. --- lib/convertkit-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index 6efa3424a..913641461 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -15,7 +15,7 @@ class ConvertKitAPI { protected $api_version = 'v3'; /** @var string */ - protected $api_url_base = 'http://api.blue.convertkit.com/'; + protected $api_url_base = 'http://api.convertkit.com/'; /** @var array */ protected $resources = array(); From af1b430be6d2a48ff43ece40689a4a07872224bf Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Mon, 6 Jun 2016 13:02:08 -0500 Subject: [PATCH 08/17] Updates to WP_ConvertKit::get_form_embed() to try to preserve back compat for shrotcodes using form attribute. --- lib/convertkit-api.php | 5 ++-- wp-convertkit.php | 67 +++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index 913641461..aab638283 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -59,8 +59,9 @@ public function get_resources($resource) { if ( 'forms' == $resource ) { $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); - $_resource[] = $response; - + foreach( $response as $form ) { + $_resource[] = $form; + } } elseif ( 'landing_pages' == $resource ) { $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); diff --git a/wp-convertkit.php b/wp-convertkit.php index 9977a7961..1ccb1af44 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -285,46 +285,65 @@ private static function _get_settings_page_link($query_args = array()) { /** * Retrieve hosted form markup form the API and format + * Supports attribute `form` for V2 of the API + * and `id` for V3 of the API * * @param $attributes * @return string */ public static function get_form_embed($attributes) { - $attributes = shortcode_atts(array( - 'form' => -1, - ), $attributes); - $form = $attributes['form']; + $attributes = shortcode_atts( array( + 'form' => -1, + ), $attributes ); - $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); $form = false; - if ($form_id == 0) { - return ""; - } + if ( isset( $attributes['id'] ) ){ + // There is an 'id' attribute so use v3 api to retrieve. - $forms_available = self::$api->get_resources('forms'); - foreach($forms_available as $form_available) { - if($form_available['id'] == $form_id) { - $form = $form_available; - break; + $forms_available = self::$api->get_resources( 'forms' ); + foreach ( $forms_available as $form_available ) { + if ( $form_available['id'] == $attributes['id'] ) { + $form = $form_available; + break; + } } - } - // The Form ID is not found - if ( ! $form ) - return; + // TODO make v3 api call and return form markup + + + } else { - $url = add_query_arg( array( + $form = $attributes['form']; + $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); + + // TODO add check for default_form which will be using V3 ID? + + $form = false; + if ($form_id == 0) { + return ""; + } + + // TODO get_resources uses V3 so will this work with a V2 form_id? + $forms_available = self::$api->get_resources('forms'); + foreach($forms_available as $form_available) { + if($form_available['id'] == $form_id) { + $form = $form_available; + break; + } + } + $url = add_query_arg( array( 'api_key' => self::_get_settings('api_key'), - 'v' => self::$forms_version, - ), - 'https://forms.convertkit.com/' . $form['id'] . '.html' - ); + 'v' => '2', + ), + 'https://forms.convertkit.com/' . $form['id'] . '.html' + ); + $form_markup = self::$api->get_resource( $url ); + return $form_markup; - $form_markup = self::$api->get_resource( $url ); + } - return $form_markup; } /** From b55eb8fdaca880ead98e310d8c9c148c956cf078 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Mon, 13 Jun 2016 17:00:23 -0500 Subject: [PATCH 09/17] Removing helper function wp_convertkit_get_form_embed() --- wp-convertkit.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index 1ccb1af44..bbc96dd8c 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -160,7 +160,9 @@ public static function save_post_meta($post_id, $post) { */ 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())); + $attributes = self::_get_meta(get_the_ID()); + $form = apply_filters('wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed($attributes), $attributes); + $content .= $form; } return $content; @@ -190,7 +192,10 @@ public static function page_takeover() { * @return mixed|void */ public static function shortcode($attributes, $content = null) { - return wp_convertkit_get_form_embed($attributes); + + $form = apply_filters('wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed($attributes), $attributes); + + return $form; } /** @@ -420,7 +425,6 @@ public static function upgrade() { } } - require_once('lib/template-tags.php'); WP_ConvertKit::init(); } From f7562326ad91bbdd08ebc94ebbd6d580898a28fc Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Wed, 15 Jun 2016 15:44:23 -0500 Subject: [PATCH 10/17] Refactored append_form to not call API. #23 --- wp-convertkit.php | 75 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index bbc96dd8c..c45a78fd8 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -158,11 +158,29 @@ public static function save_post_meta($post_id, $post) { * @param $content * @return string */ - public static function append_form($content) { + public static function append_form( $content ) { + if(is_singular(array('post')) || is_page()) { - $attributes = self::_get_meta(get_the_ID()); - $form = apply_filters('wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed($attributes), $attributes); - $content .= $form; + + $attributes = self::_get_meta( get_the_ID() ); + + if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) { + $form_id = $attributes['form']; + } else { + $form_id = self::_get_settings('default_form'); + } + + 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; + } } return $content; @@ -173,7 +191,9 @@ public static function append_form($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'))) { + 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)) { @@ -296,27 +316,54 @@ private static function _get_settings_page_link($query_args = array()) { * @param $attributes * @return string */ - public static function get_form_embed($attributes) { + public static function get_form_embed( $attributes ) { - $attributes = shortcode_atts( array( - 'form' => -1, - ), $attributes ); + //$attributes = shortcode_atts( array( + // 'form' => -1, + //), $attributes ); - $form = false; + if ( isset( $attributes['id'] ) ) { - if ( isset( $attributes['id'] ) ){ // There is an 'id' attribute so use v3 api to retrieve. - $forms_available = self::$api->get_resources( 'forms' ); foreach ( $forms_available as $form_available ) { if ( $form_available['id'] == $attributes['id'] ) { - $form = $form_available; + $url = $form_available['embed_url']; break; } } - // TODO make v3 api call and return form markup + $form_markup = self::$api->get_resource( $url ); + return $form_markup; + + } elseif ( isset( $attributes['form'] ) ) { + // TODO add v2 api call + $form = $attributes['form']; + $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); + + // TODO add check for default_form which will be using V3 ID? + + $form = false; + if ($form_id == 0) { + return ""; + } + // TODO get_resources uses V3 so will this work with a V2 form_id? + $forms_available = self::$api->get_resources('forms'); + foreach($forms_available as $form_available) { + if($form_available['id'] == $form_id) { + $form = $form_available; + break; + } + } + $url = add_query_arg( array( + 'api_key' => self::_get_settings('api_key'), + 'v' => '2', + ), + 'https://forms.convertkit.com/' . $form['id'] . '.html' + ); + $form_markup = self::$api->get_resource( $url ); + return $form_markup; } else { From 005b4fbd10ea2286595aa3904fd1876d01afdf6d Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Wed, 15 Jun 2016 15:45:46 -0500 Subject: [PATCH 11/17] Changed api_secret to type text. #22 --- admin/section/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/section/general.php b/admin/section/general.php index 22246dedc..c9c40696f 100644 --- a/admin/section/general.php +++ b/admin/section/general.php @@ -87,7 +87,7 @@ public function api_key_callback() { */ public function api_secret_callback() { $html = sprintf( - '', + '', $this->settings_key, isset($this->options['api_secret']) ? esc_attr($this->options['api_secret']) : '' ); From 8e63557a7a4eeb248e2950073cc912e05fec536b Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Thu, 16 Jun 2016 11:01:07 -0500 Subject: [PATCH 12/17] Reworking how the shortcode callback works. Removing API call. --- wp-convertkit.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index c45a78fd8..0f3449f75 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -213,9 +213,39 @@ public static function page_takeover() { */ public static function shortcode($attributes, $content = null) { - $form = apply_filters('wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed($attributes), $attributes); + 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' + ); + } elseif ( isset( $attributes['form'] ) ) { + $form_id = $attributes['form']; + $url = add_query_arg( array( + 'k' => self::_get_settings( 'api_key' ), + 'v' => '2', + ), + 'https://api.convertkit.com/forms/' . $form_id . '/embed' + ); + } else { + $form_id = self::_get_settings( 'default_form' ); + $url = add_query_arg( array( + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), + 'https://forms.convertkit.com/' . $form_id . '.html' + ); + } + + if ( 0 < $form_id ) { + $form_markup = self::$api->get_resource( $url ); + } else { + $form_markup = ''; + } - return $form; + return apply_filters('wp_convertkit_get_form_embed', $form_markup, $attributes ); } /** From 456c7812a823cfa9500560c72e9e30c8dd78d93f Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Thu, 16 Jun 2016 11:01:50 -0500 Subject: [PATCH 13/17] Increasing landing page and api timeout to 10 seconds. --- lib/convertkit-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index aab638283..5d264a96d 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -130,7 +130,7 @@ public function get_resource($url) { if(!empty($url) && isset($this->markup[$url])) { $resource = $this->markup[$url]; } else if(!empty($url)) { - $response = wp_remote_get($url, array( 'timeout' => 2 )); + $response = wp_remote_get($url, array( 'timeout' => 10 )); if(!is_wp_error($response)) { if(!function_exists('str_get_html')) { @@ -182,7 +182,7 @@ private function _get_api_response($path = '') { $args = array('api_key' => $this->api_key); $api_path = $this->api_url_base . $this->api_version; $url = add_query_arg($args, path_join($api_path, $path)); - $response = wp_remote_get($url, array( 'timeout' => 2, 'sslverify' => false)); + $response = wp_remote_get($url, array( 'timeout' => 10, 'sslverify' => false)); if(is_wp_error($response)) { return array(); From a0ea57aad30107765d3a163b5827b343a22a341b Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Thu, 16 Jun 2016 12:07:54 -0500 Subject: [PATCH 14/17] Removing unused get_form_embed() method. --- wp-convertkit.php | 94 ++--------------------------------------------- 1 file changed, 4 insertions(+), 90 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index 0f3449f75..c2d8a4ba0 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -164,9 +164,12 @@ public static function append_form( $content ) { $attributes = self::_get_meta( get_the_ID() ); + $form_id = 0; + if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) { $form_id = $attributes['form']; } else { + if ( -1 == $attributes['form'] ) $form_id = self::_get_settings('default_form'); } @@ -338,96 +341,7 @@ private static function _get_settings_page_link($query_args = array()) { return add_query_arg($query_args, admin_url('options-general.php')); } - /** - * Retrieve hosted form markup form the API and format - * Supports attribute `form` for V2 of the API - * and `id` for V3 of the API - * - * @param $attributes - * @return string - */ - public static function get_form_embed( $attributes ) { - - //$attributes = shortcode_atts( array( - // 'form' => -1, - //), $attributes ); - - if ( isset( $attributes['id'] ) ) { - - // There is an 'id' attribute so use v3 api to retrieve. - $forms_available = self::$api->get_resources( 'forms' ); - foreach ( $forms_available as $form_available ) { - if ( $form_available['id'] == $attributes['id'] ) { - $url = $form_available['embed_url']; - break; - } - } - - $form_markup = self::$api->get_resource( $url ); - return $form_markup; - - } elseif ( isset( $attributes['form'] ) ) { - // TODO add v2 api call - $form = $attributes['form']; - $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); - - // TODO add check for default_form which will be using V3 ID? - - $form = false; - if ($form_id == 0) { - return ""; - } - - // TODO get_resources uses V3 so will this work with a V2 form_id? - $forms_available = self::$api->get_resources('forms'); - foreach($forms_available as $form_available) { - if($form_available['id'] == $form_id) { - $form = $form_available; - break; - } - } - $url = add_query_arg( array( - 'api_key' => self::_get_settings('api_key'), - 'v' => '2', - ), - 'https://forms.convertkit.com/' . $form['id'] . '.html' - ); - $form_markup = self::$api->get_resource( $url ); - return $form_markup; - - } else { - - $form = $attributes['form']; - $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); - - // TODO add check for default_form which will be using V3 ID? - - $form = false; - if ($form_id == 0) { - return ""; - } - - // TODO get_resources uses V3 so will this work with a V2 form_id? - $forms_available = self::$api->get_resources('forms'); - foreach($forms_available as $form_available) { - if($form_available['id'] == $form_id) { - $form = $form_available; - break; - } - } - $url = add_query_arg( array( - 'api_key' => self::_get_settings('api_key'), - 'v' => '2', - ), - 'https://forms.convertkit.com/' . $form['id'] . '.html' - ); - $form_markup = self::$api->get_resource( $url ); - return $form_markup; - - } - - } - + /** * Run version specific upgrade. */ From eedd827e4aaacc8143975a079db0dc209394a4db Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Thu, 16 Jun 2016 12:23:04 -0500 Subject: [PATCH 15/17] Added message to select to indicate error contacting API. #24 --- lib/convertkit-api.php | 2 +- wp-convertkit.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index 5d264a96d..6acb8616a 100644 --- a/lib/convertkit-api.php +++ b/lib/convertkit-api.php @@ -53,7 +53,7 @@ public function get_resources($resource) { } if (is_null($api_response) || is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) { - $this->resources[$resource] = array(); + $this->resources[$resource] = array( array('id' => '-2', 'name' => 'Error contacting API' ) ); } else { $_resource = array(); diff --git a/wp-convertkit.php b/wp-convertkit.php index c2d8a4ba0..2f7e23139 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -341,7 +341,7 @@ private static function _get_settings_page_link($query_args = array()) { return add_query_arg($query_args, admin_url('options-general.php')); } - + /** * Run version specific upgrade. */ From cde43f825c62ca332f92018600139b417b95c4a4 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Fri, 24 Jun 2016 13:35:39 -0500 Subject: [PATCH 16/17] Added posts_per_page to get all post IDs. Remove upgrade option when finished. --- wp-convertkit.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wp-convertkit.php b/wp-convertkit.php index 2f7e23139..b755b327e 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -362,8 +362,9 @@ public static function upgrade() { if ( ! $posts ) { $args = array( - 'post_type' => array( 'post', 'page' ), - 'fields' => 'ids', + 'post_type' => array( 'post', 'page' ), + 'fields' => 'ids', + 'posts_per_page' => -1, ); $result = new WP_Query( $args ); @@ -402,8 +403,10 @@ public static function upgrade() { } // Done scanning posts, upgrade complete. - if ( empty( $posts ) ) + if ( empty( $posts ) ) { update_option( 'convertkit_version', self::VERSION ); + delete_option( '_wp_convertkit_upgrade_posts' ); + } } else { From 9f588996d796c2b41927a86ca72f2ccb78f44be8 Mon Sep 17 00:00:00 2001 From: Daniel Espinoza Date: Tue, 28 Jun 2016 10:46:53 -0500 Subject: [PATCH 17/17] Updated contributors, changelog, and tested version. --- readme.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/readme.txt b/readme.txt index f478a1fff..dba1cd2dc 100755 --- a/readme.txt +++ b/readme.txt @@ -1,10 +1,10 @@ === ConvertKit === -Contributors: nickohrn, davidlamarwheeler, nathanbarry, growdev +Contributors: nathanbarry, davidlamarwheeler, growdev, nickohrn Donate link: https://convertkit.com Tags: email, marketing, embed form, convertkit, capture Requires at least: 3.6 -Tested up to: 4.5.2 -Stable tag: 1.4.0 +Tested up to: 4.5.3 +Stable tag: 1.4.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -44,6 +44,10 @@ Yes, for it to work you must first have an account on ConvertKit.com == Changelog == +### 1.4.1 + +* Add upgrade routine to change ID to form_id for API version 3.0 + ### 1.4.0 * Update ConvertKit API to version 3.0