Example #1
0
/**
 * Return URL to the profile page of a user
 *
 * @since 2.0.0 bbPress (r2688)
 *
 * @param int $user_id Optional. User id
 * @param string $user_nicename Optional. User nicename
 * @uses bbp_get_user_id() To get user id
 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
 * @uses add_query_arg() To add custom args to the url
 * @uses home_url() To get blog home url
 * @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user
 *                        profile url, user id and user nicename
 * @return string User profile url
 */
function bbp_get_user_profile_url($user_id = 0, $user_nicename = '')
{
    // Use displayed user ID if there is one, and one isn't requested
    $user_id = bbp_get_user_id($user_id);
    if (empty($user_id)) {
        return false;
    }
    // Allow early overriding of the profile URL to cut down on processing
    $early_profile_url = apply_filters('bbp_pre_get_user_profile_url', $user_id);
    if (is_string($early_profile_url)) {
        return $early_profile_url;
    }
    // Pretty permalinks
    if (bbp_use_pretty_urls()) {
        // Get username if not passed
        if (empty($user_nicename)) {
            $user_nicename = bbp_get_user_nicename($user_id);
        }
        $url = trailingslashit(bbp_get_root_url() . bbp_get_user_slug()) . $user_nicename;
        $url = user_trailingslashit($url);
        $url = home_url($url);
        // Unpretty permalinks
    } else {
        $url = add_query_arg(array(bbp_get_user_rewrite_id() => $user_id), home_url('/'));
    }
    return apply_filters('bbp_get_user_profile_url', $url, $user_id, $user_nicename);
}
Example #2
0
/**
 * Register the default repair tools
 *
 * @since 2.6.0 bbPress (r5885)
 */
