/**
 * Post by email routine for legacy forums.
 *
 * Validates the parsed data and posts the various BuddyPress content.
 *
 * @since 1.0-RC3
 *
 * @param bool $retval True by default.
 * @param array $data {
 *     An array of arguments.
 *
 *     @type array $headers Email headers.
 *     @type string $content The email body content.
 *     @type string $subject The email subject line.
 *     @type int $user_id The user ID who sent the email.
 *     @type bool $is_html Whether the email content is HTML or not.
 *     @type int $i The email message number.
 * }
 * @param array $params Parsed paramaters from the email address querystring.
 *   See {@link BP_Reply_By_Email_Parser::get_parameters()}.
 * @return array|object Array of the parsed item on success. WP_Error object
 *  on failure.
 */
function bp_rbe_legacy_forums_post($retval, $data, $params)
{
    // Forum reply
    if (!empty($params['t'])) {
        if (bp_is_active('groups') && bp_is_active('forums')) {
            bp_rbe_log('Message #' . $data['i'] . ': this is a forum reply');
            // get all group member data for the user in one swoop!
            $group_member_data = bp_rbe_get_group_member_info($data['user_id'], $params['g']);
            // user is not a member of the group anymore
            if (empty($group_member_data)) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_not_group_member' );
                return new WP_Error('user_not_group_member');
            }
            // user is banned from group
            if ((int) $group_member_data->is_banned == 1) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_banned_from_group' );
                return new WP_Error('user_banned_from_group');
            }
            // Don't allow reply flooding
            // Only available in 1.6+
            if (function_exists('bp_forums_reply_exists') && bp_forums_reply_exists(esc_sql($data['content']), $params['t'], $data['user_id'])) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'forum_reply_exists' );
                return new WP_Error('forum_reply_exists');
            }
            /* okay, we should be good to post now! */
            $forum_post_id = bp_rbe_groups_new_group_forum_post(array('post_text' => $data['content'], 'topic_id' => $params['t'], 'user_id' => $data['user_id'], 'group_id' => $params['g']));
            if (!$forum_post_id) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'forum_reply_fail' );
                return new WP_Error('forum_reply_fail');
            }
            bp_rbe_log('Message #' . $data['i'] . ': forum reply successfully posted!');
            // could potentially add attachments
            // @todo
            do_action('bp_rbe_new_forum_post', false, $forum_post_id, $data['user_id'], $params['g'], $data['headers']);
            return array('legacy_forums_post_id' => $forum_post_id);
        }
        // New forum topic
    } elseif (!empty($params['g'])) {
        if (bp_is_active('groups') && bp_is_active('forums')) {
            bp_rbe_log('Message #' . $data['i'] . ': this is a new forum topic');
            bp_rbe_log('Message #' . $data['i'] . ': body contents - ' . $data['content']);
            bp_rbe_log('Subject - ' . $subject);
            if (empty($data['content']) || empty($data['subject'])) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'new_forum_topic_empty' );
                return new WP_Error('new_forum_topic_empty');
            }
            // get all group member data for the user in one swoop!
            $group_member_data = bp_rbe_get_group_member_info($data['user_id'], $params['g']);
            // user is not a member of the group anymore
            if (empty($group_member_data)) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_not_group_member' );
                return new WP_Error('user_not_group_member');
            }
            // user is banned from group
            if ((int) $group_member_data->is_banned == 1) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_banned_from_group' );
                return new WP_Error('user_banned_from_group');
            }
            /* okay, we should be good to post now! */
            $topic = bp_rbe_groups_new_group_forum_topic(array('topic_title' => $data['subject'], 'topic_text' => $data['content'], 'user_id' => $data['user_id'], 'group_id' => $params['g']));
            if (!$topic) {
                //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'new_topic_fail' );
                return new WP_Error('new_topic_fail');
            }
            bp_rbe_log('Message #' . $data['i'] . ': forum topic successfully posted!');
            // could potentially add attachments
            do_action('bp_rbe_new_forum_topic', false, $topic, $data['user_id'], $params['g'], $data['headers']);
            return array('legacy_forums_topic_id' => $topic);
        }
    }
}
 /**
  * Post new topic by email handler.
  *
  * For bbPress, the logic in this method is the same as {@link bbp_new_topic_handler()}.
  * It's duplicated because bbPress doesn't utilize hooks for verifying topics.
  *
  * @todo No fancy support for topic tags, subscriptions yet. Will probably need shortcodes.
  *
  * @param array $data {
  *     An array of arguments.
  *
  *     @type array $headers Email headers.
  *     @type string $content The email body content.
  *     @type string $subject The email subject line.
  *     @type int $user_id The user ID who sent the email.
  *     @type bool $is_html Whether the email content is HTML or not.
  *     @type int $i The email message number.
  * }
  * @param array $params Parsed paramaters from the email address querystring.
  *   See {@link BP_Reply_By_Email_Parser::get_parameters()}.
  * @return array|object Array of the parsed item on success. WP_Error object
  *  on failure.
  */
 private function post_new_topic($data, $params)
 {
     //private function post_new_topic( $connection, $i, $headers, $params, $body, $topic_author ) {
     /** SETUP DATA ***************************************************/
     $i = $data['i'];
     $topic_author = $data['user_id'];
     $forum_id = $params[$this->forum_id_param];
     /* current email is a bbPress new topic, let's proceed! */
     // let RBE know that we're in the process of rendering a bbP new topic
     // BuddyPress group new topic
     if (!empty($params[$this->item_id_param])) {
         bp_rbe_log('Message #' . $i . ': this is a bbPress group forum new topic');
         // bbPress
     } else {
         bp_rbe_log('Message #' . $i . ': this is a bbPress new topic');
     }
     // other variables
     $anonymous_data = 0;
     /** GROUP PERMISSIONS ********************************************/
     // posting from a BP group
     if (!empty($params[$this->item_id_param])) {
         global $bp;
         // set group ID and cache it in global for later use
         // $bp->rbe->temp->group_id gets passed to the set_group_id() method later on
         $group_id = $bp->rbe->temp->group_id = $params[$this->item_id_param];
         // get all group member data for the user in one swoop!
         $group_member_data = bp_rbe_get_group_member_info($topic_author, $group_id);
         // user is not a member of the group anymore
         if (empty($group_member_data)) {
             //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'user_not_group_member' );
             return new WP_Error('user_not_group_member', '', $data);
         }
         // user is banned from group
         if ((int) $group_member_data->is_banned == 1) {
             //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'user_banned_from_group' );
             return new WP_Error('user_banned_from_group', '', $data);
         }
         // override groups_get_current_group() with our cached group ID
         add_filter('groups_get_current_group', array($this, 'set_group_id'));
         // temporarily add some GES filters here
         add_filter('bp_ass_activity_notification_subject', 'wp_specialchars_decode');
         add_filter('bp_ass_activity_notification_content', 'wp_specialchars_decode');
     }
     /** TOPIC / FORUM PERMISSIONS ************************************/
     // Allow member to pass default cap checks.
     // The reason why we keep the user_can() checks below is b/c bbPress
     // plugins may disable cap access for a specific user if they have hooked into
     // the 'bbp_map_meta_caps' filter.
     add_filter('bbp_map_meta_caps', array($this, 'map_forum_meta_caps'), 5, 4);
     // User cannot create topics
     if (!user_can($topic_author, 'publish_topics')) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_topic_permissions' );
         return new WP_Error('bbp_topic_permissions', '', $data);
     }
     // Forum is a category
     if (bbp_is_forum_category($forum_id)) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_edit_topic_forum_category' );
         //bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
         return new WP_Error('bbp_edit_topic_forum_category', '', $data);
         // Forum is not a category
     } else {
         // Forum is closed and user cannot access
         if (bbp_is_forum_closed($forum_id) && !user_can($topic_author, 'edit_forum')) {
             //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_edit_topic_forum_closed' );
             //bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
             return new WP_Error('bbp_edit_topic_forum_closed', '', $data);
         }
         // Forum is private and user cannot access
         if (bbp_is_forum_private($forum_id)) {
             if (!user_can($topic_author, 'read_private_forums')) {
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_edit_topic_forum_private' );
                 //bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
                 return new WP_Error('bbp_edit_topic_forum_private', '', $data);
             }
         }
         // Forum is hidden and user cannot access
         if (bbp_is_forum_hidden($forum_id)) {
             if (!user_can($topic_author, 'read_hidden_forums')) {
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_edit_topic_forum_hidden' );
                 //bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
                 return new WP_Error('bbp_edit_topic_forum_hidden', '', $data);
             }
         }
     }
     /** UNFILTERED HTML **********************************************/
     // Remove wp_filter_kses filters from title and content for capable users
     if (user_can($topic_author, 'unfiltered_html')) {
         remove_filter('bbp_new_topic_pre_title', 'wp_filter_kses');
         remove_filter('bbp_new_topic_pre_content', 'wp_filter_kses');
     }
     /** TOPIC DATA ***************************************************/
     $topic_content = $data['content'];
     $topic_title = $data['subject'];
     bp_rbe_log('Message #' . $i . ': body contents - ' . $topic_content);
     bp_rbe_log('Subject - ' . $topic_title);
     if (empty($topic_content) || empty($topic_title)) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_new_forum_topic_empty' );
         return new WP_Error('bbp_new_forum_topic_empty', '', $data);
     }
     // Filter and sanitize
     $topic_title = apply_filters('bbp_new_topic_pre_title', $topic_title);
     $topic_content = apply_filters('bbp_new_topic_pre_content', $topic_content);
     /** Topic Tags ****************************************************/
     /* TODO
     		if ( bbp_allow_topic_tags() ) {
     
     			// Escape tag input
     			$terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
     
     			// Explode by comma
     			if ( strstr( $terms, ',' ) ) {
     				$terms = explode( ',', $terms );
     			}
     
     			// Add topic tag ID as main key
     			$terms = array( bbp_get_topic_tag_tax_id() => $terms );
     		}
     		*/
     /** TOPIC MODERATION *********************************************/
     // Post Flooding
     if (!bbp_check_for_flood($anonymous_data, $topic_author)) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_topic_flood' );
         //bbp_add_error( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
         return new WP_Error('bbp_topic_flood', '', $data);
     }
     // Topic Duplicate
     if (!bbp_check_for_duplicate(array('post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data))) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_topic_duplicate' );
         return new WP_Error('bbp_topic_duplicate', '', $data);
     }
     // Topic Blacklist
     if (!bbp_check_for_blacklist($anonymous_data, $topic_author, $topic_title, $topic_content)) {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_topic_blacklist' );
         return new WP_Error('bbp_topic_blacklist', '', $data);
     }
     // Topic Status
     // Maybe put into moderation
     if (!bbp_check_for_moderation($anonymous_data, $topic_author, $topic_title, $topic_content)) {
         $topic_status = bbp_get_pending_status_id();
         // Default
     } else {
         $topic_status = bbp_get_public_status_id();
     }
     /** POSTING TIME! ************************************************/
     // bbP hook before save
     do_action('bbp_new_topic_pre_extras', $forum_id);
     // Setup reply data
     $topic_data = apply_filters('bbp_new_topic_pre_insert', array('post_author' => $topic_author, 'post_title' => $topic_title, 'post_content' => $topic_content, 'post_status' => $topic_status, 'post_parent' => $forum_id, 'post_type' => bbp_get_topic_post_type(), 'comment_status' => 'closed'));
     // Insert topic
     $topic_id = wp_insert_post($topic_data);
     // Topic posted!
     if (!is_wp_error($topic_id)) {
         // more internal logging
         bp_rbe_log('Message #' . $i . ': bbPress topic successfully posted!');
         // Problem posting
     } else {
         //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bbp_topic_error' );
         return new WP_Error('bbp_topic_error', '', $data);
     }
     /** AFTER POSTING ************************************************/
     // stuff that needs to happen after a bbP topic is posted occurs here... bbP
     // should preferably do the following at the 'bbp_new_reply' hook, until then
     // do what bbP does inline.
     // Trash Check ////////////////////////////////////////////////////
     // If the forum is trash, or the topic_status is switched to
     // trash, trash it properly
     if (get_post_field('post_status', $forum_id) == bbp_get_trash_status_id() || $topic_data['post_status'] == bbp_get_trash_status_id()) {
         // Trash the reply
         wp_trash_post($topic_id);
     }
     // Spam Check /////////////////////////////////////////////////////
     // If reply or topic are spam, officially spam this reply
     if ($topic_data['post_status'] == bbp_get_spam_status_id()) {
         add_post_meta($topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id());
     }
     // Reply By Email /////////////////////////////////////////////////
     // Add a RBE marker to the post's meta
     // Could potentially show that post was made via email on the frontend
     add_post_meta($topic_id, 'bp_rbe', 1);
     /** POST HOOKS ***************************************************/
     // RBE Custom Hooks ///////////////////////////////////////////////
     // change activity action
     add_filter('bbp_before_record_activity_parse_args', array($this, 'change_activity_action'));
     // add RBE's special activity hook
     add_action('bp_activity_after_save', array($this, 'activity_rbe_hook'));
     // bbPress Topic Hooks ////////////////////////////////////////////
     do_action('bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author);
     do_action('bbp_new_topic_post_extras', $topic_id);
     return array('bbp_topic_id' => $topic_id);
 }