public function delete() { if ($this->shub_item_id) { // delete all the messages for this item. $messages = shub_get_multiple('shub_message', array('shub_item_id' => $this->shub_item_id), 'shub_message_id'); foreach ($messages as $message) { if ($message && isset($message['shub_item_id']) && $message['shub_item_id'] == $this->shub_item_id) { shub_delete_from_db('shub_message', 'shub_message_id', $message['shub_message_id']); shub_delete_from_db('shub_message_comment', 'shub_message_id', $message['shub_message_id']); shub_delete_from_db('shub_message_link', 'shub_message_id', $message['shub_message_id']); shub_delete_from_db('shub_message_read', 'shub_message_id', $message['shub_message_id']); } } shub_delete_from_db('shub_item', 'shub_item_id', $this->shub_item_id); } }
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 get_accounts() { $this->accounts = shub_get_multiple('shub_account', array('shub_extension' => $this->id), 'shub_account_id'); return $this->accounts; }
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 static function get_failed() { return shub_get_multiple('shub_outbox', array('shub_status' => _SHUB_OUTBOX_STATUS_FAILED), 'shub_outbox_id'); }
public function get_products() { $products = shub_get_multiple('shub_product', array(), 'shub_product_id', 'product_name ASC'); foreach ($products as $key => $val) { if (isset($val['product_data'])) { $products[$key]['product_data'] = @json_decode($val['product_data'], true); } } return $products; }
/** static stuff **/ public static function get_all_extras() { $return = array(); $data = shub_get_multiple('shub_extra', array(), 'shub_extra_id', 'extra_order'); foreach ($data as $d) { $return[$d['shub_extra_id']] = new SupportHubExtra($d['shub_extra_id']); } return $return; }
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; }
<?php // show logs $myListTable = new SupportHubLogList(); $myListTable->set_columns(array('log_time' => __('Time', 'support_hub'), 'log_extension' => __('Extension', 'support_hub'), 'log_error_level' => __('Error Level', 'support_hub'), 'log_subject' => __('Subject', 'support_hub'), 'log_data' => __('Data', 'support_hub'))); $latest_logs = shub_get_multiple('shub_log', isset($_REQUEST['search']) && is_array($_REQUEST['search']) ? $_REQUEST['search'] : array(), 'shub_log_id', 'shub_log_id DESC LIMIT 1000'); $myListTable->set_data($latest_logs); $myListTable->prepare_items(); ?> <div class="wrap"> <h2> <?php _e('Log Settings', 'support_hub'); ?> </h2> <form action="" method="post"> <input type="hidden" name="_process" value="save_log_settings"> <?php wp_nonce_field('save-log-settings'); ?> <table class="form-table"> <tbody> <tr> <th class="width1"> <?php _e('Enable Logging', 'support_hub'); ?>