public function load_by_network_key($network_key, $ticket, $type, $debug = false)
 {
     switch ($type) {
         case 'ticket':
             $existing = shub_get_single('shub_message', 'network_key', $network_key);
             if ($existing) {
                 // load it up.
                 $this->load($existing['shub_message_id']);
             }
             if ($ticket && isset($ticket['ticket_id']) && $ticket['ticket_id'] == $network_key) {
                 // get the messages from the API
                 $api = $this->account->get_api();
                 $api_result = $api->api('ticket', 'message', array('ticket_ids' => $network_key));
                 if ($api_result && isset($api_result['tickets'][$network_key]) && count($api_result['tickets'][$network_key])) {
                     //print_r($api_result);
                     if (isset($ticket['staff']) && !empty($ticket['staff']['email'])) {
                         $ticket['reply_from_shub_user_id'] = $this->account->get_api_user_to_id($ticket['staff']);
                     }
                     $all_comments = $api_result['tickets'][$network_key];
                     $comments = array();
                     foreach ($all_comments as $comment_id => $comment) {
                         if (isset($comment['cache']) && $comment['cache'] == 'autoreply' || isset($comment['message_type_id']) && $comment['message_type_id'] == 3) {
                             // this is an auto reply, don't bother importing it into the system here
                         } else {
                             $comment['id'] = $comment['ticket_message_id'];
                             $comment['shub_user_id'] = $this->account->get_api_user_to_id($comment['user']);
                             $comment['timestamp'] = $comment['message_time'];
                             $comments[] = $comment;
                         }
                     }
                     if (!$existing) {
                         $this->create_new();
                     }
                     $this->update('shub_account_id', $this->account->get('shub_account_id'));
                     $this->update('shub_item_id', $this->item->get('shub_item_id'));
                     // create/update a user entry for this comments.
                     $shub_user_id = $this->account->get_api_user_to_id($ticket['user']);
                     $this->update('shub_user_id', $shub_user_id);
                     $this->update('title', $ticket['subject']);
                     // latest comment goes in summary
                     $this->update('summary', $comments[count($comments) - 1]['content']);
                     $this->update('last_active', $ticket['last_message_timestamp']);
                     $this->update('shub_type', $type);
                     $this->update('shub_data', $ticket);
                     $this->update('shub_link', $ticket['url']);
                     $this->update('network_key', $network_key);
                     if ($this->get('shub_status') != _shub_MESSAGE_STATUS_HIDDEN) {
                         // we have to decide if we're updating the message status from answered to unanswered.
                         // if this message status is already answered and the existing comment count matches the new comment count then we don't update the status
                         // this is because we insert a placeholder "comment" into the db while the API does the push, and when we read again a few minutes later it overwrites this placeholder comment, so really it's not a new comment coming in just the one we posted through the API that takes a while to come back through.
                         if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED && count($comments) == count($this->get_comments())) {
                             // don't do anything
                         } else {
                             // comment count is different
                             $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED);
                         }
                     }
                     $this->update('comments', $comments);
                     // add the extra fields from UCM into the ticket.
                     if (!empty($ticket['extra']) && is_array($ticket['extra'])) {
                         foreach ($ticket['extra'] as $extra_key => $extra_val) {
                             $extra_val = trim($extra_val);
                             if (strlen($extra_val)) {
                                 // add this one into the system.
                                 $ExtraField = new SupportHubExtra();
                                 if (!$ExtraField->load_by('extra_name', $extra_key)) {
                                     $ExtraField->create_new();
                                     $ExtraField->update('extra_name', $extra_key);
                                 }
                                 $ExtraField->save_and_link(array('extra_value' => $extra_val), 'ucm', $this->account->get('shub_account_id'), $this->get('shub_message_id'), $shub_user_id);
                             }
                         }
                     }
                 }
                 return $this->get('shub_message_id');
             }
             break;
     }
     return false;
 }
 public function init()
 {
     if (isset($_REQUEST['_process'])) {
         // fix up WordPress bork:
         // todo: don't overwrite default superglobals, run stripslashes every time before we use the content, because another plugin might be stripslashing already
         $_POST = stripslashes_deep($_POST);
         $_GET = stripslashes_deep($_GET);
         $_REQUEST = stripslashes_deep($_REQUEST);
         $process_action = $_REQUEST['_process'];
         $process_options = array();
         $shub_message_id = false;
         if ($process_action == 'send_shub_message' && check_admin_referer('shub_send-message')) {
             // we are sending a social message! yay!
             $send_time = time();
             // default: now
             if (isset($_POST['schedule_date']) && isset($_POST['schedule_time']) && !empty($_POST['schedule_date']) && !empty($_POST['schedule_time'])) {
                 $date = $_POST['schedule_date'];
                 $time_hack = $_POST['schedule_time'];
                 $time_hack = str_ireplace('am', '', $time_hack);
                 $time_hack = str_ireplace('pm', '', $time_hack);
                 $bits = explode(':', $time_hack);
                 if (strpos($_POST['schedule_time'], 'pm')) {
                     $bits[0] += 12;
                 }
                 // add the time if it exists
                 $date .= ' ' . implode(':', $bits) . ':00';
                 $send_time = strtotime($date);
                 //echo $date."<br>".$send_time."<br>".shub_print_date($send_time,true);exit;
             } else {
                 if (isset($_POST['schedule_date']) && !empty($_POST['schedule_date'])) {
                     $send_time = strtotime($_POST['schedule_date']);
                 }
             }
             // wack a new entry into the shub_message database table and pass that onto our message_managers below
             $shub_message_id = shub_update_insert('shub_message_id', false, 'shub_message', array('post_id' => isset($_POST['post_id']) ? $_POST['post_id'] : 0, 'sent_time' => $send_time));
             if ($shub_message_id) {
                 $process_options['shub_message_id'] = $shub_message_id;
                 $process_options['send_time'] = $send_time;
             } else {
                 die('Failed to create social message');
             }
             /* @var $message_manager shub_facebook */
             $message_count = 0;
             foreach ($this->message_managers as $name => $message_manager) {
                 $message_count += $message_manager->handle_process($process_action, $process_options);
             }
             if ($shub_message_id && !$message_count) {
                 // remove the gobal social message as nothing worked.
                 shub_delete_from_db('shub_message', 'shub_message_id', $shub_message_id);
             } else {
                 if ($shub_message_id) {
                     shub_update_insert('shub_message_id', $shub_message_id, 'shub_message', array('message_count' => $message_count));
                 }
             }
             if (isset($_POST['debug']) && $_POST['debug']) {
                 echo "<br><hr> Successfully sent {$message_count} messages <hr><br><pre>";
                 print_r($_POST);
                 print_r($process_options);
                 echo "</pre><hr><br>Completed";
                 exit;
             }
             header("Location: admin.php?page=support_hub_sent");
             exit;
         } else {
             if ($process_action == 'save_general_settings') {
                 if (check_admin_referer('save-general-settings')) {
                     if (isset($_POST['possible_shub_manager_enabled'])) {
                         foreach ($_POST['possible_shub_manager_enabled'] as $id => $tf) {
                             if (isset($_POST['shub_manager_enabled'][$id]) && $_POST['shub_manager_enabled'][$id]) {
                                 update_option('shub_manager_enabled_' . $id, 1);
                             } else {
                                 update_option('shub_manager_enabled_' . $id, 0);
                             }
                         }
                         header("Location: admin.php?page=support_hub_settings");
                         exit;
                     }
                 }
             } else {
                 if ($process_action == 'save_log_settings') {
                     if (check_admin_referer('save-log-settings')) {
                         if (!empty($_POST['enable_logging'])) {
                             update_option('shub_logging_enabled', time() + 3600 * 24);
                         }
                         if (!empty($_POST['remove_logs'])) {
                             global $wpdb;
                             $wpdb->query($wpdb->prepare("DELETE FROM `" . _support_hub_DB_PREFIX . "shub_log` WHERE log_time <= %d", $_POST['remove_logs']));
                         }
                         header("Location: admin.php?page=support_hub_settings&tab=logs");
                         exit;
                     }
                 } else {
                     if ($process_action == 'save_encrypted_vault') {
                         if (check_admin_referer('save-encrypted-vault') && !empty($_POST['public_key']) && !empty($_POST['private_key'])) {
                             update_option('shub_encrypt_public_key', $_POST['public_key']);
                             update_option('shub_encrypt_private_key', $_POST['private_key']);
                             header("Location: admin.php?page=support_hub_settings&tab=extra");
                             exit;
                         }
                     } else {
                         if ($process_action == 'save_extra_details') {
                             $shub_extra_id = !empty($_REQUEST['shub_extra_id']) ? (int) $_REQUEST['shub_extra_id'] : 0;
                             if (check_admin_referer('save-extra' . $shub_extra_id)) {
                                 $shub_extra = new SupportHubExtra($shub_extra_id);
                                 if (isset($_REQUEST['butt_delete'])) {
                                     $shub_extra->delete();
                                     header("Location: admin.php?page=support_hub_settings&tab=extra");
                                     exit;
                                 }
                                 if (!$shub_extra->get('shub_extra_id')) {
                                     $shub_extra->create_new();
                                 }
                                 $shub_extra->update($_POST);
                                 $shub_extra_id = $shub_extra->get('shub_extra_id');
                                 header("Location: admin.php?page=support_hub_settings&tab=extra");
                                 //&shub_extra_id=" . $shub_extra_id );
                                 exit;
                             }
                         } else {
                             if ($process_action == 'save_product_details') {
                                 $shub_product_id = !empty($_REQUEST['shub_product_id']) ? (int) $_REQUEST['shub_product_id'] : 0;
                                 if (check_admin_referer('save-product' . $shub_product_id)) {
                                     $shub_product = new SupportHubProduct($shub_product_id);
                                     if (isset($_REQUEST['butt_delete'])) {
                                         $shub_product->delete();
                                         header("Location: admin.php?page=support_hub_settings&tab=products");
                                         exit;
                                     }
                                     if (!$shub_product->get('shub_product_id')) {
                                         $shub_product->create_new();
                                     }
                                     $shub_product->update($_POST);
                                     $shub_product_id = $shub_product->get('shub_product_id');
                                     header("Location: admin.php?page=support_hub_settings&tab=products");
                                     //&shub_product_id=" . $shub_product_id );
                                     exit;
                                 }
                             } else {
                                 // just process each request normally:
                                 /* @var $message_manager shub_facebook */
                                 foreach ($this->message_managers as $name => $message_manager) {
                                     $message_manager->handle_process($process_action, $process_options);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }