public function load($shub_item_id = false)
 {
     if (!$shub_item_id) {
         $shub_item_id = $this->shub_item_id;
     }
     $this->reset();
     $this->shub_item_id = $shub_item_id;
     if ($this->shub_item_id) {
         $data = shub_get_single('shub_item', 'shub_item_id', $this->shub_item_id);
         foreach ($this->details as $key => $val) {
             $this->details[$key] = $data && isset($data[$key]) ? $data[$key] : $val;
             if (in_array($key, $this->json_fields)) {
                 $this->details[$key] = @json_decode($this->details[$key], true);
                 if (!is_array($this->details[$key])) {
                     $this->details[$key] = array();
                 }
             }
         }
         if (!is_array($this->details) || $this->details['shub_item_id'] != $this->shub_item_id) {
             $this->reset();
             return false;
         }
     }
     foreach ($this->details as $key => $val) {
         $this->{$key} = $val;
     }
     return $this->shub_item_id;
 }
 public function init()
 {
     if (isset($_GET[_SUPPORT_HUB_LINK_REWRITE_PREFIX]) && strlen($_GET[_SUPPORT_HUB_LINK_REWRITE_PREFIX]) > 0) {
         // check hash
         $bits = explode(':', $_GET[_SUPPORT_HUB_LINK_REWRITE_PREFIX]);
         if (defined('AUTH_KEY') && isset($bits[1])) {
             $shub_message_link_id = (int) $bits[0];
             if ($shub_message_link_id > 0) {
                 $correct_hash = substr(md5(AUTH_KEY . ' shub extension link ' . $shub_message_link_id), 1, 5);
                 if ($correct_hash == $bits[1]) {
                     // link worked! log a visit and redirect.
                     $link = shub_get_single('shub_message_link', 'shub_message_link_id', $shub_message_link_id);
                     if ($link) {
                         if (!preg_match('#^http#', $link['link'])) {
                             $link['link'] = 'http://' . trim($link['link']);
                         }
                         shub_update_insert('shub_message_link_click_id', false, 'shub_message_link_click', array('shub_message_link_id' => $shub_message_link_id, 'click_time' => time(), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'url_referrer' => $_SERVER['HTTP_REFERER']));
                         header("Location: " . $link['link']);
                         exit;
                     }
                 }
             }
         }
     }
 }
