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/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']) : '' ); diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php index dcbc99b68..6acb8616a 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.convertkit.com/'; /** @var array */ protected $resources = array(); @@ -45,26 +45,35 @@ 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(); + $this->resources[$resource] = array( array('id' => '-2', 'name' => 'Error contacting API' ) ); } else { $_resource = array(); - // v3 doesn't have landing_pages resource. Instead check 'type' for 'hosted' + if ( 'forms' == $resource ) { - foreach ( $api_response as $form ){ - if ( 'embed' == $form['type'] ){ - $_resource[] = $form; - } + $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); + foreach( $response as $form ) { + $_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; @@ -121,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')) { @@ -173,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 )); + $response = wp_remote_get($url, array( 'timeout' => 10, 'sslverify' => false)); if(is_wp_error($response)) { return array(); @@ -181,7 +190,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/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 diff --git a/wp-convertkit.php b/wp-convertkit.php index ed97516ee..b755b327e 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); } /** @@ -156,9 +158,32 @@ 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()) { - $content .= wp_convertkit_get_form_embed(self::_get_meta(get_the_ID())); + + $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'); + } + + 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; @@ -169,7 +194,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)) { @@ -188,7 +215,40 @@ public static function page_takeover() { * @return mixed|void */ public static function shortcode($attributes, $content = null) { - return wp_convertkit_get_form_embed($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 apply_filters('wp_convertkit_get_form_embed', $form_markup, $attributes ); } /** @@ -281,48 +341,84 @@ 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 - * - * @param $attributes - * @return string + * Run version specific upgrade. */ - public static function get_form_embed($attributes) { - $attributes = shortcode_atts(array( - 'form' => -1, - ), $attributes); + public static function upgrade() { - $form = $attributes['form']; + $current_version = get_option( 'convertkit_version' ); - $form_id = intval(($form < 0) ? self::_get_settings('default_form') : $form); - $form = false; + if ( ! $current_version) { - if ($form_id == 0) { - return ""; - } + // Run 1.4.1 upgrade + $settings = self::_get_settings( ); - $forms_available = self::$api->get_resources('forms'); - foreach($forms_available as $form_available) { - if($form_available['id'] == $form_id) { - $form = $form_available; - break; - } - } + if ( isset( $settings['api_key'] ) ) { - $url = add_query_arg( array( - 'api_key' => self::_get_settings('api_key'), - 'v' => self::$forms_version, - ), - 'https://forms.convertkit.com/' . $form['id'] . '.html' - ); + // Get all posts and pages to track what has been updated + $posts = get_option( '_wp_convertkit_upgrade_posts' ); + + if ( ! $posts ) { + + $args = array( + 'post_type' => array( 'post', 'page' ), + 'fields' => 'ids', + 'posts_per_page' => -1, + ); + + $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 ); + } - $form_markup = self::$api->get_resource( $url ); + // 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'] ) && ( 0 < $post_settings['form'] ) ) + $post_settings['form'] = 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 ); + + unset($posts[ $key ]); + update_option( '_wp_convertkit_upgrade_posts', $posts ); + } + + // Done scanning posts, upgrade complete. + if ( empty( $posts ) ) { + update_option( 'convertkit_version', self::VERSION ); + delete_option( '_wp_convertkit_upgrade_posts' ); + } + + } else { + + update_option( 'convertkit_version', self::VERSION ); + + } + + } - return $form_markup; } } - require_once('lib/template-tags.php'); WP_ConvertKit::init(); }