/**
  * 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;
 }
 /**
  * Render list input field and description.
  *
  * @since 0.1.0
  */
 public function render_list_field()
 {
     $html = '<select class="cre-admin-select-list" name="cleverreach_extension[list_id]">';
     $client = new Api\Cleverreach();
     $helper = new Core\Cre_Helper();
     if ($client->has_valid_api_key()) {
         $group = new Api\Cleverreach_Group_Adapter($client);
         $html .= $helper->parse_list_html($this->list_id, $group->get_list(), 'list_id', esc_html__('Please select a list', 'cleverreachextension'));
     }
     $html .= '</select>';
     echo wp_kses($html, $helper->allowed_html_select());
 }
 /**
  * Render list input field and description.
  *
  * @since 0.1.0
  */
 public function render_list_field()
 {
     $html = '<select class="cre-admin-select-list" name="cleverreach_extension[list_id]">';
     $client = new Api\Cleverreach();
     $helper = new Core\Cre_Helper();
     $default = esc_html__('Please select a list', 'cleverreach-extension');
     if ($client->has_valid_api_key()) {
         $group = new Api\Cleverreach_Group_Adapter($client);
         $html .= $helper->parse_list_html($this->list_id, $group->get_list(), 'list_id', $default);
     } else {
         $html .= '<option value="">' . $default . '</option>';
     }
     $html .= '</select>';
     echo '<div class="cre-input-container">';
     echo wp_kses($html, $helper->allowed_html_select());
     printf('<div class="dashicons-before cre-info-message %s cre-js-response"></div>', $client->has_valid_api_key() && $helper->has_option('list_id') ? 'confirmed' : 'invalid');
     echo '</div>';
 }
 /**
  * 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;
 }