/**
  * Define connection via SOAP client and Api Key.
  *
  * @since 0.1.0
  */
 public function __construct()
 {
     $this->client = new \SoapClient('http://api.cleverreach.com/soap/interface_v5.1.php?wsdl');
     $helper = new Core\Cre_Helper();
     $this->api_key = sanitize_key(trim(apply_filters('cleverreach_extension_api_key', $helper->get_option('api_key'))));
     $this->list_id = sanitize_key(absint(trim(apply_filters('cleverreach_extension_list_id', $helper->get_option('list_id')))));
 }
 /**
  * Parse shortcode parameters.
  *
  * @since 0.1.0
  * @param $params
  *
  * @return string
  */
 public function parse_shortcode($params)
 {
     $helper = new Cre_Helper();
     $client = new Api\Cleverreach();
     $form = new Api\Cleverreach_Form_Adapter($client);
     // Parse shortcode attributes.
     $atts = shortcode_atts(array('form_id' => $helper->get_option('form_id')), $params, 'cleverreach_extension');
     $html = '<div class="cr_form-container">';
     // Parse form according to shortcode attributes.
     if ('custom' === $atts['form_id']) {
         $html .= apply_filters('cleverreach_extension_subscribe_form', esc_html__('Please apply your own form within your plugin or theme.', 'cleverreachextension'));
     } else {
         $result = $form->get_embedded_code($atts['form_id']);
         $html .= $result->data;
     }
     $html .= '</div>';
     // end of .cr_form-container
     return $html;
 }
 /**
  * Formats array for drop down in terms of Visual Composer.
  *
  * @since  0.3.0
  * @access private
  * @param  string
  *
  * @return array
  */
 private function get_values($param)
 {
     $return = array();
     $items = array();
     $defined_options = $this->helper->get_option_group();
     if ($this->client->has_valid_api_key() && $defined_options['list_id']) {
         if ('list' == $param) {
             $group = new Api\Cleverreach_Group_Adapter($this->client);
             $items = $this->helper->parse_list($group->get_list(), 'list_id');
         } elseif ('form' == $param) {
             $form = new Api\Cleverreach_Form_Adapter($this->client);
             $items = $this->helper->parse_list($form->get_list($defined_options['list_id']), 'form_id', true);
         }
         // Prepare drop down lists in terms of Visual Composer.
         foreach ($items as $item) {
             $return[$item['name']] = $item['id'];
         }
     }
     return $return;
 }
 /**
  * Parse shortcode parameters.
  *
  * @since 0.3.0
  * @param $params
  *
  * @return string
  */
 public function parse_shortcode($params)
 {
     $helper = new Cre_Helper();
     $client = new Api\Cleverreach();
     $form = new Api\Cleverreach_Form_Adapter($client);
     // Parse shortcode attributes with defaults.
     $atts = shortcode_atts(array('list_id' => $helper->get_option('list_id'), 'form_id' => $helper->get_option('form_id'), 'custom_form' => $helper->get_option('custom_form'), 'source' => $helper->get_option('source')), $params, 'cleverreach_extension');
     $html = '<div class="cr_form-container">';
     // Render (custom/default) form.
     if ('custom' === $atts['form_id'] || $atts['custom_form']) {
         wp_enqueue_script('cleverreach-extension');
         $html .= apply_filters('cleverreach_extension_subscribe_form', esc_html__('Please apply your own form within your plugin or theme.', 'cleverreach-extension'));
     } else {
         if ('true' === $helper->get_option('ajax')) {
             wp_enqueue_script('cleverreach-extension');
             wp_enqueue_script('cleverreach-extension-fallback');
         }
         // Get form code or message from CleverReach.
         $embedded_code = $form->get_embedded_code($atts['form_id']);
         if (is_object($embedded_code)) {
             // Force HTTPS everywhere.
             $embedded_code = str_replace('http://', 'https://', $embedded_code->data);
             if ('true' === $helper->get_option('ajax')) {
                 // Remove default CleverReach scripts (including recaptcha widget).
                 $embedded_code = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $embedded_code);
             }
         }
         $html .= $embedded_code;
     }
     // Include custom `form_id`.
     if (!empty($atts['form_id'])) {
         $form_id = 'custom' === $atts['form_id'] ? $helper->get_option('form_id') : $atts['form_id'];
         $html = str_replace('<form ', '<form data-form="' . $form_id . '" ', $html);
     }
     // Include custom `list_id`.
     if (!empty($atts['list_id'])) {
         $html = str_replace('<form ', '<form data-list="' . $atts['list_id'] . '" ', $html);
     }
     // Include custom `source`.
     if (!empty($atts['source'])) {
         $html = str_replace('<form ', '<form data-source="' . $atts['source'] . '" ', $html);
     }
     return $html . '</div>';
     // end of .cr_form-container
 }
 /**
  * Render promotion section.
  * For users without api key only.
  *
  * @since 0.1.0
  *
  * @return string
  */
 public function render_promotion_notice()
 {
     $result = '';
     $helper = new Core\Cre_Helper();
     if (!$helper->has_option('api_key')) {
         $result .= '<p>';
         $result .= esc_html__('Still need a CleverReach account?', 'cleverreachextension') . ' ';
         $result .= '<a href="' . esc_url('http://www.cleverreach.com/frontend/account.php?rk=85097mbwkysub"') . '">';
         $result .= esc_html__('Sign up for free!', 'cleverreachextension');
         $result .= '</a>';
         $result .= '</p>';
         $result .= '<p><a href="' . esc_url('http://www.cleverreach.com/frontend/account.php?rk=85097mbwkysub"') . '">';
         $result .= '<img src="http://s3-eu-west-1.amazonaws.com/cloud-files.crsend.com/img/affiliate/en/468_60.png" />';
         $result .= '</a></p>';
     }
     return $result;
 }
