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 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);
 }
<?php

if (!isset($shub_account_id) || !isset($shub_message_id)) {
    exit;
}
?>

	<?php 
if ($shub_account_id && $shub_message_id) {
    $bbpress = new shub_bbpress_account($shub_account_id);
    if ($shub_account_id && $bbpress->get('shub_account_id') == $shub_account_id) {
        $bbpress_message = new shub_bbpress_message($bbpress, false, $shub_message_id);
        $bbpress_message->output_message_page('popup');
    }
}
if ($shub_account_id && !(int) $shub_message_id) {
    $bbpress = new shub_bbpress_account($shub_account_id);
    if ($shub_account_id && $bbpress->get('shub_account_id') == $shub_account_id) {
        /* @var $groups shub_bbpress_item[] */
        $groups = $bbpress->get('groups');
        //print_r($groups);
        ?>
	    <form action="" method="post" enctype="multipart/form-data">
		    <input type="hidden" name="_process" value="send_bbpress_message">
			<?php 
        wp_nonce_field('send-bbpress' . (int) $bbpress->get('shub_account_id'));
        ?>
		    <?php 
        $fieldset_data = array('heading' => array('type' => 'h3', 'title' => 'Compose message'), 'class' => 'tableclass tableclass_form tableclass_full', 'elements' => array('item' => array('title' => __('bbpress Group', 'support_hub'), 'fields' => array()), 'message' => array('title' => __('message', 'support_hub'), 'field' => array('type' => 'textarea', 'name' => 'message', 'id' => 'bbpress_compose_message', 'value' => '')), 'type' => array('title' => __('Type', 'support_hub'), 'fields' => array('<input type="radio" name="post_type" id="post_type_wall" value="wall" checked> ', '<label for="post_type_wall">', __('Wall Post', 'support_hub'), '</label>', '<input type="radio" name="post_type" id="post_type_link" value="link"> ', '<label for="post_type_link">', __('Link Post', 'support_hub'), '</label>', '<input type="radio" name="post_type" id="post_type_picture" value="picture"> ', '<label for="post_type_picture">', __('Picture Post', 'support_hub'), '</label>')), 'link' => array('title' => __('Link', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link', 'id' => 'message_link_url', 'value' => ''), '<div id="bbpress_link_loading_message"></div>', '<span class="bbpress-type-link bbpress-type-option"></span>')), 'link_picture' => array('title' => __('Link Picture', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_picture', 'value' => ''), 'Full URL (eg: http://) to the picture to use for this link preview', '<span class="bbpress-type-link bbpress-type-option"></span>')), 'link_name' => array('title' => __('Link Title', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_name', 'value' => ''), 'Title to use instead of the automatically generated one from the Link page', '<span class="bbpress-type-link bbpress-type-option"></span>')), 'link_caption' => array('title' => __('Link Caption', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_caption', 'value' => ''), 'Caption to use instead of the automatically generated one from the Link page', '<span class="bbpress-type-link bbpress-type-option"></span>')), 'link_description' => array('title' => __('Link Description', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_description', 'value' => ''), 'Description to use instead of the automatically generated one from the Link page', '<span class="bbpress-type-link bbpress-type-option"></span>')), 'picture' => array('title' => __('Picture', 'support_hub'), 'fields' => array('<input type="file" name="picture" value="">', '<span class="bbpress-type-picture bbpress-type-option"></span>')), 'schedule' => array('title' => __('Schedule', 'support_hub'), 'fields' => array(array('type' => 'date', 'name' => 'schedule_date', 'value' => ''), array('type' => 'time', 'name' => 'schedule_time', 'value' => ''), ' ', sprintf(__('Currently: %s', 'support_hub'), date('c')), ' (Leave blank to send now, or pick a date in the future.)')), 'debug' => array('title' => __('Debug', 'support_hub'), 'field' => array('type' => 'check', 'name' => 'debug', 'value' => '1', 'checked' => false, 'help' => 'Show debug output while posting the message'))));
        foreach ($groups as $item_id => $group) {
            $fieldset_data['elements']['item']['fields'][] = '<div id="bbpress_compose_group_select">' . '<input type="checkbox" name="compose_group_id[' . $item_id . ']" value="1" checked> ' . '<img src="//graph.bbpress.com/' . $item_id . '/picture"> ' . htmlspecialchars($group->get('item_name')) . '</div>';