function bbp_register_default_repair_tools()
{
    // Topic meta
    bbp_register_repair_tool(array('id' => 'bbp-sync-topic-meta', 'description' => __('Recalculate parent topic for each reply', 'bbpress'), 'callback' => 'bbp_admin_repair_topic_meta', 'priority' => 5, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_reply_post_type())));
    // Forum meta
    bbp_register_repair_tool(array('id' => 'bbp-sync-forum-meta', 'description' => __('Recalculate parent forum for each topic and reply', 'bbpress'), 'callback' => 'bbp_admin_repair_forum_meta', 'priority' => 10, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_reply_post_type())));
    // Forum visibility
    bbp_register_repair_tool(array('id' => 'bbp-sync-forum-visibility', 'description' => __('Recalculate private and hidden forums', 'bbpress'), 'callback' => 'bbp_admin_repair_forum_visibility', 'priority' => 15, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_forum_post_type())));
    // Sync all topics in all forums
    bbp_register_repair_tool(array('id' => 'bbp-sync-all-topics-forums', 'description' => __('Recalculate last activity in each topic and forum', 'bbpress'), 'callback' => 'bbp_admin_repair_freshness', 'priority' => 20, 'overhead' => esc_html__('High', 'bbpress'), 'components' => array(bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type())));
    // Sync all sticky topics in all forums
    bbp_register_repair_tool(array('id' => 'bbp-sync-all-topics-sticky', 'description' => __('Recalculate sticky relationship of each topic', 'bbpress'), 'callback' => 'bbp_admin_repair_sticky', 'priority' => 25, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_topic_post_type())));
    // Sync all hierarchical reply positions
    bbp_register_repair_tool(array('id' => 'bbp-sync-all-reply-positions', 'description' => __('Recalculate the position of each reply', 'bbpress'), 'callback' => 'bbp_admin_repair_reply_menu_order', 'priority' => 30, 'overhead' => esc_html__('High', 'bbpress'), 'components' => array(bbp_get_reply_post_type())));
    // Sync all BuddyPress group forum relationships
    bbp_register_repair_tool(array('id' => 'bbp-group-forums', 'description' => __('Repair BuddyPress Group Forum relationships', 'bbpress'), 'callback' => 'bbp_admin_repair_group_forum_relationship', 'priority' => 35, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_forum_post_type())));
    // Update closed topic counts
    bbp_register_repair_tool(array('id' => 'bbp-sync-closed-topics', 'description' => __('Repair closed topics', 'bbpress'), 'callback' => 'bbp_admin_repair_closed_topics', 'priority' => 40, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_topic_post_type())));
    // Count topics
    bbp_register_repair_tool(array('id' => 'bbp-forum-topics', 'description' => __('Count topics in each forum', 'bbpress'), 'callback' => 'bbp_admin_repair_forum_topic_count', 'priority' => 45, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_forum_post_type(), bbp_get_topic_post_type())));
    // Count forum replies
    bbp_register_repair_tool(array('id' => 'bbp-forum-replies', 'description' => __('Count replies in each forum', 'bbpress'), 'callback' => 'bbp_admin_repair_forum_reply_count', 'priority' => 50, 'overhead' => esc_html__('High', 'bbpress'), 'components' => array(bbp_get_forum_post_type(), bbp_get_reply_post_type())));
    // Count topic replies
    bbp_register_repair_tool(array('id' => 'bbp-topic-replies', 'description' => __('Count replies in each topic', 'bbpress'), 'callback' => 'bbp_admin_repair_topic_reply_count', 'priority' => 55, 'overhead' => esc_html__('High', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_reply_post_type())));
    // Count topic voices
    bbp_register_repair_tool(array('id' => 'bbp-topic-voices', 'description' => __('Count voices in each topic', 'bbpress'), 'callback' => 'bbp_admin_repair_topic_voice_count', 'priority' => 60, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_user_rewrite_id())));
    // Count non-published replies to each topic
    bbp_register_repair_tool(array('id' => 'bbp-topic-hidden-replies', 'description' => __('Count pending, spammed, & trashed replies in each topic', 'bbpress'), 'callback' => 'bbp_admin_repair_topic_hidden_reply_count', 'priority' => 65, 'overhead' => esc_html__('High', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_reply_post_type())));
    // Recount topics for each user
    bbp_register_repair_tool(array('id' => 'bbp-user-topics', 'description' => __('Recount topics for each user', 'bbpress'), 'callback' => 'bbp_admin_repair_user_topic_count', 'priority' => 70, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_user_rewrite_id())));
    // Recount topics for each user
    bbp_register_repair_tool(array('id' => 'bbp-user-replies', 'description' => __('Recount replies for each user', 'bbpress'), 'callback' => 'bbp_admin_repair_user_reply_count', 'priority' => 75, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_reply_post_type(), bbp_get_user_rewrite_id())));
    // Remove unpublished topics from user favorites
    bbp_register_repair_tool(array('id' => 'bbp-user-favorites', 'description' => __('Remove unpublished topics from user favorites', 'bbpress'), 'callback' => 'bbp_admin_repair_user_favorites', 'priority' => 80, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_user_rewrite_id())));
    // Remove unpublished topics from user subscriptions
    bbp_register_repair_tool(array('id' => 'bbp-user-topic-subscriptions', 'description' => __('Remove unpublished topics from user subscriptions', 'bbpress'), 'callback' => 'bbp_admin_repair_user_topic_subscriptions', 'priority' => 85, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_topic_post_type(), bbp_get_user_rewrite_id())));
    // Remove unpublished forums from user subscriptions
    bbp_register_repair_tool(array('id' => 'bbp-user-forum-subscriptions', 'description' => __('Remove unpublished forums from user subscriptions', 'bbpress'), 'callback' => 'bbp_admin_repair_user_forum_subscriptions', 'priority' => 90, 'overhead' => esc_html__('Medium', 'bbpress'), 'components' => array(bbp_get_forum_post_type(), bbp_get_user_rewrite_id())));
    // Remove unpublished forums from user subscriptions
    bbp_register_repair_tool(array('id' => 'bbp-user-role-map', 'description' => __('Remap existing users to default forum roles', 'bbpress'), 'callback' => 'bbp_admin_repair_user_roles', 'priority' => 95, 'overhead' => esc_html__('Low', 'bbpress'), 'components' => array(bbp_get_user_rewrite_id())));
}
Example #3
0
 /**
  * Add permalink structures for new archive-style destinations.
  *
  * - Users
  * - Topic Views
  * - Search
  *
  * @since bbPress (r4930)
  */
 public static function add_permastructs()
 {
     // Get unique ID's
     $user_id = bbp_get_user_rewrite_id();
     $view_id = bbp_get_view_rewrite_id();
     $search_id = bbp_get_search_rewrite_id();
     // Get root slugs
     $user_slug = bbp_get_user_slug();
     $view_slug = bbp_get_view_slug();
     $search_slug = bbp_get_search_slug();
     // User Permastruct
     add_permastruct($user_id, $user_slug . '/%' . $user_id . '%', array('with_front' => false, 'ep_mask' => EP_NONE, 'paged' => false, 'feed' => false, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => false));
     // Topic View Permastruct
     add_permastruct($view_id, $view_slug . '/%' . $view_id . '%', array('with_front' => false, 'ep_mask' => EP_NONE, 'paged' => false, 'feed' => false, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => false));
     // Search Permastruct
     add_permastruct($user_id, $search_slug . '/%' . $search_id . '%', array('with_front' => false, 'ep_mask' => EP_NONE, 'paged' => true, 'feed' => false, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => false));
 }
