/**
  * Register styles and scripts used in rendering the Admin UI
  */
 public function admin_scripts()
 {
     $page = isset($_GET['page']) ? $_GET['page'] : '';
     if ($page == 'followup-emails' || $page == 'followup-emails-settings' || $page == 'followup-emails-queue') {
         if (WC_FUE_Compatibility::is_wc_version_gt('2.1')) {
             $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
             wp_register_script('ajax-chosen', WC()->plugin_url() . '/assets/js/chosen/ajax-chosen.jquery' . $suffix . '.js', array('jquery', 'chosen'), WC()->version);
             wp_register_script('chosen', WC()->plugin_url() . '/assets/js/chosen/chosen.jquery' . $suffix . '.js', array('jquery'), WC()->version);
         } else {
             // For WC < 2.1
             woocommerce_admin_scripts();
         }
         wp_enqueue_script('fue-queue', FUE_TEMPLATES_URL . '/js/queue.js', array('jquery', 'chosen'), FUE_VERSION);
         wp_enqueue_script('woocommerce_admin');
         wp_enqueue_script('farbtastic');
         wp_enqueue_script('ajax-chosen');
         wp_enqueue_script('chosen');
         wp_enqueue_script('jquery-ui-sortable');
         wp_enqueue_script('jquery-ui-autocomplete', null, array('jquery-ui-core'));
         ?>
         <style type="text/css">
             .chzn-choices li.search-field .default {
                 width: auto !important;
             }
             select option[disabled] {display:none;}
         </style>
         <?php 
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
         if (!empty($_GET['tab']) && $_GET['tab'] == 'order_import') {
             wp_enqueue_script('jquery-ui-progressbar', false, array('jquery', 'jquery-ui'));
             wp_enqueue_script('fue_wc_order_import', FUE_TEMPLATES_URL . '/js/wc_order_import.js', array('jquery', 'jquery-ui-progressbar'), FUE_VERSION);
         }
     } elseif ($page == 'followup-emails-form' || $page == 'followup-emails-reports') {
         if ($page == 'followup-emails-form') {
             wp_enqueue_script('fue-form-woocommerce', plugins_url('templates/js/email-form-woocommerce.js', FUE_FILE), array('jquery'), FUE_VERSION);
         }
         wp_enqueue_script('select2');
         wp_enqueue_style('select2');
         wp_enqueue_script('woocommerce_admin');
         wp_enqueue_script('farbtastic');
         wp_enqueue_script('jquery-ui-sortable');
         wp_enqueue_script('jquery-ui-core', null, array('jquery'));
         wp_enqueue_script('jquery-ui-datepicker', null, array('jquery-ui-core'));
         wp_enqueue_script('jquery-ui-autocomplete', null, array('jquery-ui-core'));
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
         wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/base/jquery-ui.css');
     }
     $screen = get_current_screen();
     if ($screen->id == 'follow_up_email') {
         wp_enqueue_script('fue-form-woocommerce', plugins_url('templates/js/email-form-woocommerce.js', FUE_FILE), array('jquery'), FUE_VERSION);
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
     }
     wp_enqueue_script('wc-product-search', plugins_url('templates/js/fue-select.js', FUE_FILE), array('jquery', 'select2'), FUE_VERSION);
 }
 /**
  * Extract data from an order and mark it as 'recorded' so we don't make
  * the mistake of processing it again and giving us duplicate and unreliable data
  *
  * @param WC_Order $order
  */
 public static function record_order($order)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     $order_categories = array();
     $wc2 = WC_FUE_Compatibility::is_wc_version_gt('2.0');
     $order_id = $order->id;
     $user_id = WC_FUE_Compatibility::get_order_user_id($order);
     $recorded = get_post_meta($order_id, '_fue_recorded', true);
     if ($recorded == 1) {
         return;
     }
     if ($user_id > 0) {
         $user = new WP_User($user_id);
         $email = $user->user_email;
     } else {
         $user_id = 0;
         $email = $order->billing_email;
     }
     $customer = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}followup_customers WHERE user_id = %d AND email_address = %s", $user_id, $email));
     if (!$customer) {
         $insert = array('user_id' => $user_id, 'email_address' => $email, 'total_purchase_price' => $order->order_total, 'total_orders' => 1);
         $wpdb->insert($wpdb->prefix . 'followup_customers', $insert);
         $customer_id = $wpdb->insert_id;
         $customer = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}followup_customers WHERE id = %d", $customer_id));
     } else {
         $total_orders = $customer->total_orders + 1;
         $total_purchases = $customer->total_purchase_price + $order->order_total;
         $wpdb->update($wpdb->prefix . 'followup_customers', array('total_purchase_price' => $total_purchases, 'total_orders' => $total_orders), array('id' => $customer->id));
     }
     // record order
     $wpdb->insert($wpdb->prefix . 'followup_customer_orders', array('followup_customer_id' => $customer->id, 'order_id' => $order_id, 'price' => $order->order_total));
     if ($wc2) {
         $order_item_ids = $wpdb->get_results("SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = {$order_id}");
         foreach ($order_item_ids as $order_item) {
             $product_id = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item->order_item_id} AND meta_key = '_product_id'");
             $variation_id = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$order_item->order_item_id} AND meta_key = '_variation_id'");
             if ($product_id) {
                 $insert = array('order_id' => $order_id, 'product_id' => $product_id, 'variation_id' => $variation_id);
                 $wpdb->insert($wpdb->prefix . 'followup_order_items', $insert);
                 // get the categories
                 $cat_ids = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
                 if ($cat_ids) {
                     foreach ($cat_ids as $cat_id) {
                         $order_categories[] = $cat_id;
                     }
                 }
             }
         }
     } else {
         $order_items = get_post_meta($order_id, '_order_items', true);
         foreach ($order_items as $item) {
             $insert = array('order_id' => $order_id, 'product_id' => $item['id']);
             $wpdb->insert($wpdb->prefix . 'followup_order_items', $insert);
             // get the categories
             $cat_ids = wp_get_post_terms($item['id'], 'product_cat', array('fields' => 'ids'));
             if ($cat_ids) {
                 foreach ($cat_ids as $cat_id) {
                     $order_categories[] = $cat_id;
                 }
             }
         }
     }
     $order_categories = array_unique($order_categories);
     foreach ($order_categories as $category_id) {
         $insert = array('order_id' => $order_id, 'category_id' => $category_id);
         $wpdb->insert($wpdb->prefix . 'followup_order_categories', $insert);
     }
     update_post_meta($order_id, '_fue_recorded', true);
 }
 /**
  * Load all recipients matching the provided send type
  *
  * @param array $recipients
  * @param array $post
  *
  * @return array
  */
 public function get_manual_email_recipients($recipients, $post)
 {
     global $wpdb;
     $send_type = $post['send_type'];
     if ($send_type == 'users') {
         // Send to all users
         $users = get_users();
         foreach ($users as $user) {
             $key = $user->ID . '|' . $user->user_email . '|' . $user->display_name;
             $value = array($user->ID, $user->user_email, $user->display_name);
             if (!isset($recipients[$key])) {
                 $recipients[$key] = $value;
             }
         }
     } elseif ($send_type == 'storewide') {
         // Send to all customers
         $users = get_users(array('role' => 'customer'));
         foreach ($users as $user) {
             $key = $user->ID . '|' . $user->user_email . '|' . $user->display_name;
             $value = array($user->ID, $user->user_email, $user->display_name);
             if (!isset($recipients[$key])) {
                 $recipients[$key] = $value;
             }
         }
     } elseif ($send_type == 'customer') {
         // individual email addresses
         if (count($post['recipients']) > 0) {
             foreach (explode(',', $post['recipients'][0]) as $key) {
                 $data = explode('|', $key);
                 if (3 == count($data)) {
                     $value = array($data[0], $data[1], $data[2]);
                     if (!isset($recipients[$key])) {
                         $recipients[$key] = $value;
                     }
                 }
             }
         }
     } elseif ($send_type == 'product') {
         // customers who bought the selected products
         if (is_array($post['product_ids'])) {
             $post['product_ids'] = array_filter(array_map('intval', explode(',', $post['product_ids'][0])));
             if (WC_FUE_Compatibility::is_wc_version_gt('2.0')) {
                 // if WC >= 2.0, do a direct query
                 foreach ($post['product_ids'] as $product_id) {
                     $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_items WHERE product_id = %d", $product_id));
                     foreach ($order_ids as $row) {
                         $order = WC_FUE_Compatibility::wc_get_order($row->order_id);
                         if (!$order) {
                             continue;
                         }
                         // only on processing and completed orders
                         $order_status = WC_FUE_Compatibility::get_order_status($order);
                         if ($order_status != 'processing' && $order_status != 'completed') {
                             continue;
                         }
                         $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 0;
                         $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                         $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                         if (!isset($recipients[$key])) {
                             $recipients[$key] = $value;
                         }
                     }
                 }
             } else {
                 foreach ($post['product_ids'] as $product_id) {
                     $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_items WHERE product_id = %d", $product_id));
                     foreach ($order_ids as $order_id) {
                         // load the order and check the status
                         $order = WC_FUE_Compatibility::wc_get_order($order_id);
                         if (!$order) {
                             continue;
                         }
                         // only on processing and completed orders
                         $order_status = WC_FUE_Compatibility::get_order_status($order);
                         if ($order_status != 'processing' && $order_status != 'completed') {
                             continue;
                         }
                         $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 0;
                         $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                         $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                         if (!isset($recipients[$key])) {
                             $recipients[$key] = $value;
                             break;
                         }
                     }
                     // endforeach ( $order_items_result as $order_items )
                 }
             }
             // endif: function_exists('get_product')
         }
         // endif: is_array($post['product_ids'])
     } elseif ($send_type == 'category') {
         // customers who bought products from the selected categories
         if (is_array($post['category_ids'])) {
             foreach ($post['category_ids'] as $category_id) {
                 $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_categories WHERE category_id = %d", $category_id));
                 foreach ($order_ids as $order_id_row) {
                     // load the order and check the status
                     $order_id = $order_id_row->order_id;
                     $order = WC_FUE_Compatibility::wc_get_order($order_id);
                     if (!$order) {
                         continue;
                     }
                     // only on processing and completed orders
                     $order_status = WC_FUE_Compatibility::get_order_status($order);
                     if ($order_status != 'processing' && $order_status != 'completed') {
                         continue;
                     }
                     $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 0;
                     $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                     $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                     if (!isset($recipients[$key])) {
                         $recipients[$key] = $value;
                     }
                 }
                 // endforeach ( $order_items_result as $order_items )
             }
         }
         // endif: is_array($post['product_ids'])
     } elseif ($send_type == 'timeframe') {
         $from_ts = strtotime($post['timeframe_from']);
         $to_ts = strtotime($post['timeframe_to']);
         $from = date('Y-m-d', $from_ts) . ' 00:00:00';
         $to = date('Y-m-d', $to_ts) . ' 23:59:59';
         $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_type = 'shop_order' AND post_status = 'publish' AND post_date BETWEEN %s AND %s", $from, $to));
         foreach ($order_ids as $order) {
             $order_id = $order->ID;
             $order = WC_FUE_Compatibility::wc_get_order($order_id);
             if (!$order) {
                 continue;
             }
             $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 0;
             $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
             $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
             if (!isset($recipients[$key])) {
                 $recipients[$key] = $value;
             }
         }
     }
     return $recipients;
 }