</form>
		</div>
	<?php 
        }
    }
} else {
    // show account overview:
    $myListTable = new SupportHub_Account_Data_List_Table();
    $accounts = $shub_ucm->get_accounts();
    foreach ($accounts as $account_id => $account) {
        $a = new shub_ucm_account($account['shub_account_id']);
        $accounts[$account_id]['edit_link'] = $a->link_edit();
        $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);
    $myListTable->prepare_items();
    ?>
	<div class="wrap">
		<h2>
			<?php 
    _e('UCM Accounts', 'support_hub');
    ?>
			<a href="?page=<?php 
    echo esc_attr($_GET['page']);
    ?>
&tab=<?php 
    echo esc_attr($_GET['tab']);
    ?>
    public function load_latest_item_data($debug = false)
    {
        // serialise this result into envato_data.
        if (!$this->account) {
            echo 'No envato account linked, please try again';
            return;
        }
        $api = $this->account->get_api();
        $network_key = $this->get('network_key');
        if (!$network_key) {
            echo 'No envato item 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');
        if ($debug) {
            echo "Getting the latest 60 comments for item: " . $network_key . " (last message in database is from " . shub_print_date($last_message_received, true) . ")<br>\n";
        }
        $newest_message_received = 0;
        $endpoint = 'v1/discovery/search/search/comment?term=&item_id=' . $network_key . '&sort_by=newest&page_size=60';
        $api_result = $api->api($endpoint);
        if ($debug) {
            echo "API Result took :" . $api_result['took'] . ' seconds and produced ' . count($api_result['matches']) . ' results';
        }
        $count = 0;
        if (isset($api_result['matches']) && is_array($api_result['matches'])) {
            //foreach($api_result['matches'] as $item_message){
            while ($api_result['matches']) {
                $item_message = array_pop($api_result['matches']);
                if (!$item_message['id']) {
                    continue;
                }
                $message_time = strtotime($item_message['last_comment_at']);
                $newest_message_received = max($newest_message_received, $message_time);
                if ($message_time <= $last_message_received) {
                    continue;
                }
                // all done here.
                // check if we have this message in our database already.
                $envato_message = new shub_message($this->account, $this, false);
                $envato_message->load_by_network_key($item_message['id'], $item_message, 'item_comment', $debug);
                $count++;
                if ($debug) {
                    ?>
					<div>
					<pre> Imported message ID: <?php 
                    echo $envato_message->get('network_key');
                    ?>
 </pre>
					</div>
				<?php 
                }
            }
        }
        SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'Imported  ' . $count . ' new messages into database');
        if ($debug) {
            echo " imported {$count} new item comments <br>";
        }
        $this->update('last_message', $newest_message_received);
        $this->update('last_checked', time());
    }
 function column_default($item, $column_name)
 {
     if (is_object($item)) {
         return $item->get($column_name);
     } else {
         if (is_array($item) && isset($item[$column_name])) {
             switch ($column_name) {
                 case 'log_data':
                     $data = maybe_unserialize($item[$column_name]);
                     if (!is_array($data)) {
                         $data_test = @json_decode($data, true);
                         if (is_array($data_test)) {
                             $data = $data_test;
                         }
                     }
                     if (is_array($data)) {
                         echo '<div style="max-height:100px; overflow-y:auto;"><pre>';
                         $lines = explode("\n", var_export($data, true));
                         echo htmlspecialchars(implode("\n", array_merge(array_slice($lines, 0, 12), count($lines) > 11 ? array("etc...") : array())));
                         echo '</pre></div>';
                         return false;
                     } else {
                         return $data;
                     }
                     break;
                 case 'log_time':
                     return shub_print_date($item[$column_name], true);
                     break;
             }
             return $item[$column_name];
         } else {
             return 'No';
         }
     }
 }
    /**
     * @param $message - an array holding a result from the shub_message row
     * @param array $existing_rows - optional array passed in from the individual extension already having data for this row.
     * @return array
     */
    public function output_row($message, $existing_rows = array())
    {
        $extension_message = $this->get_message(false, false, $message['shub_message_id']);
        $messages = $extension_message->get_comments();
        $return = array();
        ob_start();
        echo $this->get_friendly_icon();
        ?>
        <a href="<?php 
        echo $extension_message->get_link();
        ?>
"
           target="_blank"><?php 
        echo htmlspecialchars($extension_message->get('account') ? $extension_message->get('account')->get('account_name') : 'Item');
        ?>
</a> <br/>
        <?php 
        echo htmlspecialchars($extension_message->get_type_pretty());
        ?>
        <?php 
        $return['shub_column_account'] = ob_get_clean();
        ob_start();
        $shub_product_id = $extension_message->get_product_id();
        $product_data = array();
        $item_data = array();
        $item = $extension_message->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');
            if (!empty($product_data['image'])) {
                ?>
                <img src="<?php 
                echo esc_attr($product_data['image']);
                ?>
" class="shub_friendly_icon">
            <?php 
            }
            ?>
            <?php 
            if (!empty($product_data['url'])) {
                ?>
                <a href="<?php 
                echo esc_url($product_data['url']);
                ?>
" target="_blank"><?php 
                echo htmlspecialchars($shub_product->get('product_name'));
                ?>
</a>
                <?php 
            } else {
                ?>
 <?php 
                echo htmlspecialchars($shub_product->get('product_name'));
                ?>
 <?php 
            }
        }
        $return['shub_column_product'] = ob_get_clean();
        $return['shub_column_time'] = '<span class="shub_time" data-time="' . esc_attr($extension_message->get('last_active')) . '" data-date="' . esc_attr(shub_print_date($extension_message->get('last_active'), true)) . '">' . shub_pretty_date($extension_message->get('last_active')) . '</span>';
        ob_start();
        // work out who this is from.
        $from = $extension_message->get_from();
        ?>
        <div class="shub_from_holder">
            <div class="shub_from_full">
                <?php 
        foreach ($from as $id => $from_data) {
            ?>
                    <div>
                        <a href="<?php 
            echo esc_url($from_data->get_link());
            ?>
" target="_blank"><img src="<?php 
            echo esc_attr($from_data->get_image());
            ?>
" class="shub_from_picture"></a> <?php 
            echo htmlspecialchars($from_data->get_name());
            ?>
                    </div>
                    <?php 
        }
        ?>
            </div>
            <?php 
        reset($from);
        if (isset($from_data)) {
            echo '<a href="' . $from_data->get_link() . '" target="_blank">' . '<img src="' . esc_attr($from_data->get_image()) . '" class="shub_from_picture"></a> ';
            echo '<span class="shub_from_count">';
            if (count($from) > 1) {
                echo '+' . (count($from) - 1);
            }
            echo '</span>';
        }
        ?>
        </div>
        <?php 
        $return['shub_column_from'] = ob_get_clean();
        ob_start();
        ?>
        <span style="float:right;">
		    <?php 
        echo count($messages) > 0 ? '(' . count($messages) . ')' : '';
        ?>
	    </span>
        <div class="shub_message_summary<?php 
        echo !isset($message['read_time']) || !$message['read_time'] ? ' unread' : '';
        ?>