Example #4
0
/**
 * Add checks for bbPress conditions to parse_query action
 *
 * If it's a user page, WP_Query::bbp_is_single_user is set to true.
 * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true
 * and the the 'wp-admin/includes/user.php' file is included.
 * In addition, on user/user edit pages, WP_Query::home is set to false & query
 * vars 'bbp_user_id' with the displayed user id and 'author_name' with the
 * displayed user's nicename are added.
 *
 * If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true
 * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true
 * If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
 *
 * If it's a view page, WP_Query::bbp_is_view is set to true
 * If it's a search page, WP_Query::bbp_is_search is set to true
 *
 * @since bbPress (r2688)
 *
 * @param WP_Query $posts_query
 *
 * @uses get_query_var() To get {@link WP_Query} query var
 * @uses is_email() To check if the string is an email
 * @uses get_user_by() To try to get the user by email and nicename
 * @uses get_userdata() to get the user data
 * @uses current_user_can() To check if the current user can edit the user
 * @uses is_user_member_of_blog() To check if user profile page exists
 * @uses WP_Query::set_404() To set a 404 status
 * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
 * @uses bbp_get_view_query_args() To get the view query args
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses remove_action() To remove the auto save post revision action
 */
function bbp_parse_query($posts_query)
{
    // Bail if $posts_query is not the main loop
    if (!$posts_query->is_main_query()) {
        return;
    }
    // Bail if filters are suppressed on this query
    if (true === $posts_query->get('suppress_filters')) {
        return;
    }
    // Bail if in admin
    if (is_admin()) {
        return;
    }
    // Get query variables
    $bbp_view = $posts_query->get(bbp_get_view_rewrite_id());
    $bbp_user = $posts_query->get(bbp_get_user_rewrite_id());
    $is_edit = $posts_query->get(bbp_get_edit_rewrite_id());
    // It is a user page - We'll also check if it is user edit
    if (!empty($bbp_user)) {
        /** Find User *********************************************************/
        // Setup the default user variable
        $the_user = false;
        // If using pretty permalinks, use the email or slug
        if (get_option('permalink_structure')) {
            // Email was passed
            if (is_email($bbp_user)) {
                $the_user = get_user_by('email', $bbp_user);
                // Try nicename
            } else {
                $the_user = get_user_by('slug', $bbp_user);
            }
        }
        // No user found by slug/email, so try the ID if it's numeric
        if (empty($the_user) && is_numeric($bbp_user)) {
            $the_user = get_user_by('id', $bbp_user);
        }
        // 404 and bail if user does not have a profile
        if (empty($the_user->ID) || !bbp_user_has_profile($the_user->ID)) {
            $posts_query->set_404();
            return;
        }
        /** User Exists *******************************************************/
        $is_favs = $posts_query->get(bbp_get_user_favorites_rewrite_id());
        $is_subs = $posts_query->get(bbp_get_user_subscriptions_rewrite_id());
        $is_topics = $posts_query->get(bbp_get_user_topics_rewrite_id());
        $is_replies = $posts_query->get(bbp_get_user_replies_rewrite_id());
        // View or edit?
        if (!empty($is_edit)) {
            // We are editing a profile
            $posts_query->bbp_is_single_user_edit = true;
            // Load the core WordPress contact methods
            if (!function_exists('_wp_get_user_contactmethods')) {
                include_once ABSPATH . 'wp-includes/registration.php';
            }
            // Load the edit_user functions
            if (!function_exists('edit_user')) {
                require_once ABSPATH . 'wp-admin/includes/user.php';
            }
            // Load the grant/revoke super admin functions
            if (is_multisite() && !function_exists('revoke_super_admin')) {
                require_once ABSPATH . 'wp-admin/includes/ms.php';
            }
            // Editing a user
            $posts_query->bbp_is_edit = true;
            // User favorites
        } elseif (!empty($is_favs)) {
            $posts_query->bbp_is_single_user_favs = true;
            // User subscriptions
        } elseif (!empty($is_subs)) {
            $posts_query->bbp_is_single_user_subs = true;
            // User topics
        } elseif (!empty($is_topics)) {
            $posts_query->bbp_is_single_user_topics = true;
            // User topics
        } elseif (!empty($is_replies)) {
            $posts_query->bbp_is_single_user_replies = true;
            // User profile
        } else {
            $posts_query->bbp_is_single_user_profile = true;
        }
        // Looking at a single user
        $posts_query->bbp_is_single_user = true;
        // Make sure 404 is not set
        $posts_query->is_404 = false;
        // Correct is_home variable
        $posts_query->is_home = false;
        // User is looking at their own profile
        if (get_current_user_id() === $the_user->ID) {
            $posts_query->bbp_is_single_user_home = true;
        }
        // Set bbp_user_id for future reference
        $posts_query->set('bbp_user_id', $the_user->ID);
        // Set author_name as current user's nicename to get correct posts
        $posts_query->set('author_name', $the_user->user_nicename);
        // Set the displayed user global to this user
        bbpress()->displayed_user = $the_user;
        // View Page
    } elseif (!empty($bbp_view)) {
        // Check if the view exists by checking if there are query args are set
        $view_args = bbp_get_view_query_args($bbp_view);
        // Bail if view args is false (view isn't registered)
        if (false === $view_args) {
            $posts_query->set_404();
            return;
        }
        // Correct is_home variable
        $posts_query->is_home = false;
        // We are in a custom topic view
        $posts_query->bbp_is_view = true;
        // Search Page
    } elseif (isset($posts_query->query_vars[bbp_get_search_rewrite_id()])) {
        // Check if there are search query args set
        $search_terms = bbp_get_search_terms();
        if (!empty($search_terms)) {
            $posts_query->bbp_search_terms = $search_terms;
        }
        // Correct is_home variable
        $posts_query->is_home = false;
        // We are in a search query
        $posts_query->bbp_is_search = true;
        // Forum/Topic/Reply Edit Page
    } elseif (!empty($is_edit)) {
        // Get the post type from the main query loop
        $post_type = $posts_query->get('post_type');
        // Check which post_type we are editing, if any
        if (!empty($post_type)) {
            switch ($post_type) {
                // We are editing a forum
                case bbp_get_forum_post_type():
                    $posts_query->bbp_is_forum_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
                    // We are editing a topic
                // We are editing a topic
                case bbp_get_topic_post_type():
                    $posts_query->bbp_is_topic_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
                    // We are editing a reply
                // We are editing a reply
                case bbp_get_reply_post_type():
                    $posts_query->bbp_is_reply_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
            }
            // We are editing a topic tag
        } elseif (bbp_is_topic_tag()) {
            $posts_query->bbp_is_topic_tag_edit = true;
            $posts_query->bbp_is_edit = true;
        }
        // We save post revisions on our own
        remove_action('pre_post_update', 'wp_save_post_revision');
        // Topic tag page
    } elseif (bbp_is_topic_tag()) {
        $posts_query->set('bbp_topic_tag', get_query_var('term'));
        $posts_query->set('post_type', bbp_get_topic_post_type());
        $posts_query->set('posts_per_page', bbp_get_topics_per_page());
        // Do topics on forums root
    } elseif (is_post_type_archive(array(bbp_get_forum_post_type(), bbp_get_topic_post_type())) && 'topics' === bbp_show_on_root()) {
        $posts_query->bbp_show_topics_on_root = true;
    }
}
Example #5
0
/**
 * Return URL to the profile page of a user
 *
 * @since bbPress (r2688)
 *
 * @param int $user_id Optional. User id
 * @param string $user_nicename Optional. User nicename
 * @uses bbp_get_user_id() To get user id
 * @uses WP_Rewrite::using_permalinks() To check if the blog is using
 *                                       permalinks
 * @uses add_query_arg() To add custom args to the url
 * @uses home_url() To get blog home url
 * @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user
 *                        profile url, user id and user nicename
 * @return string User profile url
 */
