diff --git a/admin/class-convertkit-settings.php b/admin/class-convertkit-settings.php new file mode 100644 index 000000000..237ed24a0 --- /dev/null +++ b/admin/class-convertkit-settings.php @@ -0,0 +1,162 @@ +settings_key ); + $api_key = $general_options && array_key_exists( 'api_key', $general_options ) ? $general_options['api_key'] : null; + $api_secret = $general_options && array_key_exists( 'api_secret', $general_options ) ? $general_options['api_secret'] : null; + $debug = $general_options && array_key_exists( 'debug', $general_options ) ? $general_options['debug'] : null; + $this->api = new ConvertKit_API( $api_key, $api_secret, $debug ); + + add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); + add_action( 'admin_init', array( $this, 'register_sections' ) ); + } + + /** + * Add the options page + */ + public function add_settings_page() { + add_options_page( + __( 'ConvertKit', 'convertkit' ), + __( 'ConvertKit', 'convertkit' ), + 'manage_options', + $this->settings_key, + array( $this, 'display_settings_page' ) + ); + + add_action( 'admin_print_styles', array( $this, 'admin_styles' ) ); + } + + /** + * Options page callback + */ + public function display_settings_page() { + if ( isset( $_GET['tab'] ) ) { // WPCS: CSRF ok. + $active_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // WPCS: CSRF ok. + } else { + $active_section = $this->sections[0]->name; + } + + ?> +
+ sections ) > 1 ) { + $this->display_section_nav( $active_section ); + } else { + ?> +

+ + +
+ sections as $section ) : + if ( $active_section === $section->name ) : + $section->render(); + endif; + endforeach; + ?>

', '' ); ?>

+ +
+ +

