From 8a75458869a2833b150e21adb5b56e006c210432 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Fri, 4 Sep 2015 23:45:13 +1000 Subject: [PATCH 01/23] bbpress fixes and re-sending failed messages --- assets/css/social.css | 6 +++ assets/js/social.js | 50 ++++++++++++++++--- classes/class-support-hub-message.php | 38 ++++++++++++-- classes/class-support-hub-outbox.php | 6 +-- classes/class-support-hub.php | 36 +++++++++++++ .../bbpress/class.shub_bbpress_message.php | 1 + .../envato/class.shub_envato_account.php | 1 + extensions/envato/class.shub_envato_user.php | 1 - 8 files changed, 124 insertions(+), 15 deletions(-) diff --git a/assets/css/social.css b/assets/css/social.css index 6e81cc5..b345a07 100644 --- a/assets/css/social.css +++ b/assets/css/social.css @@ -433,4 +433,10 @@ span.buyer_badge, padding: 4px; overflow: hidden; margin-bottom: 5px; +} +.shub_has_message_text .shub_hide_when_no_message{ + display: inherit !important; +} +.shub_hide_when_no_message{ + display: none !important; } \ No newline at end of file diff --git a/assets/js/social.js b/assets/js/social.js index e1089c6..9f0658c 100644 --- a/assets/js/social.js +++ b/assets/js/social.js @@ -62,16 +62,52 @@ ucm.social = { }); return false; }).delegate('.shub_message_reply textarea','keyup',function(){ - var a = this; - if (!jQuery(a).prop('scrollTop')) { + var a = jQuery(this); + if (!a.prop('scrollTop')) { do { - var b = jQuery(a).prop('scrollHeight'); - var h = jQuery(a).height(); - jQuery(a).height(h - 5); + var b = a.prop('scrollHeight'); + var h = a.height(); + a.height(h - 5); } - while (b && (b != jQuery(a).prop('scrollHeight'))); + while (b && (b != a.prop('scrollHeight'))); } - jQuery(a).height(jQuery(a).prop('scrollHeight') + 10); + // show the reply buttons and actions if we have content. + var txt = a.val(); + var $replybox = a.parents('.shub_message_reply').first(); + if(txt.length > 0){ + $replybox.addClass('shub_has_message_text'); + }else{ + $replybox.removeClass('shub_has_message_text'); + } + + a.height(a.prop('scrollHeight') + 10); + }).delegate('.shub_message_action_button','click',function(){ + var pt = jQuery(this).parents('.shub_message_actions').first(); + var post_data = { + action: 'support_hub_message-actions', + wp_nonce: support_hub.wp_nonce, + form_auth_key: ucm.form_auth_key + }; + var button_post = jQuery(this).data('post'); + for(var i in button_post){ + if(button_post.hasOwnProperty(i)){ + post_data[i] = button_post[i]; + } + } + jQuery.ajax({ + url: ajaxurl, + method: 'POST', + data: post_data, + dataType: 'json', + success: function(r){ + if(r && typeof r.message != 'undefined' && r.message.length > 0){ + pt.append("
" + r.message + "
"); + }else{ + pt.append("
Unknown error, please check logs. " + r+ "
"); + } + } + }); + return false; }).delegate('.shub_send_message_reply_button','click',function(){ // send a message! var pt = jQuery(this).parents('.shub_message_reply_box').first(); diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index fc40855..0fc7d93 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -301,7 +301,7 @@ public function get_comments($message_data = false) { return strtotime($a['created_at']) > strtotime($b['created_at']); }); }else{ - $messages = shub_get_multiple('shub_message_comment',array('shub_message_id'=>$this->shub_message_id),'shub_message_comment_id'); //@json_decode($this->get('comments'),true); + $messages = shub_get_multiple('shub_message_comment',array('shub_message_id'=>$this->shub_message_id),'shub_message_comment_id','shub_message_comment_id'); //@json_decode($this->get('comments'),true); } return $messages; } @@ -431,6 +431,7 @@ public function output_message_list( $allow_reply = true ){ // is this a queued-to-send message? $extra_class = ''; $comment_status = ''; + $message_error = false; if(!empty($comment['shub_outbox_id'])){ $shub_outbox = new SupportHubOutbox($comment['shub_outbox_id']); if($shub_outbox->get('shub_outbox_id') != $comment['shub_outbox_id']){ @@ -446,6 +447,7 @@ public function output_message_list( $allow_reply = true ){ case _SHUB_OUTBOX_STATUS_FAILED: $extra_class .= ' outbox_failed'; $comment_status = 'Failed to send message! Please check logs.'; + $message_error = true; break; } } @@ -506,6 +508,16 @@ public function output_message_list( $allow_reply = true ){
+ + + +
$this->get('shub_account_id'), 'message-id' => $message_id, 'network' => $this->network, - ))); ?>" class="btn button shub_send_message_reply_button"> - + ))); ?>" class="btn button shub_send_message_reply_button shub_hide_when_no_message"> + -
+
+
+ + +
+
+ + +
+
+ + +
diff --git a/classes/class-support-hub-outbox.php b/classes/class-support-hub-outbox.php index 2b5beda..d675779 100644 --- a/classes/class-support-hub-outbox.php +++ b/classes/class-support-hub-outbox.php @@ -122,11 +122,11 @@ public function delete(){ } } - public function send_queued(){ + public function send_queued($force=false){ if($this->shub_outbox_id){ // check the status of it. // todo - find any ones that are stuck in 'SENDING' status for too long and send those as well. - if($this->get('shub_status') == _SHUB_OUTBOX_STATUS_QUEUED){ + if($force || $this->get('shub_status') == _SHUB_OUTBOX_STATUS_QUEUED){ $managers = SupportHub::getInstance()->message_managers; if(!empty($this->shub_extension) && isset($managers[$this->shub_extension]) && $managers[$this->shub_extension]->is_enabled()){ // find the message manager responsible for this message and fire off the reply. @@ -137,7 +137,7 @@ public function send_queued(){ $this->update('shub_status', _SHUB_OUTBOX_STATUS_SENDING); // sweet! we're here, send the reply. ob_start(); - $status = $message->send_queued_comment_reply($this->shub_message_comment_id,$this); + $status = $message->send_queued_comment_reply($this->shub_message_comment_id, $this, true); $errors = ob_get_clean(); if($status){ // success! it worked! flag it as sent. diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index f892520..91a75c1 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -50,6 +50,8 @@ public function __construct( $file ) { add_action( 'wp_ajax_support_hub_fb_url_info' , array( $this, 'admin_ajax' ) ); add_action( 'wp_ajax_support_hub_request_extra_details' , array( $this, 'admin_ajax' ) ); add_action( 'wp_ajax_support_hub_queue-watch' , array( $this, 'admin_ajax' ) ); + add_action( 'wp_ajax_support_hub_resend_outbox_message' , array( $this, 'admin_ajax' ) ); + add_action( 'wp_ajax_support_hub_delete_outbox_message' , array( $this, 'admin_ajax' ) ); add_filter('set-screen-option', array( $this, 'set_screen_options' ), 10, 3); @@ -265,6 +267,40 @@ public function admin_ajax(){ echo json_encode( $return ); break; + case 'resend_outbox_message': + $shub_outbox_id = !empty($_REQUEST['shub_outbox_id']) ? (int)$_REQUEST['shub_outbox_id'] : false; + if($shub_outbox_id){ + if (!headers_sent())header('Content-type: text/javascript'); + $pending = new SupportHubOutbox($shub_outbox_id); + if($pending->get('shub_outbox_id') == $shub_outbox_id) { + ob_start(); + echo $pending->send_queued(true); + $return = array( + 'message' => 'Message Resent. Please refresh the page. ' . ob_get_clean() + ); + echo json_encode($return); + } + exit; + + } + break; + case 'delete_outbox_message': + $shub_outbox_id = !empty($_REQUEST['shub_outbox_id']) ? (int)$_REQUEST['shub_outbox_id'] : false; + if($shub_outbox_id){ + if (!headers_sent())header('Content-type: text/javascript'); + // remove the comment from the database. + $pending = new SupportHubOutbox($shub_outbox_id); + if($pending->get('shub_outbox_id') == $shub_outbox_id) { + shub_delete_from_db('shub_message_comment', 'shub_message_comment_id', $pending->get('shub_message_comment_id')); + $pending->delete(); + $return = array( + 'message' => 'Deleted Successfully. Please re-load the page.' + ); + echo json_encode($return); + } + exit; + } + break; case 'request_extra_details': diff --git a/extensions/bbpress/class.shub_bbpress_message.php b/extensions/bbpress/class.shub_bbpress_message.php index 3b9d6ad..5a8c418 100644 --- a/extensions/bbpress/class.shub_bbpress_message.php +++ b/extensions/bbpress/class.shub_bbpress_message.php @@ -243,6 +243,7 @@ public function send_queued_comment_reply($bbpress_message_comment_id, $shub_out return true; }else{ echo "Failed to send comment, check debug log."; + print_r($api_result); return false; } /*if((int) $api_result > 0){ diff --git a/extensions/envato/class.shub_envato_account.php b/extensions/envato/class.shub_envato_account.php index 0d03a03..9606ec6 100644 --- a/extensions/envato/class.shub_envato_account.php +++ b/extensions/envato/class.shub_envato_account.php @@ -127,6 +127,7 @@ public function get_item($shub_item_id){ 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? diff --git a/extensions/envato/class.shub_envato_user.php b/extensions/envato/class.shub_envato_user.php index 4b2f6dd..5fb26f9 100644 --- a/extensions/envato/class.shub_envato_user.php +++ b/extensions/envato/class.shub_envato_user.php @@ -130,7 +130,6 @@ public function update_purchase_history(){ } $api_result_purchase_history = $api->api('v2/market/buyer/purchases', array(), false); -// echo 'her';print_r($api_result_purchase_history);exit; // 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. From ae31c126ce409ec44297bdbe2aa0814fe2679859 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 03:20:38 +1000 Subject: [PATCH 02/23] added basic ucm integration --- classes/class-support-hub-message.php | 9 +- classes/class-support-hub.php | 45 +- extensions/bbpress/bbpress_settings.php | 1 + extensions/envato/class.shub_envato.php | 65 +- .../envato/class.shub_envato_account.php | 9 + .../envato/class.shub_envato_message.php | 18 +- extensions/ucm/class.shub_ucm.php | 501 ++------------- extensions/ucm/class.shub_ucm_account.php | 237 +------ extensions/ucm/class.shub_ucm_item.php | 84 +++ extensions/ucm/class.shub_ucm_message.php | 603 ++++-------------- extensions/ucm/class.shub_ucm_product.php | 184 ------ extensions/ucm/init.ucm.php | 2 +- extensions/ucm/{ucm-logo.png => logo.png} | Bin extensions/ucm/shub_ucm.js | 2 +- extensions/ucm/ucm_settings.php | 85 +-- 15 files changed, 405 insertions(+), 1440 deletions(-) create mode 100644 extensions/ucm/class.shub_ucm_item.php delete mode 100644 extensions/ucm/class.shub_ucm_product.php rename extensions/ucm/{ucm-logo.png => logo.png} (100%) diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index 0fc7d93..9e71958 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -180,7 +180,7 @@ public function parse_links($content = false){ private function _update_comments($data, $existing_messages){ if(is_array($data)){ - $last_message_user_name = false; + $last_message_user_id = false; foreach($data as $message){ if($message['id']){ // does this id exist in the db already? @@ -214,7 +214,7 @@ private function _update_comments($data, $existing_messages){ 'message_text' => isset($message['content']) ? $message['content'] : '', 'shub_user_id' => $shub_user_id, )); - $last_message_user_name = isset($message['username']) ? $message['username'] : false; + $last_message_user_id = $shub_user_id; if(isset($existing_messages[$shub_message_comment_id])){ unset($existing_messages[$shub_message_comment_id]); } @@ -223,9 +223,8 @@ private function _update_comments($data, $existing_messages){ }*/ } } - if($last_message_user_name){ - $account_user_data = $this->account->get('account_data'); - if($account_user_data && isset($account_user_data['user']) && $last_message_user_name == $account_user_data['user']['username']){ + 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); diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index 91a75c1..e7bd8a1 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -604,20 +604,15 @@ public function add_menu_item() { $message_count = $this->get_unread_count();; $menu_label = sprintf( __( 'Support Hub %s', 'support_hub' ), $message_count > 0 ? "" . (int)$message_count . "" : ''); - add_menu_page( __( 'Support Hub Inbox', 'support_hub' ), $menu_label, 'edit_pages', 'support_hub_main', array($this, 'show_dashboard'), 'dashicons-format-chat', "21.1" ); + add_menu_page( __( 'Support Hub Inbox', 'support_hub' ), $menu_label, 'edit_pages', 'support_hub_main', array($this, 'show_inbox'), 'dashicons-format-chat', "21.1" ); - $page = add_submenu_page('support_hub_main', __( 'Dashboard', 'support_hub' ), __('Dashboard' ,'support_hub'), 'edit_pages', 'support_hub_main' , array($this, 'show_dashboard')); - add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); - // hack to rmeove default submenu $menu_label = sprintf( __( 'Inbox %s', 'support_hub' ), $message_count > 0 ? "" . number_format_i18n($message_count) . "" : '' ); - $page = add_submenu_page('support_hub_main', __( 'Support Hub Inbox', 'support_hub' ), $menu_label, 'edit_pages', 'support_hub_inbox' , array($this, 'show_inbox')); + $page = add_submenu_page('support_hub_main', __( 'Support Hub Inbox', 'support_hub' ), $menu_label, 'edit_pages', 'support_hub_main' , array($this, 'show_inbox')); add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); add_action("load-$page", array( $this, 'screen_options' )); - - //$page = add_submenu_page('support_hub_main', __( 'Compose', 'support_hub' ), __('Compose' ,'support_hub'), 'edit_pages', 'support_hub_compose' , array($this, 'show_compose')); //add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); @@ -631,6 +626,10 @@ public function add_menu_item() { $page = add_submenu_page('support_hub_main', __( 'Outbox', 'support_hub' ), $menu_label, 'edit_pages', 'support_hub_outbox' , array($this, 'show_outbox')); add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); + + $page = add_submenu_page('support_hub_main', __( 'Dashboard', 'support_hub' ), __('Dashboard' ,'support_hub'), 'edit_pages', 'support_hub_dashboard' , array($this, 'show_dashboard')); + add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); + $page = add_submenu_page('support_hub_main', __( 'Settings', 'support_hub' ), __( 'Settings', 'support_hub' ), 'edit_pages', 'support_hub_settings' , array($this, 'show_settings')); add_action( 'admin_print_styles-'.$page, array( $this, 'inbox_assets' ) ); @@ -891,7 +890,7 @@ public function message_user_summary($user_hints, $current_extension, $message_o // example: if two users submit $extra_shub_user_id = $this_extra->get('shub_user_id'); if(!empty($extra_shub_user_id) && !in_array($extra_shub_user_id,$user_hints['shub_user_id'])){ - echo "


ADDING $extra_shub_user_id TO THE LIST IN DATA!! PLEASE REPORT THIS ERROR SO DTBAKER CAN INVESTIGATE


"; + echo "


Error: Displaying extra data for the user '$extra_shub_user_id' when that user ID isn't already in the list. Please report this bug to dtbaker.


"; // moved the user_hints loop to the top to build up the user id list first. $user_hints['shub_user_id'][] = $extra_shub_user_id; } @@ -900,21 +899,23 @@ public function message_user_summary($user_hints, $current_extension, $message_o } } foreach($extra_datas as $extra_data){ - //echo $extra_data->get('shub_extra_data_id') . ' '; - ?> -
- get('shub_extra_id')]->get('extra_name'));?>: - get('shub_extra_id')]->get('field_type')){ - case 'encrypted': - echo '(encrypted)'; - break; - default: - echo shub_forum_text($extra_data->get('extra_value'),false); - } + if(isset($extras[$extra_data->get('shub_extra_id')])) { ?> -
- + get('shub_extra_id')]->get('extra_name')); ?> + : + get('shub_extra_id')]->get('field_type')) { + case 'encrypted': + echo '(encrypted)'; + break; + default: + echo shub_forum_text($extra_data->get('extra_value'), false); + } + ?> +
+ message_managers['bbpress']; if($current_account !== false){ $shub_bbpress_account = new shub_bbpress_account($current_account); + if($shub_envato_account->get('shub_extension') != 'bbpress')die('Wrong extension:' .$shub_envato_account->get('shub_extension')); if(isset($_GET['manualrefresh'])){ $network_key = isset( $_REQUEST['network_key'] ) ? (int) $_REQUEST['network_key'] : 0; diff --git a/extensions/envato/class.shub_envato.php b/extensions/envato/class.shub_envato.php index bd65943..7e88e2b 100644 --- a/extensions/envato/class.shub_envato.php +++ b/extensions/envato/class.shub_envato.php @@ -588,8 +588,8 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() if (!$api) { break; } - $result = $api->api('v1/market/private/user/verify-purchase:' . $purchase_code . '.json'); -// $result = $api->api( 'v2/market/author/sale?code='.$purchase_code ); +// $result = $api->api('v1/market/private/user/verify-purchase:' . $purchase_code . '.json'); + $result = $api->api( 'v2/market/author/sale?code='.$purchase_code ); /* { "amount": "12.60", @@ -627,10 +627,10 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() "support_amount": "" } }*/ - if ($result && !empty($result['verify-purchase']) && !empty($result['verify-purchase']['item_id'])) { + if ($result && !empty($result['item']) && !empty($result['item']['id'])) { // valid purchase code. // what is the username attached to this purchase result? - $envato_username = $result['verify-purchase']['buyer']; + $envato_username = $result['buyer']; // find this user in our system. $shub_user = new SupportHubUser_Envato(); $shub_user->load_by_meta('envato_username', strtolower($envato_username)); @@ -647,7 +647,7 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() // 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'] == $result['verify-purchase']['item_id']) { + if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $result['item']['id']) { $exists = $existing_product['shub_product_id']; } } @@ -658,24 +658,21 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() $newproduct->load($exists); } if (!$newproduct->get('product_name')) { - $newproduct->update('product_name', $result['verify-purchase']['item_name']); + $newproduct->update('product_name', $result['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'] = $result['verify-purchase']['item_id']; + $existing_product_data['envato_item_id'] = $result['item']['id']; } if (empty($existing_product_data['envato_item_data'])) { // get these item details from api - $item_data = $api->api('v2/market/catalog/item?id=' . $result['verify-purchase']['item_id']); - if (!empty($item_data) && $item_data['id'] == $result['verify-purchase']['item_id']) { - $existing_product_data['envato_item_data'] = $item_data; - if (empty($existing_product_data['image'])) { - $existing_product_data['image'] = $item_data['thumbnail_url']; - } - if (empty($existing_product_data['url'])) { - $existing_product_data['url'] = $item_data['url']; - } + $existing_product_data['envato_item_data'] = $result['item']; + if (empty($existing_product_data['image'])) { + $existing_product_data['image'] = $result['item']['thumbnail_url']; + } + if (empty($existing_product_data['url'])) { + $existing_product_data['url'] = $result['item']['url']; } } $newproduct->update('product_data', $existing_product_data); @@ -691,7 +688,7 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() $possible_purchases = shub_get_multiple('shub_envato_purchase', array( 'shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), - 'purchase_time' => strtotime($result['verify-purchase']['created_at']), + 'purchase_time' => strtotime($result['sold_at']), )); foreach ($possible_purchases as $possible_purchase) { if (empty($possible_purchases['purchase_code'])) { @@ -713,13 +710,13 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() } if (!$shub_envato_purchase_id) { // add new one - $raw_purchase_data = array_merge(array('verify-purchase' => $result['verify-purchase']), is_array($api_raw_data) ? $api_raw_data : array()); + $raw_purchase_data = array_merge(array('author/sale' => $result), is_array($api_raw_data) ? $api_raw_data : array()); $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array( 'shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'envato_user_id' => 0, - 'api_type' => 'verify-purchase', - 'purchase_time' => strtotime($result['verify-purchase']['created_at']), + 'api_type' => 'author/sale', + 'purchase_time' => strtotime($result['sold_at']), 'purchase_code' => $purchase_code, 'purchase_data' => json_encode($raw_purchase_data), )); @@ -727,13 +724,13 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() if ($shub_envato_purchase_id) { // support expiry time is 6 months from the purchase date, or as specified by the api result. - if(strtotime($result['verify-purchase']['created_at']) < strtotime("2015-09-01")){ + if(strtotime($result['sold_at']) < strtotime("2015-09-01")){ $support_expiry_time = strtotime("+6 months", strtotime("2015-09-01")); }else{ - $support_expiry_time = strtotime("+6 months", strtotime($result['verify-purchase']['created_at'])); + $support_expiry_time = strtotime("+6 months", strtotime($result['sold_at'])); } - if (!empty($result['verify-purchase']['supported_until'])) { - $support_expiry_time = strtotime($result['verify-purchase']['supported_until']); + if (!empty($result['supported_until'])) { + $support_expiry_time = strtotime($result['supported_until']); } $existing_support = shub_get_single('shub_envato_support', array( @@ -746,14 +743,14 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() 'shub_user_id' => $shub_user->get('shub_user_id'), )); } - if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == strtotime($result['verify-purchase']['created_at'])) { + if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == strtotime($result['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' => 'verify-purchase', - 'support_data' => json_encode($result['verify-purchase']), + 'api_type' => 'author/sale', + 'support_data' => json_encode($result), )); } } else { @@ -762,10 +759,10 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() 'shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, - 'api_type' => 'verify-purchase', - 'start_time' => strtotime($result['verify-purchase']['created_at']), + 'api_type' => 'author/sale', + 'start_time' => strtotime($result['sold_at']), 'end_time' => $support_expiry_time, - 'support_data' => json_encode($result['verify-purchase']), + 'support_data' => json_encode($result), )); } } @@ -796,12 +793,12 @@ public function extra_validate_data($status, $extra, $value, $network, $account_ }else{ $result = false; } - if($result && !empty($result['verify-purchase'])){ + if($result && !empty($result['item'])){ $status['success'] = true; $status['data'] = $possible_purchase_code; - $result['verify-purchase']['time'] = time(); - $result['verify-purchase']['valid_purchase_code'] = true; - $status['extra_data'] = $result['verify-purchase']; + $result['time'] = time(); + $result['valid_purchase_code'] = true; + $status['extra_data'] = $result; }else{ $status['success'] = false; $status['message'] = 'Invalid purchase code, please try again.'; diff --git a/extensions/envato/class.shub_envato_account.php b/extensions/envato/class.shub_envato_account.php index 9606ec6..ab28322 100644 --- a/extensions/envato/class.shub_envato_account.php +++ b/extensions/envato/class.shub_envato_account.php @@ -293,6 +293,15 @@ public function run_cron( $debug = false ){ $this->update_author_sale_history( $debug, false ); + // confirm the token every so often. + $last_token_confirmation = $this->get('last_token_refresh'); + if(!$last_token_confirmation || $last_token_confirmation < time() - 86400){ + if(!$this->confirm_token()){ + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'envato', 'Cookie Error: Failed to confirm Envato account cookie. The cookie must have expired. Please put a new cookie into the settings area.'); + } + $this->save_account_data(array('last_token_refresh',time())); + } + } } diff --git a/extensions/envato/class.shub_envato_message.php b/extensions/envato/class.shub_envato_message.php index 7e5b68d..6cc1598 100644 --- a/extensions/envato/class.shub_envato_message.php +++ b/extensions/envato/class.shub_envato_message.php @@ -51,15 +51,19 @@ public function load_by_network_key($network_key, $message_data, $type, $debug = $first_comment = current($comments); if(!empty($first_comment['username'])) { $comment_user = new SupportHubUser_Envato(); - $res = $comment_user->load_by( 'user_username', $first_comment['username']); + $res = $comment_user->load_by( 'envato_username', $first_comment['username']); if(!$res){ - $comment_user -> create_new(); - if(!$comment_user->get('user_username'))$comment_user -> update('user_username', $first_comment['username']); - $comment_user -> update_user_data(array( - 'image' => $first_comment['profile_image_url'], - 'envato' => $first_comment, - )); + $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); diff --git a/extensions/ucm/class.shub_ucm.php b/extensions/ucm/class.shub_ucm.php index fe4a60c..36b8e22 100644 --- a/extensions/ucm/class.shub_ucm.php +++ b/extensions/ucm/class.shub_ucm.php @@ -2,44 +2,10 @@ class shub_ucm extends SupportHub_extension { - public function init(){ - if(isset($_GET[_support_hub_ucm_LINK_REWRITE_PREFIX]) && strlen($_GET[_support_hub_ucm_LINK_REWRITE_PREFIX]) > 0){ - // check hash - $bits = explode(':',$_GET[_support_hub_ucm_LINK_REWRITE_PREFIX]); - if(defined('AUTH_KEY') && isset($bits[1])){ - $shub_ucm_message_link_id = (int)$bits[0]; - if($shub_ucm_message_link_id > 0){ - $correct_hash = substr(md5(AUTH_KEY.' ucm link '.$shub_ucm_message_link_id),1,5); - if($correct_hash == $bits[1]){ - // link worked! log a visit and redirect. - $link = shub_get_single('shub_ucm_message_link','shub_ucm_message_link_id',$shub_ucm_message_link_id); - if($link){ - if(!preg_match('#^http#',$link['link'])){ - $link['link'] = 'http://'.trim($link['link']); - } - shub_update_insert('shub_ucm_message_link_click_id',false,'shub_ucm_message_link_click',array( - 'shub_ucm_message_link_id' => $shub_ucm_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 init_menu(){ - - } - - public function get_friendly_icon(){ - return ''; - } + public function init(){ + parent::init(); +// add_filter('supporthub_message_user_sidebar',array($this,'filter_message_user_sidebar'),10,2); + } public function page_assets($from_master=false){ if(!$from_master)SupportHub::getInstance()->inbox_assets(); @@ -56,11 +22,6 @@ public function settings_page(){ } - private function reset() { - $this->accounts = array(); - } - - public function compose_to(){ $accounts = $this->get_accounts(); if(!count($accounts)){ @@ -125,263 +86,19 @@ public function compose_type($defaults){ } - public function get_accounts() { - $this->accounts = shub_get_multiple( 'shub_ucm', array(), 'shub_ucm_id' ); - return $this->accounts; - } - - - public function get_url($url, $post_data = false){ - // get feed from fb: - - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); - if($post_data){ - curl_setopt($ch, CURLOPT_POST,true); - curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); - } - $data = curl_exec($ch); - $feed = @json_decode($data,true); - //print_r($feed); - return $feed; - - } - - public static function format_person($data,$ucm_account){ - $return = ''; - if($data->get_link()){ - $return .= ''; - } - $return .= htmlspecialchars($data->get_name()); - if($data->get_link()){ - $return .= ''; - } - return $return; - } - - public function load_all_messages($search=array(),$order=array(),$limit_batch=0){ - $this->search_params = $search; - $this->search_order = $order; - $this->search_limit = $limit_batch; - - $sql = "SELECT m.*, m.last_active AS `message_time`, mr.read_time FROM `"._support_hub_DB_PREFIX."shub_ucm_message` m "; - $sql .= " LEFT JOIN `"._support_hub_DB_PREFIX."shub_ucm_message_read` mr ON ( m.shub_ucm_message_id = mr.shub_ucm_message_id AND mr.user_id = ".get_current_user_id()." )"; - $sql .= " LEFT JOIN `"._support_hub_DB_PREFIX."shub_ucm_product` ei ON ( m.shub_ucm_product_id = ei.shub_ucm_product_id )"; - $sql .= " WHERE 1 "; - if(isset($search['shub_status']) && $search['shub_status'] !== false){ - $sql .= " AND m.`shub_status` = ".(int)$search['shub_status']; - } - if(isset($search['shub_ucm_product_id']) && $search['shub_ucm_product_id'] !== false){ - $sql .= " AND m.`shub_ucm_product_id` = ".(int)$search['shub_ucm_product_id']; - } - if(isset($search['shub_product_id']) && (int)$search['shub_product_id']){ - $sql .= " AND (m.`shub_product_id` = ".(int)$search['shub_product_id']; - $sql .= " OR ei.`shub_product_id` = ".(int)$search['shub_product_id'] . " )"; - } - if(isset($search['shub_message_id']) && $search['shub_message_id'] !== false){ - $sql .= " AND m.`shub_message_id` = ".(int)$search['shub_message_id']; - } - if(isset($search['shub_ucm_id']) && $search['shub_ucm_id'] !== false){ - $sql .= " AND m.`shub_ucm_id` = ".(int)$search['shub_ucm_id']; - } - if(isset($search['generic']) && !empty($search['generic'])){ - // todo: search product comments too.. not just title (first comment) and summary (last comment) - $sql .= " AND (`title` LIKE '%".esc_sql($search['generic'])."%'"; - $sql .= " OR `summary` LIKE '%".esc_sql($search['generic'])."%' )"; - } - - if(empty($order)){ - $sql .= " ORDER BY `last_active` ASC "; - }else{ - switch($order['orderby']){ - case 'shub_column_time': - $sql .= " ORDER BY `last_active` "; - $sql .= $order['order'] == 'asc' ? 'ASC' : 'DESC'; - break; - } + public static function format_person($data,$ucm_account){ + $return = ''; + if($data && isset($data['username'])){ + $return .= ''; } - if($limit_batch){ - $sql .= " LIMIT ".$this->limit_start.', '.$limit_batch; - $this->limit_start += $limit_batch; - } - //$this->all_messages = query($sql); - global $wpdb; - $this->all_messages = $wpdb->get_results($sql, ARRAY_A); - return $this->all_messages; - } - - - - // used in our Wp "outbox" view showing combined messages. - public function get_message_details($shub_message_id){ - if(!$shub_message_id)return array(); - $messages = $this->load_all_messages(array('shub_message_id'=>$shub_message_id)); - // we want data for our colum outputs in the WP table: - /*'shub_column_time' => __( 'Date/Time', 'support_hub' ), - 'shub_column_account' => __( 'Social Accounts', 'support_hub' ), - 'shub_column_summary' => __( 'Summary', 'support_hub' ), - 'shub_column_links' => __( 'Link Clicks', 'support_hub' ), - 'shub_column_stats' => __( 'Stats', 'support_hub' ), - 'shub_column_action' => __( 'Action', 'support_hub' ),*/ - $data = array( - 'shub_column_account' => '', - 'shub_column_summary' => '', - 'shub_column_links' => '', - ); - $link_clicks = 0; - foreach($messages as $message){ - $ucm_message = new shub_ucm_message(false, false, $message['shub_ucm_message_id']); - $data['message'] = $ucm_message; - $data['shub_column_account'] .= '
'.htmlspecialchars( $ucm_message->get('ucm_product') ? $ucm_message->get('ucm_product')->get( 'product_name' ) : 'Share' ) .'
'; - $data['shub_column_summary'] .= '
'.htmlspecialchars( $ucm_message->get_summary() ) .'
'; - // how many link clicks does this one have? - $sql = "SELECT count(*) AS `link_clicks` FROM "; - $sql .= " `"._support_hub_DB_PREFIX."shub_ucm_message` m "; - $sql .= " LEFT JOIN `"._support_hub_DB_PREFIX."shub_ucm_message_link` ml USING (shub_ucm_message_id) "; - $sql .= " LEFT JOIN `"._support_hub_DB_PREFIX."shub_ucm_message_link_click` lc USING (shub_ucm_message_link_id) "; - $sql .= " WHERE 1 "; - $sql .= " AND m.shub_ucm_message_id = ".(int)$message['shub_ucm_message_id']; - $sql .= " AND lc.shub_ucm_message_link_id IS NOT NULL "; - $sql .= " AND lc.user_agent NOT LIKE '%Google%' "; - $sql .= " AND lc.user_agent NOT LIKE '%Yahoo%' "; - $sql .= " AND lc.user_agent NOT LIKE '%ucmexternalhit%' "; - $sql .= " AND lc.user_agent NOT LIKE '%Meta%' "; - $res = shub_qa1($sql); - $link_clicks = $res && $res['link_clicks'] ? $res['link_clicks'] : 0; - $data['shub_column_links'] .= '
'. $link_clicks .'
'; - } - if(count($messages) && $link_clicks > 0){ - //$data['shub_column_links'] = '
'. $link_clicks .'
'; - } - return $data; - - } - - - public function get_unread_count($search=array()){ - if(!get_current_user_id())return 0; - $sql = "SELECT count(*) AS `unread` FROM `"._support_hub_DB_PREFIX."shub_ucm_message` m "; - $sql .= " WHERE 1 "; - $sql .= " AND m.shub_ucm_message_id NOT IN (SELECT mr.shub_ucm_message_id FROM `"._support_hub_DB_PREFIX."shub_ucm_message_read` mr WHERE mr.user_id = '".(int)get_current_user_id()."' AND mr.shub_ucm_message_id = m.shub_ucm_message_id)"; - $sql .= " AND m.`shub_status` = "._shub_MESSAGE_STATUS_UNANSWERED; - if(isset($search['shub_ucm_product_id']) && $search['shub_ucm_product_id'] !== false){ - $sql .= " AND m.`shub_ucm_product_id` = ".(int)$search['shub_ucm_product_id']; - } - if(isset($search['shub_ucm_id']) && $search['shub_ucm_id'] !== false){ - $sql .= " AND m.`shub_ucm_id` = ".(int)$search['shub_ucm_id']; - } - $res = shub_qa1($sql); - return $res ? $res['unread'] : 0; - } - - - public function output_row($message, $foo=array()){ - $ucm_message = new shub_ucm_message(false, false, $message['shub_ucm_message_id']); - $messages = $ucm_message->get_comments(); - $return = array(); - - ob_start(); - ?> - - get('ucm_account') ? $ucm_message->get('ucm_account')->get( 'ucm_name' ) : 'product' ); ?>
- get_type_pretty() ); ?> - get_product_id(); - if($shub_product_id) { - $shub_product = new SupportHubProduct(); - $shub_product->load($shub_product_id); - $product_data = $shub_product->get('product_data'); - if(!empty($product_data['image'])){ - ?> - - - - get('product_name') ); ?> - get('product_name') ); ?> get_from(); - ?> -
-
- $from_shub_user){ - ?> -
- get_image()){ ?> - - get_name()); ?> -
- -
- get_image()){ ?> - - get_name()); - echo ''; - if ( count( $from ) > 1 ) { - echo '+' . ( count( $from ) - 1 ); - } - echo ''; + if($data && isset($data['username'])){ + $return .= htmlspecialchars($data['username']); } - ?> -
- - - 0 ? '('.count( $messages ).')' : ''; ?> - -
so we can easily see) - $title = strip_tags($ucm_message->get( 'title' )); - $summary = strip_tags($ucm_message->get( 'summary' )); - echo htmlspecialchars( strlen( $title ) > 80 ? substr( $title, 0, 80 ) . '...' : $title ) . ($summary!=$title ? '
' .htmlspecialchars( strlen( $summary ) > 80 ? substr( $summary, 0, 80 ) . '...' : $summary ) : ''); - ?> -
- - - get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED){ ?> - - - - - '; + } + return $return; + } public function init_js(){ ?> @@ -507,31 +224,16 @@ public function handle_process($process, $options = array()){ } } return $message_count; - break; - case 'save_ucm': - $shub_ucm_id = isset($_REQUEST['shub_ucm_id']) ? (int)$_REQUEST['shub_ucm_id'] : 0; - if(check_admin_referer( 'save-ucm'.$shub_ucm_id )) { - $ucm = new shub_ucm_account( $shub_ucm_id ); - if ( isset( $_POST['butt_delete'] ) ) { - $ucm->delete(); - $redirect = 'admin.php?page=support_hub_settings&tab=ucm'; - } else { - $ucm->save_data( $_POST ); - $shub_ucm_id = $ucm->get( 'shub_ucm_id' ); - if ( isset( $_POST['butt_save_reconnect'] ) ) { - $redirect = $ucm->link_connect(); - } else { - $redirect = $ucm->link_edit(); - } - } - header( "Location: $redirect" ); - exit; - } - break; } + parent::handle_process($process, $options); } + + public function get_account($shub_account_id){ + return new shub_ucm_account($shub_account_id); + } + public function get_message($ucm_account = false, $ucm_product = false, $shub_ucm_message_id = false){ return new shub_ucm_message($ucm_account, $ucm_product, $shub_ucm_message_id); } @@ -540,9 +242,9 @@ public function run_cron( $debug = false ){ if($debug)echo "Starting ucm Cron Job \n"; $accounts = $this->get_accounts(); foreach($accounts as $account){ - $shub_ucm_account = new shub_ucm_account( $account['shub_ucm_id'] ); + $shub_ucm_account = new shub_ucm_account( $account['shub_account_id'] ); $shub_ucm_account->run_cron($debug); - $products = $shub_ucm_account->get('products'); + $products = $shub_ucm_account->get('items'); /* @var $products shub_ucm_product[] */ foreach($products as $product){ $product->run_cron($debug); @@ -555,30 +257,30 @@ public function find_other_user_details($user_hints, $current_extension, $messag $details = array( 'messages' => array(), 'user' => array(), + 'user_ids' => array(), ); // find other ucm messages by this user. - if(!empty($user_hints['shub_user_id'])) { - if (!is_array($user_hints['shub_user_id'])) $user_hints['shub_user_id'] = array($user_hints['shub_user_id']); - foreach ($user_hints['shub_user_id'] as $shub_user_id) { - if ((int)$shub_user_id > 0) { - $messages = shub_get_multiple('shub_ucm_message', array( - 'shub_user_id' => $shub_user_id - ), 'shub_ucm_message_id', '`last_active` DESC'); - if (is_array($messages)) { - foreach ($messages as $message) { - if ($current_extension == 'ucm' && $message_object->get('shub_ucm_message_id') == $message['shub_ucm_message_id']) continue; - if (!isset($details['messages']['ucm' . $message['shub_ucm_message_id']])) { - // $other_message = new shub_ucm_message(); - // $other_message->load($message['shub_ucm_message_id']); - $details['messages']['ucm' . $message['shub_ucm_message_id']] = array( - 'summary' => $message['title'], - 'time' => $message['last_active'], - 'network' => 'ucm', - 'message_id' => $message['shub_ucm_message_id'], - 'network_message_comment_id' => 0, -// 'message_status' => $other_message->get('shub_status'), - ); + if(!empty($user_hints['shub_user_id'])){ + if(!is_array($user_hints['shub_user_id']))$user_hints['shub_user_id'] = array($user_hints['shub_user_id']); + foreach($user_hints['shub_user_id'] as $shub_user_id) { + if((int)$shub_user_id > 0) { + $details['user_ids'][$shub_user_id] = $shub_user_id; + $shub_user = new SupportHubUser_ucm($shub_user_id); + $envato_username = $shub_user->get_meta('envato_username'); + if ($envato_username) { + foreach ($envato_username as $envato_username1) { + if (!empty($envato_username1)) { + // todo - display multiple. + $details['user']['username'] = $envato_username1; + $details['user']['url'] = 'http://themeforest.net/user/' . $envato_username1; + // see if we can find any other matching user accounts for this username + $other_users = new SupportHubUser_ucm(); + $other_users->load_by('user_username', $envato_username1); + 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. + $details['user_ids'][$other_users->get('shub_user_id')] = $other_users->get('shub_user_id'); + } } } } @@ -589,119 +291,4 @@ public function find_other_user_details($user_hints, $current_extension, $messag return $details; } - public function get_install_sql() { - - global $wpdb; - - $sql = <<< EOT - - - -CREATE TABLE {$wpdb->prefix}shub_ucm ( - shub_ucm_id int(11) NOT NULL AUTO_INCREMENT, - shub_user_id int(11) NOT NULL DEFAULT '0', - ucm_name varchar(50) NOT NULL, - last_checked int(11) NOT NULL DEFAULT '0', - import_stream int(11) NOT NULL DEFAULT '0', - post_stream int(11) NOT NULL DEFAULT '0', - ucm_data longtext NOT NULL, - ucm_api_url varchar(255) NOT NULL, - ucm_username varchar(255) NOT NULL, - ucm_api_key varchar(255) NOT NULL, - PRIMARY KEY shub_ucm_id (shub_ucm_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - - -CREATE TABLE {$wpdb->prefix}shub_ucm_message ( - shub_ucm_message_id int(11) NOT NULL AUTO_INCREMENT, - shub_ucm_id int(11) NOT NULL, - shub_message_id int(11) NOT NULL DEFAULT '0', - shub_ucm_product_id int(11) NOT NULL, - shub_product_id int(11) NOT NULL DEFAULT '-1', - ucm_ticket_id varchar(255) NOT NULL, - summary text NOT NULL, - title text NOT NULL, - last_active int(11) NOT NULL DEFAULT '0', - comments longtext NOT NULL, - type varchar(20) NOT NULL, - link varchar(255) NOT NULL, - data text NOT NULL, - status tinyint(1) NOT NULL DEFAULT '0', - user_id int(11) NOT NULL DEFAULT '0', - shub_user_id int(11) NOT NULL DEFAULT '0', - PRIMARY KEY shub_ucm_message_id (shub_ucm_message_id), - KEY shub_ucm_id (shub_ucm_id), - KEY shub_message_id (shub_message_id), - KEY shub_product_id (shub_product_id), - KEY shub_user_id (shub_user_id), - KEY last_active (last_active), - KEY shub_ucm_product_id (shub_ucm_product_id), - KEY ucm_ticket_id (ucm_ticket_id), - KEY status (status) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - - -CREATE TABLE {$wpdb->prefix}shub_ucm_message_read ( - shub_ucm_message_id int(11) NOT NULL, - read_time int(11) NOT NULL DEFAULT '0', - user_id int(11) NOT NULL DEFAULT '0', - PRIMARY KEY shub_ucm_message_id (shub_ucm_message_id,user_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - - -CREATE TABLE {$wpdb->prefix}shub_ucm_message_comment ( - shub_ucm_message_comment_id int(11) NOT NULL AUTO_INCREMENT, - shub_ucm_message_id int(11) NOT NULL, - ucm_ticket_message_id varchar(255) NOT NULL, - time int(11) NOT NULL, - message_from text NOT NULL, - message_to text NOT NULL, - message_text text NOT NULL, - data text NOT NULL, - user_id int(11) NOT NULL DEFAULT '0', - shub_user_id int(11) NOT NULL DEFAULT '0', - shub_outbox_id int(11) NOT NULL DEFAULT '0', - PRIMARY KEY shub_ucm_message_comment_id (shub_ucm_message_comment_id), - KEY shub_ucm_message_id (shub_ucm_message_id), - KEY shub_user_id (shub_user_id), - KEY ucm_ticket_message_id (ucm_ticket_message_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - - -CREATE TABLE {$wpdb->prefix}shub_ucm_message_link ( - shub_ucm_message_link_id int(11) NOT NULL AUTO_INCREMENT, - shub_ucm_message_id int(11) NOT NULL DEFAULT '0', - link varchar(255) NOT NULL, - PRIMARY KEY shub_ucm_message_link_id (shub_ucm_message_link_id), - KEY shub_ucm_message_id (shub_ucm_message_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - -CREATE TABLE {$wpdb->prefix}shub_ucm_message_link_click ( - shub_ucm_message_link_click_id int(11) NOT NULL AUTO_INCREMENT, - shub_ucm_message_link_id int(11) NOT NULL DEFAULT '0', - click_time int(11) NOT NULL, - ip_address varchar(20) NOT NULL, - user_agent varchar(100) NOT NULL, - url_referrer varchar(255) NOT NULL, - PRIMARY KEY shub_ucm_message_link_click_id (shub_ucm_message_link_click_id), - KEY shub_ucm_message_link_id (shub_ucm_message_link_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - -CREATE TABLE {$wpdb->prefix}shub_ucm_product ( - shub_ucm_product_id int(11) NOT NULL AUTO_INCREMENT, - shub_ucm_id int(11) NOT NULL, - shub_product_id int(11) NOT NULL DEFAULT '0', - product_name varchar(50) NOT NULL, - last_message int(11) NOT NULL DEFAULT '0', - last_checked int(11) NOT NULL, - product_id varchar(255) NOT NULL, - ucm_data text NOT NULL, - PRIMARY KEY shub_ucm_product_id (shub_ucm_product_id), - KEY shub_ucm_id (shub_ucm_id) -) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; - -EOT; - return $sql; - } - } diff --git a/extensions/ucm/class.shub_ucm_account.php b/extensions/ucm/class.shub_ucm_account.php index 34bfee5..7261d12 100644 --- a/extensions/ucm/class.shub_ucm_account.php +++ b/extensions/ucm/class.shub_ucm_account.php @@ -1,191 +1,30 @@ load($shub_ucm_id); - } - - private $shub_ucm_id = false; // the current user id in our system. - private $details = array(); - - /* @var $products shub_ucm_product[] */ - private $products = array(); - - - private $json_fields = array('ucm_data'); - - private function reset(){ - $this->shub_ucm_id = false; - $this->details = array( - 'shub_ucm_id' => false, - 'shub_user_id' => 0, - 'ucm_name' => false, - 'last_checked' => false, - 'ucm_data' => array(), - 'ucm_api_url' => false, - 'ucm_username' => false, - 'ucm_api_key' => false, - ); - $this->products = array(); - foreach($this->details as $field_id => $field_data){ - $this->{$field_id} = $field_data; - } - } - - public function create_new(){ - $this->reset(); - $this->shub_ucm_id = shub_update_insert('shub_ucm_id',false,'shub_ucm',array( - 'ucm_name' => '' - )); - $this->load($this->shub_ucm_id); - } - - public function load($shub_ucm_id = false){ - if(!$shub_ucm_id)$shub_ucm_id = $this->shub_ucm_id; - $this->reset(); - $this->shub_ucm_id = (int)$shub_ucm_id; - if($this->shub_ucm_id){ - $data = shub_get_single('shub_ucm','shub_ucm_id',$this->shub_ucm_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_ucm_id'] != $this->shub_ucm_id){ - $this->reset(); - return false; - } - } - foreach($this->details as $key=>$val){ - $this->{$key} = $val; - } - $this->products = array(); - if(!$this->shub_ucm_id)return false; - foreach(shub_get_multiple('shub_ucm_product',array('shub_ucm_id'=>$this->shub_ucm_id),'shub_ucm_product_id') as $product){ - $product = new shub_ucm_product($this, $product['shub_ucm_product_id']); - $this->products[$product->get('product_id')] = $product; - } - return $this->shub_ucm_id; + public function __construct($shub_account_id){ + parent::__construct($shub_account_id); + $this->shub_extension = 'ucm'; } - public function get($field){ - return isset($this->{$field}) ? $this->{$field} : false; - } + public function confirm_api(){ + // confirm API and do a call to get the ucm user id and save it in the account shub_user_id field so we can display when composing a message. - public function save_data($post_data){ - if(!$this->get('shub_ucm_id')){ - $this->create_new(); - } - if(is_array($post_data)){ - foreach($this->details as $details_key => $details_val){ - if(isset($post_data[$details_key])){ - if(($details_key == 'ucm_api_key') && $post_data[$details_key] == 'password')continue; - $this->update($details_key,$post_data[$details_key]); - } - } - } - if(!isset($post_data['import_stream'])){ - $this->update('import_stream', 0); - } - // save the active ucm products. - if(isset($post_data['save_ucm_products']) && $post_data['save_ucm_products'] == 'yep') { - $currently_active_products = $this->products; - $data = $this->get('ucm_data'); - $available_products = isset($data['products']) && is_array($data['products']) ? $data['products'] : array(); - if(isset($post_data['ucm_product']) && is_array($post_data['ucm_product'])){ - foreach($post_data['ucm_product'] as $ucm_product_id => $yesno){ - if(isset($currently_active_products[$ucm_product_id])){ - if(isset($post_data['ucm_product_product'][$ucm_product_id])){ - $currently_active_products[$ucm_product_id]->update('shub_product_id',$post_data['ucm_product_product'][$ucm_product_id]); - } - unset($currently_active_products[$ucm_product_id]); - } - if($yesno && isset($available_products[$ucm_product_id])){ - // we are adding this product to the list. check if it doesn't already exist. - if(!isset($this->products[$ucm_product_id])){ - $product = new shub_ucm_product($this); - $product->create_new(); - $product->update('shub_ucm_id', $this->shub_ucm_id); - //$product->update('ucm_token', 'same'); // $available_products[$ucm_product_id]['access_token'] - $product->update('product_name', $available_products[$ucm_product_id]['name']); - $product->update('product_id', $ucm_product_id); - $product->update('ucm_data', $available_products[$ucm_product_id]); - $product->update('shub_product_id', isset($post_data['ucm_product_product'][$ucm_product_id]) ? $post_data['ucm_product_product'][$ucm_product_id] : 0); - } - } - } - } - // remove any products that are no longer active. - foreach($currently_active_products as $product){ - $product->delete(); - } - } - $this->load(); - return $this->get('shub_ucm_id'); - } - public function update($field,$value){ - // what fields to we allow? or not allow? - if(in_array($field,array('shub_ucm_id')))return; - if($this->shub_ucm_id){ - $this->{$field} = $value; - if(in_array($field,$this->json_fields)){ - $value = json_encode($value); - } - shub_update_insert('shub_ucm_id',$this->shub_ucm_id,'shub_ucm',array( - $field => $value, - )); + $api = $this->get_api(); + + $api_result = $api->api('user','get'); + if($api_result && !empty($api_result['email'])){ + $shub_user_id = $this->get_api_user_to_id($api_result); + if($shub_user_id){ + $this->update('shub_user_id',$shub_user_id); + return true; + } } + echo 'Failed to get User ID from api. Please confirm API details.'; + exit; } - public function delete(){ - if($this->shub_ucm_id) { - // delete all the products for this twitter account. - $products = $this->get('products'); - foreach($products as $product){ - $product->delete(); - } - shub_delete_from_db( 'shub_ucm', 'shub_ucm_id', $this->shub_ucm_id ); - } - } - - public function is_active(){ - // is there a 'last_checked' date? - if(!$this->get('last_checked')){ - return false; // never checked this account, not active yet. - }else{ - return true; - - // do we have a token? - if($this->get('ucm_token')){ - // assume we have access, we remove the token if we get a ucm failure at any point. - return true; - } - } - return false; - } - - public function is_product_active($ucm_product_id){ - if(isset($this->products[$ucm_product_id]) && $this->products[$ucm_product_id]->get('product_id') == $ucm_product_id){ - return true; - }else{ - return false; - } - } - - public function save_account_data($user_data){ - // serialise this result into ucm_data. - if(is_array($user_data)){ - // yes, this member has some products, save these products to the account ready for selection in the settings area. - $save_data = $this->get('ucm_data'); - if(!is_array($save_data))$save_data=array(); - $save_data = array_merge($save_data,$user_data); - $this->update('ucm_data',$save_data); - } - } - public function load_available_products(){ + public function load_available_items(){ // serialise this result into ucm_data. $api = $this->get_api(); @@ -239,7 +78,7 @@ public function load_available_products(){ if(is_array($api_result) && isset($api_result['faq_products']) && count($api_result['faq_products'])){ $this->save_account_data(array( - 'products' => $api_result['faq_products'], + 'items' => $api_result['faq_products'], )); }else{ echo 'Failed to find any FAQ products, please create some in UCM first. Please check logs for any errors.'; @@ -272,13 +111,17 @@ public function get_api_user_to_id($ucm_user_data){ // find a matching user account with these purchases. foreach($ucm_user_data['envato']['purchases'] as $purchase){ if(!empty($purchase['license_code'])) { - if ($comment_user->load_by_meta('envato_license_code', strtolower($purchase['license_code']))) { - // found! yay! - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Found a user based on license code.',array( - 'license_code' => $purchase['license_code'], - 'found_user_id' => $comment_user->get('shub_user_id'), - )); - break; + // pull in the license code using the envato module if it's enabled. + if(isset(SupportHub::getInstance()->message_managers['envato'])) { + $result = SupportHub::getInstance()->message_managers['envato']->pull_purchase_code(false, $purchase['license_code'], array()); + if ($result && !empty($result['shub_user_id'])) { + $comment_user->load($result['shub_user_id']); + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Found a user based on license code.',array( + 'license_code' => $purchase['license_code'], + 'found_user_id' => $comment_user->get('shub_user_id'), + )); + break; + } } } } @@ -350,24 +193,10 @@ public function get_picture(){ $data = $this->get('ucm_data'); return $data && isset($data['pictureUrl']) && !empty($data['pictureUrl']) ? $data['pictureUrl'] : false; } - - - /** - * Links for wordpress - */ - public function link_connect(){ - return 'admin.php?page=support_hub_settings&tab=ucm&ucm_do_oauth_connect&shub_ucm_id='.$this->get('shub_ucm_id'); - } - public function link_edit(){ - return 'admin.php?page=support_hub_settings&tab=ucm&shub_ucm_id='.$this->get('shub_ucm_id'); - } - public function link_new_message(){ - return 'admin.php?page=support_hub_main&shub_ucm_id='.$this->get('shub_ucm_id').'&shub_ucm_message_id=new'; - } - public function link_refresh(){ - return 'admin.php?page=support_hub_settings&tab=ucm&manualrefresh&shub_ucm_id='.$this->get('shub_ucm_id').'&ucm_stream=true'; - } + public function get_item($shub_item_id){ + return new shub_ucm_item($this, $shub_item_id); + } } diff --git a/extensions/ucm/class.shub_ucm_item.php b/extensions/ucm/class.shub_ucm_item.php new file mode 100644 index 0000000..87e7472 --- /dev/null +++ b/extensions/ucm/class.shub_ucm_item.php @@ -0,0 +1,84 @@ +get_messages(array( + 'shub_status' => _shub_MESSAGE_STATUS_PENDINGSEND, + )); + $now = time(); + foreach($messages as $message){ + if(isset($message['message_time']) && $message['message_time'] < $now){ + $shub_ucm_message = new shub_ucm_message(false, $this, $message['shub_ucm_message_id']); + $shub_ucm_message->send_queued($debug); + } + }*/ + + $this->load_latest_item_data($debug); + } + + public function load_latest_item_data($debug = false){ + // serialise this result into ucm_data. + if(!$this->account){ + echo 'No ucm account linked, please try again'; + return; + } + + $api = $this->account->get_api(); + + $ucm_product_id = $this->get('network_key'); + if(!$ucm_product_id){ + echo 'No ucm product id found'; + return; + } + + // we keep a record of the last message received so we know where to stop checking the feed + $last_message_received = (int)$this->get('last_message'); + + // dont want to import ALL tickets, so we pick a 20 day limit if we haven't done this yet + if(!$last_message_received){ + $last_message_received = strtotime('-20 days'); + } + $last_message_received = false; + + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Loading latest tickets for product ('.$ucm_product_id.') "'.$this->get('product_name').'" modified since '.shub_print_date($last_message_received,true)); + // find any messages from this particular UCM product that have been updated since our last scrape time. + $tickets = $api->api('ticket','list',array('search'=>array('faq_product_id'=>$ucm_product_id,'time_from'=>$last_message_received,'status_id'=>0))); + if($debug)echo "Getting the latest tickets for product: ".$ucm_product_id." (last message in database is from ".shub_print_date($last_message_received,true).")
\n"; + + $newest_message_received = 0; + + $count = 0; + foreach($tickets['tickets'] as $ticket){ + $message_time = $ticket['last_message_timestamp']; + $newest_message_received = max($newest_message_received,$message_time); + //if($message_time <= $last_message_received)break; // all done here. + + $ucm_message = new shub_ucm_message($this->account, $this, false); + $ucm_message -> load_by_network_key($ticket['ticket_id'], $ticket, 'ticket', $debug); + $count++; + if($debug) { + ?> +
+
 Imported Ticket ID: get( 'network_key' ); ?> with  message. 
+
+ log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'Imported '.$count.' product tickets into database (from a total of '.count($tickets['tickets']).' returned by the api)'); + if($debug)echo " imported $count new product tickets
"; + + $this->update('last_message',$newest_message_received); + $this->update('last_checked',time()); + } + + public function link_refresh(){ + return 'admin.php?page=support_hub_settings&tab=ucm&manualrefresh&shub_account_id='.$this->get('shub_account_id').'&network_key='.$this->get('network_key'); + } + + +} diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index 8305445..8877378 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -1,96 +1,67 @@ ucm_account = $ucm_account; - $this->ucm_product = $ucm_product; - $this->load($shub_ucm_message_id); - } - - /* @var $ucm_product shub_ucm_product */ - private $ucm_product= false; - /* @var $ucm_account shub_ucm_account */ - private $ucm_account = false; - private $shub_ucm_message_id = false; // the current user id in our system. - private $details = array(); - - private function reset(){ - $this->shub_ucm_message_id = false; - $this->details = array( - 'shub_ucm_message_id' => '', - 'shub_ucm_product_id' => '', - 'shub_ucm_id' => '', - 'shub_product_id' => -1, - 'ucm_id' => '', - 'title' => '', - 'summary' => '', - 'last_active' => '', - 'comments' => '', - 'type' => '', - 'link' => '', - 'data' => '', - 'shub_status' => '', - 'user_id' => '', - 'shub_user_id' => 0, - ); - foreach($this->details as $key=>$val){ - $this->{$key} = ''; - } - } + protected $network = 'ucm'; - public function create_new(){ - $this->reset(); - $this->shub_ucm_message_id = shub_update_insert('shub_ucm_message_id',false,'shub_ucm_message',array( - 'title' => '', - )); - $this->load($this->shub_ucm_message_id); - } - public function load_by_ucm_id($ucm_ticket_id, $ticket, $type, $debug = false){ + public function load_by_network_key($network_key, $ticket, $type, $debug = false){ switch($type){ case 'ticket': - $existing = shub_get_single('shub_ucm_message', 'ucm_ticket_id', $ucm_ticket_id); + $existing = shub_get_single('shub_message', 'network_key', $network_key); if($existing){ // load it up. - $this->load($existing['shub_ucm_message_id']); + $this->load($existing['shub_message_id']); } - if($ticket && isset($ticket['ticket_id']) && $ticket['ticket_id'] == $ucm_ticket_id){ + if($ticket && isset($ticket['ticket_id']) && $ticket['ticket_id'] == $network_key){ // get the messages from the API - $api = $this->ucm_account->get_api(); - $api_result = $api->api('ticket','message',array('ticket_ids'=>$ucm_ticket_id)); - if($api_result && isset($api_result['tickets'][$ucm_ticket_id]) && count($api_result['tickets'][$ucm_ticket_id])) { + $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); - $all_comments = $api_result['tickets'][$ucm_ticket_id]; + $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_ucm_id', $this->ucm_account->get('shub_ucm_id')); - $this->update('shub_ucm_product_id', $this->ucm_product->get('shub_ucm_product_id')); + $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->ucm_account->get_api_user_to_id($ticket['user']); + $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('type', $type); - $this->update('data', json_encode($ticket)); - $this->update('link', $ticket['url']); - $this->update('ucm_ticket_id', $ucm_ticket_id); - if($this->get('shub_status')!=_shub_MESSAGE_STATUS_HIDDEN) $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED); - $this->update('comments', json_encode($comments)); + $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'])){ @@ -105,14 +76,14 @@ public function load_by_ucm_id($ucm_ticket_id, $ticket, $type, $debug = false){ } $ExtraField->save_and_link(array( 'extra_value' => $extra_val - ),'ucm',$this->ucm_account->get('shub_ucm_id'),$this->get('shub_ucm_message_id'),$shub_user_id); + ),'ucm',$this->account->get('shub_account_id'),$this->get('shub_message_id'),$shub_user_id); } } } } - return $this->get('shub_ucm_message_id'); + return $this->get('shub_message_id'); } break; @@ -121,320 +92,26 @@ public function load_by_ucm_id($ucm_ticket_id, $ticket, $type, $debug = false){ } - public function load($shub_ucm_message_id = false){ - if(!$shub_ucm_message_id)$shub_ucm_message_id = $this->shub_ucm_message_id; - $this->reset(); - $this->shub_ucm_message_id = $shub_ucm_message_id; - if($this->shub_ucm_message_id){ - $data = shub_get_single('shub_ucm_message','shub_ucm_message_id',$this->shub_ucm_message_id); - foreach($this->details as $key=>$val){ - $this->details[$key] = $data && isset($data[$key]) ? $data[$key] : $val; - } - if(!is_array($this->details) || !isset($this->details['shub_ucm_message_id']) || $this->details['shub_ucm_message_id'] != $this->shub_ucm_message_id){ - $this->reset(); - return false; - } - } - foreach($this->details as $key=>$val){ - $this->{$key} = $val; - } - if(!$this->ucm_account && $this->get('shub_ucm_id')){ - $this->ucm_account = new shub_ucm_account($this->get('shub_ucm_id')); - } - if(!$this->ucm_product && $this->get('shub_ucm_product_id')) { - $this->ucm_product = new shub_ucm_product($this->ucm_account, $this->get('shub_ucm_product_id')); - } - return $this->shub_ucm_message_id; - } - - public function get($field){ - return isset($this->{$field}) ? $this->{$field} : false; - } - - public function update($field,$value){ - // what fields to we allow? or not allow? - if(in_array($field,array('shub_ucm_message_id')))return; - if($this->shub_ucm_message_id){ - $this->{$field} = $value; - shub_update_insert('shub_ucm_message_id',$this->shub_ucm_message_id,'shub_ucm_message',array( - $field => $value, - )); - // special processing for certain fields. - if($field == 'comments'){ - // we push all thsee messages into a shub_ucm_message_comment database table - // this is so we can do quick lookups on message ids so we dont import duplicate products from graph (ie: a reply on a message comes in as a separate product sometimes) - $data = @json_decode($value,true); - if(is_array($data)) { - // clear previous message history. - $existing_messages = $this->get_comments(); //shub_get_multiple('shub_ucm_message_comment',array('shub_ucm_message_id'=>$this->shub_ucm_message_id),'shub_ucm_message_comment_id'); - //shub_delete_from_db('shub_ucm_message_comment','shub_ucm_message_id',$this->shub_ucm_message_id); - $remaining_messages = $this->_update_comments( $data , $existing_messages); - // $remaining_messages contains any messages that no longer exist... - // todo: remove these? yer prolly. do a quick test on removing a message - i think the only thing is it will show the 'from' name still. - } - } + public function get_link() { + $item = $this->get('item'); + $url = '#'; + if($this->account){ + $bits = @parse_url($this->account->get('ucm_api_url')); + if($bits && !empty($bits['path'])){ + $url = $bits['scheme'].'://'.$bits['host'].str_replace('ext.php','',$bits['path']); + $url .= '?m=ticket&p=ticket_admin'; + } + if($item){ + $url .= '&faq_product_id='.$item->get('network_key'); + } } + return $url; } - public function parse_links($content = false){ - if(!$this->get('shub_ucm_message_id'))return; - // strip out any links in the tweet and write them to the ucm_message_link table. - $url_clickable = '~ - ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation - ( # 2: URL - [\\w]{1,20}+:// # Scheme and hier-part prefix - (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long - [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character - (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character - [\'.,;:!?)] # Punctuation URL character - [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character - )* - ) - (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing) - ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character. - // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times. - if(!$content){ - $content = $this->get('summary'); - $doing_summary = true; - } - $summary = ' ' . $content . ' '; - if(strlen($summary) && preg_match_all($url_clickable,$summary,$matches)){ - foreach($matches[2] as $id => $url){ - $url = trim($url); - if(strlen($url)) { - // wack this url into the database and replace it with our rewritten url. - $shub_ucm_message_link_id = shub_update_insert( 'shub_ucm_message_link_id', false, 'shub_ucm_message_link', array( - 'shub_ucm_message_id' => $this->get('shub_ucm_message_id'), - 'link' => $url, - ) ); - if($shub_ucm_message_link_id) { - $new_link = trailingslashit( get_site_url() ); - $new_link .= strpos( $new_link, '?' ) === false ? '?' : '&'; - $new_link .= _support_hub_ucm_LINK_REWRITE_PREFIX . '=' . $shub_ucm_message_link_id; - // basic hash to stop brute force. - if(defined('AUTH_KEY')){ - $new_link .= ':'.substr(md5(AUTH_KEY.' ucm link '.$shub_ucm_message_link_id),1,5); - } - $newsummary = trim(preg_replace('#'.preg_quote($url,'#').'#',$new_link,$summary, 1)); - if(strlen($newsummary)){// just incase. - $summary = $newsummary; - } - } - } - } - } - if(isset($doing_summary) && $doing_summary){ - $this->update('summary',$summary); - } - return trim($summary); - } - private function _update_comments($data, $existing_messages){ - if(is_array($data)){ - foreach($data as $message){ - if($message['ticket_message_id']){ - // does this id exist in the db already? - $exists = shub_get_single('shub_ucm_message_comment',array('ucm_ticket_message_id','shub_ucm_message_id'),array($message['ticket_message_id'],$this->shub_ucm_message_id)); - - // create/update a user entry for this comments. - // create/update a user entry for this comments. - $shub_user_id = $this->ucm_account->get_api_user_to_id($message['user']); - - $shub_ucm_message_comment_id = shub_update_insert('shub_ucm_message_comment_id',$exists ? $exists['shub_ucm_message_comment_id'] : false,'shub_ucm_message_comment',array( - 'shub_ucm_message_id' => $this->shub_ucm_message_id, - 'ucm_ticket_message_id' => $message['ticket_message_id'], - 'time' => $message['message_time'], - 'data' => json_encode($message), - 'message_from' => '', - 'message_to' => '', - 'message_text' => isset($message['content']) ? $message['content'] : '', - 'shub_user_id' => $shub_user_id, - )); - if(isset($existing_messages[$shub_ucm_message_comment_id])){ - unset($existing_messages[$shub_ucm_message_comment_id]); - } - /*if(isset($message['comments']) && is_array($message['comments'])){ - $existing_messages = $this->_update_messages($message['comments'], $existing_messages); - }*/ - } - } - } - return $existing_messages; - } - - public function delete(){ - if($this->shub_ucm_message_id) { - shub_delete_from_db( 'shub_ucm_message', 'shub_ucm_message_id', $this->shub_ucm_message_id ); - } - } - - - public function mark_as_read(){ - if($this->shub_ucm_message_id && get_current_user_id()){ - $sql = "REPLACE INTO `"._support_hub_DB_PREFIX."shub_ucm_message_read` SET `shub_ucm_message_id` = ".(int)$this->shub_ucm_message_id.", `user_id` = ".(int)get_current_user_id().", read_time = ".(int)time(); - shub_query($sql); - } - } - - public function get_summary() { - // who was the last person to contribute to this post? show their details here instead of the 'summary' box maybe? - $title = $this->get( 'title' ); - return htmlspecialchars( strlen( $title ) > 80 ? substr( $title, 0, 80 ) . '...' : $title ); -// $summary = $this->get( 'summary' ); -// return htmlspecialchars( strlen( $title ) > 80 ? substr( $title, 0, 80 ) . '...' : $title ) . ($summary!=$title ? '
' .htmlspecialchars( strlen( $summary ) > 80 ? substr( $summary, 0, 80 ) . '...' : $summary ) : ''); - } - - private $can_reply = false; - private function _output_block($ucm_data,$level){ - if($level == 1){ - // display the info from the main 'message' table - $comments = $this->get_comments(); - $comment = array_shift($comments); - $comment['title'] = $this->get('title'); - $comment['shub_user_id'] = $this->get('shub_user_id'); - }else{ - // display the info from the 'comments' table. - $comment = $ucm_data; - $comments = array(); - } -// echo '
';echo $level;print_r($comments);echo '
'; - //echo '
';print_r($ucm_data);echo '
'; - $from = new SupportHubUser_ucm($comment['shub_user_id']); - ?> -
-
- get_image()){ ?> - - -
-
- ucm_account); ?> - display_name) . ')'; - } - ?> - -
-
- -
- -
- -
- -
-
-
-
-
-
- '; print_r($ucm_data); echo ''; - //} - if(count($comments)){ - // recursively print out our messages! - //$messages = array_reverse($messages); - foreach($comments as $comment){ - $this->_output_block($comment,$level+1); - } - } - if($level <= 1) { - if ( $this->can_reply && isset( $ucm_data['post_id'] ) && $ucm_data['post_id'] ) { - $this->reply_box( $ucm_data['post_id'], $level ); - } - } - ?> -
- can_reply = $can_reply; - // used in shub_ucm_list.php to display the full message and its messages - switch($this->get('type')){ - default: - $ucm_data = @json_decode($this->get('data'),true); - $ucm_data['message'] = $this->get('title'); - $ucm_data['user_id'] = $this->get('user_id'); -// $ucm_data['comments'] = array_reverse($this->get_comments()); - //echo '
'; print_r($ucm_data['comments']); echo '
'; - $this->_output_block($ucm_data,1); - - break; - } - } - - public function reply_box($ucm_id,$level=1){ - if($this->ucm_account && $this->shub_ucm_message_id) { - $user_data = $this->ucm_account->get('ucm_data'); - $from = new SupportHubUser_ucm($user_data['user']['shub_user_id']); - ?> -
-
- -
-
- ucm_account ); ?> -
-
- - -
-
- (debug)
- 'yes' - ); - shub_module_form::generate_form_element($reply_option['field']); - } - echo '
'; - } - } - } - ?> -
-
- -
- (incorrect settings, please report this bug) -
- attachment_name = $local_filename; - } - } public function send_queued($debug = false){ - if($this->ucm_account && $this->shub_ucm_message_id) { + if($this->account && $this->shub_message_id) { // send this message out to ucm. // this is run when user is composing a new message from the UI, if ( $this->get( 'shub_status' ) == _shub_MESSAGE_STATUS_SENDING ) @@ -445,22 +122,22 @@ public function send_queued($debug = false){ case 'product_post': - if(!$this->ucm_product) { + if(!$this->item) { echo 'No ucm product defined'; return false; } $this->update( 'shub_status', _shub_MESSAGE_STATUS_SENDING ); - $api = $this->ucm_account->get_api(); - $ucm_product_id = $this->ucm_product->get('product_id'); - if($debug)echo "Sending a new message to ucm product ID: $ucm_product_id
\n"; + $api = $this->account->get_api(); + $item_id = $this->item->get('product_id'); + if($debug)echo "Sending a new message to ucm product ID: $item_id
\n"; $result = false; $post_data = array(); $post_data['summary'] = $this->get('summary'); $post_data['title'] = $this->get('title'); $now = time(); $send_time = $this->get('last_active'); - $result = $api->api('v1/products/'.$ucm_product_id.'/posts',array(),'POST',$post_data,'location'); + $result = $api->api('v1/products/'.$item_id.'/posts',array(),'POST',$post_data,'location'); if($debug)echo "API Post Result:
\n".var_export($result,true)."
\n"; if($result && preg_match('#https://api.ucm.com/v1/posts/(.*)$#',$result,$matches)){ // we have a result from the API! this should be an API call in itself: @@ -487,96 +164,8 @@ public function send_queued($debug = false){ } return false; } - public function send_reply($ucm_id, $message, $debug = false, $extra_data = array()){ - if($this->ucm_account && $this->shub_ucm_message_id) { - - - $api = $this->ucm_account->get_api(); - if($debug)echo "Type: ".$this->get('type')."
\n"; - switch($this->get('type')) { - case 'ticket': - - if(!$this->ucm_product){ - echo 'Error no product, report this'; - return false; - } - if(!$ucm_id)$ucm_id = $this->get('ucm_id'); - - $ucm_post_data = @json_decode($this->get('data'),true); - - if($debug)echo "Sending a reply to ucm Topic ID: $ucm_id
\n"; - $api_result = false; - try{ - $extra_data['api'] = 1; - $api_result = $api->newPost('Reply to: '.((isset($ucm_post_data['post_title'])) ? $ucm_post_data['post_title'] : 'Post'),$message,array( - 'post_type' => 'reply', - 'post_parent' => $ucm_id, - 'custom_fields' => array( - array( - 'key' => 'support_hub', - 'value' => json_encode($extra_data), - ) - ) - )); - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'API Result: ', $api_result); - }catch(Exception $e){ - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'API Error: ', $e); - if($debug){ - echo "API Error: ".$e; - } - } - 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('ucm_id')); - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'API Result: ', $api_result); - $reply_post = $api->getPost($api_result); - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'API Result: ', $api_result); - - if($parent_topic && $parent_topic['post_id'] == $this->get('ucm_id') && $reply_post && $reply_post['post_id'] == $api_result && $reply_post['post_parent'] == $this->get('ucm_id')){ - // all looks hunky dory - $comments = @json_decode($this->get('comments'),true); - 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_ucm_id($this->get('ucm_id'),$parent_topic,$this->get('type'),$debug); - $existing_messages = $this->get_comments(); - foreach($existing_messages as $existing_message){ - if(!$existing_message['user_id'] && $existing_message['message_text'] == $message){ - shub_update_insert('shub_ucm_message_comment_id',$existing_message['shub_ucm_message_comment_id'],'shub_ucm_message_comment',array( - 'user_id' => get_current_user_id(), - )); - } - } - $this->update('shub_status', _shub_MESSAGE_STATUS_ANSWERED); - } - - } - break; - } - - } - } - public function get_comments($message_data = false) { - if($message_data){ - $messages = $message_data; - if(!is_array($messages))$messages=array(); - usort($messages,function($a,$b){ - if(isset($a['id'])){ - return $a['id'] > $b['id']; - } - return strtotime($a['created_at']) > strtotime($b['created_at']); - }); - }else{ - $messages = shub_get_multiple('shub_ucm_message_comment',array('shub_ucm_message_id'=>$this->shub_ucm_message_id),'shub_ucm_message_comment_id','time'); //@json_decode($this->get('comments'),true); - } - return $messages; - } public function get_type_pretty() { $type = $this->get('type'); @@ -590,7 +179,7 @@ public function get_type_pretty() { } public function get_from() { - if($this->shub_ucm_message_id){ + if($this->shub_message_id){ $from = array(); if($this->get('shub_user_id')){ $from[$this->get('shub_user_id')] = new SupportHubUser_ucm($this->get('shub_user_id')); @@ -605,30 +194,70 @@ public function get_from() { } return array(); } + public function message_sidebar_data(){ + + // find if there is a product here + $shub_product_id = $this->get_product_id(); + $product_data = array(); + $item_data = array(); + $item = $this->get('item'); + if(!$shub_product_id && $item){ + $shub_product_id = $item->get('shub_product_id'); + $item_data = $item->get('item_data'); + if(!is_array($item_data))$item_data = array(); + } + if($shub_product_id) { + $shub_product = new SupportHubProduct(); + $shub_product->load( $shub_product_id ); + $product_data = $shub_product->get( 'product_data' ); + } + ?> + + + + +
+ get('account') ? $this->get('account')->get( 'account_name' ) : 'N/A' ); ?>
+ get('last_active'), true ); ?>
+ + + + +
+ get('shub_product_id') >= 0){ - return $this->get('shub_product_id'); - }else{ - return $this->ucm_product->get('shub_product_id'); - } - } - - public function save_product_id($new_product_id){ - if($new_product_id == $this->ucm_product->get('shub_product_id')){ - // setting it back to default. - $this->update('shub_product_id', -1); - }else{ - $this->update('shub_product_id', $new_product_id); - } - } - + /*$data = $this->get('shub_data'); + if(!empty($data['buyer_and_author']) && $data['buyer_and_author'] && $data['buyer_and_author'] !== 'false'){ + // hmm - this doesn't seem to be a "purchased" flag. + ?> + PURCHASED
+ shub_ucm_message_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']; + } + return $user_hints; + } + public function get_user($shub_user_id){ + return new SupportHubUser_ucm($shub_user_id); + } + public function get_reply_user(){ + return new SupportHubUser_ucm($this->account->get('shub_user_id')); + } } \ No newline at end of file diff --git a/extensions/ucm/class.shub_ucm_product.php b/extensions/ucm/class.shub_ucm_product.php deleted file mode 100644 index 04548a3..0000000 --- a/extensions/ucm/class.shub_ucm_product.php +++ /dev/null @@ -1,184 +0,0 @@ -ucm_account = $ucm_account; - $this->load($shub_ucm_product_id); - } - - /* @var $ucm_account shub_ucm_account */ - private $ucm_account = false; - private $shub_ucm_product_id = false; // the current user id in our system. - private $details = array(); - private $json_fields = array('ucm_data'); - - private function reset(){ - $this->shub_ucm_product_id = false; - $this->details = array( - 'shub_ucm_product_id' => '', - 'shub_ucm_id' => '', - 'shub_product_id' => '', - 'product_name' => '', - 'last_message' => '', - 'last_checked' => '', - 'product_id' => '', - 'ucm_data' => array(), - ); - foreach($this->details as $field_id => $field_data){ - $this->{$field_id} = $field_data; - } - } - - public function create_new(){ - $this->reset(); - $this->shub_ucm_product_id = shub_update_insert('shub_ucm_product_id',false,'shub_ucm_product',array( - 'product_name' => '', - )); - $this->load($this->shub_ucm_product_id); - } - - public function load($shub_ucm_product_id = false){ - if(!$shub_ucm_product_id)$shub_ucm_product_id = $this->shub_ucm_product_id; - $this->reset(); - $this->shub_ucm_product_id = $shub_ucm_product_id; - if($this->shub_ucm_product_id){ - $data = shub_get_single('shub_ucm_product','shub_ucm_product_id',$this->shub_ucm_product_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_ucm_product_id'] != $this->shub_ucm_product_id){ - $this->reset(); - return false; - } - } - foreach($this->details as $key=>$val){ - $this->{$key} = $val; - } - return $this->shub_ucm_product_id; - } - - public function get($field){ - return isset($this->{$field}) ? $this->{$field} : false; - } - - public function update($field,$value){ - // what fields to we allow? or not allow? - if(in_array($field,array('shub_ucm_product_id')))return; - if($this->shub_ucm_product_id){ - $this->{$field} = $value; - if(in_array($field,$this->json_fields)){ - $value = json_encode($value); - } - shub_update_insert('shub_ucm_product_id',$this->shub_ucm_product_id,'shub_ucm_product',array( - $field => $value, - )); - } - } - public function delete(){ - if($this->shub_ucm_product_id) { - // delete all the messages for this twitter account. - $messages = shub_get_multiple('shub_ucm_message',array( - 'shub_ucm_product_id' => $this->shub_ucm_product_id, - ),'shub_ucm_message_id'); - foreach($messages as $message){ - if($message && isset($message['shub_ucm_product_id']) && $message['shub_ucm_product_id'] == $this->shub_ucm_product_id){ - shub_delete_from_db( 'shub_ucm_message', 'shub_ucm_message_id', $message['shub_ucm_message_id'] ); - shub_delete_from_db( 'shub_ucm_message_comment', 'shub_ucm_message_id', $message['shub_ucm_message_id'] ); - shub_delete_from_db( 'shub_ucm_message_link', 'shub_ucm_message_id', $message['shub_ucm_message_id'] ); - shub_delete_from_db( 'shub_ucm_message_read', 'shub_ucm_message_id', $message['shub_ucm_message_id'] ); - } - } - shub_delete_from_db( 'shub_ucm_product', 'shub_ucm_product_id', $this->shub_ucm_product_id ); - } - } - - public function get_messages($search=array()){ - $ucm = new shub_ucm(); - $search['shub_ucm_product_id'] = $this->shub_ucm_product_id; - return $ucm->load_all_messages($search); - //return get_m ultiple('shub_ucm_message',$search,'shub_ucm_message_id','exact','last_active'); - } - - public function run_cron($debug = false){ - // find all messages that haven't been sent yet. - $messages = $this->get_messages(array( - 'shub_status' => _shub_MESSAGE_STATUS_PENDINGSEND, - )); - $now = time(); - foreach($messages as $message){ - if(isset($message['message_time']) && $message['message_time'] < $now){ - $shub_ucm_message = new shub_ucm_message(false, $this, $message['shub_ucm_message_id']); - $shub_ucm_message->send_queued($debug); - } - } - - $this->load_latest_product_data($debug); - } - - public function load_latest_product_data($debug = false){ - // serialise this result into ucm_data. - if(!$this->ucm_account){ - echo 'No ucm account linked, please try again'; - return; - } - - $api = $this->ucm_account->get_api(); - - $ucm_product_id = $this->get('product_id'); - if(!$ucm_product_id){ - echo 'No ucm product id found'; - return; - } - - // we keep a record of the last message received so we know where to stop checking the feed - $last_message_received = (int)$this->get('last_message'); - - // dont want to import ALL tickets, so we pick a 20 day limit if we haven't done this yet - if(!$last_message_received){ - $last_message_received = strtotime('-20 days'); - } - - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Loading latest tickets for product ('.$ucm_product_id.') "'.$this->get('product_name').'" modified since '.shub_print_date($last_message_received,true)); - // find any messages from this particular UCM product that have been updated since our last scrape time. - $tickets = $api->api('ticket','list',array('search'=>array('faq_product_id'=>$ucm_product_id,'time_from'=>$last_message_received,'status_id'=>0))); - if($debug)echo "Getting the latest tickets for product: ".$ucm_product_id." (last message in database is from ".shub_print_date($last_message_received,true).")
\n"; - - $newest_message_received = 0; - - $count = 0; - foreach($tickets['tickets'] as $ticket){ - $message_time = $ticket['last_message_timestamp']; - $newest_message_received = max($newest_message_received,$message_time); - //if($message_time <= $last_message_received)break; // all done here. - - $ucm_message = new shub_ucm_message($this->ucm_account, $this, false); - $ucm_message -> load_by_ucm_id($ticket['ticket_id'], $ticket, 'ticket', $debug); - $count++; - if($debug) { - ?> -
-
 Imported Ticket ID: get( 'ucm_id' ); ?> with  message. 
-
- log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'Imported '.$count.' product tickets into database (from a total of '.count($tickets['tickets']).' returned by the api)'); - if($debug)echo " imported $count new product tickets
"; - - $this->update('last_message',$newest_message_received); - $this->update('last_checked',time()); - } - - public function link_refresh(){ - return 'admin.php?page=support_hub_settings&tab=ucm&manualrefresh&shub_ucm_id='.$this->get('shub_ucm_id').'&ucm_product_id='.$this->get('product_id'); - } - - -} diff --git a/extensions/ucm/init.ucm.php b/extensions/ucm/init.ucm.php index c7276a4..8c4dc3a 100644 --- a/extensions/ucm/init.ucm.php +++ b/extensions/ucm/init.ucm.php @@ -8,7 +8,7 @@ function shub_managers_ucm( $shub ){ require_once 'class.shub_ucm.php'; require_once 'class.shub_ucm_user.php'; require_once 'class.shub_ucm_account.php'; - require_once 'class.shub_ucm_product.php'; + require_once 'class.shub_ucm_item.php'; require_once 'class.shub_ucm_message.php'; $shub['ucm'] = new shub_ucm(); }else{ diff --git a/extensions/ucm/ucm-logo.png b/extensions/ucm/logo.png similarity index 100% rename from extensions/ucm/ucm-logo.png rename to extensions/ucm/logo.png diff --git a/extensions/ucm/shub_ucm.js b/extensions/ucm/shub_ucm.js index 0926b71..e632546 100644 --- a/extensions/ucm/shub_ucm.js +++ b/extensions/ucm/shub_ucm.js @@ -6,7 +6,7 @@ ucm.social.ucm = { f.show(); f.find('textarea')[0].focus(); }).delegate('.ucm_check_all','change',function(){ - jQuery('.check_ucm_product').prop('checked', !!jQuery(this).prop('checked')); + jQuery('.check_item').prop('checked', !!jQuery(this).prop('checked')); }); } diff --git a/extensions/ucm/ucm_settings.php b/extensions/ucm/ucm_settings.php index 7cb5f4c..83991b9 100644 --- a/extensions/ucm/ucm_settings.php +++ b/extensions/ucm/ucm_settings.php @@ -1,23 +1,30 @@ message_managers['ucm']; if($current_account !== false){ $shub_ucm_account = new shub_ucm_account($current_account); + if($shub_ucm_account->get('shub_extension') != 'ucm')die('Wrong extension:' .$shub_ucm_account->get('shub_extension')); if(isset($_GET['manualrefresh'])){ - $ucm_product_id = isset( $_REQUEST['ucm_product_id'] ) ? (int) $_REQUEST['ucm_product_id'] : 0; - /* @var $products shub_ucm_product[] */ - $products = $shub_ucm_account->get( 'products' ); - if ( ! $ucm_product_id || ! $products || ! isset( $products[ $ucm_product_id ] ) ) { - die( 'No products found to refresh' ); - } - ?> - Manually refreshing product data... please wait... - run_cron( true ); + $network_key = isset( $_REQUEST['network_key'] ) ? (int) $_REQUEST['network_key'] : 0; + if(!$network_key){ + // update?? products? + $shub_ucm_account->confirm_api(); + $shub_ucm_account->load_available_items(); + }else { + /* @var $items shub_item[] */ + $items = $shub_ucm_account->get('items'); + if (!$network_key || !$items || !isset($items[$network_key])) { + die('No items found to refresh'); + } + ?> + Manually refreshing item data... please wait... + run_cron(true); + } - }else if(isset($_GET['ucm_do_oauth_connect'])){ + }else if(isset($_GET['do_connect'])){ // connect to ucm. and if that isnt' found ?>
@@ -25,11 +32,12 @@ get('shub_ucm_id') && $shub_ucm_account->get('shub_ucm_id') == $current_account && $shub_ucm_account->get( 'ucm_api_url' ) && + if($shub_ucm_account->get('shub_account_id') && $shub_ucm_account->get('shub_account_id') == $current_account && $shub_ucm_account->get( 'ucm_api_url' ) && $shub_ucm_account->get( 'ucm_api_key' )) { // now we load in a list of ucm products to manage and redirect the user back to the 'edit' screen where they can continue managing the account. - $shub_ucm_account->load_available_products(); + $shub_ucm_account->confirm_api(); + $shub_ucm_account->load_available_items(); $url = $shub_ucm_account->link_edit(); ?>

You have successfully connected UCM with the Support Hub plugin. Please click the button below:

@@ -53,10 +61,10 @@
- - - get( 'shub_ucm_id' ) ); ?> + + + + get( 'shub_account_id' ) ); ?>

