static function backend_html($value = "", $ajax = true) { $owner = false; $response = array(); $response_text = __("Could not connect to Mailchimp with this API Key. Please try again with a different key", 'avia_framework'); $response_class = "av-notice-error"; $trigger_global_save = false; $list_output_default = __("You might also want to check your internet connection and make sure that mailchimp.com is available", 'avia_framework'); if ($ajax) { if (!empty($value)) { $api = new av_mailchimp_api($value); $owner = $api->api_owner(); } if ($owner) { $api->store_lists(); $trigger_global_save = true; $response_class = ""; $response_text = __("We were able to connect to your Mailchimp account", 'avia_framework'); $response_text .= " ({$owner})"; $response_text .= " avia_trigger_save"; } } else { $owner = get_option('av_mailchimp_owner'); if ($owner) { $response_class = ""; $response_text = __("Last time we checked we were able to connected to your Mailchimp account", 'avia_framework'); $response_text .= " ({$owner})"; } } if ($owner) { $lists = get_option('av_chimplist'); $list_fields = get_option('av_chimplist_field'); $list_output_default = __("We were not able to find any Newsletter List on your Mailchimp that your visitors can subscribe to. Please create at least one in your Mailchimp back-end and refresh the list data here to use Mailchimp with this theme.", 'avia_framework'); if (!empty($list_fields)) { $list_output = ""; $list_output_default = ""; $list_output .= "<div class='av-verification-cell'><strong>" . __('Available Lists', 'avia_framework') . ":</strong></div>"; foreach ($list_fields as $key => $list_items) { $sub = $lists[$key]['stats']->member_count; $list_output .= "<div class='av-verification-cell av-verification-cell-heading'>"; $list_output .= "<strong>{$lists[$key]['name']}</strong>"; $list_output .= "<small class='av-verification-extra-data'> (" . __('Subscriber', 'avia_framework') . ": {$sub})</small>"; $list_output .= "<small class='av-verification-extra-data av-verification-extra-data-right'>(" . __('ID', 'avia_framework') . ": {$key})</small>"; $list_output .= "</div>"; foreach ($list_items as $key => $field) { $required = !empty($field->required) ? "<span class='av-verification-required'>*</span>" : ""; $list_output .= "<div class='av-verification-cell av-verification-cell-sub'>"; $list_output .= "{$field->name} {$required}"; $list_output .= "<span class='av-verification-extra-data av-verification-extra-data-right'>{$field->type}</span>"; $list_output .= "</div>"; } } $list_output .= "<div class='av-verification-cell av-verification-cell-heading'><strong>" . __('If you ever change the fields in your list please re-validate your API key to update the list data presented here.', 'avia_framework') . "</strong></div>"; } } $output = "<div class='av-verification-response-wrapper'>"; $output .= "<div class='av-text-notice {$response_class}'>"; $output .= $response_text; $output .= "</div>"; if (!empty($list_output) || !empty($list_output_default)) { $output .= "<div class='av-verification-mailchimp-list'>"; if (!empty($list_output)) { $output .= $list_output; } if (!empty($list_output_default)) { $output .= "<div class='av-verification-cell'>" . $list_output_default . "</div>"; } $output .= "</div>"; } $output .= "</div>"; return $output; }
public function send(&$instance) { $params = $instance->form_params; if (isset($_POST['avia_generated_form' . $params['avia_formID']])) { $form_suffix = '_' . $params['avia_formID']; $suffix_length = strlen($form_suffix) * -1; $merge_fields = array(); $post_data = array(); $mail = ""; $status = !empty($params['double_opt_in']) ? "pending" : "subscribed"; // subscribed // pending foreach ($_POST as $key => $value) { $key = substr($key, 0, $suffix_length); $key = str_replace('avia_', '', $key); if (isset($_POST['ajax_mailchimp'])) { $value = urldecode($value); } $post_data[$key] = $value; } //make sure that the username is not filled in, otherwise a bot has sent the form. if so simply fake the send event if (!empty($post_data['username'])) { return true; } //iterate over form fields to generate the merge field data if (empty($this->fields)) { $all_fields = get_option('av_chimplist_field'); $this->fields = $all_fields[$params['mailchimp']]; } foreach ($this->fields as $field) { $value = !empty($post_data[$field->merge_id]) ? $post_data[$field->merge_id] : false; if ($value !== false) { if ($field->merge_id != 0) { $merge_fields[$field->tag] = $value; } else { $mail = $value; } } } $data_to_send = array('email_address' => $mail, 'status' => $status); if (!empty($merge_fields)) { $data_to_send['merge_fields'] = $merge_fields; } $data_to_send = apply_filters('avf_mailchimp_subscriber_data', $data_to_send, $this); $api = new av_mailchimp_api($this->api_key); $this->add_user = $api->post('lists/' . $params['mailchimp'] . '/members', $data_to_send); //user was successfully added if (isset($this->add_user->id)) { return true; } //if we got no id the user was not added which means we got an error. $error_key = "general"; if ($this->add_user->title == "Invalid Resource") { $error_key = 'all'; if (strpos($this->add_user->detail, 'email') !== false) { $error_key = 'email'; } if (strpos($this->add_user->detail, 'merge fields') !== false) { $error_key = 'invalid_field'; } } if ($this->add_user->title == "Member Exists") { $error_key = 'already'; } $instance->error_msg = "<div class='avia-mailchimp-ajax-error av-form-error-container'>" . $api->message($error_key) . "</div>"; add_action('wp_footer', array($this, 'print_js_error'), 2, 100000); } return false; }