+
+ is_registerable ) { + array_push( $this->sections, $section_instance ); + } + } + + /** + * Register each section + */ + public function register_sections() { + wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) ); + $this->register_section( 'ConvertKit_Settings_General' ); + $this->register_section( 'ConvertKit_Settings_Wishlist' ); + $this->register_section( 'ConvertKit_Settings_ContactForm7' ); + } +} + +if ( is_admin() ) { + $convertkit_settings = new ConvertKit_Settings(); + + include plugin_dir_path( __FILE__ ) . '../lib/class-multi-value-field-table.php'; + include 'section/class-convertkit-settings-base.php'; + include 'section/class-convertkit-settings-general.php'; + include 'section/class-convertkit-settings-wishlist.php'; + include 'section/class-convertkit-settings-contactform7.php'; +} diff --git a/admin/section/base.php b/admin/section/base.php deleted file mode 100644 index 94610a482..000000000 --- a/admin/section/base.php +++ /dev/null @@ -1,71 +0,0 @@ -api = $convertkit_settings->api; - $this->options = get_option($this->settings_key); - if (empty($this->tab_text)) $this->tab_text = $this->title; - - $this->register_section(); - } - - /** - * Register settings section - */ - public function register_section() { - if(false == get_option($this->settings_key)) { - add_option($this->settings_key); - } - - add_settings_section( - $this->name, // Section name (machine-readable) - $this->title, // Section title - array($this, 'print_section_info'), // Info callback - $this->settings_key // Settings page - ); - - $this->register_fields(); - - register_setting( - $this->settings_key, // Page - $this->settings_key, // Settings DB Key - array($this, 'sanitize_settings') - ); - } - - /** - * Renders the section - */ - public function render() { - do_settings_sections( $this->settings_key ); - settings_fields( $this->settings_key ); - submit_button(); - } - - /** - * Register settings fields - */ - abstract public function register_fields(); - - /** - * Prints help info for this section - */ - abstract public function print_section_info(); -} diff --git a/admin/section/class-convertkit-settings-base.php b/admin/section/class-convertkit-settings-base.php new file mode 100644 index 000000000..10a7ce167 --- /dev/null +++ b/admin/section/class-convertkit-settings-base.php @@ -0,0 +1,120 @@ +api = $convertkit_settings->api; + $this->options = get_option( $this->settings_key ); + if ( empty( $this->tab_text ) ) { + $this->tab_text = $this->title; + } + + $this->register_section(); + } + + /** + * Register settings section + */ + public function register_section() { + if ( false === get_option( $this->settings_key ) ) { + add_option( $this->settings_key ); + } + + add_settings_section( + $this->name, + $this->title, + array( $this, 'print_section_info' ), + $this->settings_key + ); + + $this->register_fields(); + + register_setting( + $this->settings_key, + $this->settings_key, + array( $this, 'sanitize_settings' ) + ); + } + + /** + * Renders the section + */ + public function render() { + do_settings_sections( $this->settings_key ); + settings_fields( $this->settings_key ); + submit_button(); + } + + /** + * Register settings fields + */ + abstract public function register_fields(); + + /** + * Prints help info for this section + */ + abstract public function print_section_info(); +} diff --git a/admin/section/class-convertkit-settings-contactform7.php b/admin/section/class-convertkit-settings-contactform7.php new file mode 100644 index 000000000..95b057766 --- /dev/null +++ b/admin/section/class-convertkit-settings-contactform7.php @@ -0,0 +1,281 @@ +is_registerable = false; + return; + } + + $this->settings_key = '_wp_convertkit_integration_contactform7_settings'; + $this->name = 'contactform7'; + $this->title = 'Contact Form 7 Integration Settings'; + $this->tab_text = 'Contact Form 7'; + + $this->get_cf7_forms(); + + parent::__construct(); + } + + /** + * Gets available forms from CF7 + */ + public function get_cf7_forms() { + + $forms = array(); + + $args = array( + 'orderby' => 'ID', + 'order' => 'ASC', + 'post_type' => 'wpcf7_contact_form', + ); + + $result = new WP_Query( $args ); + + foreach ( $result->posts as $post ) { + $forms[] = array( + 'id' => $post->ID, + 'name' => $post->post_title, + ); + } + + wp_reset_postdata(); + $this->forms = $forms; + } + + /** + * Register and add settings + */ + public function register_fields() { + + $forms = $this->api->get_resources( 'forms' ); + + foreach ( $this->forms as $form ) { + + add_settings_field( + sprintf( '%s_title', $form['id'] ), + 'Contact Form 7 Form', + array( $this, 'cf7_title_callback' ), + $this->settings_key, + $this->name, + array( + 'cf7_form_id' => $form['id'], + 'cf7_form_name' => $form['name'], + 'sortable' => true, + ) + ); + + add_settings_field( + sprintf( '%s_form', $form['id'] ), + 'ConvertKit Form', + array( $this, 'cf7_form_callback' ), + $this->settings_key, + $this->name, + array( + 'cf7_form_id' => $form['id'], + 'forms' => $forms, + 'sortable' => false, + ) + ); + + add_settings_field( + sprintf( '%s_email', $form['id'] ), + 'CF7 Email Field', + array( $this, 'cf7_email_callback' ), + $this->settings_key, + $this->name, + array( + 'cf7_email_id' => 'your-email', + 'sortable' => false, + ) + ); + + add_settings_field( + sprintf( '%s_name', $form['id'] ), + 'CF7 Name Field', + array( $this, 'cf7_name_callback' ), + $this->settings_key, + $this->name, + array( + 'cf7_name_id' => 'your-name', + 'sortable' => false, + ) + ); + + } // End foreach(). + } + + /** + * Prints help info for this section + */ + public function print_section_info() { + ?>

text* fields named your-name and your-email. ' ); + esc_html_e( 'These fields will be sent to ConvertKit for the subscription.', 'convertkit' ); + ?>

settings_key ][ $this->name ]; + + foreach ( $fields as $field ) { + list( $cf7_form_id, $field_type ) = explode( '_', $field['id'] ); + + if ( ! in_array( $field_type, $columns, true ) ) { + $table->add_column( $field_type, $field['title'], $field['args']['sortable'] ); + array_push( $columns, $field_type ); + } + + if ( ! isset( $rows[ $cf7_form_id ] ) ) { + $rows[ $cf7_form_id ] = array(); + } + + $rows[ $cf7_form_id ][ $field_type ] = call_user_func( $field['callback'], $field['args'] ); + } + + foreach ( $rows as $row ) { + $table->add_item( $row ); + } + + $table->prepare_items(); + $table->display(); + } + + /** + * Renders the section + * + * Called from ConvertKitSettings::display_settings_page() + * + * @return void + */ + public function render() { + global $wp_settings_sections; + + if ( ! isset( $wp_settings_sections[ $this->settings_key ] ) ) { + return; + } + + foreach ( $wp_settings_sections[ $this->settings_key ] as $section ) { + if ( $section['title'] ) { + ?>' . esc_html( $section['title'] ) . ''; + } + if ( $section['callback'] ) { + call_user_func( $section['callback'], $section ); + } + + $this->do_settings_table(); + settings_fields( $this->settings_key ); + submit_button(); + } + } + + /** + * Display form title in first column + * + * @param array $args Name argument. + * @return string + */ + public function cf7_title_callback( $args ) { + return $args['cf7_form_name']; + } + + /** + * Display CF7 to CK mapping + * + * @param array $args Settings to display. + * @return string $html + */ + public function cf7_form_callback( $args ) { + $cf7_form_id = $args['cf7_form_id']; + $forms = $args['forms']; + + $html = sprintf( ''; + + return $html; + } + + + /** + * Display email in first column + * + * @param array $args Email setting. + * @return string + */ + public function cf7_email_callback( $args ) { + return $args['cf7_email_id']; + } + + /** + * Display form title in first column + * + * @param array $args Name setting. + * @return string + */ + public function cf7_name_callback( $args ) { + return $args['cf7_name_id']; + } + + /** + * Sanitizes the settings + * + * @param array $input Values to be sanitized. + * @return array sanitized settings + */ + public function sanitize_settings( $input ) { + // Settings page can be paginated; combine input with existing options. + $output = $this->options; + + foreach ( $input as $key => $value ) { + $output[ $key ] = stripslashes( $input[ $key ] ); + } + $sanitize_hook = 'sanitize' . $this->settings_key; + return apply_filters( $sanitize_hook, $output, $input ); + } +} diff --git a/admin/section/class-convertkit-settings-general.php b/admin/section/class-convertkit-settings-general.php new file mode 100644 index 000000000..4868f07b5 --- /dev/null +++ b/admin/section/class-convertkit-settings-general.php @@ -0,0 +1,179 @@ +settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG; + $this->name = 'general'; + $this->title = __( 'General Settings', 'convertkit' ); + $this->tab_text = __( 'General', 'convertkit' ); + + parent::__construct(); + } + + /** + * Register and add settings + */ + public function register_fields() { + add_settings_field( + 'api_key', + 'API Key', + array( $this, 'api_key_callback' ), + $this->settings_key, + $this->name + ); + + add_settings_field( + 'api_secret', + 'API Secret', + array( $this, 'api_secret_callback' ), + $this->settings_key, + $this->name + ); + + add_settings_field( + 'default_form', + 'Default Form', + array( $this, 'default_form_callback' ), + $this->settings_key, + $this->name, + $this->api->get_resources( 'forms' ) + ); + + add_settings_field( + 'debug', + 'Debug', + array( $this, 'debug_callback' ), + $this->settings_key, + $this->name + ); + } + + /** + * Prints help info for this section + */ + public function print_section_info() { + ?> +

+

+

[convertkit]' ); ?>

+ ', + $this->settings_key, + isset( $this->options['api_key'] ) ? esc_attr( $this->options['api_key'] ) : '' + ); + + $html .= '

' . __( 'Get your ConvertKit API Key', 'convertkit' ) . '

'; + + echo $html; // WPCS: XSS ok. + } + + /** + * Renders the input for api key entry + */ + public function api_secret_callback() { + $html = sprintf( + '', + $this->settings_key, + isset( $this->options['api_secret'] ) ? esc_attr( $this->options['api_secret'] ) : '' + ); + + $html .= '

'; + $html .= __( 'Get your ConvertKit API Secret.', 'convertkit' ) . ''; + $html .= ' ' . __( 'This setting is required to unsubscribe subscribers.', 'convertkit' ) . '

'; + + echo $html; // WPCS: XSS ok. + } + + /** + * Renders the form select list + * + * @param array $forms Form listing. + */ + public function default_form_callback( $forms ) { + + // Check for error in response. + if ( isset( $forms[0]['id'] ) && '-2' === $forms[0]['id'] ) { + $html = '

' . __( 'Error connecting to API. Please verify your site can connect to https://api.convertkit.com','convertkit' ) . '

'; + } else { + $html = sprintf( ''; + } + + if ( empty( $this->options['api_key'] ) ) { + $html .= '

' . __( 'Enter your API Key above to get your available forms.', 'convertkit' ) . '

'; + } + + if ( empty( $forms ) ) { + $html .= '

' . __( 'There are no forms setup in your account. You can go here to create one.', 'convertkit' ) . '

'; + } + + echo $html; // WPCS: XSS ok. + } + + /** + * Renders the input for debug setting + */ + public function debug_callback() { + + $debug = ''; + if ( isset( $this->options['debug'] ) && 'on' === $this->options['debug'] ) { + $debug = 'checked'; + } + + echo sprintf( // WPCS: XSS OK + '%s', + $this->settings_key, + $debug, + __( 'Save connection data to a log file.','convertkit' ) + ); + + } + + /** + * Sanitizes the settings + * + * @param array $settings The settings fields submitted. + * @return array Sanitized settings. + */ + public function sanitize_settings( $settings ) { + + // Clear the api transient. + delete_transient( 'convertkit_get_api_response' ); + return shortcode_atts( array( + 'api_key' => '', + 'api_secret' => '', + 'default_form' => 0, + 'debug' => '', + ), $settings ); + } +} diff --git a/admin/section/class-convertkit-settings-wishlist.php b/admin/section/class-convertkit-settings-wishlist.php new file mode 100644 index 000000000..0af1a9b5b --- /dev/null +++ b/admin/section/class-convertkit-settings-wishlist.php @@ -0,0 +1,248 @@ +is_registerable = false; + return; + } + + $this->settings_key = '_wp_convertkit_integration_wishlistmember_settings'; + $this->name = 'wishlist-member'; + $this->title = 'WishList Member Integration Settings'; + $this->tab_text = 'WishList Member'; + $this->wlm_levels = $this->get_wlm_levels(); + + parent::__construct(); + } + + /** + * Gets membership levels from WishList Member API + * + * @return array Membership levels + */ + public function get_wlm_levels() { + $wlm_get_levels = wlmapi_get_levels(); + + if ( 1 === $wlm_get_levels['success'] ) { + $this->wlm_levels = $wlm_get_levels['levels']['level']; + return $this->wlm_levels; + } else { + return array(); + } + } + + /** + * Register and add settings + */ + public function register_fields() { + + $forms = $this->api->get_resources( 'forms' ); + $tags = $this->api->get_resources( 'tags' ); + + foreach ( $this->wlm_levels as $wlm_level ) { + add_settings_field( + sprintf( '%s_title', $wlm_level['id'] ), + __( 'WishList Membership Level', 'convertkit' ), + array( $this, 'wlm_title_callback' ) , + $this->settings_key, + $this->name, + array( + 'wlm_level_id' => $wlm_level['id'], + 'wlm_level_name' => $wlm_level['name'], + 'sortable' => true, + ) + ); + + add_settings_field( + sprintf( '%s_form', $wlm_level['id'] ), + __( 'ConvertKit Form', 'convertkit' ), + array( $this, 'wlm_level_callback' ), + $this->settings_key, + $this->name, + array( + 'wlm_level_id' => $wlm_level['id'], + 'forms' => $forms, + ) + ); + + add_settings_field( + sprintf( '%s_unsubscribe', $wlm_level['id'] ), + __( 'Unsubscribe Action', 'convertkit' ), + array( $this, 'wlm_unsubscribe_callback' ), + $this->settings_key, + $this->name, + array( + 'wlm_level_id' => $wlm_level['id'], + 'tags' => $tags, + ) + ); + } // End foreach(). + } + + /** + * Prints help info for this section + */ + public function print_section_info() { + ?>

settings_key ][ $this->name ]; + + foreach ( $fields as $field ) { + list( $wlm_level_id, $field_type ) = explode( '_', $field['id'] ); + + if ( ! in_array( $field_type, $columns, true ) ) { + $table->add_column( $field_type, $field['title'], $field['args']['sortable'] ); + array_push( $columns, $field_type ); + } + + if ( ! isset( $rows[ $wlm_level_id ] ) ) { + $rows[ $wlm_level_id ] = array(); + } + + $rows[ $wlm_level_id ][ $field_type ] = call_user_func( $field['callback'], $field['args'] ); + } + + foreach ( $rows as $row ) { + $table->add_item( $row ); + } + + $table->prepare_items(); + $table->display(); + } + + /** + * Renders the section + */ + public function render() { + global $wp_settings_sections, $wp_settings_fields; + + if ( ! isset( $wp_settings_sections[ $this->settings_key ] ) ) { + return; + } + + foreach ( $wp_settings_sections[ $this->settings_key ] as $section ) { + if ( $section['title'] ) { + echo '

' . esc_html( $section['title'] ) . '

'; + } + if ( $section['callback'] ) { + call_user_func( $section['callback'], $section ); + } + + $this->do_settings_table(); + settings_fields( $this->settings_key ); + submit_button(); + } + } + + /** + * Title for WishList Membership Level + * + * @param array $arguments Arguments from add_settings_field(). + * @return string WishList Membership Level title. + */ + public function wlm_title_callback( $arguments ) { + return $arguments['wlm_level_name']; + } + + /** + * CK Form select for WishList Membership Level + * + * @param array $arguments Arguments from add_settings_field(). + * @return string Select element. + */ + public function wlm_level_callback( $arguments ) { + $wlm_level_id = $arguments['wlm_level_id']; + $forms = $arguments['forms']; + + $html = sprintf( ''; + + return $html; + } + + /** + * Action to take when customer membership lapses + * + * @param array $arguments Arguments from add_settings_field(). + * @return string Checkbox and label. + */ + public function wlm_unsubscribe_callback( $arguments ) { + $wlm_level_id = $arguments['wlm_level_id']; + $tags = $arguments['tags']; + + $html = sprintf( ''; + + return $html; + } + + /** + * Sanitizes the settings + * + * @param array $input The settings fields submitted. + * @return array Sanitized settings. + */ + public function sanitize_settings( $input ) { + // Settings page can be paginated; combine input with existing options. + $output = $this->options; + + foreach ( $input as $key => $value ) { + list( $level_id, $setting ) = explode( '_', $key ); + + $output[ $key ] = stripslashes( $input[ $key ] ); + } + $sanitize_filter = 'sanitize' . $this->settings_key; + return apply_filters( $sanitize_filter, $output, $input ); + } +} diff --git a/admin/section/contactform7.php b/admin/section/contactform7.php deleted file mode 100644 index b83493730..000000000 --- a/admin/section/contactform7.php +++ /dev/null @@ -1,268 +0,0 @@ -is_registerable = false; - return; - } - - $this->settings_key = '_wp_convertkit_integration_contactform7_settings'; - $this->name = 'contactform7'; - $this->title = 'Contact Form 7 Integration Settings'; - $this->tab_text = 'Contact Form 7'; - - $this->get_cf7_forms(); - - parent::__construct(); - } - - /** - * Gets available forms from CF7 - * - * @return array - */ - public function get_cf7_forms() { - - $forms = array(); - - $posts = get_posts( array( - 'numberposts' => -1, - 'orderby' => 'ID', - 'order' => 'ASC', - 'post_type' => 'wpcf7_contact_form' ) - ); - - foreach( $posts as $post ){ - $forms[] = array( - 'id' => $post->ID, - 'name' => $post->post_title, - ); - } - $this->forms = $forms; - } - - /** - * Register and add settings - */ - public function register_fields() { - - $forms = $this->api->get_resources('forms'); - - foreach($this->forms as $form) { - - add_settings_field( - sprintf('%s_title', $form['id']), - 'Contact Form 7 Form', - array($this, 'cf7_title_callback'), - $this->settings_key, - $this->name, - array( - 'cf7_form_id' => $form['id'], - 'cf7_form_name' => $form['name'], - 'sortable' => true - ) - ); - - add_settings_field( - sprintf('%s_form', $form['id']), - 'ConvertKit Form', - array($this, 'cf7_form_callback'), - $this->settings_key, - $this->name, - array( - 'cf7_form_id' => $form['id'], - 'forms' => $forms, - 'sortable' => false - ) - ); - - add_settings_field( - sprintf('%s_email', $form['id']), - 'CF7 Email Field', - array($this, 'cf7_email_callback'), - $this->settings_key, - $this->name, - array( - 'cf7_email_id' => 'your-email', - 'sortable' => false - ) - ); - - add_settings_field( - sprintf('%s_name', $form['id']), - 'CF7 Name Field', - array($this, 'cf7_name_callback'), - $this->settings_key, - $this->name, - array( - 'cf7_name_id' => 'your-name', - 'sortable' => false - ) - ); - - } - } - - /** - * Prints help info for this section - */ - public function print_section_info() { - ?>

text* fields named your-name and your-email. ', 'convertkit' ); - echo __( 'These fields will be sent to ConvertKit for the subscription.', 'convertkit' ); - ?>

settings_key][$this->name]; - - foreach ($fields as $field) { - list($cf7_form_id, $field_type) = explode('_', $field['id']); - - if (!in_array($field_type, $columns)) { - $table->add_column($field_type, $field['title'], $field['args']['sortable']); - array_push($columns, $field_type); - } - - if (!isset($rows[$cf7_form_id])) { - $rows[$cf7_form_id] = array(); - } - - $rows[$cf7_form_id][$field_type] = call_user_func($field['callback'], $field['args']); - } - - foreach ($rows as $row) { - $table->add_item($row); - } - - $table->prepare_items(); - $table->display(); - } - - /** - * Renders the section - * - * Called from ConvertKitSettings::display_settings_page() - * @return void - */ - public function render() { - global $wp_settings_sections; - - if (!isset($wp_settings_sections[$this->settings_key])) return; - - foreach ($wp_settings_sections[$this->settings_key] as $section) { - if ($section['title']) echo "

{$section['title']}

\n"; - if ($section['callback']) call_user_func($section['callback'], $section); - - $forms = $this->api->get_resources('forms'); - - if (!empty($forms)) { - $this->do_settings_table(); - settings_fields($this->settings_key); - submit_button(); - } else { - ?> -

' . __('General Settings', 'convertkit') . ''); - ?>

- ', $this->settings_key, $cf7_form_id); - $html .= ''; - foreach($forms as $form) { - $html .= - ''; - } - $html .= ''; - - return $html; - } - - - /** - * Display email in first column - * - * @param array $args - * @return string - */ - public function cf7_email_callback( $args ) { - return $args['cf7_email_id']; - } - - /** - * Display form title in first column - * - * @param array $args - * @return string - */ - public function cf7_name_callback( $args ) { - return $args['cf7_name_id']; - } - - /** - * Sanitizes the settings - * - * @param array $input - * @return array sanitized settings - */ - public function sanitize_settings($input) { - // Settings page can be paginated; combine input with existing options - $output = $this->options; - - foreach($input as $key => $value) { - $output[$key] = stripslashes( $input[$key] ); - } - - return apply_filters( 'sanitize{$this->settings_key}', $output, $input); - } -} diff --git a/admin/section/general.php b/admin/section/general.php deleted file mode 100644 index 3cf836355..000000000 --- a/admin/section/general.php +++ /dev/null @@ -1,172 +0,0 @@ -settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG; - $this->name = 'general'; - $this->title = __( 'General Settings', 'convertkit' ); - $this->tab_text = __( 'General', 'convertkit' ); - - parent::__construct(); - } - - /** - * Register and add settings - */ - public function register_fields() { - add_settings_field( - 'api_key', - 'API Key', - array($this, 'api_key_callback'), - $this->settings_key, - $this->name - ); - - add_settings_field( - 'api_secret', - 'API Secret', - array($this, 'api_secret_callback'), - $this->settings_key, - $this->name - ); - - add_settings_field( - 'default_form', - 'Default Form', - array($this, 'default_form_callback'), - $this->settings_key, - $this->name, - $this->api->get_resources('forms') - ); - - add_settings_field( - 'debug', - 'Debug', - array($this, 'debug_callback'), - $this->settings_key, - $this->name - ); - - } - - /** - * Prints help info for this section - */ - public function print_section_info() { - ?> -

-

-

[convertkit] shortcode.', 'convertkit' ); ?>

- ', - $this->settings_key, - isset($this->options['api_key']) ? esc_attr($this->options['api_key']) : '' - ); - - $html .= '

' . __('Get your ConvertKit API Key', 'convertkit' ) . '

'; - - echo $html; - } - - /** - * Renders the input for api key entry - */ - public function api_secret_callback() { - $html = sprintf( - '', - $this->settings_key, - isset($this->options['api_secret']) ? esc_attr($this->options['api_secret']) : '' - ); - - $html .= '

' . __( 'Get your ConvertKit API Secret.', 'convertkit' ) . '' . ' ' . __( 'This setting is required to unsubscribe subscribers.', 'convertkit' ) . '

'; - - echo $html; - } - - /** - * Renders the form select list - * - * @param array $forms Form listing - */ - public function default_form_callback($forms) { - - // check for error in response - if ( isset( $forms[0]['id'] ) && '-2' == $forms[0]['id'] ) { - $html = '

' . __('Error connecting to API. Please verify your site can connect to https://api.convertkit.com','convertkit') . '

'; - } else { - $html = sprintf( ''; - } - - if ( empty( $this->options['api_key'] ) ) { - $html .= '

' . __( 'Enter your API Key above to get your available forms.', 'convertkit' ) . '

'; - } - - if (empty($forms)) { - $html .= '

' . __('There are no forms setup in your account. You can go here to create one.', 'convertkit' ) . '

'; - } - - echo $html; - } - - /** - * Renders the input for debug setting - */ - public function debug_callback() { - - $debug = ''; - if ( isset( $this->options['debug'] ) && 'on' == $this->options['debug'] ) { - $debug = 'checked'; - } - - $html = sprintf( - '%s', - $this->settings_key, - $debug, - __('Save connection data to a log file.','convertkit') - ); - - echo $html; - } - - /** - * Sanitizes the settings - * - * @param array $settings The settings fields submitted - * @return array Sanitized settings - */ - public function sanitize_settings($settings) { - - // clear api transient - delete_transient( 'convertkit_get_api_response' ); - return shortcode_atts(array( - 'api_key' => '', - 'api_secret' => '', - 'default_form' => 0, - 'debug' => '' - ), $settings); - } -} diff --git a/admin/section/wishlist_member.php b/admin/section/wishlist_member.php deleted file mode 100644 index 74e04b594..000000000 --- a/admin/section/wishlist_member.php +++ /dev/null @@ -1,244 +0,0 @@ -is_registerable = false; - } - - $this->settings_key = '_wp_convertkit_integration_wishlistmember_settings'; - $this->name = 'wishlist-member'; - $this->title = 'WishList Member Integration Settings'; - $this->tab_text = 'WishList Member'; - - $this->wlm_levels = $this->get_wlm_levels(); - - parent::__construct(); - } - - /** - * Gets membership levels from WishList Member API - * - * @return array Membership levels - */ - public function get_wlm_levels() { - $wlm_get_levels = wlmapi_get_levels(); - - if ($wlm_get_levels['success'] == 1) { - return $this->wlm_levels = $wlm_get_levels['levels']['level']; - } else { - return array(); - } - } - - /** - * Register and add settings - */ - public function register_fields() { - - $forms = $this->api->get_resources('forms'); - - foreach($this->wlm_levels as $wlm_level) { - add_settings_field( - sprintf('%s_title', $wlm_level['id']), - __( 'WishList Membership Level', 'convertkit' ), - array($this, 'wlm_title_callback'), - $this->settings_key, - $this->name, - array( - 'wlm_level_id' => $wlm_level['id'], - 'wlm_level_name' => $wlm_level['name'], - 'sortable' => true - ) - ); - - add_settings_field( - sprintf('%s_form', $wlm_level['id']), - __( 'ConvertKit Form', 'convertkit' ), - array($this, 'wlm_level_callback'), - $this->settings_key, - $this->name, - array( - 'wlm_level_id' => $wlm_level['id'], - 'forms' => $forms - ) - ); - - add_settings_field( - sprintf('%s_unsubscribe', $wlm_level['id']), - __( 'Unsubscribe', 'convertkit' ), - array($this, 'wlm_unsubscribe_callback'), - $this->settings_key, - $this->name, - array( - 'wlm_level_id' => $wlm_level['id'] - ) - ); - } - } - - /** - * Prints help info for this section - */ - public function print_section_info() { - ?>

- settings_key][$this->name]; - - foreach ($fields as $field) { - list($wlm_level_id, $field_type) = explode('_', $field['id']); - - if (!in_array($field_type, $columns)) { - $table->add_column($field_type, $field['title'], $field['args']['sortable']); - array_push($columns, $field_type); - } - - if (!isset($rows[$wlm_level_id])) { - $rows[$wlm_level_id] = array(); - } - - $rows[$wlm_level_id][$field_type] = call_user_func($field['callback'], $field['args']); - } - - foreach ($rows as $row) { - $table->add_item($row); - } - - $table->prepare_items(); - $table->display(); - } - - /** - * Renders the section - */ - public function render() { - global $wp_settings_sections, $wp_settings_fields; - - if (!isset($wp_settings_sections[$this->settings_key])) return; - - foreach ($wp_settings_sections[$this->settings_key] as $section) { - if ($section['title']) echo "

{$section['title']}

\n"; - if ($section['callback']) call_user_func($section['callback'], $section); - - $forms = $this->api->get_resources('forms'); - - if (!empty($forms)) { - $this->do_settings_table(); - settings_fields($this->settings_key); - submit_button(); - } else { - ?> -

. -

- ', $this->settings_key, $wlm_level_id); - $html .= ''; - foreach($forms as $form) { - $html .= sprintf( - '', - esc_attr($form['id']), - selected($this->options[$wlm_level_id . '_form'], $form['id'], false), - esc_html($form['name']) - ); - } - $html .= ''; - - return $html; - } - - /** - * Unsubscribe field for WishList Membership Level - * - * @param array $arguments Arguments from add_settings_field() - * @return string Checkbox and label - */ - public function wlm_unsubscribe_callback($arguments) { - $wlm_level_id = $arguments['wlm_level_id']; - - $html = sprintf( - '', - $this->settings_key, - $wlm_level_id, - checked($this->options[$wlm_level_id . '_unsubscribe'], 1, false) - ); - $html .= sprintf('', $this->settings_key, $wlm_level_id, __( 'Unsubscribe if removed from level', 'convertkit' )); - - return $html; - } - - /** - * Sanitizes the settings - * - * @param array $input The settings fields submitted - * @return array Sanitized settings - */ - public function sanitize_settings($input) { - // Settings page can be paginated; combine input with existing options - $output = $this->options; - - foreach($input as $key => $value) { - list($level_id, $setting) = explode('_', $key); - - // Unsubscribe must be manually set when moving from true to false - if (!isset($input["{$level_id}_unsubscribe"])) { - $output["{$level_id}_unsubscribe"] = 0; - } - - $output[$key] = stripslashes( $input[$key] ); - } - - return apply_filters("sanitize{$this->settings_key}", $output, $input); - } -} diff --git a/admin/settings.php b/admin/settings.php deleted file mode 100644 index 495944368..000000000 --- a/admin/settings.php +++ /dev/null @@ -1,127 +0,0 @@ -settings_key); - $api_key = $general_options && array_key_exists("api_key", $general_options) ? $general_options['api_key'] : null; - $api_secret = $general_options && array_key_exists("api_secret", $general_options) ? $general_options['api_secret'] : null; - $debug = $general_options && array_key_exists("debug", $general_options) ? $general_options['debug'] : null; - $this->api = new ConvertKitAPI( $api_key, $api_secret, $debug ); - - add_action('admin_menu', array($this, 'add_settings_page')); - add_action('admin_init', array($this, 'register_sections')); - } - - /** - * Add the options page - */ - public function add_settings_page() { - $settings = add_options_page( - __('ConvertKit', 'convertkit'), - __('ConvertKit', 'convertkit'), - 'manage_options', - $this->settings_key, - array($this, 'display_settings_page') - ); - - add_action('admin_print_styles', array($this, 'admin_styles')); - } - - /** - * Options page callback - */ - public function display_settings_page() { - $active_section = (isset($_GET['tab'])) ? $_GET['tab'] : $this->sections[0]->name; - ?> -
- sections) > 1) { - $this->display_section_nav($active_section); - } else { - ?> -

- - -
- sections as $section): - if ($active_section == $section->name): - $section->render(); - endif; - endforeach; - ?>

plugin documentation.', 'convertkit' ); ?>

-
-
- -

- - is_registerable) { - array_push($this->sections, $section_instance); - } - } - - /** - * Register each section - */ - public function register_sections() { - wp_register_style( 'wp-convertkit-admin', plugins_url('../resources/backend/wp-convertkit.css', __FILE__) ); - - $this->register_section('ConvertKitSettingsGeneral'); - $this->register_section('ConvertKitSettingsWishlistMember'); - $this->register_section('ConvertKitSettingsContactForm7'); - } -} - -if( is_admin() ) { - $convertkit_settings = new ConvertKitSettings(); - - include 'section/base.php'; - include 'section/general.php'; - include 'section/wishlist_member.php'; - include 'section/contactform7.php'; -} diff --git a/lib/class-ck-widget-form.php b/lib/class-ck-widget-form.php index 275d2e150..f3c81b8fd 100644 --- a/lib/class-ck-widget-form.php +++ b/lib/class-ck-widget-form.php @@ -1,4 +1,10 @@ array( 'type' => 'text', 'std' => __( 'ConvertKit Form', 'convertkit' ), - 'label' => __( 'Title', 'convertkit' ) + 'label' => __( 'Title', 'convertkit' ), ), 'form' => array( 'type' => 'select', 'std' => '', 'label' => __( 'Form', 'convertkit' ), - 'options' => $this->get_forms() + 'options' => $this->get_forms(), ), ); $widget_ops = array( 'classname' => $this->widget_cssclass, 'description' => $this->widget_description, - 'customize_selective_refresh' => true + 'customize_selective_refresh' => true, ); parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); - } /** * Output the html at the start of a widget. * - * @param array $args - * @return string + * @param array $args Widget arguments. + * @param array $instance Widget settings. */ public function widget_start( $args, $instance ) { echo $args['before_widget']; - - if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) { + $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); + if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } } @@ -99,8 +104,7 @@ public function widget_start( $args, $instance ) { /** * Output the html at the end of a widget. * - * @param array $args - * @return string + * @param array $args After widget setting. */ public function widget_end( $args ) { echo $args['after_widget']; @@ -111,8 +115,8 @@ public function widget_end( $args ) { * * @see WP_Widget * - * @param array $args - * @param array $instance + * @param array $args Widget arguments. + * @param array $instance Widget settings. */ public function widget( $args, $instance ) { @@ -124,9 +128,9 @@ public function widget( $args, $instance ) { $form_id = $instance['form']; $url = add_query_arg( array( - 'api_key' => WP_ConvertKit::get_api_key(), - 'v' => WP_ConvertKit::get_forms_version(), - ), + 'api_key' => WP_ConvertKit::get_api_key(), + 'v' => WP_ConvertKit::get_forms_version(), + ), 'https://forms.convertkit.com/' . $form_id . '.html' ); @@ -148,7 +152,7 @@ public function widget( $args, $instance ) { /** * Outputs the settings update form. * - * @param array $instance + * @param array $instance Widget settings. * @return null */ public function form( $instance ) { @@ -183,22 +187,24 @@ public function form( $instance ) { break; case 'select' : - - if ( empty ( $setting['options'] ) ) { - $query_args = array('page' => WP_ConvertKit::SETTINGS_PAGE_SLUG); - $settings_page_url = add_query_arg($query_args, admin_url('options-general.php')); + if ( empty( $setting['options'] ) ) { + $query_args = array( + 'page' => WP_ConvertKit::SETTINGS_PAGE_SLUG, + ); + $settings_page_url = add_query_arg( $query_args, admin_url( 'options-general.php' ) ); ?> -

-

settings.','convertkit'), $settings_page_url); ?>

+

+ +

settings.','convertkit' ), $settings_page_url ); ?>

', + $this->_args['singular'], + $item['id'] + ); + } + + /** + * Get the bulk actions for this table + * + * @return array Bulk actions + */ + public function get_bulk_actions() { + return $this->_bulk_actions; + } + + /** + * Get a list of columns + * + * @return array + */ + public function get_columns() { + return $this->_columns; + } + + /** + * Add a column to the table + * + * @param string $key Machine-readable column name. + * @param string $title Title shown to the user. + * @param boolean $sortable Whether or not this is sortable (defaults false) + */ + public function add_column( $key, $title, $sortable = false ) { + $this->_columns[ $key ] = $title; + + if ( $sortable ) { + $this->_sortable_columns[ $key ] = array( $key, false ); + } + } + + /** + * Add an item (row) to the table + * + * @param array $item A row's worth of data. + */ + public function add_item( $item ) { + array_push( $this->_data, $item ); + } + + /** + * Add a bulk action to the table + * + * @param string $key Machine-readable action name + * @param string $name Title shown to the user + */ + public function add_bulk_action( $key, $name ) { + $this->_bulk_actions[ $key ] = $name; + } + + /** + * Prepares the items (rows) to be rendered + */ + public function prepare_items() { + $total_items = count( $this->_data ); + $per_page = 25; + + $columns = $this->_columns; + $hidden = array(); + $sortable = $this->_sortable_columns; + + $this->_column_headers = array( $columns, $hidden, $sortable ); + + $current_page = $this->get_pagenum(); + + $sorted_data = $this->reorder( $this->_data ); + + $data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ),$per_page ); + + $this->items = $data; + + $this->set_pagination_args( array( + 'total_items' => $total_items, + 'per_page' => $per_page, + 'total_pages' => ceil( $total_items / $per_page ), + )); + } + + /** + * Reorder the data according to the sort parameters + * + * @return array Row data, sorted + */ + public function reorder( $data ) { + function usort_reorder( $a, $b ) { + + if ( empty( $_REQUEST['orderby'] ) ) { // WPCS: CSRF ok. + $orderby = 'title'; + } else { + $orderby = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ); // WPCS: CSRF ok. + } + + if ( empty( $_REQUEST['order'] ) ) { // WPCS: CSRF ok. + $order = 'asc'; + } else { + $order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // WPCS: CSRF ok. + } + $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); //Determine sort order. + return ( 'asc' === $order ) ? $result : -$result; //Send final sort direction to usort. + } + usort( $data, 'usort_reorder' ); + + return $data; + } +} diff --git a/lib/convertkit-api.php b/lib/convertkit-api.php deleted file mode 100644 index f3d2b5bdd..000000000 --- a/lib/convertkit-api.php +++ /dev/null @@ -1,288 +0,0 @@ -api_key = $api_key; - $this->api_secret = $api_secret; - $this->debug = $debug; - } - - /** - * Gets a resource index - * - * GET /{$resource}/ - * - * @param string $resource Resource type - * @return object API response - */ - public function get_resources($resource) { - - if(!array_key_exists($resource, $this->resources)) { - - 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( array('id' => '-2', 'name' => 'Error contacting API' ) ); - } else { - $_resource = array(); - - if ( 'forms' == $resource ) { - $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); - foreach( $response as $form ) { - if ( isset( $form['archived'] ) && $form['archived'] ) - continue; - $_resource[] = $form; - } - } elseif ( 'landing_pages' == $resource ) { - - $response = isset( $api_response['forms']) ? $api_response['forms'] : array(); - foreach( $response as $landing_page ){ - if ( 'hosted' == $landing_page['type'] ){ - if ( isset( $landing_page['archived'] ) && $landing_page['archived'] ) - continue; - $_resource[] = $landing_page; - } - } - } elseif ( 'subscription_forms' == $resource ) { - foreach( $api_response as $mapping ){ - if ( isset( $mapping['archived'] ) && $mapping['archived'] ) - continue; - $_resource[ $mapping['id'] ] = $mapping['form_id']; - } - } - - $this->resources[$resource] = $_resource; - } - } - - return $this->resources[$resource]; - } - - /** - * Adds a subscriber to a form - * - * @param string $form_id Form ID - * @param array $options Array of user data - * @return object - */ - public function form_subscribe($form_id, $options) { - $request = $this->api_version . sprintf('/forms/%s/subscribe', $form_id); - - $args = array( - 'api_key' => $this->api_key, - 'email' => $options['email'], - 'name' => $options['name'], - ); - - return $this->make_request($request, 'POST', $args); - } - - /** - * Remove subscription from a form - * - * @param array $options Array of user data - * @return object Response object - */ - public function form_unsubscribe($options) { - $request = $this->api_version . '/unsubscribe'; - - $args = array( - 'api_secret' => $this->api_secret, - 'email' => $options['email'] - ); - - return $this->make_request($request, 'PUT', $args); - } - - /** - * Get markup from ConvertKit for the provided $url - * - * @param $url - * @return string - */ - public function get_resource($url) { - $resource = ''; - - if(!empty($url) && isset($this->markup[$url])) { - $resource = $this->markup[$url]; - } else if(!empty($url)) { - $response = wp_remote_get($url, array( 'timeout' => 10 )); - - if(!is_wp_error($response)) { - if(!function_exists('str_get_html')) { - require_once(dirname(__FILE__).'/../vendor/simple-html-dom/simple-html-dom.php'); - } - - if(!function_exists('url_to_absolute')) { - require_once(dirname(__FILE__).'/../vendor/url-to-absolute/url-to-absolute.php'); - } - - $url_parts = parse_url($url); - - $body = wp_remote_retrieve_body($response); - $html = str_get_html($body); - foreach($html->find('a, link') as $element) { - if(isset($element->href)) { - $element->href = url_to_absolute($url, $element->href); - } - } - - foreach($html->find('img, script') as $element) { - if(isset($element->src)) { - $element->src = url_to_absolute($url, $element->src); - } - } - - foreach($html->find('form') as $element) { - if(isset($element->action)) { - $element->action = url_to_absolute($url, $element->action); - } else { - $element->action = $url; - } - } - - // check `status_code` for 200, otherwise log error - if ( '200' == $response['response']['code'] ) { - $this->markup[$url] = $resource = $html->save(); - } else { - $this->log('Status Code (' . $response['response']['code'] . ') for URL (' . $url .'): ' . $html->save() ); - } - } - } - - return $resource; - } - - /** - * Do a remote request. - * - * @param string $path - * @return array - */ - 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)); - - $this->log( "API Request (_get_api_response): " . $url ); - - $data = get_transient( 'convertkit_get_api_response' ); - - if ( ! $data ) { - - $response = wp_remote_get( $url, array( 'timeout' => 10, 'sslverify' => false ) ); - - if ( is_wp_error( $response ) ) { - $this->log( "Error: " . $response->get_error_message() ); - - return array( 'error' => $response->get_error_message() ); - } else { - $data = json_decode( wp_remote_retrieve_body( $response ), true ); - } - - set_transient( 'convertkit_get_api_response', $data, 300 ); - - $this->log( "API Response (_get_api_response): " . print_r( $data, true ) ); - } else { - $this->log( "Transient Response (_get_api_response)" ); - } - - return $data; - } - - /** - * Make a request to the ConvertKit API - * - * @param string $request Request string - * @param string $method HTTP Method - * @param array $args Request arguments - * @return object Response object - */ - public function make_request($request, $method = 'GET', $args = array()) { - - $url = $this->build_request_url($request, $args); - $this->log( "API Request (make_request): " . $url ); - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HEADER, false); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - if ( 'PUT' == $method ){ - curl_setopt($ch, CURLOPT_PUT, true); - } - - $results = curl_exec($ch); - curl_close($ch); - - $this->log( "API Response (make_request): " . print_r( json_decode( $results ), true) ); - - return json_decode($results); - } - - /** - * Build the full request URL - * - * @param string $request Request path - * @param array $args Request arguments - * @return string Request URL - */ - public function build_request_url($request, array $args) { - return $this->api_url_base . $request . '?' . http_build_query( $args ); - } - - /** - * @param $message - */ - public function log( $message ) { - - if ( 'on' == $this->debug ) { - $dir = dirname( __FILE__ ); - - $handle = fopen( trailingslashit( $dir ) . 'log.txt', 'a' ); - if ( $handle ) { - $time = date_i18n( 'm-d-Y @ H:i:s -' ); - fwrite( $handle, $time . " " . $message . "\n" ); - fclose( $handle ); - } - } - - } - -} diff --git a/lib/integration/class-convertkit-contactform7-integration.php b/lib/integration/class-convertkit-contactform7-integration.php new file mode 100644 index 000000000..5c96192c9 --- /dev/null +++ b/lib/integration/class-convertkit-contactform7-integration.php @@ -0,0 +1,84 @@ +options = get_option( '_wp_convertkit_integration_wishlistmember_settings' ); + $api_key = $general_options && array_key_exists( 'api_key', $general_options ) ? $general_options['api_key'] : null; + $api_secret = $general_options && array_key_exists( 'api_secret', $general_options ) ? $general_options['api_secret'] : null; + $debug = $general_options && array_key_exists( 'debug', $general_options ) ? $general_options['debug'] : null; + $this->api = new ConvertKit_API( $api_key, $api_secret, $debug ); + + add_action( 'wpcf7_submit', array( $this, 'handle_wpcf7_submit' ), 10, 2 ); + } + + /** + * Handle checking submitted CF7 forms for a CK form mapping. + * + * If a mapping is found and options exist then the form submitter is subscribed. + * + * @param WPCF7_ContactForm $contact_form + * @param $result + */ + public function handle_wpcf7_submit( $contact_form, $result ) { + + if ( $result['demo_mode'] ) { + return; + } + + if ( 'mail_sent' === $result['status'] ) { + + $mapping = get_option( '_wp_convertkit_integration_contactform7_settings' ); + + if ( is_array( $mapping ) ) { + if ( isset( $mapping[ $contact_form->id() ] ) && 'default' !== $mapping[ $contact_form->id() ] ) { + $submission = WPCF7_Submission::get_instance(); + + if ( $submission ) { + $posted_data = $submission->get_posted_data(); + + $name = $posted_data['your-name']; + $email = $posted_data['your-email']; + + if ( ! empty( $email ) ) { + $this->api->form_subscribe( $mapping[ $contact_form->id() ], + array( + 'email' => $email, + 'name' => $name, + ) + ); + } + } + } + } + } + } + +} + +new ConvertKit_ContactForm7_Integration(); diff --git a/lib/integration/class-convertkit-wishlist-integration.php b/lib/integration/class-convertkit-wishlist-integration.php new file mode 100644 index 000000000..83917b2c6 --- /dev/null +++ b/lib/integration/class-convertkit-wishlist-integration.php @@ -0,0 +1,179 @@ +options = get_option( '_wp_convertkit_integration_wishlistmember_settings' ); + $api_key = $general_options && array_key_exists( 'api_key', $general_options ) ? $general_options['api_key'] : null; + $api_secret = $general_options && array_key_exists( 'api_secret', $general_options ) ? $general_options['api_secret'] : null; + $debug = $general_options && array_key_exists( 'debug', $general_options ) ? $general_options['debug'] : null; + $this->api = new ConvertKit_API( $api_key,$api_secret,$debug ); + + // When a user has levels added or registers check for a mapping to a ConvertKit form. + add_action( 'wishlistmember_add_user_levels', array( $this, 'add_user_levels' ), 10, 2 ); + + // When a user has levels removed check for a mapping to a ConvertKit tag, or if the subscriber + // should be removed from ConvertKit. + add_action( 'wishlistmember_remove_user_levels', array( $this, 'remove_user_levels' ), 10, 2 ); + } + + /** + * Callback function for wishlistmember_add_user_levels action + * + * @param string $member_id ID for member that has just had levels added. + * @param array $levels Levels to which member was added. + */ + public function add_user_levels( $member_id, $levels ) { + $member = $this->get_member( $member_id ); + + foreach ( $levels as $wlm_level_id ) { + if ( ! isset( $this->options[ $wlm_level_id . '_form' ] ) ) { + continue; + } + + $this->member_resource_subscribe( + $member, + $this->options[ $wlm_level_id . '_form' ] + ); + } + } + + /** + * Note: Form level unsubscribe is not available in v3 of the API. + * + * Callback function for wishlistmember_remove_user_levels action + * + * @param string $member_id ID for member that has just had levels removed. + * @param array $levels Levels from which member was removed. + */ + public function remove_user_levels( $member_id, $levels ) { + + $member = $this->get_member( $member_id ); + + foreach ( $levels as $wlm_level_id ) { + // get the mapping if it is set + $unsubscribe = ( isset( $this->options[ $wlm_level_id . '_unsubscribe' ] ) ) ? $this->options[ $wlm_level_id . '_unsubscribe' ] : 0; + + if ( $unsubscribe && 'unsubscribe' === $unsubscribe ) { + // If mapping is set to "Unsubscribe from all" + $this->member_resource_unsubscribe( $member ); + } elseif ( $unsubscribe ) { + // If mapping is a positive integer then tag customer + $this->member_tag( $member, $this->options[ $wlm_level_id . '_unsubscribe' ] ); + } + } + + } + + /** + * Subscribes a member to a ConvertKit resource + * + * @param array $member UserInfo from WishList Member. + * @param string $form_id ConvertKit form id. + * @return object Response object from API + */ + public function member_resource_subscribe( $member, $form_id ) { + + // Check for temp email. + if ( preg_match( '/temp_[a-f0-9]{32}/', $member['user_email'] ) ) { + $email = $member['wlm_origemail']; + } else { + $email = $member['user_email']; + } + + // Note Wishlist Member combines first and last name into 'display_name'. + return $this->api->form_subscribe( + $form_id, + array( + 'email' => $email, + 'name' => $member['display_name'], + ) + ); + } + + /** + * Unsubscribes a member from a ConvertKit resource + * + * @param array $member UserInfo from WishList Member. + * @param string $form_id ConvertKit form id. + * @return object Response object from API + */ + public function member_resource_unsubscribe( $member ) { + return $this->api->form_unsubscribe( + array( + 'email' => $member['user_email'], + ) + ); + } + + /** + * Tag a member + * + * @param array $member UserInfo from WishList Member + * @param string $tag ConvertKit Tag ID + * @return object Response object from API + */ + public function member_tag( $member, $tag ) { + return $this->api->add_tag( + $tag, + array( + 'email' => $member['user_email'], + ) + ); + } + + /** + * Gets a WLM member using the wlmapi functions + * + * @param string $id The member id. + * @return array The member fields from the API request + */ + public function get_member( $id ) { + if ( ! function_exists( 'wlmapi_get_member' ) ) { + return false; + } + + $wlm_get_member = wlmapi_get_member( $id ); + + if ( 0 === $wlm_get_member['success'] ) { + return false; + } + + return $wlm_get_member['member'][0]['UserInfo']; + } + +} + +new ConvertKit_Wishlist_Integration(); diff --git a/lib/integration/contactform7.php b/lib/integration/contactform7.php deleted file mode 100644 index 9bb5626e1..000000000 --- a/lib/integration/contactform7.php +++ /dev/null @@ -1,65 +0,0 @@ -options = get_option('_wp_convertkit_integration_wishlistmember_settings'); - $api_key = $general_options && array_key_exists("api_key", $general_options) ? $general_options['api_key'] : null; - $api_secret = $general_options && array_key_exists("api_secret", $general_options) ? $general_options['api_secret'] : null; - $debug = $general_options && array_key_exists("debug", $general_options) ? $general_options['debug'] : null; - $this->api = new ConvertKitAPI($api_key,$api_secret,$debug); - - add_action( 'wpcf7_submit', array( $this, 'handle_wpcf7_submit' ), 10, 2); - } - - /** - * Handle checking submitted CF7 forms for a CK form mapping. - * - * If a mapping is found and options exist then the form submitter is subscribed. - * - * @param WPCF7_ContactForm $contact_form - * @param $result - */ - public function handle_wpcf7_submit( $contact_form, $result ) { - - if ( $result['demo_mode'] ) { - return; - } - - if ( 'mail_sent' == $result['status'] ) { - - $mapping = get_option( '_wp_convertkit_integration_contactform7_settings' ); - - if ( is_array( $mapping ) ) { - if ( isset( $mapping[ $contact_form->id() ]) && 'default' != $mapping[ $contact_form->id() ] ) { - $submission = WPCF7_Submission::get_instance(); - - if ( $submission ) { - $posted_data = $submission->get_posted_data(); - - $name = $posted_data['your-name']; - $email = $posted_data['your-email']; - - if ( ! empty( $email ) ) { - $this->api->form_subscribe( $mapping[ $contact_form->id() ], array( 'email' => $email, 'name' => $name ) ); - } - } - } - } - } - } - -} - -$convertkit_contactform7_integration = new ConvertKitContactForm7Integration; diff --git a/lib/integration/wishlist_member.php b/lib/integration/wishlist_member.php deleted file mode 100644 index dd13bded8..000000000 --- a/lib/integration/wishlist_member.php +++ /dev/null @@ -1,146 +0,0 @@ -options = get_option('_wp_convertkit_integration_wishlistmember_settings'); - $api_key = $general_options && array_key_exists("api_key", $general_options) ? $general_options['api_key'] : null; - $api_secret = $general_options && array_key_exists("api_secret", $general_options) ? $general_options['api_secret'] : null; - $debug = $general_options && array_key_exists("debug", $general_options) ? $general_options['debug'] : null; - $this->api = new ConvertKitAPI($api_key,$api_secret,$debug); - - add_action( - 'wishlistmember_add_user_levels', // hook - array($this, 'add_user_levels'), // function to call - null, // priority (default is fine) - 2 // number of arguments passed - ); - - add_action( - 'wishlistmember_remove_user_levels', // hook - array($this, 'remove_user_levels'), // function to call - null, // priority - 2 // number of arguments passed - ); - } - - /** - * Callback function for wishlistmember_add_user_levels action - * - * @param string $member_id ID for member that has just had levels added - * @param array $levels Levels to which member was added - */ - public function add_user_levels($member_id, $levels) { - $member = $this->get_member($member_id); - - foreach ($levels as $wlm_level_id) { - if (!isset($this->options[$wlm_level_id . '_form'])) continue; - - $this->member_resource_subscribe( - $member, - $this->options[$wlm_level_id . '_form'] - ); - } - } - - /** - * Note: Form level unsubscribe is not available in v3 of the API. - * - * Callback function for wishlistmember_remove_user_levels action - * - * @param string $member_id ID for member that has just had levels removed - * @param array $levels Levels from which member was removed - */ - public function remove_user_levels($member_id, $levels) { - - $member = $this->get_member($member_id); - - foreach ($levels as $wlm_level_id) { - if ( - isset($this->options[$wlm_level_id . '_form']) - && isset($this->options[$wlm_level_id . '_unsubscribe']) - && $this->options[$wlm_level_id . '_unsubscribe'] == '1' - ) { - $this->member_resource_unsubscribe( - $member, - $this->options[$wlm_level_id . '_form'] - ); - } - } - - } - - /** - * Subscribes a member to a ConvertKit resource - * - * @param array $member UserInfo from WishList Member - * @param string $form_id ConvertKit form id - * @return object Response object from API - */ - public function member_resource_subscribe($member, $form_id) { - - // Check for temp email - if ( preg_match('/temp_[a-f0-9]{32}/', $member['user_email'] ) ) { - $email = $member['wlm_origemail']; - } else { - $email = $member['user_email']; - } - - // Note Wishlist Member combines first and last name into 'display_name' - return $this->api->form_subscribe( - $form_id, - array( - 'email' => $email, - 'name' => $member['display_name'], - ) - ); - } - - /** - * Unsubscribes a member from a ConvertKit resource - * - * @param array $member UserInfo from WishList Member - * @param string $form_id ConvertKit form id - * @return object Response object from API - */ - public function member_resource_unsubscribe($member, $form_id) { - return $this->api->form_unsubscribe( - array( - 'email' => $member['user_email'] - ) - ); - } - - /** - * Gets a WLM member using the wlmapi functions - * - * @param string $id The member id - * @return array The member fields from the API request - */ - public function get_member($id) { - if (!function_exists('wlmapi_get_member')) return false; - - $wlm_get_member = wlmapi_get_member($id); - - if ($wlm_get_member['success'] == 0) return false; - - return $wlm_get_member['member'][0]['UserInfo']; - } - - } - - $convertkit_wishlist_integration = new ConvertKitWishlistIntegration; -} diff --git a/lib/multi_value_field_table.php b/lib/multi_value_field_table.php deleted file mode 100644 index 47b0c0102..000000000 --- a/lib/multi_value_field_table.php +++ /dev/null @@ -1,145 +0,0 @@ - 'item', - 'plural' => 'items', - 'ajax' => false - )); - } - - /** - * Set default column attributes - * - * @param array $item A singular item (one full row's worth of data) - * @param array $column_name The name/slug of the column to be processed - * @return string Text or HTML to be placed inside the column - */ - public function column_default($item, $column_name) { - return $item[$column_name]; - } - - /** - * Provide a callback function to render the checkbox column - * - * @param array $item A row's worth of data - * @return string The formatted string with a checkbox - */ - public function column_cb($item) { - return sprintf( - '', - $this->_args['singular'], - $item['id'] - ); - } - - /** - * Get the bulk actions for this table - * - * @return array Bulk actions - */ - public function get_bulk_actions() { - return $this->_bulk_actions; - } - - /** - * Get a list of columns - * - * @return array - */ - public function get_columns() { - return $this->_columns; - } - - /** - * Add a column to the table - * - * @param string $key Machine-readable column name - * @param string $title Title shown to the user - * @param boolean $sortable Whether or not this is sortable (defaults false) - */ - public function add_column($key, $title, $sortable = false) { - $this->_columns[$key] = $title; - - if ($sortable) $this->_sortable_columns[$key] = array($key, false); - } - - /** - * Add an item (row) to the table - * - * @param array $item A row's worth of data - */ - public function add_item($item) { - array_push($this->_data, $item); - } - - /** - * Add a bulk action to the table - * - * @param string $key Machine-readable action name - * @param string $name Title shown to the user - */ - public function add_bulk_action($key, $name) { - $this->_bulk_actions[$key] = $name; - } - - /** - * Prepares the items (rows) to be rendered - */ - public function prepare_items() { - $total_items = count($this->_data); - $per_page = 25; - - $columns = $this->_columns; - $hidden = array(); - $sortable = $this->_sortable_columns; - - $this->_column_headers = array($columns, $hidden, $sortable); - - $current_page = $this->get_pagenum(); - - $sorted_data = $this->reorder($this->_data); - - $data = array_slice($sorted_data, (($current_page-1)*$per_page),$per_page); - - $this->items = $data; - - $this->set_pagination_args(array( - 'total_items' => $total_items, - 'per_page' => $per_page, - 'total_pages' => ceil($total_items/$per_page) - )); - } - - /** - * Reorder the data according to the sort parameters - * - * @return array Row data, sorted - */ - public function reorder($data) { - function usort_reorder($a, $b) { - $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title - $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc - $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order - return ($order==='asc') ? $result : -$result; //Send final sort direction to usort - } - usort($data, 'usort_reorder'); - - return $data; - } -} diff --git a/lib/template-tags.php b/lib/template-tags.php index d38c432de..6c93ce649 100644 --- a/lib/template-tags.php +++ b/lib/template-tags.php @@ -5,6 +5,6 @@ * @param $attributes * @return mixed|void */ -function wp_convertkit_get_form_embed($attributes) { - return apply_filters('wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed($attributes), $attributes); -} \ No newline at end of file +function wp_convertkit_get_form_embed( $attributes ) { + return apply_filters( 'wp_convertkit_get_form_embed', WP_ConvertKit::get_form_embed( $attributes ), $attributes ); +} diff --git a/readme.txt b/readme.txt index 112b9685f..9b06d6d7e 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.7.3 -Stable tag: 1.4.6 +Stable tag: 1.4.7 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -44,6 +44,12 @@ Yes, for it to work you must first have an account on ConvertKit.com == Changelog == +### 1.4.7 2017-06-01 +* Code refactor with WordPress Code Standards +* Added ability to tag a customer when WishList Member membership lapses +* Added WishList Member tag a customer +* Removed curl and replaced with wp_remote_request + ### 1.4.6 2017-03-29 * Fix for landing pages not appearing. * Added code to API to not return status_code 404 content diff --git a/views/backend/meta-boxes/meta-box.php b/views/backend/meta-boxes/meta-box.php index 809ed6073..24c25e89b 100644 --- a/views/backend/meta-boxes/meta-box.php +++ b/views/backend/meta-boxes/meta-box.php @@ -1,47 +1,58 @@ + - + - post_type) { ?> + post_type ) { ?> - +

Default to use the form specified on the settings page,', 'convertkit'), esc_attr(esc_url($settings_link))); - echo __('None to not display a form, or any other option to specify a particular form for this piece of content.', 'convertkit'); + /* translators: 1: settings url */ + printf( __( 'Choose Default to use the form specified on the settings page,', 'convertkit' ), esc_attr( esc_url( $settings_link ) ) ); // WPCS: XSS ok. + echo __( 'None to not display a form, or any other option to specify a particular form for this piece of content.', 'convertkit' ); // WPCS: XSS ok. ?>

- - + +

- -

+

- '', 'default_form' => 0, ); - /** @var array */ + /** + * @var array + */ private static $forms_markup = array(); /** @var array */ @@ -69,44 +79,44 @@ public static function init() { * Add WP Actions */ private static function add_actions() { - add_action( 'plugins_loaded', array(__CLASS__, 'load_textdomain' ) ); - if(is_admin()) { - add_action('add_meta_boxes_page', array(__CLASS__, 'add_meta_boxes')); - add_action('add_meta_boxes_post', array(__CLASS__, 'add_meta_boxes')); + add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) ); + if ( is_admin() ) { + add_action( 'add_meta_boxes_page', array( __CLASS__, 'add_meta_boxes' ) ); + add_action( 'add_meta_boxes_post', array( __CLASS__, 'add_meta_boxes' ) ); } else { - add_action('template_redirect', array(__CLASS__, 'page_takeover')); + add_action( 'template_redirect', array( __CLASS__, 'page_takeover' ) ); } - add_action('widgets_init', array(__CLASS__,'ck_register_widgets')); + add_action( 'widgets_init', array( __CLASS__, 'ck_register_widgets' ) ); - add_action('save_post', array(__CLASS__, 'save_post_meta'), 10, 2); + add_action( 'save_post', array( __CLASS__, 'save_post_meta' ), 10, 2 ); - add_action('init', array(__CLASS__, 'upgrade') , 10); + add_action( 'init', array( __CLASS__, 'upgrade' ) , 10 ); } /** * Load plugin textdomain */ public static function load_textdomain() { - load_plugin_textdomain( 'convertkit', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); + load_plugin_textdomain( 'convertkit', false, basename( dirname( __FILE__ ) ) . '/languages/' ); } /** * Add WP Filters */ private static function add_filters() { - if(!is_admin()) { - add_filter('the_content', array(__CLASS__, 'append_form')); + if ( ! is_admin() ) { + add_filter( 'the_content', array( __CLASS__, 'append_form' ) ); } - add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(__CLASS__, 'add_settings_page_link')); + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, 'add_settings_page_link' ) ); } /** * Register ConvertKit shortcodes */ private static function register_shortcodes() { - add_shortcode('convertkit', array(__CLASS__, 'shortcode')); + add_shortcode( 'convertkit', array( __CLASS__, 'shortcode' ) ); } /** @@ -115,23 +125,25 @@ private static function register_shortcodes() { * @param $links * @return array */ - public static function add_settings_page_link($links) { - $settings_link = sprintf('%s', self::_get_settings_page_link(), __('Settings', 'convertkit')); + public static function add_settings_page_link( $links ) { + $settings_link = sprintf( '%s', self::_get_settings_page_link(), __( 'Settings', 'convertkit' ) ); - return array('settings' => $settings_link) + $links; + return array( + 'settings' => $settings_link, + ) + $links; } /** * Add Meta Boxes callback * - * @param $post + * @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'); + 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'); + 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' ); } } @@ -140,40 +152,55 @@ 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'); + public static function display_meta_box( $post ) { + $forms = self::$api->get_resources( 'forms' ); + $landing_pages = self::$api->get_resources( 'landing_pages' ); - $meta = self::_get_meta($post->ID); + $meta = self::_get_meta( $post->ID ); $settings_link = self::_get_settings_page_link(); - include('views/backend/meta-boxes/meta-box.php'); + include( 'views/backend/meta-boxes/meta-box.php' ); } /** * Save post meta callback * - * @param $post_id - * @param $post + * @param int $post_id Post id. + * @param WP_Post $post The post. */ - public static function save_post_meta($post_id, $post) { - $data = stripslashes_deep($_POST); - if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id) || !isset($data['wp-convertkit-save-meta-nonce']) || !wp_verify_nonce($data['wp-convertkit-save-meta-nonce'], 'wp-convertkit-save-meta')) { + public static function save_post_meta( $post_id, $post ) { + if ( wp_is_post_autosave( $post_id ) + || wp_is_post_revision( $post_id ) + || ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) // WPCS input var okay. + || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { // WPCS input var okay. return; } - - self::_set_meta($post_id, $data['wp-convertkit']); + if ( isset( $_POST['wp-convertkit'] ) ) { // WPCS input var okay. + $form = ''; + if ( isset( $_POST['wp-convertkit']['form'] ) ) { // WPCS input var okay. + $form = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form'] ) ); // WPCS input var okay. + } + $landing_page = ''; + if ( isset( $_POST['wp-convertkit']['landing_page'] ) ) { // WPCS input var okay. + $landing_page = sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['landing_page'] ) ); // WPCS input var okay. + } + $meta = array( + 'form' => $form, + 'landing_page' => $landing_page, + ); + update_post_meta( $post_id, self::POST_META_KEY, $meta ); + } } /** * Page/Post display callback * - * @param $content + * @param string $content The post content. * @return string */ public static function append_form( $content ) { - if(is_singular(array('post')) || is_page()) { + if ( is_singular( array( 'post' ) ) || is_page() ) { $attributes = self::_get_meta( get_the_ID() ); @@ -182,15 +209,16 @@ public static function append_form( $content ) { if ( isset( $attributes['form'] ) && ( 0 < $attributes['form'] ) ) { $form_id = $attributes['form']; } else { - if ( -1 == $attributes['form'] ) - $form_id = self::_get_settings('default_form'); + 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, - ), + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), 'https://forms.convertkit.com/' . $form_id . '.html' ); @@ -207,13 +235,15 @@ 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'))) { - $landing_page = self::$api->get_resource($landing_page_url); + if ( isset( $queried_object->post_type ) + && 'page' === $queried_object->post_type ) { + + $landing_page_url = self::_get_meta( $queried_object->ID, 'landing_page' ); - if(!empty($landing_page)) { - echo $landing_page; + $landing_page = self::$api->get_resource( $landing_page_url ); + + if ( ! empty( $landing_page ) ) { + echo $landing_page; // WPCS: XSS ok. exit; } } @@ -223,25 +253,25 @@ public static function page_takeover() { /** * Register widget. */ - public static function ck_register_widgets(){ + public static function ck_register_widgets() { register_widget( 'CK_Widget_Form' ); } /** * Shortcode callback * - * @param $attributes + * @param array $attributes Shortcode attributes. * @param null $content * @return mixed|void */ - public static function shortcode($attributes, $content = null) { + 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, - ), + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), 'https://forms.convertkit.com/' . $form_id . '.html' ); } elseif ( isset( $attributes['form'] ) ) { @@ -249,15 +279,15 @@ public static function shortcode($attributes, $content = null) { $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, - ), + 'api_key' => self::_get_settings( 'api_key' ), + 'v' => self::$forms_version, + ), 'https://forms.convertkit.com/' . $form_id . '.html' ); } @@ -268,7 +298,7 @@ public static function shortcode($attributes, $content = null) { $form_markup = ''; } - return apply_filters('wp_convertkit_get_form_embed', $form_markup, $attributes ); + return apply_filters( 'wp_convertkit_get_form_embed', $form_markup, $attributes ); } /** @@ -277,7 +307,7 @@ public static function shortcode($attributes, $content = null) { * @return array|null */ private static function _get_meta_defaults() { - if(is_null(self::$meta_defaults)) { + if ( is_null( self::$meta_defaults ) ) { self::$meta_defaults = array( 'form' => -1, 'landing_page' => '', @@ -288,43 +318,31 @@ private static function _get_meta_defaults() { } /** - * @param $post_id - * @param null $meta_key + * Get selected post meta + * + * @param int $post_id Post ID. + * @param null $meta_key Key string to get. * * @return array|bool */ - private static function _get_meta($post_id, $meta_key = null) { - $post_id = empty($post_id) ? get_the_ID() : $post_id; + private static function _get_meta( $post_id, $meta_key = null ) { + $post_id = empty( $post_id ) ? get_the_ID() : $post_id; - $meta = get_post_meta($post_id, self::POST_META_KEY, true); + $meta = get_post_meta( $post_id, self::POST_META_KEY, true ); $meta_defaults = self::_get_meta_defaults(); - if(empty($meta)) { + if ( empty( $meta ) ) { $meta = $meta_defaults; - $old_value = intval(get_post_meta($post_id, '_convertkit_convertkit_form', true)); - if(0 !== $old_value) { + $old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) ); + if ( 0 !== $old_value ) { $meta['form'] = $old_value; } } - $meta = shortcode_atts($meta_defaults, $meta); - - return is_null($meta_key) ? $meta : (isset($meta[$meta_key]) ? $meta[$meta_key] : false); - } - - /** - * @param $post_id - * @param $meta - * - * @return mixed - */ - private static function _set_meta($post_id, $meta) { - $post_id = empty($post_id) ? get_the_ID() : $post_id; - - update_post_meta($post_id, self::POST_META_KEY, $meta); + $meta = shortcode_atts( $meta_defaults, $meta ); - return $meta; + return is_null( $meta_key ) ? $meta : ( isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : false ); } /** @@ -333,54 +351,62 @@ private static function _set_meta($post_id, $meta) { * @param null $settings_key * @return mixed|null|void */ - private static function _get_settings($settings_key = null) { - $settings = get_option(self::SETTINGS_NAME, self::$settings_defaults); + private static function _get_settings( $settings_key = null ) { + $settings = get_option( self::SETTINGS_NAME, self::$settings_defaults ); - return is_null($settings_key) ? $settings : (isset($settings[$settings_key]) ? $settings[$settings_key] : null); + return is_null( $settings_key ) ? $settings : ( isset( $settings[ $settings_key ] ) ? $settings[ $settings_key ] : null); } /** * Get instance of ConvertKitAPI */ private static function _api_connect() { - $api_key = self::_get_settings('api_key'); - $api_secret = self::_get_settings('api_secret'); - $debug = self::_get_settings('debug'); + $api_key = self::_get_settings( 'api_key' ); + $api_secret = self::_get_settings( 'api_secret' ); + $debug = self::_get_settings( 'debug' ); - self::$api = new ConvertKitAPI($api_key,$api_secret,$debug); + self::$api = new ConvertKit_API( $api_key, $api_secret, $debug ); } /** * Get instance of the CK API * - * @return ConvertKitAPI + * @return ConvertKit_API */ - public static function get_api(){ + public static function get_api() { return self::$api; } - public static function get_api_key(){ + /** + * Get ConvertKit API key + * + * @return mixed|null|void + */ + public static function get_api_key() { return self::_get_settings( 'api_key' ); } - public static function get_forms_version(){ + /** + * @return string + */ + public static function get_forms_version() { return self::$forms_version; } - /** * Get link to the plugin settings page * - * @param array $query_args + * @param array $query_args Args to add to URL. * @return string */ - private static function _get_settings_page_link($query_args = array()) { - $query_args = array('page' => self::SETTINGS_PAGE_SLUG) + $query_args; + private static function _get_settings_page_link( $query_args = array() ) { + $query_args = array( + 'page' => self::SETTINGS_PAGE_SLUG, + ) + $query_args; - return add_query_arg($query_args, admin_url('options-general.php')); + return add_query_arg( $query_args, admin_url( 'options-general.php' ) ); } - /** * Run version specific upgrade. */ @@ -388,14 +414,12 @@ public static function upgrade() { $current_version = get_option( 'convertkit_version' ); - if ( ! $current_version) { - - // Run 1.4.1 upgrade - $settings = self::_get_settings( ); + if ( ! $current_version ) { + // Run 1.4.1 upgrade. + $settings = self::_get_settings(); if ( isset( $settings['api_key'] ) ) { - - // Get all posts and pages to track what has been updated + // Get all posts and pages to track what has been updated. $posts = get_option( '_wp_convertkit_upgrade_posts' ); if ( ! $posts ) { @@ -403,18 +427,17 @@ public static function upgrade() { $args = array( 'post_type' => array( 'post', 'page' ), 'fields' => 'ids', - 'posts_per_page' => -1, ); $result = new WP_Query( $args ); - if (! is_wp_error( $result ) ) { + 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');; + // 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'] ) ) { @@ -425,19 +448,19 @@ public static function upgrade() { } // 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. + // 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'] ) ) + 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'] ) ) + } + 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 ]); + unset( $posts[ $key ] ); update_option( '_wp_convertkit_upgrade_posts', $posts ); } @@ -446,19 +469,14 @@ public static function upgrade() { update_option( 'convertkit_version', self::VERSION ); delete_option( '_wp_convertkit_upgrade_posts' ); } - } else { - update_option( 'convertkit_version', self::VERSION ); - - } - - } - + } // End if(). + } // End if(). } } WP_ConvertKit::init(); -} +} // End if(). -include 'admin/settings.php'; +include 'admin/class-convertkit-settings.php';