public static function count_read($no_cache = false)
 {
     if (wp_cache_get('mm_count_read') == false || $no_cache == true) {
         $model = new MM_Conversation_Model();
         global $wpdb;
         $sql = "SELECT COUNT(DISTINCT conversation.id)\r\n                    FROM " . $model->get_table() . " conversation\r\n                    INNER JOIN " . MM_Message_Status_Model::model()->get_table() . " mstat ON mstat.conversation_id=conversation.id\r\n                    INNER JOIN {$wpdb->postmeta} meta ON meta.meta_key='_conversation_id' AND meta.meta_value=conversation.id\r\n                    INNER JOIN {$wpdb->postmeta} send_to ON send_to.meta_key='_send_to' AND send_to.post_id=meta.post_id\r\n                    WHERE mstat.user_id = %d AND mstat.status = %d AND mstat.type = %d AND send_to.meta_value = %d  AND site_id=%d";
         $sql = $wpdb->prepare($sql, get_current_user_id(), MM_Message_Status_Model::STATUS_READ, MM_Message_Status_Model::TYPE_CONVERSATION, get_current_user_id(), get_current_blog_id());
         $count = $wpdb->get_var($sql);
         wp_cache_set('mm_count_read', $count);
     }
     return wp_cache_get('mm_count_read');
 }
 function _reply_message($conv_id, $message_id, $user_id, $model)
 {
     //load conversation
     $conversation = MM_Conversation_Model::model()->find($conv_id);
     $conversation->status = MM_Message_Status_Model::STATUS_UNREAD;
     //we will add new message to this conversation
     $conversation->save();
     //update users from this conversation, now save the message
     $m = new MM_Message_Model();
     $m->import($model->export());
     $m->send_to = $user_id;
     $m->conversation_id = $conversation->id;
     $m->status = MM_Message_Model::UNREAD;
     $mess = MM_Message_Model::model()->find($message_id);
     if (!empty($m->reply_to)) {
         if (!is_numeric($m->reply_to)) {
             wp_send_json(array('status' => 'fail', 'errors' => $model->get_errors()));
             exit;
         }
         $title = $m->subject;
         $description = $m->content;
         $price = $m->reply_to;
         $product_html = get_custom_order_button($title, $description, $price);
     }
     if (empty($m->subject)) {
         $m->subject = __("Re:", mmg()->domain) . ' ' . $mess->subject;
     }
     if (isset($product_html)) {
         $m->subject = __('You have new estimates!', 'artgorae');
         $m->content = __('You have new estimates!', 'artgorae') . '<br/><br/>' . $product_html;
     }
     $m->save();
     //update status for send to
     $status = MM_Message_Status_Model::model()->find_one_with_attributes(array('conversation_id' => $conversation->id, 'user_id' => $user_id));
     if (is_object($status)) {
         $status->status = MM_Message_Status_Model::STATUS_UNREAD;
         $status->save();
     }
     //update index
     $conversation->update_index($m->id);
     do_action('mm_message_sent', $m);
 }