function bbp_get_user_profile_url($user_id = 0, $user_nicename = '')
{
    global $wp_rewrite;
    // Use displayed user ID if there is one, and one isn't requested
    $user_id = bbp_get_user_id($user_id);
    if (empty($user_id)) {
        return false;
    }
    // Allow early overriding of the profile URL to cut down on processing
    $early_profile_url = apply_filters('bbp_pre_get_user_profile_url', (int) $user_id);
    if (is_string($early_profile_url)) {
        return $early_profile_url;
    }
    // Pretty permalinks
    if ($wp_rewrite->using_permalinks()) {
        $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%';
        // Get username if not passed
        if (empty($user_nicename)) {
            $user = get_userdata($user_id);
            if (!empty($user->user_nicename)) {
                $user_nicename = $user->user_nicename;
            }
        }
        $url = str_replace('%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url);
        $url = home_url(user_trailingslashit($url));
        // Unpretty permalinks
    } else {
        $url = add_query_arg(array(bbp_get_user_rewrite_id() => $user_id), home_url('/'));
    }
    return apply_filters('bbp_get_user_profile_url', $url, $user_id, $user_nicename);
}
Example #6
0
 /**
  * Register bbPress-specific rewrite rules for uri's that are not
  * setup for us by way of custom post types or taxonomies. This includes:
  * - Front-end editing
  * - Topic views
  * - User profiles
  *
  * @since bbPress (r2688)
  * @param WP_Rewrite $wp_rewrite bbPress-sepecific rules are appended in
  *                                $wp_rewrite->rules
  */
 public static function generate_rewrite_rules($wp_rewrite)
 {
     // Slugs
     $view_slug = bbp_get_view_slug();
     $user_slug = bbp_get_user_slug();
     // Unique rewrite ID's
     $edit_id = bbp_get_edit_rewrite_id();
     $view_id = bbp_get_view_rewrite_id();
     $user_id = bbp_get_user_rewrite_id();
     $favs_id = bbp_get_user_favorites_rewrite_id();
     $subs_id = bbp_get_user_subscriptions_rewrite_id();
     $tops_id = bbp_get_user_topics_rewrite_id();
     $reps_id = bbp_get_user_replies_rewrite_id();
     // Rewrite rule matches used repeatedly below
     $root_rule = '/([^/]+)/?$';
     $edit_rule = '/([^/]+)/edit/?$';
     $feed_rule = '/([^/]+)/feed/?$';
     $page_rule = '/([^/]+)/page/?([0-9]{1,})/?$';
     // User profile rules
     $tops_rule = '/([^/]+)/topics/?$';
     $reps_rule = '/([^/]+)/replies/?$';
     $favs_rule = '/([^/]+)/' . bbp_get_user_favorites_slug() . '/?$';
     $subs_rule = '/([^/]+)/' . bbp_get_user_subscriptions_slug() . '/?$';
     $tops_page_rule = '/([^/]+)/topics/page/?([0-9]{1,})/?$';
     $reps_page_rule = '/([^/]+)/replies/page/?([0-9]{1,})/?$';
     $favs_page_rule = '/([^/]+)/' . bbp_get_user_favorites_slug() . '/page/?([0-9]{1,})/?$';
     $subs_page_rule = '/([^/]+)/' . bbp_get_user_subscriptions_slug() . '/page/?([0-9]{1,})/?$';
     // New bbPress specific rules to merge with existing that are not
     // handled automatically by custom post types or taxonomy types
     $bbp_rules = array(bbp_get_forum_slug() . $edit_rule => 'index.php?' . bbp_get_forum_post_type() . '=' . $wp_rewrite->preg_index(1) . '&' . $edit_id . '=1', bbp_get_topic_slug() . $edit_rule => 'index.php?' . bbp_get_topic_post_type() . '=' . $wp_rewrite->preg_index(1) . '&' . $edit_id . '=1', bbp_get_reply_slug() . $edit_rule => 'index.php?' . bbp_get_reply_post_type() . '=' . $wp_rewrite->preg_index(1) . '&' . $edit_id . '=1', bbp_get_topic_tag_tax_slug() . $edit_rule => 'index.php?' . bbp_get_topic_tag_tax_id() . '=' . $wp_rewrite->preg_index(1) . '&' . $edit_id . '=1', $user_slug . $tops_page_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $tops_id . '=1&paged=' . $wp_rewrite->preg_index(2), $user_slug . $reps_page_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $reps_id . '=1&paged=' . $wp_rewrite->preg_index(2), $user_slug . $favs_page_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $favs_id . '=1&paged=' . $wp_rewrite->preg_index(2), $user_slug . $subs_page_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $subs_id . '=1&paged=' . $wp_rewrite->preg_index(2), $user_slug . $tops_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $tops_id . '=1', $user_slug . $reps_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $reps_id . '=1', $user_slug . $favs_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $favs_id . '=1', $user_slug . $subs_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $subs_id . '=1', $user_slug . $edit_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1) . '&' . $edit_id . '=1', $user_slug . $root_rule => 'index.php?' . $user_id . '=' . $wp_rewrite->preg_index(1), $view_slug . $page_rule => 'index.php?' . $view_id . '=' . $wp_rewrite->preg_index(1) . '&paged=' . $wp_rewrite->preg_index(2), $view_slug . $feed_rule => 'index.php?' . $view_id . '=' . $wp_rewrite->preg_index(1) . '&feed=' . $wp_rewrite->preg_index(2), $view_slug . $root_rule => 'index.php?' . $view_id . '=' . $wp_rewrite->preg_index(1));
     // Merge bbPress rules with existing
     $wp_rewrite->rules = array_merge($bbp_rules, $wp_rewrite->rules);
     // Return merged rules
     return $wp_rewrite;
 }