Setup Instructions:

    @@ -73,7 +81,7 @@ - + (e.g. My UCM Install) @@ -82,7 +90,7 @@ - + (e.g. http://mysite.com/ucm/ext.php?m=api&h=v1&) @@ -91,10 +99,10 @@ - + - get( 'shub_ucm_id' ) ) { ?> + get( 'shub_account_id' ) ) { ?> @@ -108,12 +116,12 @@ - +
    get( 'ucm_data' ); - if ( $data && isset( $data['products'] ) && is_array( $data['products'] ) && count( $data['products'] ) > 0 ) { - $ucm_products = $shub_ucm_account->get('products'); + $data = $shub_ucm_account->get( 'account_data' ); + if ( $data && isset( $data['items'] ) && is_array( $data['items'] ) && count( $data['items'] ) > 0 ) { + $ucm_products = $shub_ucm_account->get('items'); ?>
    - check all - @@ -133,33 +141,34 @@ get_products(); - foreach ( $data['products'] as $product_id => $product_data ) { + foreach ( $data['items'] as $product_id => $product_data ) { ?> - is_product_active( $product_id ) ? ' checked' : ''; ?>> + is_item_active( $product_id ) ? ' checked' : ''; ?>> + 'ucm_product_product['.$product_id.']', + 'name' => 'item_product['.$product_id.']', 'type' => 'select', 'blank' => __('- None -','support_hub'), - 'value' => $shub_ucm_account->is_product_active( $product_id ) ? $ucm_products[ $product_id ]->get( 'shub_product_id' ) : (isset($product_data['shub_product_id']) ? $product_data['shub_product_id'] : 0), + 'value' => $shub_ucm_account->is_item_active( $product_id ) ? $ucm_products[ $product_id ]->get( 'shub_product_id' ) : (isset($product_data['shub_product_id']) ? $product_data['shub_product_id'] : 0), 'options' => $products, 'options_array_id' => 'product_name', 'class' => 'shub_product_dropdown', )); ?> - is_product_active( $product_id ) && $ucm_products[ $product_id ]->get( 'last_checked' ) ? shub_print_date( $ucm_products[ $product_id ]->get( 'last_checked' ), true ): 'N/A';?> + is_item_active( $product_id ) && $ucm_products[ $product_id ]->get( 'last_checked' ) ? shub_print_date( $ucm_products[ $product_id ]->get( 'last_checked' ), true ): 'N/A';?> is_product_active( $product_id ) ) { + if ( $shub_ucm_account->is_item_active( $product_id ) ) { echo 're-load product tickets'; } ?> @@ -181,7 +190,7 @@

    - get( 'shub_ucm_id' ) ) { ?> + get( 'shub_account_id' ) ) { ?> get_accounts(); foreach($accounts as $account_id => $account){ - $a = new shub_ucm_account($account['shub_ucm_id']); + $a = new shub_ucm_account($account['shub_account_id']); $accounts[$account_id]['edit_link'] = $a->link_edit(); - $accounts[$account_id]['title'] = $a->get('ucm_name'); + $accounts[$account_id]['title'] = $a->get('account_name'); $accounts[$account_id]['last_checked'] = $a->get('last_checked') ? shub_print_date( $a->get('last_checked') ) : 'N/A'; } $myListTable->set_data($accounts); @@ -216,7 +225,7 @@

    - +

    search_box( 'search', 'search_id' ); From b4b98802f1f503c5e8bdaba5452db0fbdb636506 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 03:24:08 +1000 Subject: [PATCH 03/23] ucm --- extensions/ucm/class.shub_ucm_item.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/ucm/class.shub_ucm_item.php b/extensions/ucm/class.shub_ucm_item.php index 87e7472..05947c8 100644 --- a/extensions/ucm/class.shub_ucm_item.php +++ b/extensions/ucm/class.shub_ucm_item.php @@ -41,7 +41,7 @@ public function load_latest_item_data($debug = false){ if(!$last_message_received){ $last_message_received = strtotime('-20 days'); } - $last_message_received = false; +// $last_message_received = false; SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Loading latest tickets for product ('.$ucm_product_id.') "'.$this->get('product_name').'" modified since '.shub_print_date($last_message_received,true)); // find any messages from this particular UCM product that have been updated since our last scrape time. From b0e53f5a8ffd0a1cd3ec3a43d6c2f98ccf1e7159 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 03:40:22 +1000 Subject: [PATCH 04/23] ucm --- extensions/ucm/class.shub_ucm_message.php | 2 +- extensions/ucm/class.ucm-api.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index 8877378..e561a35 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -99,7 +99,7 @@ public function get_link() { if($this->account){ $bits = @parse_url($this->account->get('ucm_api_url')); if($bits && !empty($bits['path'])){ - $url = $bits['scheme'].'://'.$bits['host'].str_replace('ext.php','',$bits['path']); + $url = $bits['scheme'].'://'.$bits['host'].str_replace('ext.php','',str_replace('external/m.api/h.v1','',$bits['path'])); $url .= '?m=ticket&p=ticket_admin'; } if($item){ diff --git a/extensions/ucm/class.ucm-api.php b/extensions/ucm/class.ucm-api.php index 7fa8048..a55d9a2 100644 --- a/extensions/ucm/class.ucm-api.php +++ b/extensions/ucm/class.ucm-api.php @@ -28,9 +28,9 @@ public function api($endpoint, $method=false, $params=array()){ $params['auth'] = $this->_api_key; if($params){ $headers['body'] = $params; - $response = wp_remote_post($this->_api_url . "&endpoint=$endpoint&method=$method", $headers); + $response = wp_remote_post($this->_api_url . (strpos($this->_api_url,'?') ? '&' : '?') . "endpoint=$endpoint&method=$method", $headers); }else{ - $response = wp_remote_get($this->_api_url . "&endpoint=$endpoint&method=$method", $headers); + $response = wp_remote_get($this->_api_url . (strpos($this->_api_url,'?') ? '&' : '?') . "endpoint=$endpoint&method=$method", $headers); } if( is_array($response) && isset($response['body']) && isset($response['response']['code']) && $response['response']['code'] == 200 ) { SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'API Call: '.$endpoint .'/' .$method,$response['body']); From 48df759afc8ee9509a32ce810c117e0fd2b8cc2b Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 04:11:06 +1000 Subject: [PATCH 05/23] ucm + envato api fix --- extensions/envato/class.shub_envato.php | 12 +++++-- extensions/ucm/class.shub_ucm_account.php | 40 +++++++++++++---------- extensions/ucm/class.shub_ucm_message.php | 2 +- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/extensions/envato/class.shub_envato.php b/extensions/envato/class.shub_envato.php index 7e88e2b..e7878aa 100644 --- a/extensions/envato/class.shub_envato.php +++ b/extensions/envato/class.shub_envato.php @@ -570,7 +570,7 @@ public function extra_process_login($network, $account_id, $message_id, $extra_i return false; } - public function pull_purchase_code($api, $purchase_code, $api_raw_data = array()){ + public function pull_purchase_code($api, $purchase_code, $api_raw_data = array(), $existing_shub_user_id = false){ $accounts = array(); if(!$api){ @@ -633,15 +633,21 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() $envato_username = $result['buyer']; // find this user in our system. $shub_user = new SupportHubUser_Envato(); - $shub_user->load_by_meta('envato_username', strtolower($envato_username)); + if($existing_shub_user_id){ + $shub_user->load($existing_shub_user_id); + }else{ + $shub_user->load_by_meta('envato_username', strtolower($envato_username)); + } if (!$shub_user->get('shub_user_id')) { // no users exists in our system with this username. // add a new one. $shub_user->create_new(); + } + if(!$shub_user->get('user_username')){ $shub_user->update('user_username', $envato_username); - $shub_user->add_meta('envato_username', strtolower($envato_username)); } + $shub_user->add_unique_meta('envato_username', strtolower($envato_username)); // find out which product this purchase code is relating to $existing_products = SupportHub::getInstance()->get_products(); // check if this item exists already diff --git a/extensions/ucm/class.shub_ucm_account.php b/extensions/ucm/class.shub_ucm_account.php index 7261d12..7fe87b0 100644 --- a/extensions/ucm/class.shub_ucm_account.php +++ b/extensions/ucm/class.shub_ucm_account.php @@ -107,13 +107,34 @@ public function get_api($use_db_code = true){ public function get_api_user_to_id($ucm_user_data){ //print_r($ucm_user_data);exit; $comment_user = new SupportHubUser_ucm(); + if(!empty($ucm_user_data['email'])){ + $comment_user->load_by( 'user_email', trim(strtolower($ucm_user_data['email']))); + } + if(!$comment_user->get('shub_user_id')){ + // didn't find one yet. + // find by envato username? + if(isset($ucm_user_data['envato']['user'])){ + $first = current($ucm_user_data['envato']['user']); + if($first && !empty($first['envato_username'])){ + if ($comment_user->load_by_meta('envato_username', strtolower($first['envato_username']))) { + // found! yay! + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Found a user based on envato username.',array( + 'username' => $first['envato_username'], + 'found_user_id' => $comment_user->get('shub_user_id'), + )); + } + } + } + } + + if(isset($ucm_user_data['envato']['purchases']) && is_array($ucm_user_data['envato']['purchases'])){ // find a matching user account with these purchases. foreach($ucm_user_data['envato']['purchases'] as $purchase){ if(!empty($purchase['license_code'])) { // pull in the license code using the envato module if it's enabled. if(isset(SupportHub::getInstance()->message_managers['envato'])) { - $result = SupportHub::getInstance()->message_managers['envato']->pull_purchase_code(false, $purchase['license_code'], array()); + $result = SupportHub::getInstance()->message_managers['envato']->pull_purchase_code(false, $purchase['license_code'], array(), $comment_user->get('shub_user_id')); if ($result && !empty($result['shub_user_id'])) { $comment_user->load($result['shub_user_id']); SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Found a user based on license code.',array( @@ -126,22 +147,7 @@ public function get_api_user_to_id($ucm_user_data){ } } } - if(!$comment_user->get('shub_user_id')){ - // didn't find one yet. - // find by envato username? - if(isset($ucm_user_data['envato']['user'])){ - $first = current($ucm_user_data['envato']['user']); - if($first && !empty($first['envato_username'])){ - if ($comment_user->load_by_meta('envato_username', strtolower($first['envato_username']))) { - // found! yay! - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO,'ucm','Found a user based on envato username.',array( - 'username' => $first['envato_username'], - 'found_user_id' => $comment_user->get('shub_user_id'), - )); - } - } - } - } + if(!$comment_user->get('shub_user_id')){ // find a match based on email. if(!empty($ucm_user_data['email'])){ diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index e561a35..e90b230 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -144,7 +144,7 @@ public function send_queued($debug = false){ $new_post_id = $matches[1]; $this->update('ucm_id',$new_post_id); // reload this message and messages from the graph api. - $this->load_by_ucm_id($this->get('ucm_id'),false,$this->get('type'),$debug, true); + $this->load_by_network_key($this->get('ucm_id'),false,$this->get('type'),$debug, true); }else{ echo 'Failed to send message. Error was: '.var_export($result,true); // remove from database. From e2df521715632defe930344f2637bf7543ba3d07 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 04:11:34 +1000 Subject: [PATCH 06/23] ucm + envato api fix --- extensions/envato/class.shub_envato_message.php | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/envato/class.shub_envato_message.php b/extensions/envato/class.shub_envato_message.php index 6cc1598..4ad8214 100644 --- a/extensions/envato/class.shub_envato_message.php +++ b/extensions/envato/class.shub_envato_message.php @@ -270,6 +270,7 @@ public function message_sidebar_data(){ } public function get_user_hints($user_hints = array()){ + $user_hints['shub_user_id'][] = $this->get('shub_user_id'); $comments = $this->get_comments(); $first_comment = current($comments); if(isset($first_comment['shub_user_id']) && $first_comment['shub_user_id']){ From 6a0490743ac874530ed0a959c54dd127b3c1060b Mon Sep 17 00:00:00 2001 From: dtbaker Date: Mon, 7 Sep 2015 04:15:07 +1000 Subject: [PATCH 07/23] ucm + envato api fix --- extensions/envato/class.shub_envato_message.php | 1 - extensions/ucm/class.shub_ucm_message.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/envato/class.shub_envato_message.php b/extensions/envato/class.shub_envato_message.php index 4ad8214..6cc1598 100644 --- a/extensions/envato/class.shub_envato_message.php +++ b/extensions/envato/class.shub_envato_message.php @@ -270,7 +270,6 @@ public function message_sidebar_data(){ } public function get_user_hints($user_hints = array()){ - $user_hints['shub_user_id'][] = $this->get('shub_user_id'); $comments = $this->get_comments(); $first_comment = current($comments); if(isset($first_comment['shub_user_id']) && $first_comment['shub_user_id']){ diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index e90b230..7e452ec 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -245,6 +245,7 @@ public function message_sidebar_data(){ } public function get_user_hints($user_hints = array()){ + $user_hints['shub_user_id'][] = $this->get('shub_user_id'); $comments = $this->get_comments(); $first_comment = current($comments); if(isset($first_comment['shub_user_id']) && $first_comment['shub_user_id']){ From 3daaca08e68a8f7e23d80d70cecf9ab18cb49a77 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Tue, 8 Sep 2015 20:59:26 +1000 Subject: [PATCH 08/23] better debugging --- assets/js/social.js | 79 +++++++++--- classes/class-support-hub-message.php | 6 +- classes/class-support-hub.php | 21 ++-- .../bbpress/class.shub_bbpress_message.php | 8 +- extensions/envato/class.envato-api.php | 3 + extensions/ucm/class.shub_ucm_item.php | 44 ++++--- extensions/ucm/class.shub_ucm_message.php | 119 +++++++++--------- extensions/ucm/class.ucm-api.php | 8 +- extensions/ucm/init.ucm.php | 2 +- 9 files changed, 180 insertions(+), 110 deletions(-) diff --git a/assets/js/social.js b/assets/js/social.js index 9f0658c..e488acb 100644 --- a/assets/js/social.js +++ b/assets/js/social.js @@ -131,7 +131,11 @@ ucm.social = { // add any additioal reply options to this. pt.find('[data-reply="yes"]').each(function(){ if(jQuery(this).attr('type') == 'checkbox'){ - post_data[jQuery(this).attr('name')] = this.checked ? jQuery(this).val() : false; + if(this.checked){ + post_data[jQuery(this).attr('name')] = jQuery(this).val(); + }else{ + // don't pass a 'false' into ajax, just send nothing. + } }else{ post_data[jQuery(this).attr('name')] = jQuery(this).val(); } @@ -165,7 +169,7 @@ ucm.social = { element_action.find('.action_content').html('Message Sent!'); }, function(){ // failed to send - element_action.find('.action_content').html('FAILED TO SEND MESSAGE'); + element_action.find('.action_content').html('Failed to send message. Please check logs.'); }); if(element.is('div')){ element.slideUp(); @@ -174,6 +178,8 @@ ucm.social = { element.hide(); element_action.show(); } + var pos = element_action.position(); + jQuery(window).scrollTop(pos.top-10); })(); }else{ // we are in popup. have to close modal and find this message on the page to see if we can slide it up. @@ -190,7 +196,7 @@ ucm.social = { element_action.find('.action_content').html('Message Sent!'); }, function(){ // failed to send - element_action.find('.action_content').html('FAILED TO SEND MESSAGE'); + element_action.find('.action_content').html('Failed to send message. Please check logs.'); }); if(element.is('div')){ element.slideUp(); @@ -199,6 +205,8 @@ ucm.social = { element.hide(); element_action.show(); } + var pos = element_action.position(); + jQuery(window).scrollTop(pos.top-10); })(); }else{ // cant find it on the screen, must have opened a related message. @@ -250,6 +258,40 @@ ucm.social = { $s.find('header,aside').show(); return false; }); + + ucm.social.set_inline_view(); + + }, + set_inline_view: function(){ + + // on page scroll we align the inline-sidebar with the viewport. + var inline_views = []; + jQuery('.shub_table_inline .shub_extension_message').each(function(){ + inline_views.push({ + pos: jQuery(this).position(), + height: jQuery(this).height(), + sidebar: jQuery(this).find('section.message_sidebar') + }); + }); + jQuery(window).scroll(function(){ + var currenttop = jQuery(window).scrollTop(); + // find out which elements we have to move into view. + for(var i = 0; i < inline_views.length; i++){ + if(inline_views[i].pos.top < currenttop && inline_views[i].pos.top + inline_views[i].height > currenttop){ + // calc here incase of ajax loading sidebar data + var sidebar_height = inline_views[i].sidebar.height(); + var from_top = Math.min(inline_views[i].height - sidebar_height - 20, currenttop - inline_views[i].pos.top + 20); + if(from_top > 10){ + inline_views[i].sidebar.css('padding-top',from_top+'px'); + }else{ + inline_views[i].sidebar.attr('style',''); + } + }else{ + inline_views[i].sidebar.attr('style',''); + } + } + }); + }, close_modal: function(){ tb_remove(); @@ -271,7 +313,7 @@ ucm.social = { add: function(shub_outbox_id, element, success_callback, fail_callback){ this.queue.push( { - shub_outbox_id: shub_outbox_id, + shub_outbox_id: parseInt(shub_outbox_id), element: element, success_callback: success_callback, fail_callback: fail_callback @@ -284,14 +326,6 @@ ucm.social = { var t = this; if(t.watching)return; t.watching = true; - // update the menu UI - var queue_length = 0; - for(var x = 0; x < t.queue.length; x++) { - if (typeof t.queue[x] != 'undefined') { - queue_length++; - } - } - jQuery('#shub_menu_outbox_count').text(queue_length); // (t.queue.length); //.parents('li').first().show(); var post_data = { action: 'support_hub_queue-watch', wp_nonce: support_hub.wp_nonce, @@ -303,6 +337,8 @@ ucm.social = { data: post_data, dataType: 'json', success: function(r){ + + for(var x = 0; x < t.queue.length; x++){ if(typeof t.queue[x] != 'undefined') { // find this shub_outbox_id in the queue response from server. @@ -310,10 +346,10 @@ ucm.social = { if (r && typeof r.outbox_ids != 'undefined') { for (var i in r.outbox_ids) { if (r.outbox_ids.hasOwnProperty(i) && typeof r.outbox_ids[i] != 'undefined') { - if (r.outbox_ids[i].shub_outbox_id && t.queue[x].shub_outbox_id == r.outbox_ids[i].shub_outbox_id) { + if (r.outbox_ids[i].shub_outbox_id && t.queue[x].shub_outbox_id == parseInt(r.outbox_ids[i].shub_outbox_id)) { found = true; // has it errored? - if (r.outbox_ids[i].status == 2) { + if (parseInt(r.outbox_ids[i].shub_status) == 2) { if (typeof t.queue[x].fail_callback == 'function') { t.queue[x].fail_callback(); delete(t.queue[x]); @@ -335,14 +371,23 @@ ucm.social = { } } var has_pending = false; + // update the menu UI + var queue_length = 0; if (r && typeof r.outbox_ids != 'undefined') { for (var i in r.outbox_ids) { - if (r.outbox_ids.hasOwnProperty(i) && typeof r.outbox_ids[i] != 'undefined' && typeof r.outbox_ids[i].status != 'undefined' && (parseInt(r.outbox_ids[i].status) == 0 || parseInt(r.outbox_ids[i].status) == 1)) { - // we have a pending queue to send! - has_pending = true; + if (r.outbox_ids.hasOwnProperty(i) && typeof r.outbox_ids[i] != 'undefined') { + if (typeof r.outbox_ids[i].shub_status != 'undefined' && (parseInt(r.outbox_ids[i].shub_status) == 0 || parseInt(r.outbox_ids[i].shub_status) == 1 || parseInt(r.outbox_ids[i].shub_status) == 2)) { + // new/pending/errored + queue_length++; + } + if (typeof r.outbox_ids[i].shub_status != 'undefined' && (parseInt(r.outbox_ids[i].shub_status) == 0 || parseInt(r.outbox_ids[i].shub_status) == 1)) { + // we have a pending queue to send! + has_pending = true; + } } } } + jQuery('#shub_menu_outbox_count').text(queue_length); // (t.queue.length); //.parents('li').first().show(); t.watching = false; if(has_pending) { diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index 9e71958..96d398f 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -82,7 +82,7 @@ public function load($shub_message_id = false){ /** * @param $field string the field name to return - * @return bool|string|int|array + * @return bool|string|int|array|shub_ucm_item */ public function get($field){ if(isset($this->{$field})){ @@ -279,7 +279,9 @@ public function queue_reply($network_key, $message, $debug = false, $extra_data 'message_text' => $message, 'user_id' => get_current_user_id(), )); - $this->update('shub_status',_shub_MESSAGE_STATUS_ANSWERED); + if(!empty($extra_data['archive'])){ + $this->update('shub_status',_shub_MESSAGE_STATUS_ANSWERED); + } if($debug){ echo "Successfully added comment with id $shub_message_comment_id
    \n"; } diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index e7bd8a1..bc9e1f1 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -196,7 +196,7 @@ public function admin_ajax(){ ); $message = isset( $_POST['message'] ) && $_POST['message'] ? $_POST['message'] : ''; $account_id = $_REQUEST['account-id']; - $debug = isset( $_POST['debug'] ) && (int)$_POST['debug'] > 0 ? $_POST['debug'] : false; + $debug = isset( $_POST['debug'] ) && (int)$_POST['debug'] > 0 ? true : false; if ( $message ) { // we have a message and a message manager. @@ -222,11 +222,6 @@ public function admin_ajax(){ $return['message'] .= 'Failed to queue comment reply in database.'; $return['error'] = true; } - if ($debug) { - $return['message'] .= ob_get_clean(); - } else { - //set_message( _l( 'message sent and conversation archived.' ) ); - } $outbox->update(array( 'shub_extension' => $_REQUEST['network'], @@ -234,7 +229,19 @@ public function admin_ajax(){ 'shub_message_id' => $_REQUEST['message-id'], 'shub_message_comment_id' => $message_comment_id, )); - $return['shub_outbox_id'] = $outbox->get('shub_outbox_id'); + + if ($debug) { + // send the message straight away and show any debug output + echo $outbox->send_queued(true); + $return['message'] .= ob_get_clean(); + // dont send an shub_outbox_id in debug mode + // this will keep the 'message' window open and not shrink it down so we can better display debug messages. + + } else { + //set_message( _l( 'message sent and conversation archived.' ) ); + $return['shub_outbox_id'] = $outbox->get('shub_outbox_id'); + } + } } if (!headers_sent())header('Content-type: text/javascript'); diff --git a/extensions/bbpress/class.shub_bbpress_message.php b/extensions/bbpress/class.shub_bbpress_message.php index 5a8c418..9880834 100644 --- a/extensions/bbpress/class.shub_bbpress_message.php +++ b/extensions/bbpress/class.shub_bbpress_message.php @@ -88,7 +88,7 @@ public function load_by_bbpress_id($bbpress_id, $topic_data, $type, $debug = fal // timestamp is handled in forum class $comments[$id]['content'] = $comment['post_content']; } - $this->update('title',$topic_data['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))); @@ -197,7 +197,6 @@ public function send_queued($debug = false){ return false; } - 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'])){ @@ -210,7 +209,7 @@ public function send_queued_comment_reply($bbpress_message_comment_id, $shub_out if($debug)echo "Sending a reply to bbPress Topic ID: $bbpress_id
    \n"; $outbox_data = $shub_outbox->get('message_data'); - if(isset($outbox_data['extra']) && is_array($outbox_data['extra'])){ + if($outbox_data && isset($outbox_data['extra']) && is_array($outbox_data['extra'])){ $extra_data = $outbox_data['extra']; }else{ $extra_data = array(); @@ -366,8 +365,9 @@ public function message_sidebar_data(){ ?>
    + get('title') );?>
    - get('account') ? $this->get('account')->get( 'account_name' ) : 'N/A' ); ?>
    + get('account') ? $this->get('account')->get( 'account_name' ) : 'N/A' ); ?>
    get('last_active'), true ); ?>
    diff --git a/extensions/envato/class.envato-api.php b/extensions/envato/class.envato-api.php index c9b277b..0d66179 100644 --- a/extensions/envato/class.envato-api.php +++ b/extensions/envato/class.envato-api.php @@ -59,6 +59,9 @@ public function api($endpoint, $params=array(), $personal = true){ SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'API Call: '.$endpoint,$response['body']); $header = $response['headers']; $body = @json_decode($response['body'],true); + if(!$body){ + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'envato', 'API Error, unable to JSON decode: '.$endpoint. ' '.(isset($response['response']['code']) ? $response['response']['code'] .' / ': '').(isset($response['body']) ? $response['body'] : '')); + } return $body; }else if(is_array($response) && isset($response['response']['code']) && $response['response']['code']){ SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'envato', 'API Error: '.$endpoint. ' '.(isset($response['response']['code']) ? $response['response']['code'] .' / ': '').(isset($response['body']) ? $response['body'] : ''), $response); diff --git a/extensions/ucm/class.shub_ucm_item.php b/extensions/ucm/class.shub_ucm_item.php index 05947c8..7da851e 100644 --- a/extensions/ucm/class.shub_ucm_item.php +++ b/extensions/ucm/class.shub_ucm_item.php @@ -51,23 +51,33 @@ public function load_latest_item_data($debug = false){ $newest_message_received = 0; $count = 0; - foreach($tickets['tickets'] as $ticket){ - $message_time = $ticket['last_message_timestamp']; - $newest_message_received = max($newest_message_received,$message_time); - //if($message_time <= $last_message_received)break; // all done here. - - $ucm_message = new shub_ucm_message($this->account, $this, false); - $ucm_message -> load_by_network_key($ticket['ticket_id'], $ticket, 'ticket', $debug); - $count++; - if($debug) { - ?> -
    -
     Imported Ticket ID: get( 'network_key' ); ?> with  message. 
    -
    - account->save_account_data(array( + 'reply_options' => $tickets['reply_options'] + )); + } + if(isset($tickets['tickets'])) { + foreach ($tickets['tickets'] as $ticket) { + $message_time = $ticket['last_message_timestamp']; + $newest_message_received = max($newest_message_received, $message_time); + //if($message_time <= $last_message_received)break; // all done here. + + $ucm_message = new shub_ucm_message($this->account, $this, false); + $ucm_message->load_by_network_key($ticket['ticket_id'], $ticket, 'ticket', $debug); + $count++; + if ($debug) { + ?> +
    +
     Imported Ticket ID: get('network_key'); ?>
    +                            with  message. 
    +
    + log_data(_SUPPORT_HUB_LOG_ERROR,'ucm','Failed to get a reply from the API for product '.$ucm_product_id.' "',$tickets); + } // get user, return envato_codes in meta SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'Imported '.$count.' product tickets into database (from a total of '.count($tickets['tickets']).' returned by the api)'); if($debug)echo " imported $count new product tickets
    "; diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index 7e452ec..874365c 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -4,7 +4,6 @@ class shub_ucm_message extends SupportHub_message{ protected $network = 'ucm'; - public function load_by_network_key($network_key, $ticket, $type, $debug = false){ switch($type){ @@ -89,10 +88,65 @@ public function load_by_network_key($network_key, $ticket, $type, $debug = false } 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 reply_actions(){ + $user_data = $this->account->get('account_data'); + if(isset($user_data['reply_options']) && is_array($user_data['reply_options'])){ + foreach($user_data['reply_options'] as $reply_option){ + if(isset($reply_option['title'])){ + echo '
    '; + echo ''; + if(isset($reply_option['field']) && is_array($reply_option['field'])){ + $reply_option['field']['name'] = 'extra-'.$reply_option['field']['name']; + $reply_option['field']['data'] = array( + 'reply' => 'yes' + ); + shub_module_form::generate_form_element($reply_option['field']); + } + echo '
    '; + } + } + } + } + public function get_link() { $item = $this->get('item'); $url = '#'; @@ -104,69 +158,12 @@ public function get_link() { } if($item){ $url .= '&faq_product_id='.$item->get('network_key'); + $url .= '&ticket_id='.$this->get('network_key'); } } return $url; } - - public function send_queued($debug = false){ - if($this->account && $this->shub_message_id) { - // send this message out to ucm. - // this is run when user is composing a new message from the UI, - if ( $this->get( 'shub_status' ) == _shub_MESSAGE_STATUS_SENDING ) - return; // dont double up on cron. - - - switch($this->get('type')){ - case 'product_post': - - - if(!$this->item) { - echo 'No ucm product defined'; - return false; - } - - $this->update( 'shub_status', _shub_MESSAGE_STATUS_SENDING ); - $api = $this->account->get_api(); - $item_id = $this->item->get('product_id'); - if($debug)echo "Sending a new message to ucm product ID: $item_id
    \n"; - $result = false; - $post_data = array(); - $post_data['summary'] = $this->get('summary'); - $post_data['title'] = $this->get('title'); - $now = time(); - $send_time = $this->get('last_active'); - $result = $api->api('v1/products/'.$item_id.'/posts',array(),'POST',$post_data,'location'); - if($debug)echo "API Post Result:
    \n".var_export($result,true)."
    \n"; - if($result && preg_match('#https://api.ucm.com/v1/posts/(.*)$#',$result,$matches)){ - // we have a result from the API! this should be an API call in itself: - $new_post_id = $matches[1]; - $this->update('ucm_id',$new_post_id); - // reload this message and messages from the graph api. - $this->load_by_network_key($this->get('ucm_id'),false,$this->get('type'),$debug, true); - }else{ - echo 'Failed to send message. Error was: '.var_export($result,true); - // remove from database. - $this->delete(); - return false; - } - - // successfully sent, mark is as answered. - $this->update( 'shub_status', _shub_MESSAGE_STATUS_ANSWERED ); - return true; - - break; - default: - if($debug)echo "Unknown post type: ".$this->get('type'); - } - - } - return false; - } - - - public function get_type_pretty() { $type = $this->get('type'); switch($type){ @@ -211,6 +208,7 @@ public function message_sidebar_data(){ $shub_product->load( $shub_product_id ); $product_data = $shub_product->get( 'product_data' ); } + //echo SupportHub::getInstance()->message_managers['ucm']->get_friendly_icon(); ?>
    + get('title') );?>
    get('account') ? $this->get('account')->get( 'account_name' ) : 'N/A' ); ?>
    get('last_active'), true ); ?>
    diff --git a/extensions/ucm/class.ucm-api.php b/extensions/ucm/class.ucm-api.php index a55d9a2..9a8d175 100644 --- a/extensions/ucm/class.ucm-api.php +++ b/extensions/ucm/class.ucm-api.php @@ -23,6 +23,7 @@ public function set_api_key($token){ public function api($endpoint, $method=false, $params=array()){ $headers = array( 'user-agent' => 'SupportHub WP Plugin', + 'timeout' => 20, ); //$headers['headers'] = array('Authorization' => $this->_api_key,); $params['auth'] = $this->_api_key; @@ -36,12 +37,15 @@ public function api($endpoint, $method=false, $params=array()){ SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'ucm', 'API Call: '.$endpoint .'/' .$method,$response['body']); $header = $response['headers']; $body = @json_decode($response['body'],true); + if(!$body){ + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'API Error, unable to JSON decode: '.$endpoint. ' '.(isset($response['response']['code']) ? $response['response']['code'] .' / ': '').(isset($response['body']) ? $response['body'] : '')); + } return $body; }else if(is_array($response) && isset($response['response']['code']) && $response['response']['code']){ SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'API Error: '.$endpoint. ' '.(isset($response['response']['code']) ? $response['response']['code'] .' / ': '').(isset($response['body']) ? $response['body'] : ''), $response); }else if(is_wp_error($response)){ - SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'API Error: '.$endpoint. ' '.$response->get_error_message()); - } + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR, 'ucm', 'API Error: '.$endpoint. ' '.$response->get_error_message(),$response); + } return false; } diff --git a/extensions/ucm/init.ucm.php b/extensions/ucm/init.ucm.php index 8c4dc3a..d235b9a 100644 --- a/extensions/ucm/init.ucm.php +++ b/extensions/ucm/init.ucm.php @@ -16,7 +16,7 @@ function shub_managers_ucm( $shub ){ } $shub['ucm']->id = 'ucm'; $shub['ucm']->friendly_name = 'UCM'; - $shub['ucm']->desc = 'View and Reply to Ultimate Client Manager Support Tickets.'; + $shub['ucm']->desc = 'View and Reply to Ultimate Client Manager Email Support Tickets.'; return $shub; } From 773f4cfd1ae1c4b74f2994b7a323334439d23903 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Tue, 8 Sep 2015 21:34:06 +1000 Subject: [PATCH 09/23] envato store failed API keys --- classes/class-support-hub.php | 27 ++++++++++++++++++++- extensions/envato/class.shub_envato.php | 31 +++++++++++++++++++++++++ extensions/ucm/class.shub_ucm.php | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index bc9e1f1..52ff82d 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -988,6 +988,8 @@ public function message_user_summary($user_hints, $current_extension, $message_o } } + // for debugging: + $user_bits[] = array('shub_user_id',$user->get('shub_user_id')); if (!empty($user->details['user_fname'])) { $user_bits[] = array('FName', esc_html($user->details['user_fname'])); @@ -1010,10 +1012,13 @@ public function message_user_summary($user_hints, $current_extension, $message_o if (!empty($user->details['user_email'])) { $user_bits[] = array('Email', '' . esc_html($user->details['user_email']) . ''); } - // todo - group user output together nicely (e.g. Name ) so it looks better when there are multiple linked user accounts } } } + + + //var_export($this->message_managers['envato']->pull_purchase_code(false,'61782ac6-1f67-4302-b34a-17c9e4d4f123',array(),3452)); echo 'done';exit; + if(isset($user_details['url']) && isset($user_details['username'])){ if(!is_array($user_details['url']))$user_details['url'] = array($user_details['url']); if(!is_array($user_details['username']))$user_details['username'] = array($user_details['username']); @@ -1037,6 +1042,26 @@ public function message_user_summary($user_hints, $current_extension, $message_o } */ $user_bits = apply_filters('supporthub_message_user_sidebar', $user_bits, $user_hints['shub_user_id']); + + // todo - group user output together nicely (e.g. Name ) so it looks better when there are multiple linked user accounts + foreach($user_bits as $key=>$val){ + // check if this one exists yet? + foreach($user_bits as $key_check=>$val_check){ + if($key != $key_check && $val[0] == $val_check[0]){ + // we're matching something like 'Username' with 'Username' + if(trim(strip_tags($val[1])) == trim(strip_tags($val_check[1]))){ + // same same! keep one, the longer one (which might contian a link) + if(strlen($val[1]) > strlen($val_check[1])){ + unset($user_bits[$key_check]); + }else{ + unset($user_bits[$key]); + } + } + } + } + } + + ?>
      diff --git a/extensions/envato/class.shub_envato.php b/extensions/envato/class.shub_envato.php index e7878aa..63acbe3 100644 --- a/extensions/envato/class.shub_envato.php +++ b/extensions/envato/class.shub_envato.php @@ -572,6 +572,11 @@ public function extra_process_login($network, $account_id, $message_id, $extra_i public function pull_purchase_code($api, $purchase_code, $api_raw_data = array(), $existing_shub_user_id = false){ + $purchase_code = strtolower(preg_replace('#([a-z0-9]{8})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{12})#','$1-$2-$3-$4-$5',$purchase_code)); + // todo: add documentation that it needs to be named "Purchase Code" + // todo: add a default extra value called Purchase Code. + if(strlen($purchase_code)!=36)return false; + $accounts = array(); if(!$api){ // loop through accounts and try an API call on each one. @@ -711,6 +716,8 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() // if (empty($existing_purchase['shub_user_id'])) { shub_update_insert('shub_envato_purchase_id', $shub_envato_purchase_id, 'shub_envato_purchase', array( 'shub_user_id' => $shub_user->get('shub_user_id'), + 'shub_product_id' => $newproduct->get('shub_product_id'), + 'purchase_time' => strtotime($result['sold_at']), )); // } } @@ -776,6 +783,23 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() return $result; } }while(count($accounts)); + + // if we get here it means the API request failed or it's an invalid purchase code. + // log it in our database so we can at least re-check it at a later point in time or show it as failed in the UI somehow. + + $existing_purchase = shub_get_single('shub_envato_purchase', 'purchase_code', $purchase_code); + if (!$existing_purchase) { + $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array( + 'shub_user_id' => $existing_shub_user_id, // we update this later on when we get a correct reply from API + 'shub_product_id' => 0, // update later on when product is correct + 'envato_user_id' => 0, + 'api_type' => 'fail', + 'purchase_time' => 0, + 'purchase_code' => $purchase_code, + 'purchase_data' => '', + )); + } + return false; } @@ -913,6 +937,13 @@ public function filter_message_user_sidebar($user_bits, $shub_user_ids){ $total += $sale_data['amount']; } } + }else{ + // failed API lookup, show purchase code instead. + // todo: a re-lookup button. + $user_bits[] = array( + 'Failed Purchase Code', + $purchase['purchase_code'] + ); } } if($total){ diff --git a/extensions/ucm/class.shub_ucm.php b/extensions/ucm/class.shub_ucm.php index 36b8e22..250d92a 100644 --- a/extensions/ucm/class.shub_ucm.php +++ b/extensions/ucm/class.shub_ucm.php @@ -245,7 +245,7 @@ public function run_cron( $debug = false ){ $shub_ucm_account = new shub_ucm_account( $account['shub_account_id'] ); $shub_ucm_account->run_cron($debug); $products = $shub_ucm_account->get('items'); - /* @var $products shub_ucm_product[] */ + /* @var $products shub_ucm_item[] */ foreach($products as $product){ $product->run_cron($debug); } From 588716556dafeeaeb9d92d393ad7876cad61d06c Mon Sep 17 00:00:00 2001 From: dtbaker Date: Tue, 8 Sep 2015 22:31:42 +1000 Subject: [PATCH 10/23] save order --- pages/inbox.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pages/inbox.php b/pages/inbox.php index 5459836..682b70f 100644 --- a/pages/inbox.php +++ b/pages/inbox.php @@ -5,8 +5,17 @@ Date: Tue, 8 Sep 2015 22:32:07 +1000 Subject: [PATCH 11/23] save order --- pages/inbox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/inbox.php b/pages/inbox.php index 682b70f..a0e82c0 100644 --- a/pages/inbox.php +++ b/pages/inbox.php @@ -9,7 +9,7 @@ $save_search = array('layout_type','orderquery'); foreach($save_search as $save){ if(isset($_REQUEST[$save])){ - $_SESSION['shub_save_'.$save] = $_REQUEST['save']; + $_SESSION['shub_save_'.$save] = $_REQUEST[$save]; }else if(isset($_SESSION['shub_save_'.$save])){ $_REQUEST[$save] = $_SESSION['shub_save_'.$save]; // please don't stab me. } From aa72947ce0f3e02b65a12eb4437e5a2a1c97e5c7 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Tue, 8 Sep 2015 22:43:31 +1000 Subject: [PATCH 12/23] menu count update on sending --- assets/js/social.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/assets/js/social.js b/assets/js/social.js index e488acb..50f3e62 100644 --- a/assets/js/social.js +++ b/assets/js/social.js @@ -319,6 +319,11 @@ ucm.social = { fail_callback: fail_callback } ); + var menu_count = jQuery('#shub_menu_outbox_count'); + if(menu_count.get(0)){ + var new_count = parseInt(menu_count.data('count')) + 1; + jQuery('#shub_menu_outbox_count').data('count',new_count).text(new_count); + } this.watch(); }, watching: false, From a5ce2ba5d8b2c38dacae08a815d2ab8bc34e3d73 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 09:52:46 +1000 Subject: [PATCH 13/23] sort by time --- classes/class-support-hub-message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index 96d398f..93138a7 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -302,7 +302,7 @@ public function get_comments($message_data = false) { return strtotime($a['created_at']) > strtotime($b['created_at']); }); }else{ - $messages = shub_get_multiple('shub_message_comment',array('shub_message_id'=>$this->shub_message_id),'shub_message_comment_id','shub_message_comment_id'); //@json_decode($this->get('comments'),true); + $messages = shub_get_multiple('shub_message_comment',array('shub_message_id'=>$this->shub_message_id),'shub_message_comment_id','time'); //@json_decode($this->get('comments'),true); } return $messages; } From 9e0ccd35e39a9dd266240b68ed70780ed3111424 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 10:14:41 +1000 Subject: [PATCH 14/23] cron debug --- classes/class-support-hub.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index 52ff82d..dace491 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -80,6 +80,10 @@ public function shub_init(){ foreach($this->message_managers as $name => $message_manager){ $message_manager->init(); } + + if(isset($_REQUEST['debug_shub_cron'])){ + $this->cron_run(true); + } } public function register_session(){ @@ -771,7 +775,9 @@ function cron_run( $debug = false ){ 'time' => time(), )); $this->log_data(0, 'cron', 'Starting Extension Cron: ' . $name); - $message_manager->run_cron($debug, $cron_timeout, $cron_start); + if(!isset($_REQUEST['debug_shub_cron'])){ + $message_manager->run_cron($debug, $cron_timeout, $cron_start); + } // this cron job has completed successfully. // if we've been running more than timeout, quit. if ($cron_start + $cron_timeout < time()) { From 4cd8bb58f93df51814db79f5e4a2456e43eba3be Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 10:26:47 +1000 Subject: [PATCH 15/23] envato API purchase code cache --- classes/class-support-hub.php | 3 +++ extensions/envato/class.shub_envato.php | 29 +++++++++++++++++++++++++ extensions/ucm/class.shub_ucm.php | 2 ++ 3 files changed, 34 insertions(+) diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index dace491..bdc5ce0 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -83,6 +83,7 @@ public function shub_init(){ if(isset($_REQUEST['debug_shub_cron'])){ $this->cron_run(true); + exit; } } @@ -777,6 +778,8 @@ function cron_run( $debug = false ){ $this->log_data(0, 'cron', 'Starting Extension Cron: ' . $name); if(!isset($_REQUEST['debug_shub_cron'])){ $message_manager->run_cron($debug, $cron_timeout, $cron_start); + }else{ + echo 'Starting Extension Cron: ' . $name."
      \n"; } // this cron job has completed successfully. // if we've been running more than timeout, quit. diff --git a/extensions/envato/class.shub_envato.php b/extensions/envato/class.shub_envato.php index 63acbe3..f9a36d0 100644 --- a/extensions/envato/class.shub_envato.php +++ b/extensions/envato/class.shub_envato.php @@ -577,6 +577,26 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() // todo: add a default extra value called Purchase Code. if(strlen($purchase_code)!=36)return false; + // check existing purchase code and return that if it's cached + $existing_purchase = shub_get_single('shub_envato_purchase', 'purchase_code', $purchase_code); + if ($existing_purchase && $existing_purchase['shub_envato_purchase_id']) { + // if there is a matching support id, grab that cached support data, add shub_user_id do it and return it + // this matches our return data below. + // if we don't find this then we continue below and call the API again to get fresh data. + $existing_support = shub_get_single('shub_envato_support', array( + 'shub_envato_purchase_id', + ), array( + $existing_purchase['shub_envato_purchase_id'], + )); + if($existing_support && $existing_support['shub_user_id'] && $existing_support['support_data']){ + $return = @json_decode($existing_support['support_data'],true); + if($return){ + $return['shub_user_id'] = $existing_support['shub_user_id']; + return $return; + } + } + } + $accounts = array(); if(!$api){ // loop through accounts and try an API call on each one. @@ -707,7 +727,14 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() // add it in! $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', $possible_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array( 'purchase_code' => $purchase_code, + 'api_time' => time(), )); + if (empty($possible_purchases['purchase_data'])) { + $raw_purchase_data = array_merge(array('author/sale' => $result), is_array($api_raw_data) ? $api_raw_data : array()); + $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', $possible_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array( + 'purchase_data' => json_encode($raw_purchase_data), + )); + } } } } else { @@ -728,6 +755,7 @@ public function pull_purchase_code($api, $purchase_code, $api_raw_data = array() 'shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'envato_user_id' => 0, + 'api_time' => time(), 'api_type' => 'author/sale', 'purchase_time' => strtotime($result['sold_at']), 'purchase_code' => $purchase_code, @@ -983,6 +1011,7 @@ public function get_install_sql() { shub_user_id int(11) NOT NULL, shub_product_id int(11) NOT NULL, envato_user_id int(11) NOT NULL, + api_time int(11) NOT NULL DEFAULT '0', purchase_time int(11) NOT NULL, api_type varchar(20) NOT NULL DEFAULT '', purchase_code varchar(255) NOT NULL DEFAULT '', diff --git a/extensions/ucm/class.shub_ucm.php b/extensions/ucm/class.shub_ucm.php index 250d92a..3776b73 100644 --- a/extensions/ucm/class.shub_ucm.php +++ b/extensions/ucm/class.shub_ucm.php @@ -242,11 +242,13 @@ public function run_cron( $debug = false ){ if($debug)echo "Starting ucm Cron Job \n"; $accounts = $this->get_accounts(); foreach($accounts as $account){ + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'Running Cron Job On Account: ' . $account['shub_account_id']); $shub_ucm_account = new shub_ucm_account( $account['shub_account_id'] ); $shub_ucm_account->run_cron($debug); $products = $shub_ucm_account->get('items'); /* @var $products shub_ucm_item[] */ foreach($products as $product){ + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'Running Cron Job On Item: ' . $product->get('shub_item_id')); $product->run_cron($debug); } } From 141bdd490a1d507d147ee0670754ec46050379da Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 12:24:10 +1000 Subject: [PATCH 16/23] ucm reply to fixes + product url --- assets/js/social.js | 2 +- extensions/bbpress/class.shub_bbpress_message.php | 6 ++++++ extensions/envato/class.shub_envato.php | 6 ++++++ extensions/envato/class.shub_envato_message.php | 6 ++++++ extensions/ucm/class.shub_ucm_message.php | 15 +++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/assets/js/social.js b/assets/js/social.js index 50f3e62..28fbecf 100644 --- a/assets/js/social.js +++ b/assets/js/social.js @@ -179,7 +179,7 @@ ucm.social = { element_action.show(); } var pos = element_action.position(); - jQuery(window).scrollTop(pos.top-10); + if(jQuery(window).scrollTop()>pos.top-10)jQuery(window).scrollTop(pos.top-10); })(); }else{ // we are in popup. have to close modal and find this message on the page to see if we can slide it up. diff --git a/extensions/bbpress/class.shub_bbpress_message.php b/extensions/bbpress/class.shub_bbpress_message.php index 9880834..deee370 100644 --- a/extensions/bbpress/class.shub_bbpress_message.php +++ b/extensions/bbpress/class.shub_bbpress_message.php @@ -358,9 +358,15 @@ public function message_sidebar_data(){ '; + } ?> '; + } } ?>
      diff --git a/extensions/envato/class.shub_envato.php b/extensions/envato/class.shub_envato.php index f9a36d0..ab911cd 100644 --- a/extensions/envato/class.shub_envato.php +++ b/extensions/envato/class.shub_envato.php @@ -964,6 +964,12 @@ public function filter_message_user_sidebar($user_bits, $shub_user_ids){ 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. diff --git a/extensions/envato/class.shub_envato_message.php b/extensions/envato/class.shub_envato_message.php index 6cc1598..75d50a6 100644 --- a/extensions/envato/class.shub_envato_message.php +++ b/extensions/envato/class.shub_envato_message.php @@ -240,9 +240,15 @@ public function message_sidebar_data(){ '; + } ?> '; + } } ?>
      diff --git a/extensions/ucm/class.shub_ucm_message.php b/extensions/ucm/class.shub_ucm_message.php index 874365c..991e4a7 100644 --- a/extensions/ucm/class.shub_ucm_message.php +++ b/extensions/ucm/class.shub_ucm_message.php @@ -19,6 +19,9 @@ public function load_by_network_key($network_key, $ticket, $type, $debug = false $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){ @@ -49,6 +52,7 @@ public function load_by_network_key($network_key, $ticket, $type, $debug = false $this->update('shub_data', $ticket); $this->update('shub_link', $ticket['url']); $this->update('network_key', $network_key); + SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_ERROR,'ucm','Updateing ticket '.$network_key.' with status '.$this->get('shub_status').' and existing comment count '.count($this->get_comments()).' and new comment count '.count($comments)); 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 @@ -213,9 +217,15 @@ public function message_sidebar_data(){ '; + } ?> '; + } } ?>
      @@ -257,6 +267,11 @@ public function get_user($shub_user_id){ return new SupportHubUser_ucm($shub_user_id); } public function get_reply_user(){ + // each message can have a different assigned staff member in the ucm database, this is stored in the initial API scrape of a ticket + $ticket_data = $this->get('shub_data'); + if(!empty($ticket_data['reply_from_shub_user_id'])){ + return new SupportHubUser_ucm($ticket_data['reply_from_shub_user_id']); + } return new SupportHubUser_ucm($this->account->get('shub_user_id')); } From 3e0ac73faab9edba6e54f7fed9d597c8e041d8d4 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 13:38:08 +1000 Subject: [PATCH 17/23] user images fix cross network --- classes/class-support-hub-user.php | 30 +++++++++++++++++-- .../bbpress/class.shub_bbpress_user.php | 15 +++------- extensions/envato/class.shub_envato_user.php | 17 ++++------- extensions/ucm/class.shub_ucm_user.php | 3 ++ 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/classes/class-support-hub-user.php b/classes/class-support-hub-user.php index 44098ca..8bd9c98 100644 --- a/classes/class-support-hub-user.php +++ b/classes/class-support-hub-user.php @@ -199,10 +199,34 @@ public function delete(){ public function get_link(){ return '#'; } - public function get_full_link(){ - return ''; - } + public function get_full_link(){ + $data = $this->get('user_data'); + $return = ''; + // this is for UCM: + if(!empty($data['name']) && !empty($data['last_name'])){ + $return .= htmlspecialchars($data['name'] .' '.$data['last_name'] .' '); + } + // this is for Envato + if(!empty($data['envato']['username'])){ + $return .= ''; + $return .= htmlspecialchars($data['envato']['username']); + $return .= ' '; + } + // this is fallback + if(!strlen($return)){ + $return .= ''; + $return .= htmlspecialchars($this->get('user_username')); + $return .= ' '; + } + return $return; + } + public function get_image(){ + + $data = $this->get('user_data'); + if($data && !empty($data['image'])){ + return $data['image']; + } if($this->get('user_email')){ $hash = md5(trim($this->get('user_email'))); return '//www.gravatar.com/avatar/'.$hash.'?d=wavatar'; diff --git a/extensions/bbpress/class.shub_bbpress_user.php b/extensions/bbpress/class.shub_bbpress_user.php index 61d26e7..e0d975d 100644 --- a/extensions/bbpress/class.shub_bbpress_user.php +++ b/extensions/bbpress/class.shub_bbpress_user.php @@ -4,19 +4,12 @@ class SupportHubUser_bbPress extends SupportHubUser{ public function get_image(){ - if($this->get('user_email')){ - $hash = md5(trim($this->get('user_email'))); - return '//www.gravatar.com/avatar/'.$hash.'?d=wavatar'; + $default = parent::get_image(); + if(!$default){ + return plugins_url('extensions/bbpress/default-user.jpg',_DTBAKER_SUPPORT_HUB_CORE_FILE_); } - return plugins_url('extensions/bbpress/default-user.jpg',_DTBAKER_SUPPORT_HUB_CORE_FILE_); + return $default; } - public function get_full_link(){ - $return = ''; - $return .= ''; - $return .= htmlspecialchars($this->get('user_username')); - $return .= ''; - return $return; - } } \ No newline at end of file diff --git a/extensions/envato/class.shub_envato_user.php b/extensions/envato/class.shub_envato_user.php index 5fb26f9..5910b61 100644 --- a/extensions/envato/class.shub_envato_user.php +++ b/extensions/envato/class.shub_envato_user.php @@ -52,26 +52,19 @@ public function get_image(){ if(!empty($data['image'])){ return $data['image']; } - if($this->get('user_email')){ - $hash = md5(trim($this->get('user_email'))); - return '//www.gravatar.com/avatar/'.$hash.'?d=wavatar'; + $default = parent::get_image(); + if(!$default){ + return plugins_url('extensions/envato/default-user.jpg',_DTBAKER_SUPPORT_HUB_CORE_FILE_); } - return plugins_url('extensions/envato/default-user.jpg',_DTBAKER_SUPPORT_HUB_CORE_FILE_); + return $default; } public function get_full_link(){ $data = $this->get('user_data'); if(empty($data)){ $this->refresh_data_from_api(); - $data = $this->get('user_data'); - } - $return = ''; - if(!empty($data['envato']['username'])){ - $return .= ''; - $return .= htmlspecialchars($data['envato']['username']); - $return .= ''; } - return $return; + return parent::get_full_link(); } public function get_link(){ diff --git a/extensions/ucm/class.shub_ucm_user.php b/extensions/ucm/class.shub_ucm_user.php index e01460e..2c03b63 100644 --- a/extensions/ucm/class.shub_ucm_user.php +++ b/extensions/ucm/class.shub_ucm_user.php @@ -2,4 +2,7 @@ class SupportHubUser_ucm extends SupportHubUser{ + + + } \ No newline at end of file From 8b43052007ab00c79154f0f1ea7b9e275d39e433 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 13:52:43 +1000 Subject: [PATCH 18/23] archive after activity check --- classes/class-support-hub-message.php | 8 +- classes/class-support-hub.php | 144 +++++++++++++++----------- 2 files changed, 89 insertions(+), 63 deletions(-) diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index 93138a7..d6cafb9 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -357,12 +357,14 @@ public function output_message_page($type='inline'){ data-action="set-unanswered" data-post=" $this->network, 'shub_message_id' => $message_id, + 'last_activity' => $this->get('last_active'), )));?>"> @@ -374,12 +376,14 @@ public function output_message_page($type='inline'){ data-action="set-unanswered" data-post=" $this->network, 'shub_message_id' => $message_id, + 'last_activity' => $this->get('last_active'), )));?>"> @@ -496,8 +500,9 @@ public function output_message_list( $allow_reply = true ){ echo 'Unsupported '; } if(!empty($buyer_status['presale'])){ - echo 'Pre-sale '; + //echo 'Pre-sale '; } + // todo - add a badge for staff reply. ?> @@ -550,6 +555,7 @@ public function output_message_list( $allow_reply = true ){ 'account-id' => $this->get('shub_account_id'), 'message-id' => $message_id, 'network' => $this->network, + 'last_activity' => $this->get('last_active'), ))); ?>" class="btn button shub_send_message_reply_button shub_hide_when_no_message">
    diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index bdc5ce0..37e49e6 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -130,26 +130,39 @@ public function admin_ajax(){ if(isset($_REQUEST['network']) && isset($this->message_managers[$_REQUEST['network']]) && !empty($_REQUEST['shub_message_id'])) { $shub_extension_message = $this->message_managers[$_REQUEST['network']]->get_message(false, false, $_REQUEST['shub_message_id']); if ($shub_extension_message->get('shub_message_id') == $_REQUEST['shub_message_id']) { - $shub_extension_message->update('shub_status',_shub_MESSAGE_STATUS_ANSWERED); - if (!headers_sent())header('Content-type: text/javascript'); + if (!headers_sent()) header('Content-type: text/javascript'); // we hide the element and provide an 'undo' placeholder in its place. // if it's a row we just hide it, if it's a div we slide it up nicely. - ?> - var element = jQuery('.shub_extension_message[data-network=][data-message-id=]'); - var element_action = element.prev('.shub_extension_message_action').first(); - element_action.find('.action_content').html('Message Archived. Undo'); - if(element.is('div')){ - element.slideUp(); - element_action.slideDown(); - }else{ - element.hide(); - element_action.show(); + + if (isset($_REQUEST['last_active']) && $_REQUEST['last_active'] != $shub_extension_message->get('last_active')) { + // a new message was received without updating the page. + // todo: ajax the shit out of live message updates instead of waiting for action. + // todo: do this check on bulk actions as well. + ?> + alert('There is an update to this message. Please refresh the page to see.'); + update('shub_status', _shub_MESSAGE_STATUS_ANSWERED); + ?> + var element = jQuery('.shub_extension_message[data-network=][data-message-id=]'); + var element_action = element.prev('.shub_extension_message_action').first(); + element_action.find('.action_content').html('Message Archived. Undo'); + if(element.is('div')){ + element.slideUp(); + element_action.slideDown(); + }else{ + element.hide(); + element_action.show(); + } + element_action.data('undo-type','answered'); + false, 'shub_outbox_id' => false, ); - $message = isset( $_POST['message'] ) && $_POST['message'] ? $_POST['message'] : ''; - $account_id = $_REQUEST['account-id']; - $debug = isset( $_POST['debug'] ) && (int)$_POST['debug'] > 0 ? true : false; - if ( $message ) { - - // we have a message and a message manager. - // time to queue this baby into the outbox and send it swimming - // what the hell did I just write? I need sleep! - - $outbox = new SupportHubOutbox(); - $outbox->create_new(); - if($outbox->get('shub_outbox_id')) { - if ($debug) ob_start(); - $extra_data = array(); - foreach ($_POST as $key => $val) { - if (strpos($key, 'extra-') !== false) { - $extra_data[substr($key, 6)] = $val; + if (isset($_REQUEST['last_active']) && $_REQUEST['last_active'] != $shub_extension_message->get('last_active')) { + $return['error'] = true; + $return['message'] = 'There is an update to this message. Please refresh the page to see.'; + }else { + + $message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : ''; + $account_id = $_REQUEST['account-id']; + $debug = isset($_POST['debug']) && (int)$_POST['debug'] > 0 ? true : false; + if ($message) { + + // we have a message and a message manager. + // time to queue this baby into the outbox and send it swimming + // what the hell did I just write? I need sleep! + + $outbox = new SupportHubOutbox(); + $outbox->create_new(); + if ($outbox->get('shub_outbox_id')) { + if ($debug) ob_start(); + $extra_data = array(); + foreach ($_POST as $key => $val) { + if (strpos($key, 'extra-') !== false) { + $extra_data[substr($key, 6)] = $val; + } + } + $outbox->update_outbox_data(array( + 'debug' => $debug, + 'extra' => $extra_data, + )); + $message_comment_id = $shub_extension_message->queue_reply($account_id, $message, $debug, $extra_data, $outbox->get('shub_outbox_id')); + if (!$message_comment_id) { + $return['message'] .= 'Failed to queue comment reply in database.'; + $return['error'] = true; } - } - $outbox->update_outbox_data(array( - 'debug' => $debug, - 'extra' => $extra_data, - )); - $message_comment_id = $shub_extension_message->queue_reply($account_id, $message, $debug, $extra_data, $outbox->get('shub_outbox_id')); - if(!$message_comment_id){ - $return['message'] .= 'Failed to queue comment reply in database.'; - $return['error'] = true; - } - $outbox->update(array( - 'shub_extension' => $_REQUEST['network'], - 'shub_account_id' => $account_id, - 'shub_message_id' => $_REQUEST['message-id'], - 'shub_message_comment_id' => $message_comment_id, - )); - - if ($debug) { - // send the message straight away and show any debug output - echo $outbox->send_queued(true); - $return['message'] .= ob_get_clean(); - // dont send an shub_outbox_id in debug mode - // this will keep the 'message' window open and not shrink it down so we can better display debug messages. - - } else { - //set_message( _l( 'message sent and conversation archived.' ) ); - $return['shub_outbox_id'] = $outbox->get('shub_outbox_id'); - } + $outbox->update(array( + 'shub_extension' => $_REQUEST['network'], + 'shub_account_id' => $account_id, + 'shub_message_id' => $_REQUEST['message-id'], + 'shub_message_comment_id' => $message_comment_id, + )); + + if ($debug) { + // send the message straight away and show any debug output + echo $outbox->send_queued(true); + $return['message'] .= ob_get_clean(); + // dont send an shub_outbox_id in debug mode + // this will keep the 'message' window open and not shrink it down so we can better display debug messages. + + } else { + //set_message( _l( 'message sent and conversation archived.' ) ); + $return['shub_outbox_id'] = $outbox->get('shub_outbox_id'); + } + } } } + if (!headers_sent())header('Content-type: text/javascript'); echo json_encode( $return ); exit; From 4aed14474286119b2dba21962394da2c98ec75e3 Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 13:53:26 +1000 Subject: [PATCH 19/23] archive after activity check --- classes/class-support-hub.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index 37e49e6..ffe1a51 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -146,13 +146,7 @@ public function admin_ajax(){ ?> var element = jQuery('.shub_extension_message[data-network=][data-message-id=]'); var element_action = element.prev('.shub_extension_message_action').first(); - element_action.find('.action_content').html('Message Archived. Undo'); + element_action.find('.action_content').html('Message Archived. Undo'); if(element.is('div')){ element.slideUp(); element_action.slideDown(); From e3cb0d636be7439a6a12ed3cc52f9cabd4bdb60a Mon Sep 17 00:00:00 2001 From: dtbaker Date: Wed, 9 Sep 2015 21:11:19 +1000 Subject: [PATCH 20/23] continuous message view with load more button --- assets/css/social.css | 24 ++++--- assets/js/social.js | 61 +++++++++++++----- classes/class-support-hub-message.php | 10 +-- classes/class-support-hub-table.php | 41 +++++++++--- classes/class-support-hub.php | 56 +++++++++++++---- .../bbpress/class.shub_bbpress_message.php | 5 +- .../envato/class.shub_envato_message.php | 3 +- extensions/ucm/class.shub_ucm_message.php | 15 ++++- pages/inbox.php | 62 +++++++++++++------ 9 files changed, 203 insertions(+), 74 deletions(-) diff --git a/assets/css/social.css b/assets/css/social.css index b345a07..c6f274d 100644 --- a/assets/css/social.css +++ b/assets/css/social.css @@ -230,14 +230,14 @@ tr.log_error{ background: #FBFF66 !important; } -.shub_table_inline { +#shub_table_inline { margin: 20px 0; padding: 20px; background: #FFF; border-radius: 10px; } @media (max-width: 600px) { - .shub_table_inline{ + #shub_table_inline{ margin:10px -8px; -webkit-border-radius: 0; -moz-border-radius: 0; @@ -246,14 +246,14 @@ tr.log_error{ } } @media (min-width: 961px) { - .shub_table_inline { + #shub_table_inline { width: 80%; margin-left: 10%; margin-right: 10%; } } @media (min-width: 1300px) { - .shub_table_inline { + #shub_table_inline { width: 66%; margin-left: 15%; margin-right: 15%; @@ -286,6 +286,13 @@ tr.log_error{ display: block; clear: both; } + .message_sidebar nav .shub_message_action{ + float:left; + margin-right: 10px; + } + .message_sidebar nav .responsive_sidebar_summary{ + float:left; + } #shub_search_form .tablenav.top{ display: none; } @@ -313,14 +320,14 @@ tr.log_error{ -moz-border-radius: 4px; border-radius: 4px; } -.shub_table_inline .shub_extension_message_action:after{ +#shub_table_inline .shub_extension_message_action:after{ content: ""; display: block; margin-bottom: 5px; padding-top: 5px; clear: both; } -.shub_table_inline .shub_extension_message:after{ +#shub_table_inline .shub_extension_message:after{ border-bottom: 1px solid #d4d4d4; content: ""; display: block; @@ -328,7 +335,7 @@ tr.log_error{ padding-top: 20px; clear: both; } -.shub_table_inline .primary_comment { +#shub_table_inline .primary_comment { background-color: #f5f5f5; } @@ -439,4 +446,7 @@ span.buyer_badge, } .shub_hide_when_no_message{ display: none !important; +} +#shub_continuous_more{ + text-align: center; } \ No newline at end of file diff --git a/assets/js/social.js b/assets/js/social.js index 28fbecf..899575a 100644 --- a/assets/js/social.js +++ b/assets/js/social.js @@ -172,10 +172,10 @@ ucm.social = { element_action.find('.action_content').html('Failed to send message. Please check logs.'); }); if(element.is('div')){ - element.slideUp(); + element.slideUp(function(){element.remove();}); element_action.slideDown(); }else{ - element.hide(); + element.remove(); element_action.show(); } var pos = element_action.position(); @@ -199,10 +199,10 @@ ucm.social = { element_action.find('.action_content').html('Failed to send message. Please check logs.'); }); if(element.is('div')){ - element.slideUp(); + element.slideUp(function(){element.remove();}); element_action.slideDown(); }else{ - element.hide(); + element.remove(); element_action.show(); } var pos = element_action.position(); @@ -249,6 +249,30 @@ ucm.social = { } }); return false; + }).delegate('.shub_message_load_content','click',function(){ + // action a message (archive / unarchive) + var post_data = { + action: 'support_hub_' + jQuery(this).data('action'), + wp_nonce: support_hub.wp_nonce, + form_auth_key: ucm.form_auth_key + }; + var button_post = jQuery(this).data('post'); + for(var i in button_post){ + if(button_post.hasOwnProperty(i)){ + post_data[i] = button_post[i]; + } + } + var target = jQuery(jQuery(this).data('target')); + jQuery.ajax({ + url: ajaxurl, + method: 'POST', + data: post_data, + dataType: 'html', + success: function(r){ + target.append(r); + } + }); + return false; }).delegate('.swap_layout_type','click',function(){ jQuery('#layout_type').val(jQuery(this).data('layout-type')).parents('form').get(0).submit(); return false; @@ -274,20 +298,23 @@ ucm.social = { }); }); jQuery(window).scroll(function(){ - var currenttop = jQuery(window).scrollTop(); - // find out which elements we have to move into view. - for(var i = 0; i < inline_views.length; i++){ - if(inline_views[i].pos.top < currenttop && inline_views[i].pos.top + inline_views[i].height > currenttop){ - // calc here incase of ajax loading sidebar data - var sidebar_height = inline_views[i].sidebar.height(); - var from_top = Math.min(inline_views[i].height - sidebar_height - 20, currenttop - inline_views[i].pos.top + 20); - if(from_top > 10){ - inline_views[i].sidebar.css('padding-top',from_top+'px'); - }else{ - inline_views[i].sidebar.attr('style',''); + var width = jQuery(window).width(); + if(width > 782) { + var currenttop = jQuery(window).scrollTop(); + // find out which elements we have to move into view. + for (var i = 0; i < inline_views.length; i++) { + if (inline_views[i].pos.top < currenttop && inline_views[i].pos.top + inline_views[i].height > currenttop) { + // calc here incase of ajax loading sidebar data + var sidebar_height = inline_views[i].sidebar.height(); + var from_top = Math.min(inline_views[i].height - sidebar_height - 20, currenttop - inline_views[i].pos.top + 20); + if (from_top > 10) { + inline_views[i].sidebar.css('padding-top', from_top + 'px'); + } else { + inline_views[i].sidebar.attr('style', ''); + } + } else { + inline_views[i].sidebar.attr('style', ''); } - }else{ - inline_views[i].sidebar.attr('style',''); } } }); diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index d6cafb9..873ac0f 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -367,6 +367,10 @@ public function output_message_page($type='inline'){ 'last_activity' => $this->get('last_active'), )));?>"> + + message_sidebar_data('mobile'); ?> + +
    @@ -388,9 +392,7 @@ public function output_message_page($type='inline'){

- $val){ + $val){ echo ' data-'.$key.'="'.esc_attr($val).'"'; } ?>> diff --git a/classes/class-support-hub-message.php b/classes/class-support-hub-message.php index 8b0f149..d828c59 100644 --- a/classes/class-support-hub-message.php +++ b/classes/class-support-hub-message.php @@ -422,14 +422,14 @@ public function output_message_page($type='inline'){

diff --git a/classes/class-support-hub.php b/classes/class-support-hub.php index 6cc2e93..33541a6 100644 --- a/classes/class-support-hub.php +++ b/classes/class-support-hub.php @@ -994,6 +994,7 @@ public function get_message_user_summary($user_hints, $current_extension, $messa foreach ($messages as $message) { if (!isset($other_messages[$message['shub_message_id']])) { $other_messages[$message['shub_message_id']] = array( + 'link' => '?page=support_hub_main&network='.$message['shub_extension'].'&message_id='.$message['shub_message_id'], 'summary' => $message['title'], 'time' => $message['last_active'], 'network' => $message['shub_extension'], @@ -1021,6 +1022,7 @@ public function get_message_user_summary($user_hints, $current_extension, $messa foreach ($comments as $comment) { if (!isset($other_messages[$comment['shub_message_id']])) { $other_messages[$comment['shub_message_id']] = array( + 'link' => '?page=support_hub_main&network='.$comment['shub_extension'].'&message_id='.$comment['shub_message_id'].'&message_comment_id='.$comment['shub_message_comment_id'], 'summary' => $comment['message_text'], 'time' => $comment['time'], 'network' => $comment['shub_extension'], diff --git a/pages/message.php b/pages/message.php index 912fd4a..a089eba 100644 --- a/pages/message.php +++ b/pages/message.php @@ -3,24 +3,32 @@ - message_managers[$network]) && $message_id > 0){ - $shub_extension_message = $this->message_managers[$network]->get_message( false, false, $message_id); - if($shub_extension_message->get('shub_'.$network.'_message_id') == $message_id){ - extract(array( - "shub_{$network}_id" => $shub_extension_message->get($network.'_account')->get('shub_'.$network.'_id'), - "shub_{$network}_message_id" => $message_id - )); - include( trailingslashit( SupportHub::getInstance()->dir ) . 'extensions/'.$network.'/'.$network.'_message.php'); +
+
+ message_managers[$network]) && $shub_message_id > 0){ + $shub_extension_message = $this->message_managers[$network]->get_message( false, false, $shub_message_id); + if($shub_extension_message->get('shub_message_id') == $shub_message_id){ + $shub_account_id = $shub_extension_message->get('account')->get('shub_account_id'); + include( trailingslashit( SupportHub::getInstance()->dir ) . 'extensions/'.$network.'/'.$network.'_message.php'); + } } - } + ?> +
+
- ?> +
\ No newline at end of file