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; } } } } } }
public function update($field, $value) { // what fields to we allow? or not allow? if (in_array($field, array('shub_item_id'))) { return; } if ($this->shub_item_id) { $this->{$field} = $value; if (in_array($field, $this->json_fields)) { $value = json_encode($value); } shub_update_insert('shub_item_id', $this->shub_item_id, 'shub_item', array($field => $value)); } }
public function update($field, $value = false) { if (is_array($field)) { foreach ($field as $key => $val) { if (isset($this->details[$key])) { $this->update($key, $val); } } return; } // what fields to we allow? or not allow? if (in_array($field, array('shub_product_id'))) { return; } if ($this->shub_product_id) { $this->{$field} = $value; if (in_array($field, $this->json_fields)) { $value = json_encode($value); } shub_update_insert('shub_product_id', $this->shub_product_id, 'shub_product', array($field => $value)); } }
public function add_meta($key, $val) { if ((int) $this->shub_user_id > 0) { shub_update_insert(false, false, $this->db_table_meta, array('shub_user_id' => $this->shub_user_id, 'meta_key' => $key, 'meta_val' => $val)); } }
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 extra_send_message($message, $network, $account_id, $message_id) { // save this message in the database as a new comment. // set the 'private' flag so we know this comment has been added externally to the API scrape. $shub_message = new shub_message(false, false, $message_id); $existing_comments = $shub_message->get_comments(); shub_update_insert('shub_message_comment_id', false, 'shub_message_comment', array('shub_message_id' => $shub_message->get('shub_message_id'), 'private' => 1, 'message_text' => $message, 'time' => time(), 'shub_user_id' => !empty($_SESSION['shub_oauth_envato']['shub_user_id']) ? $_SESSION['shub_oauth_envato']['shub_user_id'] : $shub_message->get('shub_user_id'))); // mark the main message as unread so it appears at the top. $shub_message->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED); $shub_message->update('last_active', time()); // todo: update the 'summary' to reflect this latest message? $shub_message->update('summary', $message); // todo: post a "Thanks for providing information, we will reply soon" message on Envato comment page }
public function queue_reply($network_key, $message, $debug = false, $extra_data = array(), $shub_outbox_id = false) { if ($this->account && $this->shub_message_id) { if ($debug) { echo "Type: " . $this->get('shub_type') . " <br>\n"; } if (!$network_key) { $network_key = $this->get('network_key'); } if ($debug) { echo "Sending a reply to Network Message ID: {$network_key} <br>\n"; } $reply_user = $this->get_reply_user(); // add a placeholder in the comments table, next time the cron runs it should pick this up and fill in all the details correctly from the API $shub_message_comment_id = shub_update_insert('shub_message_comment_id', false, 'shub_message_comment', array('shub_message_id' => $this->shub_message_id, 'shub_user_id' => $reply_user ? $reply_user->get('shub_user_id') : 0, 'shub_outbox_id' => $shub_outbox_id, 'network_key' => '', 'time' => time(), 'message_text' => $message, 'user_id' => get_current_user_id())); if ($debug) { echo "Successfully added comment with id {$shub_message_comment_id} <br>\n"; } return $shub_message_comment_id; } return false; }
public function send_queued_comment_reply($envato_message_comment_id, $shub_outbox, $debug = false) { $comments = $this->get_comments(); if (isset($comments[$envato_message_comment_id]) && !empty($comments[$envato_message_comment_id]['message_text'])) { $api = $this->account->get_api(); $outbox_data = $shub_outbox->get('message_data'); if ($outbox_data && isset($outbox_data['extra']) && is_array($outbox_data['extra'])) { $extra_data = $outbox_data['extra']; } else { $extra_data = array(); } $api_result = $api->api('ticket', 'reply', array('ticket_id' => $this->get('network_key'), 'message' => $comments[$envato_message_comment_id]['message_text'], 'extra_data' => $extra_data)); if ($api_result && !empty($api_result['ticket_message_id'])) { if ($debug) { echo 'UCM API Result:'; print_r($api_result); } // add a placeholder in the comments table, next time the cron runs it should pick this up and fill in all the details correctly from the API shub_update_insert('shub_message_comment_id', $envato_message_comment_id, 'shub_message_comment', array('network_key' => $api_result['ticket_message_id'], 'time' => time())); return true; } else { echo "Failed to send comment, check debug log. " . var_export($api_result, true); return false; } } echo 'No comment found to send.'; return false; }
public function update($field, $value = false) { if (is_array($field)) { foreach ($field as $key => $val) { $this->update($key, $val); } return; } // what fields to we allow? or not allow? if (in_array($field, array($this->db_primary_key))) { return; } if ($this->{$this->db_primary_key} && isset($this->details[$field])) { $this->{$field} = $value; $this->details[$field] = $value; if (in_array($field, $this->json_fields)) { $value = json_encode($value); } shub_update_insert($this->db_primary_key, $this->{$this->db_primary_key}, $this->db_table, array($field => $value)); } }
public function log_data($error_level, $extension, $subject, $data = array()) { if (get_option('shub_logging_enabled', 0) > time() || $error_level > 0) { shub_update_insert('shub_log_id', false, 'shub_log', array('log_time' => time(), 'log_error_level' => $error_level, 'log_extension' => $extension, 'log_subject' => $subject, 'log_data' => $data ? serialize($data) : '')); } }
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 send_queued_comment_reply($bbpress_message_comment_id, $shub_outbox, $debug = false) { $comments = $this->get_comments(); if (isset($comments[$bbpress_message_comment_id]) && !empty($comments[$bbpress_message_comment_id]['message_text'])) { $api = $this->account->get_api(); //$item_data = $this->get('item')->get('item_data'); $bbpress_post_data = $this->get('shub_data'); $bbpress_id = $this->get('network_key'); if ($debug) { echo "Sending a reply to bbPress Topic ID: {$bbpress_id} <br>\n"; } $outbox_data = $shub_outbox->get('message_data'); if ($outbox_data && isset($outbox_data['extra']) && is_array($outbox_data['extra'])) { $extra_data = $outbox_data['extra']; } else { $extra_data = array(); } $api_result = false; try { $extra_data['api'] = 1; $api_result = $api->newPost('Reply to: ' . (isset($bbpress_post_data['post_title']) ? $bbpress_post_data['post_title'] : 'Post'), $comments[$bbpress_message_comment_id]['message_text'], array('post_type' => 'reply', 'post_parent' => $bbpress_id, 'custom_fields' => array(array('key' => 'support_hub', 'value' => json_encode($extra_data))))); SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'API Result: ', $api_result); } catch (Exception $e) { SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'bbpress', 'API Error: ', $e); if ($debug) { echo "API Error: " . $e; } } if ((int) $api_result > 0) { shub_update_insert('shub_message_comment_id', $bbpress_message_comment_id, 'shub_message_comment', array('network_key' => $api_result, 'time' => time())); return true; } else { echo "Failed to send comment, check debug log."; print_r($api_result); return false; } /*if((int) $api_result > 0){ // we have a post id for our reply! // add this reply to the 'comments' array of our existing 'message' object. // grab the updated post details for both the parent topic and the newly created reply: $parent_topic = $api->getPost($this->get('network_key')); SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'API Result: ', $api_result); $reply_post = $api->getPost($api_result); SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'API Result: ', $api_result); if($parent_topic && $parent_topic['post_id'] == $this->get('network_key') && $reply_post && $reply_post['post_id'] == $api_result && $reply_post['post_parent'] == $this->get('network_key')){ // all looks hunky dory $comments = $this->get('comments'); if(!is_array($comments))$comments = array(); array_unshift($comments, $reply_post); $parent_topic['replies'] = $comments; // save this updated data to the db $this->load_by_bbpress_id($this->get('network_key'),$parent_topic,$this->get('shub_type'),$debug); $existing_messages = $this->get_comments(); foreach($existing_messages as $existing_message){ if(!$existing_message['user_id'] && $existing_message['message_text'] == $comments[$bbpress_message_comment_id]['message_text']){ shub_update_insert('shub_message_comment_id',$existing_message['shub_message_comment_id'],'shub_message_comment',array( 'user_id' => get_current_user_id(), )); } } $this->update('shub_status', _shub_MESSAGE_STATUS_ANSWERED); } }*/ } return false; }
public function update($field, $value) { // what fields to we allow? or not allow? if (in_array($field, array('shub_account_id'))) { return; } if ($this->shub_account_id) { if ($field == 'account_data') { if (is_array($value)) { // merge data with existing. $existing_data = $this->get('account_data'); if (!is_array($existing_data)) { $existing_data = array(); } $value = array_merge($existing_data, $value); } } $this->{$field} = $value; if (in_array($field, $this->json_fields)) { $value = json_encode($value); } shub_update_insert('shub_account_id', $this->shub_account_id, 'shub_account', array($field => $value)); } }
public function send_queued_comment_reply($envato_message_comment_id, $shub_outbox, $debug = false) { $comments = $this->get_comments(); if (isset($comments[$envato_message_comment_id]) && !empty($comments[$envato_message_comment_id]['message_text'])) { $api = $this->account->get_api(); $item_data = $this->get('item')->get('item_data'); if ($item_data && $item_data['url']) { $api_result = $api->post_comment($item_data['url'] . '/comments', $this->get('network_key'), $comments[$envato_message_comment_id]['message_text']); if ($api_result) { // add a placeholder in the comments table, next time the cron runs it should pick this up and fill in all the details correctly from the API shub_update_insert('shub_message_comment_id', $envato_message_comment_id, 'shub_message_comment', array('network_key' => $api_result, 'time' => time())); return true; } else { echo "Failed to send comment, check debug log."; return false; } } } return false; }