public function confirm_token()
 {
     // we quickly confirm our envato token by loading the my accounts page
     // we pull in the users email and picture from the account page and convert it into a shub_user
     // store this in the shub_user_id field so we can use it when rendering the UI for comments etc..
     $api = $this->get_api();
     $account_data = $api->get_token_account();
     if ($account_data && !empty($account_data['username'])) {
         // success
         $comment_user = new SupportHubUser_Envato();
         $res = $comment_user->load_by('user_username', $account_data['username']);
         if (!$res) {
             $comment_user->create_new();
             if (!$comment_user->get('user_username')) {
                 $comment_user->update('user_username', $account_data['username']);
             }
             if (!empty($account_data['image'])) {
                 $comment_user->update_user_data(array('image' => $account_data['image']));
             }
         }
         $shub_user_id = $comment_user->get('shub_user_id');
         if ($shub_user_id) {
             $this->update('shub_user_id', $shub_user_id);
         }
         return $shub_user_id;
     } else {
         echo "Failed to get account data. Please confirm the envato token is correct as per the help documentation. Without this you will be unable to post comment replies.";
         return false;
     }
 }
 private function _update_comments($data, $existing_messages)
 {
     if (is_array($data)) {
         $last_message_user_id = false;
         foreach ($data as $message) {
             if ($message['id']) {
                 // does this id exist in the db already?
                 $exists = shub_get_single('shub_message_comment', array('network_key', 'shub_message_id'), array($message['id'], $this->shub_message_id));
                 // create/update a user entry for this comments.
                 $shub_user_id = 0;
                 if (!empty($message['shub_user_id'])) {
                     $shub_user_id = $message['shub_user_id'];
                 } else {
                     if (!empty($message['username'])) {
                         $comment_user = new SupportHubUser_Envato();
                         $res = $comment_user->load_by('user_username', $message['username']);
                         if (!$res) {
                             $comment_user->create_new();
                             if (!$comment_user->get('user_username')) {
                                 $comment_user->update('user_username', $message['username']);
                             }
                             $comment_user->update_user_data(array('image' => $message['profile_image_url'], 'envato' => $message));
                         }
                         $shub_user_id = $comment_user->get('shub_user_id');
                     }
                 }
                 $shub_message_comment_id = shub_update_insert('shub_message_comment_id', $exists ? $exists['shub_message_comment_id'] : false, 'shub_message_comment', array('shub_message_id' => $this->shub_message_id, 'network_key' => $message['id'], 'time' => isset($message['created_at']) ? strtotime($message['created_at']) : (isset($message['timestamp']) ? $message['timestamp'] : 0), 'data' => json_encode($message), 'message_from' => isset($message['username']) ? json_encode(array("username" => $message['username'], "profile_image_url" => $message['profile_image_url'])) : '', 'message_to' => '', 'message_text' => isset($message['content']) ? $message['content'] : '', 'shub_user_id' => $shub_user_id));
                 $last_message_user_id = $shub_user_id;
                 if (isset($existing_messages[$shub_message_comment_id])) {
                     unset($existing_messages[$shub_message_comment_id]);
                 }
                 /*if(isset($message['comments']) && is_array($message['comments'])){
                       $existing_messages = $this->_update_messages($message['comments'], $existing_messages);
                   }*/
             }
         }
         if ($last_message_user_id) {
             if ($last_message_user_id == $this->account->get('shub_user_id')) {
                 // the last comment on this item was from the account owner.
                 // mark this item as resolves so it doesn;t show up in the inbox.
                 $this->update('shub_status', _shub_MESSAGE_STATUS_ANSWERED);
             }
         }
     }
     return $existing_messages;
 }
 public function extra_save_data($extra, $value, $network, $account_id, $message_id)
 {
     $shub_message = new shub_message(false, false, $message_id);
     $shub_user_id = !empty($_SESSION['shub_oauth_envato']['shub_user_id']) ? $_SESSION['shub_oauth_envato']['shub_user_id'] : $shub_message->get('shub_user_id');
     if (is_array($value) && !empty($value['extra_data']['valid_purchase_code'])) {
         // we're saving a previously validated (Above) purchase code.
         // create a shub user for this purchase and return success along with the purchase data to show
         $comment_user = new SupportHubUser_Envato();
         $res = false;
         if (!empty($value['extra_data']['buyer'])) {
             $res = $comment_user->load_by('user_username', $value['extra_data']['buyer']);
         }
         if (!$res) {
             $comment_user->create_new();
             $comment_user->update('user_username', $value['extra_data']['buyer']);
         }
         $user_data = $comment_user->get('user_data');
         if (!is_array($user_data)) {
             $user_data = array();
         }
         if (!isset($user_data['envato_codes'])) {
             $user_data['envato_codes'] = array();
         }
         $user_data_codes = array();
         $user_data_codes[$value['data']] = $value['extra_data'];
         $user_data['envato_codes'] = array_merge($user_data['envato_codes'], $user_data_codes);
         $comment_user->update_user_data($user_data);
         $shub_user_id = $comment_user->get('shub_user_id');
     }
     $extra->save_and_link(array('extra_value' => is_array($value) && !empty($value['data']) ? $value['data'] : $value, 'extra_data' => is_array($value) && !empty($value['extra_data']) ? $value['extra_data'] : false), $network, $account_id, $message_id, $shub_user_id);
 }
 public function get_user_hints($user_hints = array())
 {
     $comments = $this->get_comments();
     $first_comment = current($comments);
     if (isset($first_comment['shub_user_id']) && $first_comment['shub_user_id']) {
         $user_hints['shub_user_id'][] = $first_comment['shub_user_id'];
     }
     $message_from = @json_decode($first_comment['message_from'], true);
     if ($message_from && isset($message_from['username'])) {
         //} && $message_from['username'] != $envato_message->get('account')->get( 'account_name' )){
         // this wont work if user changes their username, oh well.
         $other_users = new SupportHubUser_Envato();
         $other_users->load_by_meta('envato_username', $message_from['username']);
         if ($other_users->get('shub_user_id') && !in_array($other_users->get('shub_user_id'), $user_hints['shub_user_id'])) {
             // pass these back to the calling method so we can get the correct values.
             $user_hints['shub_user_id'][] = $other_users->get('shub_user_id');
         }
     }
     return $user_hints;
 }