"> <?php 
        // todo - pull in comments here, not just title/summary
        // todo - style customer and admin replies differently (eg <em> so we can easily see)
        $title = strip_tags($extension_message->get('title'));
        $summary = strip_tags($extension_message->get('summary'));
        echo htmlspecialchars(strlen($title) > 80 ? substr($title, 0, 80) . '...' : $title) . ($summary != $title ? '<br/>' . htmlspecialchars(strlen($summary) > 80 ? substr($summary, 0, 80) . '...' : $summary) : '');
        ?>
        </div>
        <?php 
        $return['shub_column_summary'] = ob_get_clean();
        ob_start();
        ?>
        <a href="<?php 
        echo $extension_message->link_open();
        ?>
" class="socialmessage_open shub_modal button" data-modaltitle="<?php 
        echo esc_attr($title);
        ?>
" data-network="<?php 
        echo esc_attr($this->id);
        ?>
" data-message_id="<?php 
        echo (int) $extension_message->get('shub_message_id');
        ?>
"><?php 
        _e('Open');
        ?>
</a>
        <?php 
        if ($extension_message->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED) {
            ?>
            <a href="#" class="socialmessage_action shub_message_action button"
               data-action="set-unanswered" data-post="<?php 
            echo esc_attr(json_encode(array('network' => $this->id, 'shub_message_id' => $extension_message->get('shub_message_id'))));
            ?>
"><?php 
            _e('Inbox');
            ?>
</a>
        <?php 
        } else {
            ?>
            <a href="#" class="socialmessage_action shub_message_action button"
               data-action="set-answered" data-post="<?php 
            echo esc_attr(json_encode(array('network' => $this->id, 'shub_message_id' => $extension_message->get('shub_message_id'))));
            ?>
"><?php 
            _e('Archive');
            ?>
</a>
        <?php 
        }
        ?>
        <?php 
        $return['shub_column_action'] = ob_get_clean();
        return $return;
    }
 public function update_author_sale_history($debug = false, $do_all = false)
 {
     return;
     // todo: save sale history in db with no username/api key just for stats
     $api = $this->get_api();
     // how many days do we want to go back? maybe 60 days to start with?
     $last_sale = get_option('supporthub_envato_author_sales_last', false);
     if (!$last_sale) {
         $last_sale = strtotime('-360 days');
     }
     $last_sale_in_this_batch = 0;
     $page = 1;
     while (true) {
         $recent_sales = $api->api('v2/market/author/sales?page=' . $page, array(), true);
         //            echo "Recent sales are: ";print_r($recent_sales);exit;
         if ($debug) {
             echo "Page {$page} of sales data contained " . count($recent_sales) . " results.<br>\n";
         }
         $page++;
         if (!$recent_sales || !is_array($recent_sales)) {
             break;
         }
         foreach ($recent_sales as $recent_sale) {
             if ($recent_sale && !empty($recent_sale['sold_at']) && !empty($recent_sale['code']) && !empty($recent_sale['item']['id'])) {
                 //                    echo $recent_sale['sold_at']."<br>";
                 // add this to the database, or break if we already have this one in the db.
                 $sale_time = strtotime($recent_sale['sold_at']);
                 // we might already have this one in our database
                 // unless we are doing the intial seed
                 $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($recent_sale['code']));
                 if ($existing_purchase) {
                     if (!$do_all) {
                         break 2;
                         // stop processing once we reach one we've already saved
                     }
                     continue;
                     // exists already in the db, skip to next one.
                 }
                 $last_sale_in_this_batch = max($last_sale_in_this_batch, $sale_time);
                 // todo: check if they add username to the system, for now we use a 0 shub_user_id because we're unsure which user this purchase is related to (without doing another separate purchase call)
                 if ($debug) {
                     echo " - adding new sale to database ( " . shub_print_date($sale_time, true) . " - " . $recent_sale['item']['name'] . " )...<br>\n";
                 }
                 // for now we do all processing based on this purchase code. SLOW. but until we get usernames in the buyer result there is no other way.
                 //                    echo "Query this code: ".$recent_sale['code'];exit;
                 SupportHub::getInstance()->message_managers['envato']->pull_purchase_code($api, $recent_sale['code'], $recent_sale);
                 update_option('supporthub_envato_author_sales_last', $last_sale_in_this_batch);
                 continue;
                 // save this purchase code into the db
                 // find the product this purchase is related to
                 $existing_products = SupportHub::getInstance()->get_products();
                 // check if this item exists already
                 $exists = false;
                 foreach ($existing_products as $existing_product) {
                     if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $recent_sale['item']['id']) {
                         $exists = $existing_product['shub_product_id'];
                     }
                 }
                 $newproduct = new SupportHubProduct();
                 if (!$exists) {
                     $newproduct->create_new();
                     if (!$newproduct->get('product_name')) {
                         $newproduct->update('product_name', $recent_sale['item']['name']);
                     }
                     $existing_product_data = $newproduct->get('product_data');
                     if (!is_array($existing_product_data)) {
                         $existing_product_data = array();
                     }
                     if (empty($existing_product_data['envato_item_id'])) {
                         $existing_product_data['envato_item_id'] = $recent_sale['item']['id'];
                     }
                     if (empty($existing_product_data['envato_item_data'])) {
                         $existing_product_data['envato_item_data'] = $recent_sale['item'];
                     }
                     if (empty($existing_product_data['image'])) {
                         $existing_product_data['image'] = $recent_sale['item']['thumbnail_url'];
                     }
                     if (empty($existing_product_data['url'])) {
                         $existing_product_data['url'] = $recent_sale['item']['url'];
                     }
                     $newproduct->update('product_data', $existing_product_data);
                 } else {
                     $newproduct->load($exists);
                 }
                 if ($newproduct->get('shub_product_id')) {
                     // product has been added
                     // time to add it to the purchase db
                     // check if this already exists in the db
                     $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($recent_sale['code']));
                     if (!$existing_purchase) {
                         $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => 0, 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => $sale_time, 'envato_user_id' => 0, 'purchase_code' => $recent_sale['code'], 'api_type' => 'author/sales', 'purchase_data' => json_encode($recent_sale)));
                     } else {
                         $shub_envato_purchase_id = $existing_purchase['shub_envato_purchase_id'];
                     }
                     if ($shub_envato_purchase_id) {
                         // we have a purchase in the db
                         // add or update the support expiry based on this purchase history.
                         // work out when this purchase support expires
                         // this is the expiry date returned in the api or just 6 months from the original purchase date.
                         $support_expiry_time = strtotime("+6 months", $sale_time);
                         // todo - check for this expiry time in the new api results.
                         $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($shub_envato_purchase_id));
                         if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == $sale_time) {
                             // check the existing support expiry matches the one we have in the database.
                             if ($existing_support['end_time'] < $support_expiry_time) {
                                 // we have a support extension!
                                 $shub_envato_support_id = shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($recent_sale)));
                             }
                         } else {
                             // we are adding a new support entry
                             $shub_envato_support_id = shub_update_insert('shub_envato_support_id', false, 'shub_envato_support', array('shub_user_id' => 0, 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, 'start_time' => $sale_time, 'end_time' => $support_expiry_time, 'api_type' => 'author/sales', 'support_data' => json_encode($recent_sale)));
                         }
                     }
                 }
             }
         }
     }
 }
 public function handle_process($process, $options = array())
 {
     switch ($process) {
         case 'send_shub_message':
             $message_count = 0;
             if (check_admin_referer('shub_send-message') && isset($options['shub_message_id']) && (int) $options['shub_message_id'] > 0 && isset($_POST['bbpress_message']) && !empty($_POST['bbpress_message'])) {
                 // we have a social message id, ready to send!
                 // which bbpress accounts are we sending too?
                 $bbpress_accounts = isset($_POST['compose_bbpress_id']) && is_array($_POST['compose_bbpress_id']) ? $_POST['compose_bbpress_id'] : array();
                 foreach ($bbpress_accounts as $bbpress_account_id => $send_forums) {
                     $bbpress_account = new shub_bbpress_account($bbpress_account_id);
                     if ($bbpress_account->get('shub_account_id') == $bbpress_account_id) {
                         /* @var $available_forums shub_bbpress_item[] */
                         $available_forums = $bbpress_account->get('items');
                         if ($send_forums) {
                             foreach ($send_forums as $item_id => $tf) {
                                 if (!$tf) {
                                     continue;
                                 }
                                 // shouldnt happen
                                 switch ($item_id) {
                                     case 'share':
                                         // doing a status update to this bbpress account
                                         $bbpress_message = new shub_bbpress_message($bbpress_account, false, false);
                                         $bbpress_message->create_new();
                                         $bbpress_message->update('shub_item_id', 0);
                                         $bbpress_message->update('shub_message_id', $options['shub_message_id']);
                                         $bbpress_message->update('shub_account_id', $bbpress_account->get('shub_account_id'));
                                         $bbpress_message->update('summary', isset($_POST['bbpress_message']) ? $_POST['bbpress_message'] : '');
                                         $bbpress_message->update('title', isset($_POST['bbpress_title']) ? $_POST['bbpress_title'] : '');
                                         $bbpress_message->update('shub_link', isset($_POST['bbpress_link']) ? $_POST['bbpress_link'] : '');
                                         if (isset($_POST['track_links']) && $_POST['track_links']) {
                                             $bbpress_message->parse_links();
                                         }
                                         $bbpress_message->update('shub_type', 'share');
                                         $bbpress_message->update('shub_data', json_encode($_POST));
                                         $bbpress_message->update('user_id', get_current_user_id());
                                         // do we send this one now? or schedule it later.
                                         $bbpress_message->update('shub_status', _shub_MESSAGE_STATUS_PENDINGSEND);
                                         if (isset($options['send_time']) && !empty($options['send_time'])) {
                                             // schedule for sending at a different time (now or in the past)
                                             $bbpress_message->update('last_active', $options['send_time']);
                                         } else {
                                             // send it now.
                                             $bbpress_message->update('last_active', 0);
                                         }
                                         if (isset($_FILES['bbpress_picture']['tmp_name']) && is_uploaded_file($_FILES['bbpress_picture']['tmp_name'])) {
                                             $bbpress_message->add_attachment($_FILES['bbpress_picture']['tmp_name']);
                                         }
                                         $now = time();
                                         if (!$bbpress_message->get('last_active') || $bbpress_message->get('last_active') <= $now) {
                                             // send now! otherwise we wait for cron job..
                                             if ($bbpress_message->send_queued(isset($_POST['debug']) && $_POST['debug'])) {
                                                 $message_count++;
                                             }
                                         } else {
                                             $message_count++;
                                             if (isset($_POST['debug']) && $_POST['debug']) {
                                                 echo "message will be sent in cron job after " . shub_print_date($bbpress_message->get('last_active'), true);
                                             }
                                         }
                                         break;
                                     case 'blog':
                                         // doing a blog post to this bbpress account
                                         // not possible through api
                                         break;
                                     default:
                                         // posting to one of our available forums:
                                         // see if this is an available forum.
                                         if (isset($available_forums[$item_id])) {
                                             // push to db! then send.
                                             $bbpress_message = new shub_bbpress_message($bbpress_account, $available_forums[$item_id], false);
                                             $bbpress_message->create_new();
                                             $bbpress_message->update('shub_item_id', $available_forums[$item_id]->get('shub_item_id'));
                                             $bbpress_message->update('shub_message_id', $options['shub_message_id']);
                                             $bbpress_message->update('shub_account_id', $bbpress_account->get('shub_account_id'));
                                             $bbpress_message->update('summary', isset($_POST['bbpress_message']) ? $_POST['bbpress_message'] : '');
                                             $bbpress_message->update('title', isset($_POST['bbpress_title']) ? $_POST['bbpress_title'] : '');
                                             if (isset($_POST['track_links']) && $_POST['track_links']) {
                                                 $bbpress_message->parse_links();
                                             }
                                             $bbpress_message->update('shub_type', 'forum_post');
                                             $bbpress_message->update('shub_link', isset($_POST['link']) ? $_POST['link'] : '');
                                             $bbpress_message->update('shub_data', json_encode($_POST));
                                             $bbpress_message->update('user_id', get_current_user_id());
                                             // do we send this one now? or schedule it later.
                                             $bbpress_message->update('shub_status', _shub_MESSAGE_STATUS_PENDINGSEND);
                                             if (isset($options['send_time']) && !empty($options['send_time'])) {
                                                 // schedule for sending at a different time (now or in the past)
                                                 $bbpress_message->update('last_active', $options['send_time']);
                                             } else {
                                                 // send it now.
                                                 $bbpress_message->update('last_active', 0);
                                             }
                                             if (isset($_FILES['bbpress_picture']['tmp_name']) && is_uploaded_file($_FILES['bbpress_picture']['tmp_name'])) {
                                                 $bbpress_message->add_attachment($_FILES['bbpress_picture']['tmp_name']);
                                             }
                                             $now = time();
                                             if (!$bbpress_message->get('last_active') || $bbpress_message->get('last_active') <= $now) {
                                                 // send now! otherwise we wait for cron job..
                                                 if ($bbpress_message->send_queued(isset($_POST['debug']) && $_POST['debug'])) {
                                                     $message_count++;
                                                 }
                                             } else {
                                                 $message_count++;
                                                 if (isset($_POST['debug']) && $_POST['debug']) {
                                                     echo "message will be sent in cron job after " . shub_print_date($bbpress_message->get('last_active'), true);
                                                 }
                                             }
                                         } else {
                                             // log error?
                                         }
                                 }
                             }
                         }
                     }
                 }
             }
             return $message_count;
             break;
     }
     parent::handle_process($process, $options);
 }
 public function filter_message_user_sidebar($user_bits, $shub_user_ids)
 {
     // find purchases for these user ids and if they are in a valid support term.
     if (!empty($shub_user_ids) && is_array($shub_user_ids)) {
         foreach ($shub_user_ids as $shub_user_id) {
             if ((int) $shub_user_id > 0) {
                 // find purchases.
                 $total = 0;
                 $purchases = shub_get_multiple('shub_envato_purchase', array('shub_user_id' => $shub_user_id));
                 foreach ($purchases as $purchase) {
                     if ($purchase['shub_product_id']) {
                         $purchase_product = new SupportHubProduct($purchase['shub_product_id']);
                         $data = $purchase_product->get('product_data');
                         if (!empty($data['envato_item_data']['item'])) {
                             $support_text = '<span class="buyer_status_badges">';
                             $support = shub_get_single('shub_envato_support', 'shub_envato_purchase_id', $purchase['shub_envato_purchase_id']);
                             if ($support && !empty($support['end_time']) && $support['end_time'] <= time()) {
                                 // WHOPPS. I got this wrong in the DB initially. Hack to double check purchase happened before new support terms
                                 if (strtotime($purchase['purchase_time']) < strtotime("2015-09-01")) {
                                     $support['end_time'] = strtotime("+6 months", strtotime("2015-09-01"));
                                 }
                             }
                             if ($support && !empty($support['end_time']) && $support['end_time'] > time()) {
                                 $support_text .= '<span class="buyer_badge supported">' . shub_print_date($support['end_time']) . '</span>';
                             } else {
                                 if ($support && !empty($support['end_time'])) {
                                     $support_text .= '<span class="buyer_badge unsupported">' . shub_print_date($support['end_time']) . '</span>';
                                 } else {
                                     $support_text .= '<span class="buyer_badge unsupported">Unknown</span>';
                                 }
                             }
                             $support_text .= '</span>';
                             $user_bits[] = array('Purchase', esc_html($data['envato_item_data']['item']) . ' on ' . shub_print_date($purchase['purchase_time']) . ' support until ' . $support_text);
                             $sale_data = @json_decode($purchase['purchase_data'], true);
                             if ($sale_data && !empty($sale_data['amount'])) {
                                 $total += $sale_data['amount'];
                             }
                             if ($sale_data && !empty($sale_data['support_amount'])) {
                                 $total += $sale_data['support_amount'];
                             }
                             if (!$sale_data) {
                                 print_r($purchase);
                             }
                         }
                     } else {
                         // failed API lookup, show purchase code instead.
                         // todo: a re-lookup button.
                         $user_bits[] = array('Failed Purchase Code', $purchase['purchase_code']);
                     }
                 }
                 if ($total) {
                     $user_bits[] = array('Total', '$' . number_format($total, 2, ".", ","));
                 }
             }
         }
     }
     return $user_bits;
 }
    public function load_latest_item_data($debug = false)
    {
        // serialise this result into account_data.
        if (!$this->account) {
            echo 'No bbpress account linked, please try again';
            return;
        }
        $api = $this->account->get_api();
        $network_key = $this->get('network_key');
        if (!$network_key) {
            echo 'No bbpress forum id found';
            return;
        }
        // first we seed the cache with the latest bbpress replies and topics
        // we do this because it's not possible to filter based on "post_parent" through the WordPress API (SILLY!)
        // so this saves us calling getPost() a lot of times.
        $filter_replies = array('post_type' => 'reply', 'number' => 100, 'post_status' => 'publish');
        $api_result_latest_replies = $this->account->get_api_cache($filter_replies);
        $api_result_latest_replies = $api_result_latest_replies ? $api_result_latest_replies : $api->getPosts($filter_replies);
        $filter_topics = array('post_type' => 'topic', 'number' => 100, 'post_status' => 'publish');
        $api_result_latest_topics = $this->account->get_api_cache($filter_topics);
        $api_result_latest_topics = $api_result_latest_topics ? $api_result_latest_topics : $api->getPosts($filter_topics);
        // loop through our latest replies and see if any of them are from a thread that sits under this forum
        // COMPLETELY THE REVERSE WAY THAT WE SHOULD BE DOING IT! rar!
        $forum_topics = array();
        foreach ($api_result_latest_topics as $forum_topic) {
            if ($forum_topic['post_parent'] == $network_key) {
                $forum_topic['timestamp'] = $forum_topic['post_date']->timestamp;
                // yay! this reply is part of a topic that is part of this forum. keep it.
                if (!isset($forum_topics[$forum_topic['post_id']])) {
                    $forum_topics[$forum_topic['post_id']] = $forum_topic;
                }
                if (!isset($forum_topics[$forum_topic['post_id']]['replies'])) {
                    $forum_topics[$forum_topic['post_id']]['replies'] = array();
                }
                // we need to add our main forum_topic onto the replies array so that all messages go into the 'comments' database table.
                $forum_topics[$forum_topic['post_id']]['replies'][] = $forum_topic;
            }
        }
        foreach ($api_result_latest_replies as $forum_reply) {
            // find its parent and see if it is from this forum.
            $found_parent = false;
            foreach ($api_result_latest_topics as $forum_topic) {
                if ($forum_topic['post_id'] == $forum_reply['post_parent']) {
                    $found_parent = $forum_topic;
                    break;
                }
            }
            if (!$found_parent) {
                $api_result_parent = $api->getPost($forum_reply['post_parent']);
                if ($api_result_parent) {
                    $found_parent = $api_result_parent;
                    $api_result_latest_topics[] = $api_result_parent;
                    // add to cache so we hopefully dont have to hit it again if it's a popular topic
                }
            }
            if ($found_parent) {
                // found a parent post, check if it's part of this forum.
                if ($found_parent['post_parent'] == $network_key) {
                    $found_parent['timestamp'] = $found_parent['post_date']->timestamp;
                    $forum_reply['timestamp'] = $forum_reply['post_date']->timestamp;
                    // yay! this reply is part of a topic that is part of this forum. keep it.
                    if (!isset($forum_topics[$found_parent['post_id']])) {
                        $forum_topics[$found_parent['post_id']] = $found_parent;
                    }
                    if (!isset($forum_topics[$found_parent['post_id']]['replies'])) {
                        $forum_topics[$found_parent['post_id']]['replies'] = array();
                    }
                    $forum_topics[$found_parent['post_id']]['replies'][] = $found_parent;
                    $forum_topics[$found_parent['post_id']]['replies'][] = $forum_reply;
                    if (!isset($forum_topics[$found_parent['post_id']]['timestamp'])) {
                        $forum_topics[$found_parent['post_id']]['timestamp'] = $found_parent['timestamp'];
                    }
                    $forum_topics[$found_parent['post_id']]['timestamp'] = max($forum_reply['post_date']->timestamp, $forum_topics[$found_parent['post_id']]['timestamp']);
                }
                /*echo date('Y-m-d',$forum_reply['post_date']->timestamp);
                		echo " <a href='".$forum_reply['link']."'>'".$forum_reply['link'].'</a> ';
                		echo $forum_reply['post_content'];
                		echo "Parent is: ";
                		echo date('Y-m-d',$found_parent['post_date']->timestamp);
                		echo " <a href='".$found_parent['link']."'>'".$found_parent['link'].'</a> ';
                		echo '<hr>';*/
            } else {
            }
        }
        uasort($forum_topics, function ($a, $b) {
            return $a['timestamp'] < $b['timestamp'];
        });
        // cache them for any other bbpress forum calls that are run during the same cron job process.
        $this->account->set_api_cache($filter_replies, $api_result_latest_replies);
        $this->account->set_api_cache($filter_topics, $api_result_latest_topics);
        // 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');
        if ($debug) {
            echo "Getting the latest replies for forum: " . $network_key . " (last message in database is from " . shub_print_date($last_message_received, true) . ")<br>\n";
        }
        $newest_message_received = 0;
        SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'Found total of ' . count($forum_topics) . " forum topics from API calls");
        $count = 0;
        foreach ($forum_topics as $forum_topic) {
            $message_time = $forum_topic['timestamp'];
            $newest_message_received = max($newest_message_received, $message_time);
            if ($message_time <= $last_message_received) {
                break;
            }
            // all done here.
            $bbpress_message = new shub_bbpress_message($this->account, $this, false);
            $bbpress_message->load_by_bbpress_id($forum_topic['post_id'], $forum_topic, 'forum_topic', $debug);
            $count++;
            SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'Imported forum topic ID ' . $bbpress_message->get('network_key') . " with " . count($forum_topic['replies']) . ' replies');
            if ($debug) {
                ?>
				<div>
				<pre> Imported forum topic ID: <?php 
                echo $bbpress_message->get('network_key');
                ?>
 with <?php 
                echo count($forum_topic['replies']);
                ?>
 replies. </pre>
				</div>
			<?php 
            }
        }
        // get user, return envato_codes in meta
        SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'bbpress', 'Completed Cron Import: ' . $count . ' new forum topics');
        if ($debug) {
            echo " imported {$count} new forum comments <br>";
        }
        $this->update('last_message', $newest_message_received);
        $this->update('last_checked', time());
    }
    public function output_message_list($allow_reply = true)
    {
        $message_id = $this->get('shub_message_id');
        $comments = $this->get_comments();
        $x = 0;
        foreach ($comments as $comment) {
            $x++;
            $from_user = $this->get_user($comment['shub_user_id']);
            $time = isset($comment['time']) ? $comment['time'] : false;
            // 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']) {
                    // the outbox entry has been removed but this comment still references it
                    // todo: update this comment entry to not contain an shub_outbox_id
                } else {
                    switch ($shub_outbox->get('shub_status')) {
                        case _SHUB_OUTBOX_STATUS_QUEUED:
                        case _SHUB_OUTBOX_STATUS_SENDING:
                            $extra_class .= ' outbox_queued';
                            $comment_status = 'Currently Sending....' . $shub_outbox->get('shub_status');
                            break;
                        case _SHUB_OUTBOX_STATUS_FAILED:
                            $extra_class .= ' outbox_failed';
                            $comment_status = 'Failed to send message! Please check logs.';
                            $message_error = true;
                            break;
                    }
                }
            }
            if (!empty($comment['private'])) {
                $extra_class .= ' shub_message_private';
            }
            ?>
            <div class="shub_message shub_message_<?php 
            echo $x == 1 ? 'primary' : 'reply';
            echo $extra_class;
            ?>