/**
 * Add checks for bbPress conditions to parse_query action
 *
 * If it's a user page, WP_Query::bbp_is_single_user is set to true.
 * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true
 * and the the 'wp-admin/includes/user.php' file is included.
 * In addition, on user/user edit pages, WP_Query::home is set to false & query
 * vars 'bbp_user_id' with the displayed user id and 'author_name' with the
 * displayed user's nicename are added.
 *
 * If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true
 * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true
 * If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
 *
 * If it's a view page, WP_Query::bbp_is_view is set to true
 *
 * @since bbPress (r2688)
 *
 * @param WP_Query $posts_query
 *
 * @uses get_query_var() To get {@link WP_Query} query var
 * @uses is_email() To check if the string is an email
 * @uses get_user_by() To try to get the user by email and nicename
 * @uses WP_User to get the user data
 * @uses WP_Query::set_404() To set a 404 status
 * @uses current_user_can() To check if the current user can edit the user
 * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
 * @uses bbp_get_view_query_args() To get the view query args
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses remove_action() To remove the auto save post revision action
 */
function bbp_parse_query($posts_query)
{
    // Bail if $posts_query is not the main loop
    if (!$posts_query->is_main_query()) {
        return;
    }
    // Bail if filters are suppressed on this query
    if (true == $posts_query->get('suppress_filters')) {
        return;
    }
    // Bail if in admin
    if (is_admin()) {
        return;
    }
    // Get query variables
    $bbp_user = $posts_query->get(bbp_get_user_rewrite_id());
    $bbp_view = $posts_query->get(bbp_get_view_rewrite_id());
    $is_edit = $posts_query->get(bbp_get_edit_rewrite_id());
    // It is a user page - We'll also check if it is user edit
    if (!empty($bbp_user)) {
        // Not a user_id so try email and slug
        if (!is_numeric($bbp_user)) {
            // Email was passed
            if (is_email($bbp_user)) {
                $bbp_user = get_user_by('email', $bbp_user);
                // Try nicename
            } else {
                $bbp_user = get_user_by('slug', $bbp_user);
            }
            // If we were successful, set to ID
            if (is_object($bbp_user)) {
                $bbp_user = $bbp_user->ID;
            }
        }
        // Create new user
        $user = new WP_User($bbp_user);
        // Bail if no user
        if (!isset($user) || empty($user) || empty($user->ID)) {
            $posts_query->set_404();
            return;
        }
        /** User Exists *******************************************************/
        // View or edit?
        if (!empty($is_edit)) {
            // We are editing a profile
            $posts_query->bbp_is_single_user_edit = true;
            // Load the core WordPress contact methods
            if (!function_exists('_wp_get_user_contactmethods')) {
                include_once ABSPATH . 'wp-includes/registration.php';
            }
            // Load the edit_user functions
            if (!function_exists('edit_user')) {
                require_once ABSPATH . 'wp-admin/includes/user.php';
            }
            // Editing a user
            $posts_query->bbp_is_edit = true;
            // We are viewing a profile
        } else {
            $posts_query->bbp_is_single_user = true;
        }
        // Make sure 404 is not set
        $posts_query->is_404 = false;
        // Correct is_home variable
        $posts_query->is_home = false;
        // Set bbp_user_id for future reference
        $posts_query->set('bbp_user_id', $user->ID);
        // Set author_name as current user's nicename to get correct posts
        $posts_query->set('author_name', $user->user_nicename);
        // Set the displayed user global to this user
        bbpress()->displayed_user = $user;
        // View Page
    } elseif (!empty($bbp_view)) {
        // Check if the view exists by checking if there are query args are set
        $view_args = bbp_get_view_query_args($bbp_view);
        // Bail if view args is false (view isn't registered)
        if (false === $view_args) {
            $posts_query->set_404();
            return;
        }
        // Correct is_home variable
        $posts_query->is_home = false;
        // We are in a custom topic view
        $posts_query->bbp_is_view = true;
        // Forum/Topic/Reply Edit Page
    } elseif (!empty($is_edit)) {
        // Get the post type from the main query loop
        $post_type = $posts_query->get('post_type');
        // Check which post_type we are editing, if any
        if (!empty($post_type)) {
            switch ($post_type) {
                // We are editing a forum
                case bbp_get_forum_post_type():
                    $posts_query->bbp_is_forum_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
                    // We are editing a topic
                // We are editing a topic
                case bbp_get_topic_post_type():
                    $posts_query->bbp_is_topic_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
                    // We are editing a reply
                // We are editing a reply
                case bbp_get_reply_post_type():
                    $posts_query->bbp_is_reply_edit = true;
                    $posts_query->bbp_is_edit = true;
                    break;
            }
            // We are editing a topic tag
        } elseif (bbp_is_topic_tag()) {
            $posts_query->bbp_is_topic_tag_edit = true;
            $posts_query->bbp_is_edit = true;
        }
        // We save post revisions on our own
        remove_action('pre_post_update', 'wp_save_post_revision');
        // Topic tag page
    } elseif (bbp_is_topic_tag()) {
        $posts_query->set('bbp_topic_tag', get_query_var('term'));
        $posts_query->set('post_type', bbp_get_topic_post_type());
        $posts_query->set('posts_per_page', bbp_get_topics_per_page());
    }
}