/** * 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'))))); }
/** * Define the admin-specific functionality of the plugin. * * @since 0.1.0 * * @param string $plugin_name The name of this plugin. * @param string $plugin_slug The slug of this plugin. * @param string $plugin_version The current version of this plugin. */ public function __construct($plugin_name, $plugin_slug, $plugin_version) { $this->plugin_name = $plugin_name; $this->plugin_slug = $plugin_slug; $this->plugin_version = $plugin_version; $helper = new Core\Cre_Helper(); $this->api_key = $helper->get_option('api_key'); $this->list_id = $helper->get_option('list_id'); $this->form_id = $helper->get_option('form_id'); $this->source = $helper->get_option('source'); $this->ajax = $helper->get_option('ajax'); }
/** * 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; }
/** * 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 }
/** * Save data and return API response as JSON. * * @since 0.2.0 * * @return array Return JSON `$result` with API response. */ public function admin_ajax_controller_interaction() { check_ajax_referer($this->plugin_name . '_admin_ajax_interaction_nonce', 'nonce'); if ($_POST['cr_admin_form'] && current_user_can('manage_options')) { // Requires $_POST from admin user. // Create or update database entry. update_option('cleverreach_extension', $this->sanitize($_POST['cr_admin_form'])); $result = array(); $client = new Api\Cleverreach(); $helper = new Core\Cre_Helper(); // Add status and list options to result. if ($client->has_valid_api_key()) { $result['status'] = 'success'; $group = new Api\Cleverreach_Group_Adapter($client); $result['list_options'] = $helper->parse_list($group->get_list(), 'list_id'); } else { $result['status'] = 'error'; } // Add form options to result. if ($client->has_valid_api_key() && $helper->has_option('list_id')) { $form = new Api\Cleverreach_Form_Adapter($client); $result['form_options'] = $helper->parse_list($form->get_list($helper->get_option('list_id')), 'form_id'); } // Add shortcode value to result. if ($client->has_valid_api_key() && $helper->has_option('form_id')) { $result['shortcode_id'] = $helper->get_option('form_id'); } // Finally return JSON result. $result = json_encode($result); echo $result; die; } }
<?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'))))); }