">
                <div class="shub_message_picture">
                    <img src="<?php 
            echo $from_user->get_image();
            ?>
" />
                </div>
                <div class="shub_message_header">
                    <?php 
            if ($comment_status) {
                ?>
                        <div class="shub_comment_status"><?php 
                echo $comment_status;
                ?>
</div>
                    <?php 
            }
            ?>
                    <?php 
            echo $from_user->get_full_link();
            ?>
                    <span>
                        <?php 
            if ($time) {
                ?>
                            <span class="time" data-time="<?php 
                echo esc_attr($time);
                ?>
" data-date="<?php 
                echo esc_attr(shub_print_date($time, true));
                ?>
"><?php 
                echo shub_pretty_date($time);
                ?>
</span>
                        <?php 
            }
            ?>
                        <span class="wp_user">
                        <?php 
            // todo - better this! don't call on every message, load list in main loop and pass through all results.
            if (isset($envato_data['user_id']) && $envato_data['user_id']) {
                $user_info = get_userdata($envato_data['user_id']);
                echo ' (sent by ' . htmlspecialchars($user_info->display_name) . ')';
            }
            ?>
                        </span>
                        <span class="buyer_status_badges">
                            <?php 
            // work out of this buyer has bought something via the envato module.
            // first we have to find out what item this is:
            //echo "user:"******"<br>product:".$this->get_product_id()."<br>";
            // todo: store these as a flag in the message database so we can run stats on them and display a graph on the dashboard.
            $buyer_status = $this->get_buyer_status($comment['shub_user_id']);
            if (!empty($buyer_status['purchased'])) {
                echo '<span class="buyer_badge purchased">Purchased</span> ';
            }
            if (!empty($buyer_status['supported'])) {
                echo '<span class="buyer_badge supported">Supported</span> ';
            }
            if (!empty($buyer_status['unsupported'])) {
                echo '<span class="buyer_badge unsupported">Unsupported</span> ';
            }
            if (!empty($buyer_status['presale'])) {
                //echo '<span class="buyer_badge presale">Pre-sale</span> ';
            }
            // todo - add a badge for staff reply.
            ?>
                        </span>
                    </span>
                </div>
                <div class="shub_message_body">
                    <div>
                        <?php 
            echo shub_forum_text($comment['message_text']);
            ?>
                    </div>
                </div>
                <div class="shub_message_actions">
                    <?php 
            if ($message_error && !empty($comment['shub_outbox_id'])) {
                ?>
                        <button data-post="<?php 
                echo esc_attr(json_encode(array('action' => "support_hub_resend_outbox_message", 'shub_outbox_id' => $comment['shub_outbox_id'])));
                ?>
" class="btn button shub_message_action_button"><?php 
                _e('Re-Send');
                ?>
</button>
                        <button data-post="<?php 
                echo esc_attr(json_encode(array('action' => "support_hub_delete_outbox_message", 'shub_outbox_id' => $comment['shub_outbox_id'])));
                ?>
" class="btn button shub_message_action_button"><?php 
                _e('Delete Message');
                ?>
</button>
                    <?php 
            }
            ?>
                </div>
            </div>
        <?php 
        }
        if ($allow_reply) {
            ?>
            <div class="shub_message shub_message_reply shub_message_reply_box">
                <?php 
            $reply_shub_user = $this->get_reply_user();
            ?>
                <div class="shub_message_picture">
                      <img src="<?php 
            echo $reply_shub_user->get_image();
            ?>
" />
                </div>
                <div class="shub_message_header">
                     <?php 
            echo $reply_shub_user->get_full_link();
            ?>
                </div>
                <div class="shub_message_body">
                    <textarea placeholder="Write a reply..."></textarea>

                    <div class="shub_message_buttons">
                        <a href="#" class="shub_request_extra btn btn-default btn-xs button"
                           data-modaltitle="<?php 
            _e('Request Extra Details');
            ?>
" data-action="request_extra_details"
                           data-network="<?php 
            echo $this->network;
            ?>
"
                           data-<?php 
            echo $this->network;
            ?>
-message-id="<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Request Details');
            ?>
</a>
                        <!-- <a href="#" class="shub_template_button btn btn-default btn-xs button"
                           data-modaltitle="<?php 
            _e('Send Template Message');
            ?>
" data-action="send_template_message"
                           data-network="<?php 
            echo $this->network;
            ?>
"
                           data-<?php 
            echo $this->network;
            ?>
-message-id="<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Template');
            ?>
</a> -->

                        <button data-post="<?php 
            echo esc_attr(json_encode(array('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 shub_button_loading"><?php 
            _e('Send');
            ?>
</button>
                    </div
                </div>
                <div class="shub_message_actions shub_hide_when_no_message">
                    <div>
                        <label
                            for="message_reply_archive_<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Archive After Reply', 'shub');
            ?>
</label>
                        <input id="message_reply_archive_<?php 
            echo $message_id;
            ?>
" type="checkbox" name="archive"
                               data-reply="yes" value="1" checked>
                    </div>
                    <div>
                        <label
                            for="message_reply_private_<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Mark As Private', 'shub');
            ?>
</label>
                        <input id="message_reply_private_<?php 
            echo $message_id;
            ?>
" type="checkbox" name="private"
                               data-reply="yes" value="1">
                    </div>
                    <div>
                        <label
                            for="message_reply_notify_email_<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Notify Via Email', 'shub');
            ?>
</label>
                        <input id="message_reply_notify_email_<?php 
            echo $message_id;
            ?>
" type="checkbox" name="notify_email"
                               data-reply="yes" value="1">
                    </div>
                    <div>
                        <label
                            for="message_reply_debug_<?php 
            echo $message_id;
            ?>
"><?php 
            _e('Enable Debug Mode', 'shub');
            ?>
</label>
                        <input id="message_reply_debug_<?php 
            echo $message_id;
            ?>
" type="checkbox" name="debug"
                               data-reply="yes" value="1">
                    </div>
                    <?php 
            $this->reply_actions();
            ?>
                </div>
            </div>
            <?php 
        }
    }
    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) . ")<br>\n";
        }
        $newest_message_received = 0;
        $count = 0;
        if (isset($tickets['reply_options'])) {
            $this->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) {
                    ?>
                    <div>
                        <pre> Imported Ticket ID: <?php 
                    echo $ucm_message->get('network_key');
                    ?>
                            with <?php 
                    echo $ticket['message_count'];
                    ?>
 message. </pre>
                    </div>
                    <?php 
                }
            }
        } else {
            SupportHub::getInstance()->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 <br>";
        }
        $this->update('last_message', $newest_message_received);
        $this->update('last_checked', time());
    }