<?php

/**
 * Visual Composer Integration
 * Hooks into `cleverreach_extension` shortcode
 *
 * @since 0.2.0
 *
 * Optimized for Visual Composer v4.6.2
 * @link http://vc.wpbakery.com/
 * @docs https://wpbakery.atlassian.net/wiki/display/VC/Visual+Composer+Pagebuilder+for+WordPress
 */
use CleverreachExtension\Core;
use CleverreachExtension\Core\Api;
$values = array();
$client = new Api\Cleverreach();
$helper = new Core\Cre_Helper();
if ($client->has_valid_api_key() && $helper->has_option('list_id')) {
    $form = new Api\Cleverreach_Form_Adapter($client);
    $forms = $helper->parse_list($form->get_list($helper->get_option('list_id')), 'form_id');
    // Prepare dropdown list in terms of Visual Composer.
    foreach ($forms as $form) {
        $values[$form['name']] = $form['id'];
    }
    // Append `Custom` at the very end of the list.
    $values[esc_html__('Custom', 'cleverreachextension')] = 'custom';
}
vc_map(array('name' => esc_html__('CleverReach Form', 'cleverreachextension'), 'base' => 'cleverreach_extension', 'class' => 'cleverreach_extension', 'icon' => '', 'category' => esc_html__('CleverReach', 'cleverreachextension'), 'admin_enqueue_js' => array(), 'admin_enqueue_css' => array(), 'params' => array(array('type' => 'dropdown', 'holder' => 'div', 'class' => 'cre_form_id', 'heading' => esc_html__('Form', 'cleverreachextension'), 'param_name' => 'form_id', 'value' => $values, 'std' => $helper->get_option('form_id'), 'description' => esc_html__('Check the wiki on how to customize your form.', 'cleverreachextension')))));
 /**
  * Parse form submission via ajax and return status response.
  *
  * @since 0.1.0
  */
 public function ajax_controller_interaction()
 {
     check_ajax_referer($this->plugin_name . '_ajax_interaction_nonce', 'nonce');
     $result = $post_attr = array();
     // @TODO: if ( $_POST['cr_form'] ) {
     // Parse serialized ajax post data as `$post` (array).
     parse_str($_POST['cr_form'], $post);
     // @TODO: Get rid of `parse_str()`
     if (is_email($post['email'])) {
         // Prepare receiver adapter.
         $client = new Api\Cleverreach();
         $receiver = new Api\Cleverreach_Receiver_Adapter($client);
         // Populate `$post_attr` (array) according to CleverReach API defaults.
         foreach ($post as $key => $value) {
             if ('email' != $key) {
                 // Skip 'email' as this is not needed as separate attribute.
                 array_push($post_attr, array('key' => sanitize_html_class($key), 'value' => sanitize_text_field($value)));
             }
         }
         // Populate `$source` (string)
         $helper = new Cre_Helper();
         if ($helper->has_option('source')) {
             $source = $helper->get_option('source');
         } else {
             $source = get_bloginfo('name');
         }
         // Populate `$user` (array) according to CleverReach API defaults.
         $user = array('email' => sanitize_email($post['email']), 'registered' => time(), 'source' => esc_html($source), 'attributes' => $post_attr);
         $receiver_added = $receiver->add($user);
         // Test returned data.
         if (is_object($receiver_added) && 'SUCCESS' == $receiver_added->status) {
             $result['type'] = 'success';
             // Prepare form adapter.
             $form = new Api\Cleverreach_Form_Adapter($client);
             // Send activation mail.
             $user_data = array('user_ip' => '127.0.0.1', 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'referer' => esc_url(home_url()));
             $form->send_activation_mail($helper->get_option('form_id'), sanitize_email($post['email']), $user_data);
         } else {
             $result['type'] = 'error';
             if ('duplicate data' == $receiver_added) {
                 $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_duplicate', esc_html__('It seems like you\'re already subscribed to our list.', 'cleverreachextension')));
             } else {
                 $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_common', esc_html__('Sorry, there seems to be a problem with your data.', 'cleverreachextension')));
             }
         }
         // end of is_object() && 'SUCCESS'
     } else {
         $result['type'] = 'error';
         $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_invalid_email', esc_html__('Sorry, there seems to be a problem with your email address.', 'cleverreachextension')));
     }
     // end of is_email()
     // Finally return JSON result.
     $result = json_encode($result);
     echo $result;
     die;
 }
 /**
  * Parse form submission via ajax and return status response.
  *
  * @since   0.1.0
  *
  * @wp-hook wp_ajax_nopriv_cre_ajax_controller_interaction
  * @wp-hook wp_ajax_cre_ajax_controller_interaction
  */
 public function ajax_controller_interaction()
 {
     check_ajax_referer($this->plugin_name . '_ajax_interaction_nonce', 'nonce');
     $result = $post_attr = array();
     // @TODO: if ( $_POST['cr_form'] ) {
     // Parse serialized ajax post data as `$post` (array).
     parse_str($_POST['cr_form'], $post);
     // @TODO: Get rid of `parse_str()`
     if (is_email($post['email'])) {
         // Prepare receiver adapter.
         $helper = new Cre_Helper();
         $client = new Api\Cleverreach();
         $receiver = new Api\Cleverreach_Receiver_Adapter($client);
         $group = new Api\Cleverreach_Group_Adapter($client);
         // Populate `$post_attr` (array) according to CleverReach API defaults.
         foreach ($post as $key => $value) {
             // Join values if $value is an array.
             if (is_array($value)) {
                 $value = join(', ', $value);
             }
             if ('email' != $key) {
                 // Skip 'email' as this is not needed as separate attribute.
                 // Create fields for default forms.
                 if ('true' === $helper->get_option('ajax')) {
                     // Attribute `$key` may only contain lowercase a-z and 0-9. Everything else will be converted to `_`.
                     $key = strtolower($key);
                     $key = str_replace(' ', '_', $key);
                     $key = str_replace('-', '_', $key);
                     $key = preg_replace('/[^A-Za-z0-9\\-]/', '_', $key);
                     if ($value && $key[0] !== '_') {
                         // Exclude (hidden) fields starting with an underscore.
                         $group->attribute_add($key, 0);
                     }
                 }
                 array_push($post_attr, array('key' => sanitize_html_class($key), 'value' => sanitize_text_field($value)));
             }
         }
         // Populate `$source` (string)
         $source = get_bloginfo('name');
         if (isset($_POST['source'])) {
             $source = $_POST['source'];
         } elseif ($helper->has_option('source')) {
             $source = $helper->get_option('source');
         }
         // Populate `$user` (array) according to CleverReach API defaults.
         $user = array('email' => sanitize_email($post['email']), 'registered' => time(), 'source' => esc_html(trim($source)), 'attributes' => $post_attr);
         $list_id = empty($_POST['list']) ? $helper->get_option('list_id') : (int) $_POST['list'];
         $receiver_added = $receiver->add($user, $list_id);
         // Test returned data.
         if (is_object($receiver_added) && 'SUCCESS' == $receiver_added->status) {
             $result['type'] = 'success';
             // Prepare form adapter.
             $form = new Api\Cleverreach_Form_Adapter($client);
             // Send activation mail.
             $user_data = array('user_ip' => '127.0.0.1', 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'referer' => esc_url(home_url()));
             $form_id = empty($_POST['form']) ? $helper->get_option('form_id') : (int) $_POST['form'];
             $form->send_activation_mail($form_id, sanitize_email($post['email']), $user_data);
         } else {
             $result['type'] = 'error';
             if ('duplicate data' == $receiver_added) {
                 $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_duplicate', esc_html__('It seems like you\'re already subscribed to our list.', 'cleverreach-extension')));
             } else {
                 $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_common', esc_html__('Sorry, there seems to be a problem with your data.', 'cleverreach-extension')));
             }
         }
         // end of is_object() && 'SUCCESS'
     } else {
         $result['type'] = 'error';
         $result['status'] = sanitize_text_field(apply_filters('cleverreach_extension_error_msg_invalid_email', esc_html__('Sorry, there seems to be a problem with your email address.', 'cleverreach-extension')));
     }
     // end of is_email()
     // Finally return JSON result.
     $result = json_encode($result);
     echo $result;
     die;
 }
 /**
  * Prepare Api Key and List ID.
  *
  * @since 0.1.0
  */
 public function __construct()
 {
     $helper = new Core\Cre_Helper();
     $this->api_key = sanitize_key(trim(apply_filters('cleverreach_extension_api_key', $helper->get_option('api_key'))));
     $this->list_id = sanitize_key(absint(trim(apply_filters('cleverreach_extension_list_id', $helper->get_option('list_id')))));
 }