function lead_action_admin_action()
{
    global $Inbound_Leads;
    if (!current_user_can('level_9')) {
        die(__('User does not have admin level permissions.'));
    }
    $_POST = stripslashes_deep($_POST);
    $_REQUEST = stripslashes_deep($_REQUEST);
    // Check if we've been submitted a tag/remove.
    if (!empty($_REQUEST['ids'])) {
        check_admin_referer('lead_management-edit');
        is_array($_REQUEST['ids']) ? $pass_ids = implode(',', $_REQUEST['ids']) : ($pass_ids = $_REQUEST['ids']);
        $cat = intval($_REQUEST['wplead_list_category_action']);
        $num = count($_REQUEST['ids']);
        if (!empty($_REQUEST['wplead_list_category_action'])) {
            $query = '&cat=' . $_REQUEST['wplead_list_category_action'];
        }
        if (!empty($_REQUEST['s'])) {
            $query = '&s=' . $_REQUEST['s'];
        }
        if (!empty($_REQUEST['t'])) {
            $query = '&t=' . $_REQUEST['t'];
        }
        $term = get_term($_REQUEST['wplead_list_category_action'], 'wplead_list_category');
        $name = $term->slug;
        $this_tax = "wplead_list_category";
        /* We've been told to tag these posts with the given category. */
        if (!empty($_REQUEST['add'])) {
            foreach ($_REQUEST['ids'] as $id) {
                $fid = intval($id);
                $Inbound_Leads->add_lead_to_list($fid, $cat);
                // add to list
            }
            wp_redirect(get_option('siteurl') . "/wp-admin/edit.php?post_type=wp-lead&page=lead_management&done=add&what=" . $name . "&num={$num}{$query}");
            die;
        } elseif (!empty($_REQUEST['remove'])) {
            foreach ((array) $_REQUEST['ids'] as $id) {
                $fid = intval($id);
                remove_lead_from_list_tax($fid, $cat);
            }
            wp_redirect(get_option('siteurl') . "/wp-admin/edit.php?post_type=wp-lead&page=lead_management&done=remove&what=" . $name . "&num={$num}");
            die;
        } elseif (!empty($_REQUEST['tag']) || !empty($_REQUEST['replace_tags'])) {
            $tags = $_REQUEST['tags'];
            foreach ((array) $_REQUEST['ids'] as $id) {
                $lead_ID = intval($id);
                $append = empty($_REQUEST['replace_tags']);
                $Inbound_Leads->add_tag_to_lead($lead_ID, $tags);
            }
            wp_redirect(get_option('siteurl') . "/wp-admin/edit.php?post_type=wp-lead&page=lead_management&done=tag&what={$tags}&num={$num}{$query}&on={$pass_ids}");
            die;
        } elseif (!empty($_REQUEST['untag'])) {
            $tags = explode(',', $_REQUEST['tags']);
            foreach ((array) $_REQUEST['ids'] as $id) {
                $id = intval($id);
                $existing = wp_get_post_tags($id);
                $new = array();
                foreach ((array) $existing as $_tag) {
                    foreach ((array) $tags as $tag) {
                        if ($_tag->name != $tag) {
                            $new[] = $_tag->name;
                        }
                    }
                }
                wp_set_post_tags($id, $new);
            }
            $tags = join(', ', $tags);
            wp_redirect(get_option('siteurl') . "/wp-admin/edit.php?post_type=wp-lead&page=lead_management&done=untag&what={$tags}&num={$num}{$query}");
            die;
        } elseif (!empty($_REQUEST['delete_leads'])) {
            foreach ((array) $_REQUEST['ids'] as $id) {
                $id = intval($id);
                wp_delete_post($id, true);
            }
            wp_redirect(get_option('siteurl') . "/wp-admin/edit.php?post_type=wp-lead&page=lead_management&done=delete_leads&what=" . $name . "&num={$num}{$query}");
            die;
        } elseif (!empty($_REQUEST['export_leads'])) {
            $exported = 0;
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header('Content-Description: File Transfer');
            header("Content-type: text/csv");
            header("Content-Disposition: attachment; filename=leads-export-csv-" . date("m.d.y") . ".csv");
            header("Expires: 0");
            header("Pragma: public");
            $fh = @fopen('php://output', 'w');
            //get all keys
            foreach ((array) $_REQUEST['ids'] as $post_id) {
                $this_lead_data = get_post_custom($post_id);
                unset($this_lead_data['page_views']);
                unset($this_lead_data['wpleads_inbound_form_mapped_data']);
                unset($this_lead_data['wpleads_referral_data']);
                unset($this_lead_data['wpleads_conversion_data']);
                unset($this_lead_data['wpleads_raw_post_data']);
                foreach ($this_lead_data as $key => $val) {
                    $lead_meta_pairs[$key] = $key;
                }
            }
            // Add a header row if it hasn't been added yet
            fputcsv($fh, array_keys($lead_meta_pairs));
            $headerDisplayed = true;
            foreach ((array) $_REQUEST['ids'] as $post_id) {
                unset($this_row_data);
                $this_lead_data = get_post_custom($post_id);
                unset($this_lead_data['page_views']);
                unset($this_lead_data['wpleads_inbound_form_mapped_data']);
                unset($this_lead_data['wpleads_referral_data']);
                unset($this_lead_data['wpleads_conversion_data']);
                unset($this_lead_data['wpleads_raw_post_data']);
                foreach ($lead_meta_pairs as $key => $val) {
                    if (isset($this_lead_data[$key])) {
                        $val = $this_lead_data[$key];
                        if (is_array($val)) {
                            $val = implode(';', $val);
                        }
                    } else {
                        $val = "";
                    }
                    $this_row_data[$key] = $val;
                }
                fputcsv($fh, $this_row_data);
                $exported++;
            }
            // Close the file
            fclose($fh);
            // Make sure nothing else is sent, our file is done
            exit;
        }
    }
    die("Invalid action.");
}
 function perform_automation_action($lead_id, $automation_meta_data)
 {
     $gateway_open = apply_filters('wpleads_lead_automation_action_gateway', true, $lead_id, $automation_meta_data);
     if ($gateway_open) {
         /* SORT INTO WORDPRESS LISTS */
         $lists_wp = $automation_meta_data['automation_condition_list_add_0'][0];
         $lists_wp = explode(';', $lists_wp);
         //print_r($lists_wp); echo $lead_id; exit;
         $lists_wp = array_filter($lists_wp);
         if (is_array($lists_wp) && count($lists_wp) > 0) {
             foreach ($lists_wp as $k => $list_id) {
                 _e("Action: Synching Lead {$lead_id} with List {$list_id} <br>", 'leads');
                 // wpleads_add_lead_to_list($list_id, $lead_id, $add = true); // old list cpt function
                 add_lead_to_list_tax($lead_id, intval($list_id));
             }
         }
         /* REMOVE FROM WORDPRESS LISTS */
         $lists_wp = $automation_meta_data['automation_condition_list_remove_0'][0];
         $lists_wp = explode(';', $lists_wp);
         $lists_wp = array_filter($lists_wp);
         if (is_array($lists_wp) && count($lists_wp) > 0) {
             $categories = wp_get_post_terms($lead_id, 'wplead_list_category', array('fields' => 'ids'));
             foreach ($lists_wp as $k => $list_id) {
                 _e("Action: Removing Lead {$lead_id} from List {$list_id} <br>", 'leads');
                 // wpleads_remove_lead_from_list($list_id , $lead_id); // old for list CPT
                 remove_lead_from_list_tax($lead_id, intval($list_id));
             }
         }
         /* AWARD POINTS */
         $points = $automation_meta_data['automation_condition_points_0'][0];
         if ($points && $points[0] == '-') {
             $points = str_replace('-', '', $points);
             $points = trim($points);
             //subtract points
             $current_points = get_post_meta($lead_id, 'automation_points', true);
             $current_points ? $current_points = $current_points - $points : ($current_points = 0 - $points);
             update_post_meta($lead_id, 'automation_points', $current_points);
         } else {
             if ($points) {
                 //add points
                 $current_points = get_post_meta($lead_id, 'automation_points', true);
                 $current_points ? $current_points = $current_points + $points : ($current_points = 0 + $points);
                 update_post_meta($lead_id, 'automation_points', $current_points);
             }
         }
         /* UPDATE NUMBER OF RULES ACCOMPLISHED FOR LEAD */
         $automation_accomplished = get_post_meta($lead_id, 'automation_accomplished', true);
         $automation_accomplished = json_decode($automation_accomplished, true);
         if (!is_array($automation_accomplished)) {
             $automation_accomplished = array();
         }
         if (!array_key_exists($automation_meta_data['automation_id'][0], $automation_accomplished)) {
             $automation_accomplished[$automation_meta_data['automation_id'][0]] = $automation_meta_data['automation_name'][0];
         }
         $automation_accomplsihed_count = count($automation_accomplished);
         $automation_accomplished = json_encode($automation_accomplished);
         update_post_meta($lead_id, 'automation_accomplished', $automation_accomplished);
         do_action('automation_cron_perform_action_post', $lead_id, $automation_meta_data);
         _e("Message: Lead matched this rule & all actions completed!<br>", 'leads');
         _e("Message: Total rules accomplished for this lead {$automation_accomplsihed_count}<br>", 'leads');
     }
 }