Example #11
0
		    <tr>
			    <th class="shub_compose_label">
				    Schedule
			    </th>
			    <td colspan="2" class="shub_column last">
				    <input type="radio" name="schedule_send" id="schedule_send_now" value="now" checked>
					<label for="schedule_send_now">Send Now</label>
				    <input type="radio" name="schedule_send" id="schedule_send_later" value="later">
					<label for="schedule_send_later">Send Later</label>

				    <div id="schedule_send_later_box" style="display:none;">
					    <input type="text" name="schedule_date" value="" class="support_hub_date_field">
					    <input type="text" name="schedule_time" value="" class="support_hub_time_field">
					    <br/><strong>Please note:</strong> you cannot schedule Picture posts/tweets.
					    <br/><small> Currently: <?php 
echo shub_print_date(current_time('timestamp'), true);
?>
 (Leave blank to send now, or pick a date in the future.)</small>
				    </div>
			    </td>
		    </tr>
		    <tr>
			    <th class="shub_compose_label">
				    Track Clicks
			    </th>
			    <td colspan="2" class="shub_column last">
				    <input type="checkbox" name="track_links" value="1" checked> Yes, track link clicks.
				    <br/><small>If enabled, all links in above messages will be automatically changed (eg: <?php 
$new_link = trailingslashit(get_site_url());
$new_link .= strpos($new_link, '?') === false ? '?' : '&';
$new_link .= _support_hub_TWITTER_LINK_REWRITE_PREFIX . '=123ABC';