コード例 #1
0
 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);
 }