Plugin Directory

Changeset 2213878


Ignore:
Timestamp:
12/17/2019 08:44:07 PM (6 years ago)
Author:
travisnorthcutt
Message:

Version 1.9.0

Location:
convertkit
Files:
79 added
9 edited

Legend:

Unmodified
Added
Removed
  • convertkit/trunk/admin/section/class-convertkit-settings-general.php

    r2098190 r2213878  
    7575        $html = ob_get_clean();
    7676
    77         wp_send_json_success( $html );
     77        $woocommerce = false;
     78        if ( post_type_exists( 'product' ) ) {
     79            $args = array(
     80                $forms,
     81                get_post_type_object( 'product' )
     82            );
     83            ob_start();
     84            $this->custom_post_types_callback( $args );
     85            $woocommerce = ob_get_clean();
     86        }
     87
     88        $data = array(
     89            'default'     => $html,
     90            'woocommerce' => $woocommerce
     91        );
     92
     93        wp_send_json_success( $data );
    7894        wp_die();
    7995    }
     
    84100    public function register_fields() {
    85101        $forms = get_option( 'convertkit_forms' );
     102        $account_name = get_option( 'convertkit_account_name' );
     103        add_settings_field(
     104            'account_name',
     105            'Account Name',
     106            array( $this, 'account_name_callback' ),
     107            $this->settings_key,
     108            $this->name,
     109            $account_name
     110        );
     111
    86112        add_settings_field(
    87113            'api_key',
     
    102128        add_settings_field(
    103129            'default_form',
    104             'Default Form',
     130            'Default Form (Posts & Pages)',
    105131            array( $this, 'default_form_callback' ),
    106132            $this->settings_key,
     
    108134            $forms
    109135        );
     136
     137        /**
     138         * Register fields for supported custom post types
     139         */
     140        $this->register_custom_post_type_fields( $forms );
    110141
    111142        add_settings_field(
     
    132163            $this->name
    133164        );
     165    }
     166
     167    /**
     168     * Register fields for supported custom post types
     169     *
     170     * @param  array  $forms  Form listing.
     171     */
     172    private function register_custom_post_type_fields( $forms ) {
     173
     174        // Gets all publicly visible custom post types
     175        $post_types = get_post_types( array(
     176            'public'   => true,
     177            '_builtin' => false
     178        ), 'objects', 'and' );
     179
     180        $supported_post_types = array(
     181            'product'
     182        );
     183
     184        /**
     185         * Filters result to just the post types we support
     186         * This is important because not all post types output the same way; e.g. WooCommerce products
     187         * don't reliably use the_content that we can filter, so we may need custom code to handle
     188         * each post type when we want to add a form to it.
     189         */
     190        $post_types = array_filter( $post_types, function ( $post_type ) use ( $supported_post_types ) {
     191            return in_array( $post_type->name, $supported_post_types );
     192        } );
     193
     194        foreach ( $post_types as $post_type ) {
     195            add_settings_field(
     196                'custom_post_types',
     197                'Default form (' . $post_type->label . ')',
     198                array( $this, 'custom_post_types_callback' ),
     199                $this->settings_key,
     200                $this->name,
     201                array(
     202                    $forms,
     203                    $post_type
     204                )
     205            );
     206        }
    134207    }
    135208
     
    146219    }
    147220
     221    /**
     222     * Renders the input for api key entry
     223     */
     224    public function account_name_callback( $account_name ) {
     225        $has_api = isset( $this->options['api_key'] ) ? esc_attr( $this->options['api_key'] ) : false;
     226
     227        if ( ! $has_api ) {
     228            $html = '<p class="description">' . __( 'Add your API Key and Secret to get started', 'convertkit' ) . '</p>';
     229        } else {
     230            $html = sprintf(
     231                '<span>%s</span>',
     232                isset( $account_name ) ? esc_attr( $account_name ) : ''
     233            );
     234            $html .= '<p class="description">' . __( 'The name of your connected ConvertKit account', 'convertkit' ) . '</p>';
     235        }
     236
     237        echo $html; // WPCS: XSS ok.
     238    }
     239
    148240    /**
    149241     * Renders the input for api key entry
     
    162254
    163255        if ( ! $has_api ) {
    164             $html .= '<p class="description">' . __( 'Add you API Key to get started', 'convertkit' ) . '</p>';
     256            $html .= '<p class="description">' . __( 'Add your API Key and Secret to get started', 'convertkit' ) . '</p>';
    165257        }
    166258        echo $html; // WPCS: XSS ok.
     
    197289        } else {
    198290            $html .= sprintf( '<select id="default_form" name="%s[default_form]">', $this->settings_key );
    199             $html .= '<option value="default">' . __( 'None', 'convertkit' ) . '</option>';
    200             if ( $forms ) {
    201                 foreach ( $forms as $form ) {
    202                     $form = (array) $form;
    203                     $html .= sprintf(
    204                         '<option value="%s" %s>%s</option>',
    205                         esc_attr( $form['id'] ),
    206                         selected( $this->options['default_form'], $form['id'], false ),
    207                         esc_html( $form['name'] )
    208                     );
    209                 }
     291            $html .= $this->forms_options_list( $forms, $this->options['default_form'] );
     292            $html .= '</select>';
     293        }
     294
     295        if ( empty( $this->options['api_key'] ) ) {
     296            $html .= '<p class="description">' . __( 'Enter your API Key above to get your available forms.',
     297                    'convertkit' ) . '</p>';
     298        }
     299
     300        if ( empty( $forms ) ) {
     301            $html .= '<p class="description">' . __( 'There are no forms setup in your account. You can go <a href="https://app.convertkit.com/landing_pages/new" target="_blank">here</a> to create one.',
     302                    'convertkit' ) . '</p>';
     303        }
     304
     305        $html .= '</div>';
     306
     307        echo $html; // WPCS: XSS ok.
     308    }
     309
     310    /**
     311     * Callback used to generate settings for custom post types
     312     *
     313     * @param  array  $args
     314     */
     315    public function custom_post_types_callback( $args ) {
     316
     317        list( $forms, $post_type ) = $args;
     318        $html = '<div id="' . $post_type->name . '_form_container">';
     319
     320        $options_key = $post_type->name . '_form';
     321
     322        $selected = array_key_exists( $options_key, $this->options ) ? $this->options[ $options_key ] : false;
     323
     324        $html .= sprintf( '<select id="%s_form" name="%s[%s_form]" class=%s">', $post_type->name, $this->settings_key,
     325            $post_type->name, 'form-select-list' );
     326        $html .= $this->forms_options_list( $forms, $selected );
     327        $html .= '</select>';
     328
     329        $html .= '</div>';
     330
     331        echo $html;
     332    }
     333
     334    /**
     335     * Generates string of <option> elements to fill <select> element with list of forms
     336     *
     337     * @param  array  $forms
     338     * @param $selected
     339     *
     340     * @return string
     341     */
     342    private function forms_options_list( $forms, $selected ) {
     343        $html = '<option value="default">' . __( 'None', 'convertkit' ) . '</option>';
     344        if ( $forms ) {
     345            foreach ( $forms as $form ) {
     346                $form = (array) $form;
     347                $html .= sprintf(
     348                    '<option value="%s" %s>%s</option>',
     349                    esc_attr( $form['id'] ),
     350                    selected( $selected, $form['id'], false ),
     351                    esc_html( $form['name'] )
     352                );
    210353            }
    211             $html .= '</select>';
    212         }
    213 
    214         if ( empty( $this->options['api_key'] ) ) {
    215             $html .= '<p class="description">' . __( 'Enter your API Key above to get your available forms.', 'convertkit' ) . '</p>';
    216         }
    217 
    218         if ( empty( $forms ) ) {
    219             $html .= '<p class="description">' . __( 'There are no forms setup in your account. You can go <a href="https://app.convertkit.com/landing_pages/new" target="_blank">here</a> to create one.', 'convertkit' ) . '</p>';
    220         }
    221 
    222         $html .= '</div>';
    223 
    224         echo $html; // WPCS: XSS ok.
    225     }
    226 
     354        }
     355
     356        return $html;
     357    }
     358
     359    /**
     360     * Callback used to generate button for refreshing forms from connected ConvertKit account
     361     */
    227362    public function refresh_forms_callback() {
    228363        $has_api = isset( $this->options['api_key'] ) ? esc_attr( $this->options['api_key'] ) : false;
     
    278413     */
    279414    public function sanitize_settings( $settings ) {
    280         $defaults = array(
    281             'api_key'      => '',
    282             'api_secret'   => '',
    283             'default_form' => 0,
    284             'debug'        => '',
    285       'no_scripts'   => '',
    286         );
    287 
    288         if ( isset( $settings['api_key'] ) ) {
    289             $this->api->update_resources( $settings['api_key'] );
     415        $defaults = array(
     416            'api_key'      => '',
     417            'api_secret'   => '',
     418            'default_form' => 0,
     419            'debug'        => '',
     420            'no_scripts'   => '',
     421        );
     422
     423        if ( isset( $settings['api_key'] ) && isset( $settings['api_secret'] ) ) {
     424            $this->api->update_resources( $settings['api_key'], $settings['api_secret'] );
    290425        }
    291426
  • convertkit/trunk/includes/class-convertkit-api.php

    r2151167 r2213878  
    101101     * @param string $api_key
    102102     */
    103     public function update_resources( $api_key ) {
     103    public function update_resources( $api_key, $api_secret ) {
    104104
    105105        $this->api_key = $api_key;
     
    155155            }
    156156            $update_tags = $this->maybe_update_option( 'convertkit_tags', $tags );
    157         }
     157           
     158            $this->update_account_name( $api_secret );
     159        }
     160
    158161
    159162        return $update_forms && $update_landing_pages && $update_tags;
     163    }
     164
     165    public function update_account_name( $api_secret ) {
     166        $response = $this->_get_api_response( 'account', $api_secret );
     167
     168        return $this->maybe_update_option( 'convertkit_account_name', $response['name'] );
    160169    }
    161170
     
    482491     * @return array
    483492     */
    484     private function _get_api_response( $path = '' ) {
     493    private function _get_api_response( $path = '', $api_secret = null ) {
    485494
    486495        $args = array(
    487496            'api_key' => $this->api_key,
    488497        );
     498
     499        if ( $api_secret ) {
     500            $args['api_secret'] = $api_secret;
     501        }
     502
    489503        $api_path = $this->api_url_base . $this->api_version;
    490504        $url = add_query_arg( $args, path_join( $api_path, $path ) );
  • convertkit/trunk/includes/class-convertkit.php

    r2151167 r2213878  
    7070            add_action( 'add_meta_boxes_page', array( __CLASS__, 'add_meta_boxes' ) );
    7171            add_action( 'add_meta_boxes_post', array( __CLASS__, 'add_meta_boxes' ) );
     72            add_action( 'add_meta_boxes_product', array( __CLASS__, 'add_meta_boxes' ) );
    7273        } else {
     74            add_action( 'template_redirect', array( __CLASS__, 'append_custom_post_type_forms' ) );
    7375            add_action( 'template_redirect', array( __CLASS__, 'page_takeover' ) );
    7476            add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
     
    198200    /**
    199201     * Page/Post display callback
     202     * In the future, other post types that use the_content can be handled here with conditional cases
    200203     *
    201204     * @param string $content The post content.
     
    205208
    206209        if ( is_singular( array( 'post' ) ) || is_page() ) {
    207 
    208             $attributes = self::_get_meta( get_the_ID() );
    209 
    210             // Get post/page form setting
    211             if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) {
    212                 $form_id = $attributes['form'];
     210            $content .= self::get_form_for_post( get_the_ID(), 'default_form' );
     211        }
     212
     213        return $content;
     214    }
     215
     216    /**
     217     * Handle custom post types that don't reliably use the_content
     218     */
     219    public static function append_custom_post_type_forms() {
     220        add_action( 'woocommerce_after_single_product_summary', array( __CLASS__, 'append_woocommerce_product_form' ) );
     221    }
     222
     223    /**
     224     * Append form to WooCommerce products
     225     */
     226    public static function append_woocommerce_product_form() {
     227        $form = self::get_form_for_post( get_the_ID(), 'product_form' );
     228        echo $form;
     229    }
     230
     231    /**
     232     * @param  integer  $post_id
     233     * @param  string  $settings_key  The settings key used for the post type on the ConvertKit plugin settings page
     234     *                             Usually of the form {post_type_name}_form
     235     *
     236     * @return string
     237     */
     238    public static function get_form_for_post( $post_id, $settings_key ) {
     239        $form       = '';
     240        $attributes = self::_get_meta( $post_id );
     241
     242        // Get post/page form setting
     243        // If post form id is 0, then it's "None", and we need to stop here completely
     244        if ( isset( $attributes['form'] ) && ( 0 <= $attributes['form'] ) ) {
     245            $form_id = $attributes['form'];
     246        } else {
     247            // Get category form
     248            $form_id = self::get_category_form( $post_id );
     249
     250
     251            if ( 0 === $form_id || 'default' === $form_id ) {
     252                // Get default form for post type
     253                if ( '-1' === $attributes['form'] ) {
     254                    $form_id = self::_get_settings( $settings_key );
     255                }
     256            }
     257        }
     258
     259        if ( 0 < $form_id && ! is_array( $form_id ) ) {
     260
     261            $forms = get_option( 'convertkit_forms' );
     262
     263            if ( isset( $forms[ $form_id ]['uid'] ) ) {
     264                // new form
     265                $tag  = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
     266                $form = $tag;
    213267            } else {
    214                 // Get category form
    215                 $form_id = self::get_category_form( get_the_ID() );
    216 
    217                 if ( 0 === $form_id || 'default' === $form_id ) {
    218                     // Get global default form
    219                     if ( '-1' === $attributes['form'] ) {
    220                         $form_id = self::_get_settings( 'default_form' );
    221                     }
    222                 }
    223             }
    224 
    225             if ( 0 < $form_id && !is_array( $form_id ) ) {
    226 
    227                 $forms = get_option( 'convertkit_forms' );
    228 
    229                 if ( isset( $forms[ $form_id ]['uid'] ) ) {
    230                     // new form
    231                     $tag = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
    232                     $content .= $tag;
    233 
    234                 } else {
    235                     // old form
    236                     $url = add_query_arg(
    237                         array(
    238                             'api_key' => self::_get_settings( 'api_key' ),
    239                             'v'       => self::$forms_version,
    240                         ),
    241                         'https://forms.convertkit.com/' . $form_id . '.html'
    242                     );
    243 
    244                     $form_markup = self::$api->get_resource( $url );
    245                     $content .= $form_markup;
    246                 }
    247             }
    248         }
    249 
    250         return $content;
     268                // old form
     269                $url = add_query_arg(
     270                    array(
     271                        'api_key' => self::_get_settings( 'api_key' ),
     272                        'v'       => self::$forms_version,
     273                    ),
     274                    'https://forms.convertkit.com/' . $form_id . '.html'
     275                );
     276
     277                $form_markup = self::$api->get_resource( $url );
     278                $form        = $form_markup;
     279            }
     280        }
     281
     282        return $form;
    251283    }
    252284
  • convertkit/trunk/readme.txt

    r2151167 r2213878  
    44Tags: email, marketing, embed form, convertkit, capture
    55Requires at least: 3.6
    6 Tested up to: 5.2.2
    7 Stable tag: 1.8.1
     6Tested up to: 5.3.1
     7Stable tag: 1.9.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4646
    4747== Changelog ==
     48### 1.9.0 2019-12-17
     49* Allow appending forms to WooCommerce products
     50* Fix bug that prevented setting "none" for form on a post from overriding category setting
     51* Add account name to settings page
     52
    4853### 1.8.1 2019-05-30
    4954* Fix bug that could result in fatal error with certain other plugins active
  • convertkit/trunk/resources/backend/wp-convertkit.js

    r2008063 r2213878  
    3131                success: function (resp) {
    3232                    if ( resp.success ) {
    33                         $('#default_form_container').parent().html( resp.data );
     33                        $('#default_form_container').parent().html( resp.data.default );
     34                        $('#product_form_container').parent().html( resp.data.woocommerce );
    3435                    } else {
    3536                        alert( resp.data );
  • convertkit/trunk/vendor/autoload.php

    r2151167 r2213878  
    55require_once __DIR__ . '/composer/autoload_real.php';
    66
    7 return ComposerAutoloaderInit0915cab5ae7f3730e26acdeaaae71d3a::getLoader();
     7return ComposerAutoloaderInit29fedc03c074d1205488d365aa02d8ed::getLoader();
  • convertkit/trunk/vendor/composer/autoload_real.php

    r2151167 r2213878  
    33// autoload_real.php @generated by Composer
    44
    5 class ComposerAutoloaderInit0915cab5ae7f3730e26acdeaaae71d3a
     5class ComposerAutoloaderInit29fedc03c074d1205488d365aa02d8ed
    66{
    77    private static $loader;
     
    2020        }
    2121
    22         spl_autoload_register(array('ComposerAutoloaderInit0915cab5ae7f3730e26acdeaaae71d3a', 'loadClassLoader'), true, true);
     22        spl_autoload_register(array('ComposerAutoloaderInit29fedc03c074d1205488d365aa02d8ed', 'loadClassLoader'), true, true);
    2323        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
    24         spl_autoload_unregister(array('ComposerAutoloaderInit0915cab5ae7f3730e26acdeaaae71d3a', 'loadClassLoader'));
     24        spl_autoload_unregister(array('ComposerAutoloaderInit29fedc03c074d1205488d365aa02d8ed', 'loadClassLoader'));
    2525
    2626        $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
     
    2828            require_once __DIR__ . '/autoload_static.php';
    2929
    30             call_user_func(\Composer\Autoload\ComposerStaticInit0915cab5ae7f3730e26acdeaaae71d3a::getInitializer($loader));
     30            call_user_func(\Composer\Autoload\ComposerStaticInit29fedc03c074d1205488d365aa02d8ed::getInitializer($loader));
    3131        } else {
    3232            $map = require __DIR__ . '/autoload_namespaces.php';
  • convertkit/trunk/vendor/composer/autoload_static.php

    r2151167 r2213878  
    55namespace Composer\Autoload;
    66
    7 class ComposerStaticInit0915cab5ae7f3730e26acdeaaae71d3a
     7class ComposerStaticInit29fedc03c074d1205488d365aa02d8ed
    88{
    99    public static $prefixLengthsPsr4 = array (
     
    3939    {
    4040        return \Closure::bind(function () use ($loader) {
    41             $loader->prefixLengthsPsr4 = ComposerStaticInit0915cab5ae7f3730e26acdeaaae71d3a::$prefixLengthsPsr4;
    42             $loader->prefixDirsPsr4 = ComposerStaticInit0915cab5ae7f3730e26acdeaaae71d3a::$prefixDirsPsr4;
    43             $loader->prefixesPsr0 = ComposerStaticInit0915cab5ae7f3730e26acdeaaae71d3a::$prefixesPsr0;
     41            $loader->prefixLengthsPsr4 = ComposerStaticInit29fedc03c074d1205488d365aa02d8ed::$prefixLengthsPsr4;
     42            $loader->prefixDirsPsr4 = ComposerStaticInit29fedc03c074d1205488d365aa02d8ed::$prefixDirsPsr4;
     43            $loader->prefixesPsr0 = ComposerStaticInit29fedc03c074d1205488d365aa02d8ed::$prefixesPsr0;
    4444
    4545        }, null, ClassLoader::class);
  • convertkit/trunk/wp-convertkit.php

    r2151167 r2213878  
    44 * Plugin URI: https://convertkit.com/
    55 * Description: Quickly and easily integrate ConvertKit forms into your site.
    6  * Version: 1.8.1
     6 * Version: 1.9.0
    77 * Author: ConvertKit
    88 * Author URI: https://convertkit.com/
     
    1717define( 'CONVERTKIT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    1818define( 'CONVERTKIT_PLUGIN_PATH', __DIR__ );
    19 define( 'CONVERTKIT_PLUGIN_VERSION', '1.8.1' );
     19define( 'CONVERTKIT_PLUGIN_VERSION', '1.9.0' );
    2020
    2121require_once CONVERTKIT_PLUGIN_PATH . '/vendor/autoload.php';
Note: See TracChangeset for help on using the changeset viewer.