function shub_update_insert($primary_key_name, $primary_key_value, $table_name, $data)
{
    global $wpdb;
    if (!is_array($data)) {
        $data = array();
    }
    // does it exist?
    $exists = $primary_key_value ? shub_get_single($table_name, $primary_key_name, $primary_key_value) : false;
    if (!$exists) {
        if ($primary_key_name && $primary_key_value) {
            $data[$primary_key_name] = $primary_key_value;
        }
        $wpdb->insert(_support_hub_DB_PREFIX . $table_name, $data);
        return $wpdb->insert_id;
    } else {
        if ($primary_key_value) {
            $stat = $wpdb->update(_support_hub_DB_PREFIX . $table_name, $data, array($primary_key_name => $primary_key_value));
            return $primary_key_value;
        } else {
            echo 'database update error - please report this';
            exit;
        }
    }
}
 public function load($shub_account_id = false)
 {
     if (!$shub_account_id) {
         $shub_account_id = $this->shub_account_id;
     }
     $this->reset();
     $this->shub_account_id = (int) $shub_account_id;
     if ($this->shub_account_id) {
         $data = shub_get_single('shub_account', 'shub_account_id', $this->shub_account_id);
         foreach ($this->details as $key => $val) {
             $this->details[$key] = $data && isset($data[$key]) ? $data[$key] : $val;
             if (in_array($key, $this->json_fields)) {
                 $this->details[$key] = @json_decode($this->details[$key], true);
                 if (!is_array($this->details[$key])) {
                     $this->details[$key] = array();
                 }
             }
         }
         if (!is_array($this->details) || $this->details['shub_account_id'] != $this->shub_account_id) {
             $this->reset();
             return false;
         }
     }
     foreach ($this->details as $key => $val) {
         $this->{$key} = $val;
     }
     $this->items = array();
     if (!$this->shub_account_id) {
         return false;
     }
     foreach (shub_get_multiple('shub_item', array('shub_account_id' => $this->shub_account_id), 'shub_item_id') as $item) {
         $item = $this->get_item($item['shub_item_id']);
         $this->items[$item->get('network_key')] = $item;
     }
     return $this->shub_account_id;
 }
 public function get_meta($key = false, $val = false)
 {
     $return = array();
     if (!$key) {
         // return all meta values in an associative array.
         $all_meta = shub_get_multiple($this->db_table_meta, array('shub_user_id' => $this->shub_user_id));
         foreach ($all_meta as $meta) {
             if (!isset($return[$meta['meta_key']])) {
                 $return[$meta['meta_key']] = array();
             }
             $return[$meta['meta_key']][] = $meta['meta_val'];
         }
     } else {
         if ($key && !$val) {
             // return all matching meta values in an associative array.
             $all_meta = shub_get_multiple($this->db_table_meta, array('shub_user_id' => $this->shub_user_id, 'meta_key' => $key));
             foreach ($all_meta as $meta) {
                 $return[] = $meta['meta_val'];
             }
         } else {
             // return all matching meta values in an associative array.
             $all_meta = shub_get_single($this->db_table_meta, array('shub_user_id', 'meta_key', 'meta_val'), array($this->shub_user_id, $key, $val));
             $return = $all_meta['meta_val'];
         }
     }
     return $return;
 }
 public function update_author_sale_history($debug = false, $do_all = false)
 {
     return;
     // todo: save sale history in db with no username/api key just for stats
     $api = $this->get_api();
     // how many days do we want to go back? maybe 60 days to start with?
     $last_sale = get_option('supporthub_envato_author_sales_last', false);
     if (!$last_sale) {
         $last_sale = strtotime('-360 days');
     }
     $last_sale_in_this_batch = 0;
     $page = 1;
     while (true) {
         $recent_sales = $api->api('v2/market/author/sales?page=' . $page, array(), true);
         //            echo "Recent sales are: ";print_r($recent_sales);exit;
         if ($debug) {
             echo "Page {$page} of sales data contained " . count($recent_sales) . " results.<br>\n";
         }
         $page++;
         if (!$recent_sales || !is_array($recent_sales)) {
             break;
         }
         foreach ($recent_sales as $recent_sale) {
             if ($recent_sale && !empty($recent_sale['sold_at']) && !empty($recent_sale['code']) && !empty($recent_sale['item']['id'])) {
                 //                    echo $recent_sale['sold_at']."<br>";
                 // add this to the database, or break if we already have this one in the db.
                 $sale_time = strtotime($recent_sale['sold_at']);
                 // we might already have this one in our database
                 // unless we are doing the intial seed
                 $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($recent_sale['code']));
                 if ($existing_purchase) {
                     if (!$do_all) {
                         break 2;
                         // stop processing once we reach one we've already saved
                     }
                     continue;
                     // exists already in the db, skip to next one.
                 }
                 $last_sale_in_this_batch = max($last_sale_in_this_batch, $sale_time);
                 // todo: check if they add username to the system, for now we use a 0 shub_user_id because we're unsure which user this purchase is related to (without doing another separate purchase call)
                 if ($debug) {
                     echo " - adding new sale to database ( " . shub_print_date($sale_time, true) . " - " . $recent_sale['item']['name'] . " )...<br>\n";
                 }
                 // for now we do all processing based on this purchase code. SLOW. but until we get usernames in the buyer result there is no other way.
                 //                    echo "Query this code: ".$recent_sale['code'];exit;
                 SupportHub::getInstance()->message_managers['envato']->pull_purchase_code($api, $recent_sale['code'], $recent_sale);
                 update_option('supporthub_envato_author_sales_last', $last_sale_in_this_batch);
                 continue;
                 // save this purchase code into the db
                 // find the product this purchase is related to
                 $existing_products = SupportHub::getInstance()->get_products();
                 // check if this item exists already
                 $exists = false;
                 foreach ($existing_products as $existing_product) {
                     if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $recent_sale['item']['id']) {
                         $exists = $existing_product['shub_product_id'];
                     }
                 }
                 $newproduct = new SupportHubProduct();
                 if (!$exists) {
                     $newproduct->create_new();
                     if (!$newproduct->get('product_name')) {
                         $newproduct->update('product_name', $recent_sale['item']['name']);
                     }
                     $existing_product_data = $newproduct->get('product_data');
                     if (!is_array($existing_product_data)) {
                         $existing_product_data = array();
                     }
                     if (empty($existing_product_data['envato_item_id'])) {
                         $existing_product_data['envato_item_id'] = $recent_sale['item']['id'];
                     }
                     if (empty($existing_product_data['envato_item_data'])) {
                         $existing_product_data['envato_item_data'] = $recent_sale['item'];
                     }
                     if (empty($existing_product_data['image'])) {
                         $existing_product_data['image'] = $recent_sale['item']['thumbnail_url'];
                     }
                     if (empty($existing_product_data['url'])) {
                         $existing_product_data['url'] = $recent_sale['item']['url'];
                     }
                     $newproduct->update('product_data', $existing_product_data);
                 } else {
                     $newproduct->load($exists);
                 }
                 if ($newproduct->get('shub_product_id')) {
                     // product has been added
                     // time to add it to the purchase db
                     // check if this already exists in the db
                     $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($recent_sale['code']));
                     if (!$existing_purchase) {
                         $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => 0, 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => $sale_time, 'envato_user_id' => 0, 'purchase_code' => $recent_sale['code'], 'api_type' => 'author/sales', 'purchase_data' => json_encode($recent_sale)));
                     } else {
                         $shub_envato_purchase_id = $existing_purchase['shub_envato_purchase_id'];
                     }
                     if ($shub_envato_purchase_id) {
                         // we have a purchase in the db
                         // add or update the support expiry based on this purchase history.
                         // work out when this purchase support expires
                         // this is the expiry date returned in the api or just 6 months from the original purchase date.
                         $support_expiry_time = strtotime("+6 months", $sale_time);
                         // todo - check for this expiry time in the new api results.
                         $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($shub_envato_purchase_id));
                         if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == $sale_time) {
                             // check the existing support expiry matches the one we have in the database.
                             if ($existing_support['end_time'] < $support_expiry_time) {
                                 // we have a support extension!
                                 $shub_envato_support_id = shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($recent_sale)));
                             }
                         } else {
                             // we are adding a new support entry
                             $shub_envato_support_id = shub_update_insert('shub_envato_support_id', false, 'shub_envato_support', array('shub_user_id' => 0, 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, 'start_time' => $sale_time, 'end_time' => $support_expiry_time, 'api_type' => 'author/sales', 'support_data' => json_encode($recent_sale)));
                         }
                     }
                 }
             }
         }
     }
 }
 public function filter_message_user_sidebar($user_bits, $shub_user_ids)
 {
     // find purchases for these user ids and if they are in a valid support term.
     if (!empty($shub_user_ids) && is_array($shub_user_ids)) {
         foreach ($shub_user_ids as $shub_user_id) {
             if ((int) $shub_user_id > 0) {
                 // find purchases.
                 $total = 0;
                 $purchases = shub_get_multiple('shub_envato_purchase', array('shub_user_id' => $shub_user_id));
                 foreach ($purchases as $purchase) {
                     if ($purchase['shub_product_id']) {
                         $purchase_product = new SupportHubProduct($purchase['shub_product_id']);
                         $data = $purchase_product->get('product_data');
                         if (!empty($data['envato_item_data']['item'])) {
                             $support_text = '<span class="buyer_status_badges">';
                             $support = shub_get_single('shub_envato_support', 'shub_envato_purchase_id', $purchase['shub_envato_purchase_id']);
                             if ($support && !empty($support['end_time']) && $support['end_time'] <= time()) {
                                 // WHOPPS. I got this wrong in the DB initially. Hack to double check purchase happened before new support terms
                                 if (strtotime($purchase['purchase_time']) < strtotime("2015-09-01")) {
                                     $support['end_time'] = strtotime("+6 months", strtotime("2015-09-01"));
                                 }
                             }
                             if ($support && !empty($support['end_time']) && $support['end_time'] > time()) {
                                 $support_text .= '<span class="buyer_badge supported">' . shub_print_date($support['end_time']) . '</span>';
                             } else {
                                 if ($support && !empty($support['end_time'])) {
                                     $support_text .= '<span class="buyer_badge unsupported">' . shub_print_date($support['end_time']) . '</span>';
                                 } else {
                                     $support_text .= '<span class="buyer_badge unsupported">Unknown</span>';
                                 }
                             }
                             $support_text .= '</span>';
                             $user_bits[] = array('Purchase', esc_html($data['envato_item_data']['item']) . ' on ' . shub_print_date($purchase['purchase_time']) . ' support until ' . $support_text);
                             $sale_data = @json_decode($purchase['purchase_data'], true);
                             if ($sale_data && !empty($sale_data['amount'])) {
                                 $total += $sale_data['amount'];
                             }
                             if ($sale_data && !empty($sale_data['support_amount'])) {
                                 $total += $sale_data['support_amount'];
                             }
                             if (!$sale_data) {
                                 print_r($purchase);
                             }
                         }
                     } else {
                         // failed API lookup, show purchase code instead.
                         // todo: a re-lookup button.
                         $user_bits[] = array('Failed Purchase Code', $purchase['purchase_code']);
                     }
                 }
                 if ($total) {
                     $user_bits[] = array('Total', '$' . number_format($total, 2, ".", ","));
                 }
             }
         }
     }
     return $user_bits;
 }
 public function get_buyer_status($shub_user_id)
 {
     $return = array();
     if ($shub_product_id = $this->get_product_id()) {
         // thid is a duplicate of the code in class.shub_envato.php to determine if a user has purchased a product
         // todo: try to make them both use the same cached data.
         $purchases = shub_get_multiple('shub_envato_purchase', array('shub_user_id' => $shub_user_id, 'shub_product_id' => $shub_product_id));
         foreach ($purchases as $purchase) {
             if ($purchase['shub_product_id']) {
                 $purchase_product = new SupportHubProduct($purchase['shub_product_id']);
                 $data = $purchase_product->get('product_data');
                 if (!empty($data['envato_item_data']['item'])) {
                     $return['purchased'] = true;
                     $support = shub_get_single('shub_envato_support', 'shub_envato_purchase_id', $purchase['shub_envato_purchase_id']);
                     if ($support && !empty($support['end_time']) && $support['end_time'] <= time()) {
                         // WHOPPS. I got this wrong in the DB initially. Hack to double check purchase happened before new support terms
                         if (strtotime($purchase['purchase_time']) < strtotime("2015-09-01")) {
                             $support['end_time'] = strtotime("+6 months", strtotime("2015-09-01"));
                         }
                     }
                     if ($support && !empty($support['end_time']) && $support['end_time'] > time()) {
                         $return['supported'] = true;
                     }
                 }
             }
         }
         if (empty($return['purchased'])) {
             $return['presale'] = true;
         }
         if (empty($return['supported']) && empty($return['presale'])) {
             $return['unsupported'] = true;
         }
     } else {
         $return['unknown'] = true;
     }
     return $return;
 }
 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 update_purchase_history()
 {
     $tokens = shub_get_multiple('shub_envato_oauth', array('shub_user_id' => $this->shub_user_id));
     // find the latest token for this user, per account.
     $account_tokens = array();
     // if any of them have expired, refresh the token from the api
     foreach ($tokens as $token) {
         if (!$token['shub_account_id']) {
             continue;
         }
         if (!isset($account_tokens[$token['shub_account_id']]) || $token['expire_time'] > $account_tokens[$token['shub_account_id']]['expire_time']) {
             $account_tokens[$token['shub_account_id']] = $token;
         }
     }
     foreach ($account_tokens as $account_token) {
         $shub_envato_account = new shub_envato_account($account_token['shub_account_id']);
         // found the account, pull in the API and build the url
         $api = $shub_envato_account->get_api();
         $api->set_manual_token($account_token);
         if ($account_token['expire_time'] <= time()) {
             // renew this token!
             $new_access_token = $api->refresh_token();
             if ($new_access_token) {
                 shub_update_insert('shub_envato_oauth_id', $account_token['shub_envato_oauth_id'], 'shub_envato_oauth', array('access_token' => $new_access_token, 'expire_time' => time() + 3600));
             } else {
                 echo 'Token refresh failed';
                 return false;
             }
         }
         $api_result = $api->api('v1/market/private/user/username.json', array(), false);
         $api_result_email = $api->api('v1/market/private/user/email.json', array(), false);
         if ($api_result && !empty($api_result['username'])) {
             $this->add_unique_meta('envato_username', $api_result['username']);
         }
         if ($api_result_email && !empty($api_result_email['email'])) {
             $email = trim(strtolower($api_result_email['email']));
             // todo: not sure if best to update users eamail , if they change email accounts and stuff
             $this->update('user_email', $email);
         }
         $api_result_purchase_history = $api->api('v2/market/buyer/purchases', array(), false);
         // store this purchase history in our db for later use.
         if ($api_result_purchase_history && !empty($api_result_purchase_history['buyer']['id']) && !empty($api_result_purchase_history['buyer']['username']) && $api_result_purchase_history['buyer']['username'] == $api_result['username']) {
             // we have the buyer ID! yay! this is better than a username.
             $this->add_unique_meta('envato_user_id', $api_result_purchase_history['buyer']['id']);
             if (!empty($api_result_purchase_history['purchases']) && is_array($api_result_purchase_history['purchases'])) {
                 foreach ($api_result_purchase_history['purchases'] as $purchase) {
                     if (!empty($purchase['item']['id'])) {
                         // todo: beg envato to add the purchase code to this output so we can link it together correctly.
                         // find out which shub product this is for
                         // if we cannot find one then we create one. this helps when new items are made.
                         $existing_products = SupportHub::getInstance()->get_products();
                         // check if this item exists already
                         $exists = false;
                         foreach ($existing_products as $existing_product) {
                             if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $purchase['item']['id']) {
                                 $exists = $existing_product['shub_product_id'];
                             }
                         }
                         $newproduct = new SupportHubProduct();
                         if (!$exists) {
                             $newproduct->create_new();
                         } else {
                             $newproduct->load($exists);
                         }
                         if (!$newproduct->get('product_name')) {
                             $newproduct->update('product_name', $purchase['item']['name']);
                         }
                         $existing_product_data = $newproduct->get('product_data');
                         if (!is_array($existing_product_data)) {
                             $existing_product_data = array();
                         }
                         if (empty($existing_product_data['envato_item_id'])) {
                             $existing_product_data['envato_item_id'] = $purchase['item']['id'];
                         }
                         if (empty($existing_product_data['envato_item_data'])) {
                             $existing_product_data['envato_item_data'] = $purchase['item'];
                         }
                         if (empty($existing_product_data['image'])) {
                             $existing_product_data['image'] = $purchase['item']['thumbnail_url'];
                         }
                         if (empty($existing_product_data['url'])) {
                             $existing_product_data['url'] = $purchase['item']['url'];
                         }
                         $newproduct->update('product_data', $existing_product_data);
                         if ($newproduct->get('shub_product_id')) {
                             // product has been added
                             // time to add it to the purchase db
                             // check if this already exists in the db
                             $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($purchase['code']));
                             if (!$existing_purchase) {
                                 $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => $this->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => strtotime($purchase['sold_at']), 'envato_user_id' => $api_result_purchase_history['buyer']['id'], 'purchase_code' => $purchase['code'], 'api_type' => 'buyer/purchases', 'purchase_data' => json_encode($purchase)));
                             } else {
                                 if (!$existing_purchase['shub_user_id']) {
                                     shub_update_insert('shub_envato_purchase_id', $existing_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array('shub_user_id' => $this->get('shub_user_id')));
                                 }
                                 $shub_envato_purchase_id = $existing_purchase['shub_envato_purchase_id'];
                             }
                             if ($shub_envato_purchase_id) {
                                 // we have a purchase in the db
                                 // add or update the support expiry based on this purchase history.
                                 // work out when this purchase support expires
                                 // this is the expiry date returned in the api or just 6 months from the original purchase date.
                                 $support_expiry_time = strtotime("+6 months", strtotime($purchase['sold_at']));
                                 // todo - check for this expiry time in the new api results.
                                 $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($shub_envato_purchase_id));
                                 if ($existing_support && empty($existing_support['shub_user_id'])) {
                                     shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('shub_user_id' => $this->get('shub_user_id')));
                                 }
                                 if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == strtotime($purchase['sold_at'])) {
                                     // check the existing support expiry matches the one we have in the database.
                                     if ($existing_support['end_time'] < $support_expiry_time) {
                                         // we have a support extension!
                                         $shub_envato_support_id = shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($purchase)));
                                     }
                                 } else {
                                     // we are adding a new support entry
                                     $shub_envato_support_id = shub_update_insert('shub_envato_support_id', false, 'shub_envato_support', array('shub_user_id' => $this->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, 'start_time' => strtotime($purchase['sold_at']), 'end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($purchase)));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return true;
 }
 public function load_by_bbpress_id($bbpress_id, $topic_data, $type, $debug = false)
 {
     switch ($type) {
         case 'forum_topic':
             /*{"post_id":"3381","post_title":"Child THEME IMPOSSIBLE","post_date":{"scalar":"20150304T00:19:51","xmlrpc_type":"datetime","timestamp":1425428391},"post_date_gmt":{"scalar":"20150304T00:19:51","xmlrpc_type":"datetime","timestamp":1425428391},"post_modified":{"scalar":"20150304T00:19:51","xmlrpc_type":"datetime","timestamp":1425428391},"post_modified_gmt":{"scalar":"20150304T00:19:51","xmlrpc_type":"datetime","timestamp":1425428391},"post_status":"publish","post_type":"topic","post_name":"child-theme-impossible","post_author":"1442","post_password":"","post_excerpt":"","post_content":"Has anyone been able to create a child theme that keeps the animations please help","post_parent":"2613","post_mime_type":"","link":"http:\/\/dtbaker.net\/forums\/topic\/child-theme-impossible\/","guid":"http:\/\/dtbaker.net\/forums\/topic\/child-theme-impossible\/","menu_order":0,"comment_status":"closed","ping_status":"closed","sticky":false,"post_thumbnail":[],"post_format":"standard","terms":[],"custom_fields":[{"id":"9897","key":"author","value":""},{"id":"9898","key":"stars","value":""}],"replies":[{"post_id":"3409","post_title":"","post_date":{"scalar":"20150413T18:16:41","xmlrpc_type":"datetime","timestamp":1428949001},"post_date_gmt":{"scalar":"20150413T18:16:41","xmlrpc_type":"datetime","timestamp":1428949001},"post_modified":{"scalar":"20150413T18:16:41","xmlrpc_type":"datetime","timestamp":1428949001},"post_modified_gmt":{"scalar":"20150413T18:16:41","xmlrpc_type":"datetime","timestamp":1428949001},"post_status":"publish","post_type":"reply","post_name":"3409","post_author":"1692","post_password":"","post_excerpt":"","post_content":"I just purchased this last week and am having problems too. Support for this template seems to be nonexistent. This design has been approved by my client but I'm unable to get the theme to function correctly and I can't get any support from the developer. The deadline for this site is approaching and I'm dead in the water. Hopefully I can get a refund on this one.","post_parent":"3381","post_mime_type":"","link":"http:\/\/dtbaker.net\/forums\/reply\/3409\/","guid":"http:\/\/dtbaker.net\/forums\/reply\/3409\/","menu_order":2,"comment_status":"closed","ping_status":"closed","sticky":false,"post_thumbnail":[],"post_format":"standard","terms":[],"custom_fields":[]},{"post_id":"3394","post_title":"","post_date":{"scalar":"20150318T22:42:54","xmlrpc_type":"datetime","timestamp":1426718574},"post_date_gmt":{"scalar":"20150318T22:42:54","xmlrpc_type":"datetime","timestamp":1426718574},"post_modified":{"scalar":"20150318T22:42:54","xmlrpc_type":"datetime","timestamp":1426718574},"post_modified_gmt":{"scalar":"20150318T22:42:54","xmlrpc_type":"datetime","timestamp":1426718574},"post_status":"publish","post_type":"reply","post_name":"3394","post_author":"1458","post_password":"","post_excerpt":"","post_content":"I have not been able to and just posted that same question before I found your question..","post_parent":"3381","post_mime_type":"","link":"http:\/\/dtbaker.net\/forums\/reply\/3394\/","guid":"http:\/\/dtbaker.net\/forums\/reply\/3394\/","menu_order":1,"comment_status":"closed","ping_status":"closed","sticky":false,"post_thumbnail":[],"post_format":"standard","terms":[],"custom_fields":[]}],"timestamp":1428949001}*/
             $existing = shub_get_single('shub_message', 'network_key', $bbpress_id);
             if ($existing) {
                 // load it up.
                 $this->load($existing['shub_message_id']);
             }
             if ($topic_data && isset($topic_data['post_id']) && $topic_data['post_id'] == $bbpress_id) {
                 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'));
                 /* Array
                                     (
                                         [post_id] => 3549
                                         [post_title] => slider
                                         [post_date] => stdClass Object
                                             (
                                                 [scalar] => 20150716T12:51:14
                                                 [xmlrpc_type] => datetime
                                                 [timestamp] => 1437051074
                                             )
                 
                                         [post_date_gmt] => stdClass Object
                                             (
                                                 [scalar] => 20150716T12:51:14
                                                 [xmlrpc_type] => datetime
                                                 [timestamp] => 1437051074
                                             )
                 
                                         [post_modified] => stdClass Object
                                             (
                                                 [scalar] => 20150716T12:51:14
                                                 [xmlrpc_type] => datetime
                                                 [timestamp] => 1437051074
                                             )
                 
                                         [post_modified_gmt] => stdClass Object
                                             (
                                                 [scalar] => 20150716T12:51:14
                                                 [xmlrpc_type] => datetime
                                                 [timestamp] => 1437051074
                                             )
                 
                                         [post_status] => publish
                                         [post_type] => topic
                                         [post_name] => slider
                                         [post_author] => 1466
                                         [post_password] =>
                                         [post_excerpt] =>
                                         [post_content] => i would like to add the revolution slider to the top of the content on the home page but i am unsure where to paste the slider shortcode in the main theme php file
                                         [post_parent] => 2398
                                         [post_mime_type] =>
                                         [link] => http://dtbaker.net/forums/topic/slider/
                                         [guid] => http://dtbaker.net/forums/topic/slider/
                                         [menu_order] => 0
                                         [comment_status] => closed
                                         [ping_status] => closed
                                         [sticky] =>
                                         [post_thumbnail] => Array
                                             (
                                             )
                 
                                         [post_format] => standard
                                         [terms] => Array
                                             (
                                          $comments   )
                 
                                         [custom_fields] => Array */
                 $comments = $topic_data['replies'];
                 foreach ($comments as $id => $comment) {
                     if (empty($comment['post_id'])) {
                         continue;
                     }
                     $comments[$id]['id'] = $comment['post_id'];
                     if (!empty($comment['post_author'])) {
                         $comments[$id]['shub_user_id'] = $this->account->get_api_user_to_id($comment['post_author']);
                     }
                     // timestamp is handled in forum class
                     $comments[$id]['content'] = $comment['post_content'];
                 }
                 $this->update('title', $topic_data['post_title']);
                 // latest comment goes in summary
                 $this->update('summary', isset($comments[0]) ? $comments[0]['post_content'] : $topic_data['post_content']);
                 $this->update('last_active', !empty($topic_data['timestamp']) ? $topic_data['timestamp'] : (is_array($topic_data['post_date']) ? $topic_data['post_date']['timestamp'] : (isset($topic_data['post_date']->timestamp) ? $topic_data['post_date']->timestamp : 0)));
                 $this->update('shub_type', $type);
                 $this->update('shub_data', $topic_data);
                 $this->update('shub_link', $topic_data['link'] . '#post-' . (isset($comments[0]) ? $comments[0]['post_id'] : $topic_data['post_id']));
                 $this->update('network_key', $bbpress_id);
                 if ($this->get('shub_status') != _shub_MESSAGE_STATUS_HIDDEN) {
                     $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED);
                 }
                 $this->update('comments', $comments);
                 // create/update a user entry for this comments.
                 $shub_user_id = $this->account->get_api_user_to_id($topic_data['post_author']);
                 $this->update('shub_user_id', $shub_user_id);
                 return $this->get('shub_message_id');
             }
             break;
     }
     return false;
 }
 public function load_by_network_key($network_key, $message_data, $type, $debug = false)
 {
     switch ($type) {
         case 'item_comment':
             $existing = shub_get_single('shub_message', 'network_key', $network_key);
             if ($existing) {
                 // load it up.
                 $this->load($existing['shub_message_id']);
             }
             if ($message_data && isset($message_data['id']) && $message_data['id'] == $network_key) {
                 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'));
                 $comments = $message_data['conversation'];
                 $this->update('title', $comments[0]['content']);
                 $this->update('summary', $comments[count($comments) - 1]['content']);
                 $this->update('last_active', strtotime($message_data['last_comment_at']));
                 $this->update('shub_type', $type);
                 $this->update('shub_data', $message_data);
                 $this->update('shub_link', $message_data['url'] . '/' . $message_data['id']);
                 $this->update('network_key', $network_key);
                 SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'Updating message ' . $network_key . ' from API.', array('Current Comment Count: ' => count($this->get_comments()), 'New Comment Count: ' => count($comments), 'Current Status: ' => $this->get('shub_status')));
                 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);
                 // create/update a user entry for this comments.
                 $shub_user_id = 0;
                 $first_comment = current($comments);
                 if (!empty($first_comment['username'])) {
                     $comment_user = new SupportHubUser_Envato();
                     $res = $comment_user->load_by('envato_username', $first_comment['username']);
                     if (!$res) {
                         $res = $comment_user->load_by('user_username', $first_comment['username']);
                         if (!$res) {
                             $comment_user->create_new();
                         }
                     }
                     if (!$comment_user->get('user_username')) {
                         $comment_user->update('user_username', $first_comment['username']);
                     }
                     if (!$comment_user->get('envato_username')) {
                         $comment_user->update('envato_username', $first_comment['username']);
                     }
                     $comment_user->update_user_data(array('image' => $first_comment['profile_image_url'], 'envato' => $first_comment));
                     $shub_user_id = $comment_user->get('shub_user_id');
                 }
                 $this->update('shub_user_id', $shub_user_id);
                 return $existing;
             }
             break;
     }
 }