Example #1
0
/**
 * Register and create a custom BuddyPress menu
 */
function infinity_register_bp_menu($menu_name)
{
    global $blog_id;
    // check BP reqs and if our custom default menu already exists
    if (function_exists('bp_core_get_directory_pages') && BP_ROOT_BLOG == $blog_id && !is_nav_menu($menu_name)) {
        // doesn't exist, create it
        $menu_id = wp_create_nav_menu($menu_name);
        // get bp pages
        $pages = bp_core_get_directory_pages();
        // allowed pages
        $pages_ok = array('activity' => true, 'blogs' => true, 'forums' => true, 'groups' => true, 'links' => true, 'members' => true);
        // loop all pages
        foreach ($pages as $config) {
            // make sure we support this page
            if (array_key_exists($config->name, $pages_ok)) {
                // yep, add page as a nav item
                wp_update_nav_menu_item($menu_id, 0, array('menu-item-type' => 'post_type', 'menu-item-status' => 'publish', 'menu-item-object' => 'page', 'menu-item-object-id' => $config->id, 'menu-item-title' => $config->title, 'menu-item-attr-title' => $config->title, 'menu-item-classes' => 'icon-' . $config->name));
            }
        }
        // get location settings
        $locations = get_theme_mod('nav_menu_locations');
        // is main menu location set yet?
        if (empty($locations['main-menu'])) {
            // nope, set it
            $locations['main-menu'] = $menu_id;
            // update theme mode
            set_theme_mod('nav_menu_locations', $locations);
        }
    }
}
Example #2
0
/**
 * Register and create a default menu in CBOX.
 *
 * @param array $args Arguments to register the default menu:
 *  'menu_name' - The internal menu name we should give our new menu.
 *  'location' - The nav menu location we want our new menu to reside.
 *  'pages' - Associative array of pages. Sample looks like this:
 *       array(
 *            array(
 *                 'title'    => 'Home',
 *                 'position' => 0,
 *                 'url'      => home_url( '/' ) // custom url
 *            ),
 *            array(
 *                 'title'        => 'Members',
 *                 'position'     => 10,
 *                 'bp_directory' => 'members'   // match bp component
 *            ),
 *       )
 */
function cbox_theme_register_default_menu($args = array())
{
    global $blog_id;
    if (empty($args['menu_name']) || empty($args['location']) || empty($args['pages'])) {
        return false;
    }
    if (!is_array($args['pages'])) {
        return false;
    }
    // check BP reqs and if our custom default menu already exists
    if (function_exists('bp_core_get_directory_pages') && BP_ROOT_BLOG == $blog_id && !is_nav_menu($args['menu_name'])) {
        // menu doesn't exist, so create it
        $menu_id = wp_create_nav_menu($args['menu_name']);
        // get bp pages
        $bp_pages = bp_core_get_directory_pages();
        // now, add the pages to our menu
        foreach ($args['pages'] as $page) {
            // default args
            $params = array('menu-item-status' => 'publish', 'menu-item-title' => $page['title'], 'menu-item-classes' => 'icon-' . !empty($page['bp_directory']) ? $page['bp_directory'] : sanitize_title($page['title']), 'menu-item-position' => $page['position']);
            // support custom menu type
            if (!empty($page['type'])) {
                $params['menu-item-type'] = $page['type'];
            }
            // support custom url
            if (!empty($page['url'])) {
                $params['menu-item-url'] = $page['url'];
            }
            // add additional args for bp directories
            if (!empty($page['bp_directory'])) {
                // bp directory page doesn't exist, so stop!
                if (!array_key_exists($page['bp_directory'], get_object_vars($bp_pages))) {
                    continue;
                }
                // yep, add page as a nav item
                $params['menu-item-type'] = 'post_type';
                $params['menu-item-object'] = 'page';
                $params['menu-item-object-id'] = $bp_pages->{$page['bp_directory']}->id;
            }
            wp_update_nav_menu_item($menu_id, 0, $params);
            $params = array();
        }
        // get location settings
        $locations = get_theme_mod('nav_menu_locations');
        // is our menu location set yet?
        if (empty($locations[$args['location']])) {
            // nope, set it
            $locations[$args['location']] = $menu_id;
            // update theme mode
            set_theme_mod('nav_menu_locations', $locations);
        }
        return true;
    }
}
 /**
  * @group object_cache
  * @group bp_core_get_directory_pages
  */
 public function test_bp_core_get_user_domain_after_directory_page_update()
 {
     // Generate user
     $user_id = $this->factory->user->create(array('role' => 'subscriber'));
     // Set object cache first for user domain
     $user_domain = bp_core_get_user_domain($user_id);
     // Now change the members directory slug
     $pages = bp_core_get_directory_pages();
     $members_page = get_post($pages->members->id);
     $members_page->post_name = 'new-members-slug';
     wp_update_post($members_page);
     // Go back to members directory page and recheck user domain
     $this->go_to(trailingslashit(home_url('new-members-slug')));
     $user = new WP_User($user_id);
     $this->assertSame(home_url('new-members-slug') . '/' . $user->user_nicename . '/', bp_core_get_user_domain($user_id));
 }
 /**
  * Set up bp-core global settings.
  *
  * Sets up a majority of the BuddyPress globals that require a minimal
  * amount of processing, meaning they cannot be set in the BuddyPress class.
  *
  * @since BuddyPress (1.5.0)
  *
  * @see BP_Component::setup_globals() for description of parameters.
  *
  * @param array $args See {@link BP_Component::setup_globals()}.
  */
 public function setup_globals($args = array())
 {
     $bp = buddypress();
     /** Database **********************************************************/
     // Get the base database prefix
     if (empty($bp->table_prefix)) {
         $bp->table_prefix = bp_core_get_table_prefix();
     }
     // The domain for the root of the site where the main blog resides
     if (empty($bp->root_domain)) {
         $bp->root_domain = bp_core_get_root_domain();
     }
     // Fetches all of the core BuddyPress settings in one fell swoop
     if (empty($bp->site_options)) {
         $bp->site_options = bp_core_get_root_options();
     }
     // The names of the core WordPress pages used to display BuddyPress content
     if (empty($bp->pages)) {
         $bp->pages = bp_core_get_directory_pages();
     }
     /** Basic current user data *******************************************/
     // Logged in user is the 'current_user'
     $current_user = wp_get_current_user();
     // The user ID of the user who is currently logged in.
     $bp->loggedin_user = new stdClass();
     $bp->loggedin_user->id = isset($current_user->ID) ? $current_user->ID : 0;
     /** Avatars ***********************************************************/
     // Fetches the default Gravatar image to use if the user/group/blog has no avatar or gravatar
     $bp->grav_default = new stdClass();
     $bp->grav_default->user = apply_filters('bp_user_gravatar_default', $bp->site_options['avatar_default']);
     $bp->grav_default->group = apply_filters('bp_group_gravatar_default', $bp->grav_default->user);
     $bp->grav_default->blog = apply_filters('bp_blog_gravatar_default', $bp->grav_default->user);
     // Notifications table. Included here for legacy purposes. Use
     // bp-notifications instead.
     $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications';
     /**
      * Used to determine if user has admin rights on current content. If the
      * logged in user is viewing their own profile and wants to delete
      * something, is_item_admin is used. This is a generic variable so it
      * can be used by other components. It can also be modified, so when
      * viewing a group 'is_item_admin' would be 'true' if they are a group
      * admin, and 'false' if they are not.
      */
     bp_update_is_item_admin(bp_user_has_access(), 'core');
     // Is the logged in user is a mod for the current item?
     bp_update_is_item_mod(false, 'core');
     do_action('bp_core_setup_globals');
 }
Example #5
0
 /**
  * @group bp_core_activation_notice
  */
 public function test_bp_core_activation_notice_register_activate_pages_created_signup_allowed()
 {
     $bp = buddypress();
     $reset_bp_pages = $bp->pages;
     $reset_admin_notices = $bp->admin->notices;
     add_filter('bp_get_signup_allowed', '__return_true', 999);
     $ac = buddypress()->active_components;
     bp_core_add_page_mappings(array_keys($ac));
     // Reset pages
     $bp->pages = bp_core_get_directory_pages();
     bp_core_activation_notice();
     remove_filter('bp_get_signup_allowed', '__return_true', 999);
     $missing_pages = array();
     foreach (buddypress()->admin->notices as $notice) {
         preg_match_all('/<strong>(.+?)<\\/strong>/', $notice['message'], $missing_pages);
     }
     $this->assertNotContains('Register', $missing_pages[1]);
     $this->assertNotContains('Activate', $missing_pages[1]);
     // Reset buddypress() vars
     $bp->pages = $reset_bp_pages;
     $bp->admin->notices = $reset_admin_notices;
 }
 /**
  * @ticket BP7193
  */
 public function test_bp_core_get_directory_pages_autocreate_register_pages_multisite()
 {
     if (!is_multisite()) {
         return;
     }
     // Emulate being in the network admin area.
     if (!class_exists('BP_Members_Admin', false)) {
         require BP_PLUGIN_DIR . 'bp-members/classes/class-bp-members-admin.php';
     }
     $admin = new BP_Members_Admin();
     add_action('update_site_option_registration', array($admin, 'multisite_registration_on'), 10, 2);
     // Emulate turning registration on.
     update_site_option('registration', 'user');
     // Now check directory pages.
     $pages = bp_core_get_directory_pages();
     $this->assertNotEmpty($pages->register);
     $this->assertNotEmpty($pages->activate);
     remove_action('update_site_option_registration', array($admin, 'multisite_registration_on'), 10);
 }
 public function test_bp_core_get_directory_pages_multisite_delete_post_with_same_bp_page_id()
 {
     if (!is_multisite()) {
         return;
     }
     $dir_pages = bp_core_get_directory_pages();
     // create a blog
     $u = $this->factory->user->create();
     $b1 = $this->factory->blog->create(array('user_id' => $u));
     // switch to blog and create some dummy posts until we reach a post ID that
     // matches our BP activity page ID
     switch_to_blog($b1);
     $p = $this->factory->post->create();
     while ($p <= $dir_pages->activity->id) {
         $p = $this->factory->post->create();
     }
     // delete the post that matches the BP activity page ID on this sub-site
     wp_delete_post($dir_pages->activity->id, true);
     // restore blog
     restore_current_blog();
     // refetch BP directory pages
     $dir_pages = bp_core_get_directory_pages();
     // Now verify that our BP activity page was not wiped out
     $this->assertNotEmpty($dir_pages->activity);
 }
/**
 * Analyze the URI and break it down into BuddyPress-usable chunks.
 *
 * BuddyPress can use complete custom friendly URIs without the user having to
 * add new rewrite rules. Custom components are able to use their own custom
 * URI structures with very little work.
 *
 * The URIs are broken down as follows:
 *   - http:// example.com / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
 *   - OUTSIDE ROOT: http:// example.com / sites / buddypress / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
 *
 *	Example:
 *    - http://example.com/members/andy/profile/edit/group/5/
 *    - $bp->current_component: string 'xprofile'
 *    - $bp->current_action: string 'edit'
 *    - $bp->action_variables: array ['group', 5]
 *
 * @since 1.0.0
 */
function bp_core_set_uri_globals()
{
    global $current_blog, $wp_rewrite;
    // Don't catch URIs on non-root blogs unless multiblog mode is on.
    if (!bp_is_root_blog() && !bp_is_multiblog_mode()) {
        return false;
    }
    $bp = buddypress();
    // Define local variables.
    $root_profile = $match = false;
    $key_slugs = $matches = $uri_chunks = array();
    // Fetch all the WP page names for each component.
    if (empty($bp->pages)) {
        $bp->pages = bp_core_get_directory_pages();
    }
    // Ajax or not?
    if (defined('DOING_AJAX') && DOING_AJAX || strpos($_SERVER['REQUEST_URI'], 'wp-load.php')) {
        $path = bp_get_referer_path();
    } else {
        $path = esc_url($_SERVER['REQUEST_URI']);
    }
    /**
     * Filters the BuddyPress global URI path.
     *
     * @since 1.0.0
     *
     * @param string $path Path to set.
     */
    $path = apply_filters('bp_uri', $path);
    // Take GET variables off the URL to avoid problems.
    $path = strtok($path, '?');
    // Fetch current URI and explode each part separated by '/' into an array.
    $bp_uri = explode('/', $path);
    // Loop and remove empties.
    foreach ((array) $bp_uri as $key => $uri_chunk) {
        if (empty($bp_uri[$key])) {
            unset($bp_uri[$key]);
        }
    }
    // If running off blog other than root, any subdirectory names must be
    // removed from $bp_uri. This includes two cases:
    //
    // 1. when WP is installed in a subdirectory,
    // 2. when BP is running on secondary blog of a subdirectory
    // multisite installation. Phew!
    if (is_multisite() && !is_subdomain_install() && (bp_is_multiblog_mode() || 1 != bp_get_root_blog_id())) {
        // Blow chunks.
        $chunks = explode('/', $current_blog->path);
        // If chunks exist...
        if (!empty($chunks)) {
            // ...loop through them...
            foreach ($chunks as $key => $chunk) {
                $bkey = array_search($chunk, $bp_uri);
                // ...and unset offending keys
                if (false !== $bkey) {
                    unset($bp_uri[$bkey]);
                }
                $bp_uri = array_values($bp_uri);
            }
        }
    }
    // Get site path items.
    $paths = explode('/', bp_core_get_site_path());
    // Take empties off the end of path.
    if (empty($paths[count($paths) - 1])) {
        array_pop($paths);
    }
    // Take empties off the start of path.
    if (empty($paths[0])) {
        array_shift($paths);
    }
    // Reset indexes.
    $bp_uri = array_values($bp_uri);
    $paths = array_values($paths);
    // Unset URI indices if they intersect with the paths.
    foreach ((array) $bp_uri as $key => $uri_chunk) {
        if (isset($paths[$key]) && $uri_chunk == $paths[$key]) {
            unset($bp_uri[$key]);
        }
    }
    // Reset the keys by merging with an empty array.
    $bp_uri = array_merge(array(), $bp_uri);
    // If a component is set to the front page, force its name into $bp_uri
    // so that $current_component is populated (unless a specific WP post is being requested
    // via a URL parameter, usually signifying Preview mode).
    if ('page' == get_option('show_on_front') && get_option('page_on_front') && empty($bp_uri) && empty($_GET['p']) && empty($_GET['page_id'])) {
        $post = get_post(get_option('page_on_front'));
        if (!empty($post)) {
            $bp_uri[0] = $post->post_name;
        }
    }
    // Keep the unfiltered URI safe.
    $bp->unfiltered_uri = $bp_uri;
    // Don't use $bp_unfiltered_uri, this is only for backpat with old plugins. Use $bp->unfiltered_uri.
    $GLOBALS['bp_unfiltered_uri'] =& $bp->unfiltered_uri;
    // Get slugs of pages into array.
    foreach ((array) $bp->pages as $page_key => $bp_page) {
        $key_slugs[$page_key] = trailingslashit('/' . $bp_page->slug);
    }
    // Bail if keyslugs are empty, as BP is not setup correct.
    if (empty($key_slugs)) {
        return;
    }
    // Loop through page slugs and look for exact match to path.
    foreach ($key_slugs as $key => $slug) {
        if ($slug == $path) {
            $match = $bp->pages->{$key};
            $match->key = $key;
            $matches[] = 1;
            break;
        }
    }
    // No exact match, so look for partials.
    if (empty($match)) {
        // Loop through each page in the $bp->pages global.
        foreach ((array) $bp->pages as $page_key => $bp_page) {
            // Look for a match (check members first).
            if (in_array($bp_page->name, (array) $bp_uri)) {
                // Match found, now match the slug to make sure.
                $uri_chunks = explode('/', $bp_page->slug);
                // Loop through uri_chunks.
                foreach ((array) $uri_chunks as $key => $uri_chunk) {
                    // Make sure chunk is in the correct position.
                    if (!empty($bp_uri[$key]) && $bp_uri[$key] == $uri_chunk) {
                        $matches[] = 1;
                        // No match.
                    } else {
                        $matches[] = 0;
                    }
                }
                // Have a match.
                if (!in_array(0, (array) $matches)) {
                    $match = $bp_page;
                    $match->key = $page_key;
                    break;
                }
                // Unset matches.
                unset($matches);
            }
            // Unset uri chunks.
            unset($uri_chunks);
        }
    }
    // URLs with BP_ENABLE_ROOT_PROFILES enabled won't be caught above.
    if (empty($matches) && bp_core_enable_root_profiles()) {
        // Switch field based on compat.
        $field = bp_is_username_compatibility_mode() ? 'login' : 'slug';
        // Make sure there's a user corresponding to $bp_uri[0].
        if (!empty($bp->pages->members) && !empty($bp_uri[0]) && ($root_profile = get_user_by($field, $bp_uri[0]))) {
            // Force BP to recognize that this is a members page.
            $matches[] = 1;
            $match = $bp->pages->members;
            $match->key = 'members';
        }
    }
    // Search doesn't have an associated page, so we check for it separately.
    if (!empty($bp_uri[0]) && bp_get_search_slug() == $bp_uri[0]) {
        $matches[] = 1;
        $match = new stdClass();
        $match->key = 'search';
        $match->slug = bp_get_search_slug();
    }
    // This is not a BuddyPress page, so just return.
    if (empty($matches)) {
        return false;
    }
    $wp_rewrite->use_verbose_page_rules = false;
    // Find the offset. With $root_profile set, we fudge the offset down so later parsing works.
    $slug = !empty($match) ? explode('/', $match->slug) : '';
    $uri_offset = empty($root_profile) ? 0 : -1;
    // Rejig the offset.
    if (!empty($slug) && 1 < count($slug)) {
        // Only offset if not on a root profile. Fixes issue when Members page is nested.
        if (false === $root_profile) {
            array_pop($slug);
            $uri_offset = count($slug);
        }
    }
    // Global the unfiltered offset to use in bp_core_load_template().
    // To avoid PHP warnings in bp_core_load_template(), it must always be >= 0.
    $bp->unfiltered_uri_offset = $uri_offset >= 0 ? $uri_offset : 0;
    // We have an exact match.
    if (isset($match->key)) {
        // Set current component to matched key.
        $bp->current_component = $match->key;
        // If members component, do more work to find the actual component.
        if ('members' == $match->key) {
            $after_member_slug = false;
            if (!empty($bp_uri[$uri_offset + 1])) {
                $after_member_slug = $bp_uri[$uri_offset + 1];
            }
            // Are we viewing a specific user?
            if ($after_member_slug) {
                // If root profile, we've already queried for the user.
                if ($root_profile instanceof WP_User) {
                    $bp->displayed_user->id = $root_profile->ID;
                    // Switch the displayed_user based on compatibility mode.
                } elseif (bp_is_username_compatibility_mode()) {
                    $bp->displayed_user->id = (int) bp_core_get_userid(urldecode($after_member_slug));
                } else {
                    $bp->displayed_user->id = (int) bp_core_get_userid_from_nicename($after_member_slug);
                }
            }
            // Is this a member type directory?
            if (!bp_displayed_user_id() && $after_member_slug === apply_filters('bp_members_member_type_base', _x('type', 'member type URL base', 'buddypress')) && !empty($bp_uri[$uri_offset + 2])) {
                $matched_types = bp_get_member_types(array('has_directory' => true, 'directory_slug' => $bp_uri[$uri_offset + 2]));
                if (!empty($matched_types)) {
                    $bp->current_member_type = reset($matched_types);
                    unset($bp_uri[$uri_offset + 1]);
                }
            }
            // If the slug matches neither a member type nor a specific member, 404.
            if (!bp_displayed_user_id() && !bp_get_current_member_type() && $after_member_slug) {
                // Prevent components from loading their templates.
                $bp->current_component = '';
                bp_do_404();
                return;
            }
            // If the displayed user is marked as a spammer, 404 (unless logged-in user is a super admin).
            if (bp_displayed_user_id() && bp_is_user_spammer(bp_displayed_user_id())) {
                if (bp_current_user_can('bp_moderate')) {
                    bp_core_add_message(__('This user has been marked as a spammer. Only site admins can view this profile.', 'buddypress'), 'warning');
                } else {
                    bp_do_404();
                    return;
                }
            }
            // Bump the offset.
            if (bp_displayed_user_id()) {
                if (isset($bp_uri[$uri_offset + 2])) {
                    $bp_uri = array_merge(array(), array_slice($bp_uri, $uri_offset + 2));
                    $bp->current_component = $bp_uri[0];
                    // No component, so default will be picked later.
                } else {
                    $bp_uri = array_merge(array(), array_slice($bp_uri, $uri_offset + 2));
                    $bp->current_component = '';
                }
                // Reset the offset.
                $uri_offset = 0;
            }
        }
    }
    // Determine the current action.
    $current_action = isset($bp_uri[$uri_offset + 1]) ? $bp_uri[$uri_offset + 1] : '';
    /*
     * If a BuddyPress directory is set to the WP front page, URLs like example.com/members/?s=foo
     * shouldn't interfere with blog searches.
     */
    if (empty($current_action) && !empty($_GET['s']) && 'page' == get_option('show_on_front') && !empty($match->id)) {
        $page_on_front = (int) get_option('page_on_front');
        if ((int) $match->id === $page_on_front) {
            $bp->current_component = '';
            return false;
        }
    }
    $bp->current_action = $current_action;
    // Slice the rest of the $bp_uri array and reset offset.
    $bp_uri = array_slice($bp_uri, $uri_offset + 2);
    $uri_offset = 0;
    // Set the entire URI as the action variables, we will unset the current_component and action in a second.
    $bp->action_variables = $bp_uri;
    // Reset the keys by merging with an empty array.
    $bp->action_variables = array_merge(array(), $bp->action_variables);
}
/**
 * Analyzes the URI structure and breaks it down into parts for use in code.
 * The idea is that BuddyPress can use complete custom friendly URI's without the
 * user having to add new re-write rules.
 *
 * Future custom components would then be able to use their own custom URI structure.
 *
 * @package BuddyPress Core
 * @since BuddyPress (r100)
 *
 * The URI's are broken down as follows:
 *   - http:// domain.com / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
 *   - OUTSIDE ROOT: http:// domain.com / sites / buddypress / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
 *
 *	Example:
 *    - http://domain.com/members/andy/profile/edit/group/5/
 *    - $bp->current_component: string 'xprofile'
 *    - $bp->current_action: string 'edit'
 *    - $bp->action_variables: array ['group', 5]
 *
 */
function bp_core_set_uri_globals()
{
    global $bp, $bp_unfiltered_uri, $bp_unfiltered_uri_offset;
    global $current_blog, $nxtdb;
    // Create global component, action, and item variables
    $bp->current_component = $bp->current_action = $bp->current_item = '';
    $bp->action_variables = $bp->displayed_user->id = '';
    // Don't catch URIs on non-root blogs unless multiblog mode is on
    if (!bp_is_root_blog() && !bp_is_multiblog_mode()) {
        return false;
    }
    // Fetch all the nxt page names for each component
    if (empty($bp->pages)) {
        $bp->pages = bp_core_get_directory_pages();
    }
    // Ajax or not?
    if (strpos($_SERVER['REQUEST_URI'], 'nxt-load.php')) {
        $path = bp_core_referrer();
    } else {
        $path = esc_url($_SERVER['REQUEST_URI']);
    }
    // Filter the path
    $path = apply_filters('bp_uri', $path);
    // Take GET variables off the URL to avoid problems,
    // they are still registered in the global $_GET variable
    if ($noget = substr($path, 0, strpos($path, '?'))) {
        $path = $noget;
    }
    // Fetch the current URI and explode each part separated by '/' into an array
    $bp_uri = explode('/', $path);
    // Loop and remove empties
    foreach ((array) $bp_uri as $key => $uri_chunk) {
        if (empty($bp_uri[$key])) {
            unset($bp_uri[$key]);
        }
    }
    // Running off blog other than root
    if (is_multisite() && !is_subdomain_install() && (bp_is_multiblog_mode() || 1 != bp_get_root_blog_id())) {
        // Any subdirectory names must be removed from $bp_uri.
        // This includes two cases: (1) when nxt is installed in a subdirectory,
        // and (2) when BP is running on secondary blog of a subdirectory
        // multisite installation. Phew!
        if ($chunks = explode('/', $current_blog->path)) {
            foreach ($chunks as $key => $chunk) {
                $bkey = array_search($chunk, $bp_uri);
                if ($bkey !== false) {
                    unset($bp_uri[$bkey]);
                }
                $bp_uri = array_values($bp_uri);
            }
        }
    }
    // Set the indexes, these are incresed by one if we are not on a VHOST install
    $component_index = 0;
    $action_index = $component_index + 1;
    // Get site path items
    $paths = explode('/', bp_core_get_site_path());
    // Take empties off the end of path
    if (empty($paths[count($paths) - 1])) {
        array_pop($paths);
    }
    // Take empties off the start of path
    if (empty($paths[0])) {
        array_shift($paths);
    }
    // Unset URI indices if they intersect with the paths
    foreach ((array) $bp_uri as $key => $uri_chunk) {
        if (in_array($uri_chunk, $paths)) {
            unset($bp_uri[$key]);
        }
    }
    // Reset the keys by merging with an empty array
    $bp_uri = array_merge(array(), $bp_uri);
    // If a component is set to the front page, force its name into $bp_uri
    // so that $current_component is populated (unless a specific nxt post is being requested
    // via a URL parameter, usually signifying Preview mode)
    if ('page' == get_option('show_on_front') && get_option('page_on_front') && empty($bp_uri) && empty($_GET['p']) && empty($_GET['page_id'])) {
        $post = get_post(get_option('page_on_front'));
        if (!empty($post)) {
            $bp_uri[0] = $post->post_name;
        }
    }
    // Keep the unfiltered URI safe
    $bp_unfiltered_uri = $bp_uri;
    // Get slugs of pages into array
    foreach ((array) $bp->pages as $page_key => $bp_page) {
        $key_slugs[$page_key] = trailingslashit('/' . $bp_page->slug);
    }
    // Bail if keyslugs are empty, as BP is not setup correct
    if (empty($key_slugs)) {
        return;
    }
    // Loop through page slugs and look for exact match to path
    foreach ($key_slugs as $key => $slug) {
        if ($slug == $path) {
            $match = $bp->pages->{$key};
            $match->key = $key;
            $matches[] = 1;
            break;
        }
    }
    // No exact match, so look for partials
    if (empty($match)) {
        // Loop through each page in the $bp->pages global
        foreach ((array) $bp->pages as $page_key => $bp_page) {
            // Look for a match (check members first)
            if (in_array($bp_page->name, (array) $bp_uri)) {
                // Match found, now match the slug to make sure.
                $uri_chunks = explode('/', $bp_page->slug);
                // Loop through uri_chunks
                foreach ((array) $uri_chunks as $key => $uri_chunk) {
                    // Make sure chunk is in the correct position
                    if (!empty($bp_uri[$key]) && $bp_uri[$key] == $uri_chunk) {
                        $matches[] = 1;
                        // No match
                    } else {
                        $matches[] = 0;
                    }
                }
                // Have a match
                if (!in_array(0, (array) $matches)) {
                    $match = $bp_page;
                    $match->key = $page_key;
                    break;
                }
                // Unset matches
                unset($matches);
            }
            // Unset uri chunks
            unset($uri_chunks);
        }
    }
    // URLs with BP_ENABLE_ROOT_PROFILES enabled won't be caught above
    if (empty($matches) && defined('BP_ENABLE_ROOT_PROFILES') && BP_ENABLE_ROOT_PROFILES) {
        // Make sure there's a user corresponding to $bp_uri[0]
        if (!empty($bp->pages->members) && !empty($bp_uri[0]) && ($root_profile = get_user_by('login', $bp_uri[0]))) {
            // Force BP to recognize that this is a members page
            $matches[] = 1;
            $match = $bp->pages->members;
            $match->key = 'members';
            // Without the 'members' URL chunk, NXTClass won't know which page to load
            // This filter intercepts the nxt query and tells it to load the members page
            add_filter('request', create_function('$query_args', '$query_args["pagename"] = "' . $match->name . '"; return $query_args;'));
        }
    }
    // Search doesn't have an associated page, so we check for it separately
    if (!empty($bp_uri[0]) && bp_get_search_slug() == $bp_uri[0]) {
        $matches[] = 1;
        $match = new stdClass();
        $match->key = 'search';
        $match->slug = bp_get_search_slug();
    }
    // This is not a BuddyPress page, so just return.
    if (!isset($matches)) {
        return false;
    }
    // Find the offset. With $root_profile set, we fudge the offset down so later parsing works
    $slug = !empty($match) ? explode('/', $match->slug) : '';
    $uri_offset = empty($root_profile) ? 0 : -1;
    // Rejig the offset
    if (!empty($slug) && 1 < count($slug)) {
        array_pop($slug);
        $uri_offset = count($slug);
    }
    // Global the unfiltered offset to use in bp_core_load_template().
    // To avoid PHP warnings in bp_core_load_template(), it must always be >= 0
    $bp_unfiltered_uri_offset = $uri_offset >= 0 ? $uri_offset : 0;
    // We have an exact match
    if (isset($match->key)) {
        // Set current component to matched key
        $bp->current_component = $match->key;
        // If members component, do more work to find the actual component
        if ('members' == $match->key) {
            // Viewing a specific user
            if (!empty($bp_uri[$uri_offset + 1])) {
                // Switch the displayed_user based on compatbility mode
                if (bp_is_username_compatibility_mode()) {
                    $bp->displayed_user->id = (int) bp_core_get_userid(urldecode($bp_uri[$uri_offset + 1]));
                } else {
                    $bp->displayed_user->id = (int) bp_core_get_userid_from_nicename(urldecode($bp_uri[$uri_offset + 1]));
                }
                if (empty($bp->displayed_user->id)) {
                    // Prevent components from loading their templates
                    $bp->current_component = '';
                    bp_do_404();
                    return;
                }
                // If the displayed user is marked as a spammer, 404 (unless logged-
                // in user is a super admin)
                if (!empty($bp->displayed_user->id) && bp_core_is_user_spammer($bp->displayed_user->id)) {
                    if (is_super_admin()) {
                        bp_core_add_message(__('This user has been marked as a spammer. Only site admins can view this profile.', 'buddypress'), 'error');
                    } else {
                        bp_do_404();
                        return;
                    }
                }
                // Bump the offset
                if (isset($bp_uri[$uri_offset + 2])) {
                    $bp_uri = array_merge(array(), array_slice($bp_uri, $uri_offset + 2));
                    $bp->current_component = $bp_uri[0];
                    // No component, so default will be picked later
                } else {
                    $bp_uri = array_merge(array(), array_slice($bp_uri, $uri_offset + 2));
                    $bp->current_component = '';
                }
                // Reset the offset
                $uri_offset = 0;
            }
        }
    }
    // Set the current action
    $bp->current_action = isset($bp_uri[$uri_offset + 1]) ? $bp_uri[$uri_offset + 1] : '';
    // Slice the rest of the $bp_uri array and reset offset
    $bp_uri = array_slice($bp_uri, $uri_offset + 2);
    $uri_offset = 0;
    // Set the entire URI as the action variables, we will unset the current_component and action in a second
    $bp->action_variables = $bp_uri;
    // Remove the username from action variables if this is not a VHOST install
    // @todo - move or remove this all together
    if (defined('VHOST') && 'no' == VHOST && empty($bp->current_component)) {
        array_shift($bp_uri);
    }
    // Reset the keys by merging with an empty array
    $bp->action_variables = array_merge(array(), $bp->action_variables);
}
Example #10
0
 function go_to($url)
 {
     $GLOBALS['bp']->loggedin_user = NULL;
     $GLOBALS['bp']->pages = bp_core_get_directory_pages();
     parent::go_to($url);
     do_action('bp_init');
 }
Example #11
0
 /**
  * @group bp_core_get_directory_pages
  */
 public function test_bp_core_get_directory_pages_after_page_edit()
 {
     // Set the cache
     $pages = bp_core_get_directory_pages();
     // Update one of the posts
     switch_to_blog(bp_get_root_blog_id());
     // grab the first one
     foreach ($pages as $page) {
         $p = $page;
         break;
     }
     $post = get_post($p->id);
     $post->post_title .= ' Foo';
     wp_update_post($post);
     restore_current_blog();
     $this->assertFalse(wp_cache_get('directory_pages', 'bp'));
 }
/**
 * Filter the login redirect URL.
 *
 * @since 1.0.2
 * @package GeoDirectory_BuddyPress_Integration
 *
 * @global object $bp BuddyPress object.
 *
 * @param string $redirect_to The redirect destination URL.
 * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
 * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
 * @return string The redirect URL.
 */
function geodir_buddypress_login_redirect($redirect_to, $requested_redirect_to, $user)
{
    // Only modify the redirect if we're on the main BP blog
    if (!bp_is_root_blog()) {
        return $redirect_to;
    }
    // Only modify the redirect once the user is logged in
    if (!is_a($user, 'WP_User')) {
        return $redirect_to;
    }
    // If a 'redirect_to' parameter has been passed that contains 'wp-admin'
    if (!empty($redirect_to) && false !== strpos($redirect_to, 'wp-admin') && user_can($user, 'edit_posts')) {
        return $redirect_to;
    }
    $login_redirect = (int) get_option('gdbuddypress_login_redirect');
    switch ($login_redirect) {
        case 1:
            // Home page
            $redirect_to = trailingslashit(home_url());
            break;
        case 2:
            // Profile page
            $members_slug = bp_get_members_root_slug();
            if ($members_slug) {
                $redirect_to = trailingslashit(bp_core_get_user_domain($user->ID));
            } else {
                $username = bp_core_get_username($user->ID, $user->data->user_nicename, $user->data->user_login);
                if (bp_core_enable_root_profiles()) {
                    $redirect_to = trailingslashit(bp_get_root_domain() . '/' . $username);
                } else {
                    $bp_pages = bp_core_get_directory_pages();
                    if (isset($bp_pages->members->slug)) {
                        $members_slug = $bp_pages->members->slug;
                    } else {
                        global $bp;
                        $members_slug = defined('BP_MEMBERS_SLUG') ? BP_MEMBERS_SLUG : $bp->members->id;
                    }
                    $redirect_to = trailingslashit(bp_get_root_domain() . '/' . $members_slug . '/' . $username);
                }
            }
            break;
        case 3:
            // Menu page
            $menu_redirect = (int) get_option('gdbuddypress_menu_redirect');
            if ($menu_redirect > 0) {
                $redirect_to = get_permalink($menu_redirect);
            }
            break;
    }
    return $redirect_to;
}
 function setup_globals()
 {
     global $bp;
     /** Database **********************************************************/
     // Get the base database prefix
     if (empty($bp->table_prefix)) {
         $bp->table_prefix = bp_core_get_table_prefix();
     }
     // The domain for the root of the site where the main blog resides
     if (empty($bp->root_domain)) {
         $bp->root_domain = bp_core_get_root_domain();
     }
     // Fetches all of the core BuddyPress settings in one fell swoop
     if (empty($bp->site_options)) {
         $bp->site_options = bp_core_get_root_options();
     }
     // The names of the core NXTClass pages used to display BuddyPress content
     if (empty($bp->pages)) {
         $bp->pages = bp_core_get_directory_pages();
     }
     /** Admin Bar *********************************************************/
     // Set the 'My Account' global to prevent debug notices
     $bp->my_account_menu_id = false;
     /** Component and Action **********************************************/
     // Used for overriding the 2nd level navigation menu so it can be used to
     // display custom navigation for an item (for example a group)
     $bp->is_single_item = false;
     // Sets up the array container for the component navigation rendered
     // by bp_get_nav()
     $bp->bp_nav = array();
     // Sets up the array container for the component options navigation
     // rendered by bp_get_options_nav()
     $bp->bp_options_nav = array();
     // Contains an array of all the active components. The key is the slug,
     // value the internal ID of the component.
     //$bp->active_components = array();
     /** Basic current user data *******************************************/
     // Logged in user is the 'current_user'
     $current_user = nxt_get_current_user();
     // The user ID of the user who is currently logged in.
     $bp->loggedin_user->id = $current_user->ID;
     /** Avatars ***********************************************************/
     // Fetches the default Gravatar image to use if the user/group/blog has no avatar or gravatar
     $bp->grav_default->user = apply_filters('bp_user_gravatar_default', $bp->site_options['avatar_default']);
     $bp->grav_default->group = apply_filters('bp_group_gravatar_default', $bp->grav_default->user);
     $bp->grav_default->blog = apply_filters('bp_blog_gravatar_default', $bp->grav_default->user);
     // Notifications Table
     $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications';
     /**
      * Used to determine if user has admin rights on current content. If the
      * logged in user is viewing their own profile and wants to delete
      * something, is_item_admin is used. This is a generic variable so it
      * can be used by other components. It can also be modified, so when
      * viewing a group 'is_item_admin' would be 'true' if they are a group
      * admin, and 'false' if they are not.
      */
     bp_update_is_item_admin(bp_user_has_access(), 'core');
     // Is the logged in user is a mod for the current item?
     bp_update_is_item_mod(false, 'core');
     do_action('bp_core_setup_globals');
 }
/**
 * Verify that some BP prerequisites are set up properly, and notify the admin if not.
 *
 * On every Dashboard page, this function checks the following:
 *   - that pretty permalinks are enabled.
 *   - that every BP component that needs a WP page for a directory has one.
 *   - that no WP page has multiple BP components associated with it.
 * The administrator will be shown a notice for each check that fails.
 *
 * @global WPDB $wpdb WordPress DB object
 * @global WP_Rewrite $wp_rewrite
 *
 * @since 1.2.0
 */
function bp_core_activation_notice()
{
    global $wp_rewrite, $wpdb;
    // Only the super admin gets warnings.
    if (!bp_current_user_can('bp_moderate')) {
        return;
    }
    // Bail in user admin.
    if (is_user_admin()) {
        return;
    }
    // On multisite installs, don't load on a non-root blog, unless do_network_admin is overridden.
    if (is_multisite() && bp_core_do_network_admin() && !bp_is_root_blog()) {
        return;
    }
    // Bail if in network admin, and BuddyPress is not network activated.
    if (is_network_admin() && !bp_is_network_activated()) {
        return;
    }
    /**
     * Check to make sure that the blog setup routine has run. This can't
     * happen during the wizard because of the order which the components
     * are loaded.
     */
    if (bp_is_active('blogs')) {
        $bp = buddypress();
        $count = $wpdb->get_var("SELECT COUNT(*) FROM {$bp->blogs->table_name}");
        if (empty($count)) {
            bp_blogs_record_existing_blogs();
        }
    }
    // Add notice if no rewrite rules are enabled.
    if (empty($wp_rewrite->permalink_structure)) {
        bp_core_add_admin_notice(sprintf(__('<strong>BuddyPress is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddypress'), admin_url('options-permalink.php')), 'error');
    }
    // Get BuddyPress instance.
    $bp = buddypress();
    /**
     * Check for orphaned BP components (BP component is enabled, no WP page exists).
     */
    $orphaned_components = array();
    $wp_page_components = array();
    // Only components with 'has_directory' require a WP page to function.
    foreach (array_keys($bp->loaded_components) as $component_id) {
        if (!empty($bp->{$component_id}->has_directory)) {
            $wp_page_components[] = array('id' => $component_id, 'name' => isset($bp->{$component_id}->name) ? $bp->{$component_id}->name : ucwords($bp->{$component_id}->id));
        }
    }
    // Activate and Register are special cases. They are not components but they need WP pages.
    // If user registration is disabled, we can skip this step.
    if (bp_get_signup_allowed()) {
        $wp_page_components[] = array('id' => 'activate', 'name' => __('Activate', 'buddypress'));
        $wp_page_components[] = array('id' => 'register', 'name' => __('Register', 'buddypress'));
    }
    // On the first admin screen after a new installation, this isn't set, so grab it to suppress
    // a misleading error message.
    if (empty($bp->pages->members)) {
        $bp->pages = bp_core_get_directory_pages();
    }
    foreach ($wp_page_components as $component) {
        if (!isset($bp->pages->{$component['id']})) {
            $orphaned_components[] = $component['name'];
        }
    }
    // Special case: If the Forums component is orphaned, but the bbPress 1.x installation is
    // not correctly set up, don't show a nag. (In these cases, it's probably the case that the
    // user is using bbPress 2.x; see https://buddypress.trac.wordpress.org/ticket/4292.
    if (isset($bp->forums->name) && in_array($bp->forums->name, $orphaned_components) && !bp_forums_is_installed_correctly()) {
        $forum_key = array_search($bp->forums->name, $orphaned_components);
        unset($orphaned_components[$forum_key]);
        $orphaned_components = array_values($orphaned_components);
    }
    if (!empty($orphaned_components)) {
        $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php'));
        $notice = sprintf(__('The following active BuddyPress Components do not have associated WordPress Pages: %2$s. <a href="%1$s">Repair</a>', 'buddypress'), esc_url($admin_url), '<strong>' . implode('</strong>, <strong>', $orphaned_components) . '</strong>');
        bp_core_add_admin_notice($notice);
    }
    // BP components cannot share a single WP page. Check for duplicate assignments, and post a message if found.
    $dupe_names = array();
    $page_ids = (array) bp_core_get_directory_page_ids();
    $dupes = array_diff_assoc($page_ids, array_unique($page_ids));
    if (!empty($dupes)) {
        foreach (array_keys($dupes) as $dupe_component) {
            $dupe_names[] = $bp->pages->{$dupe_component}->title;
        }
        // Make sure that there are no duplicate duplicates :).
        $dupe_names = array_unique($dupe_names);
    }
    // If there are duplicates, post a message about them.
    if (!empty($dupe_names)) {
        $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php'));
        $notice = sprintf(__('Each BuddyPress Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %2$s. <a href="%1$s">Repair</a>', 'buddypress'), esc_url($admin_url), '<strong>' . implode('</strong>, <strong>', $dupe_names) . '</strong>');
        bp_core_add_admin_notice($notice);
    }
}
Example #15
0
 function go_to($url)
 {
     global $wpdb;
     global $current_site, $current_blog, $blog_id, $switched, $_wp_switched_stack, $public, $table_prefix, $current_user, $wp_roles;
     // note: the WP and WP_Query classes like to silently fetch parameters
     // from all over the place (globals, GET, etc), which makes it tricky
     // to run them more than once without very carefully clearing everything
     $_GET = $_POST = array();
     foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
         if (isset($GLOBALS[$v])) {
             unset($GLOBALS[$v]);
         }
     }
     $parts = parse_url($url);
     if (isset($parts['scheme'])) {
         // set the HTTP_HOST
         $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
         $req = $parts['path'];
         if (isset($parts['query'])) {
             $req .= '?' . $parts['query'];
             // parse the url query vars into $_GET
             parse_str($parts['query'], $_GET);
         }
     } else {
         $req = $url;
     }
     if (!isset($parts['query'])) {
         $parts['query'] = '';
     }
     // Scheme
     if (0 === strpos($req, '/wp-admin') && force_ssl_admin()) {
         $_SERVER['HTTPS'] = 'on';
     } else {
         unset($_SERVER['HTTPS']);
     }
     // Set this for bp_core_set_uri_globals()
     $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
     unset($_SERVER['PATH_INFO']);
     // setup $current_site and $current_blog globals for multisite based on
     // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
     if (is_multisite()) {
         $current_blog = $current_site = $blog_id = null;
         $domain = addslashes($_SERVER['HTTP_HOST']);
         if (false !== strpos($domain, ':')) {
             if (substr($domain, -3) == ':80') {
                 $domain = substr($domain, 0, -3);
                 $_SERVER['HTTP_HOST'] = substr($_SERVER['HTTP_HOST'], 0, -3);
             } elseif (substr($domain, -4) == ':443') {
                 $domain = substr($domain, 0, -4);
                 $_SERVER['HTTP_HOST'] = substr($_SERVER['HTTP_HOST'], 0, -4);
             }
         }
         $path = stripslashes($_SERVER['REQUEST_URI']);
         // Get a cleaned-up version of the wp_version string
         // (strip -src, -alpha, etc which may trip up version_compare())
         $wp_version = (double) $GLOBALS['wp_version'];
         if (version_compare($wp_version, '4.4', '>=')) {
             if (!($current_site = wp_cache_get('current_network', 'site-options'))) {
                 // Are there even two networks installed?
                 $one_network = $wpdb->get_row("SELECT * FROM {$wpdb->site} LIMIT 2");
                 // [sic]
                 if (1 === $wpdb->num_rows) {
                     $current_site = new WP_Network($one_network);
                     wp_cache_add('current_network', $current_site, 'site-options');
                 } elseif (0 === $wpdb->num_rows) {
                     ms_not_installed($domain, $path);
                 }
             }
             if (empty($current_site)) {
                 $current_site = WP_Network::get_by_path($domain, $path, 1);
             }
             // The network declared by the site trumps any constants.
             if ($current_blog && $current_blog->site_id != $current_site->id) {
                 $current_site = WP_Network::get_instance($current_blog->site_id);
             }
             if (empty($current_site)) {
                 do_action('ms_network_not_found', $domain, $path);
                 ms_not_installed($domain, $path);
             } elseif ($path === $current_site->path) {
                 $current_blog = get_site_by_path($domain, $path);
             } else {
                 // Search the network path + one more path segment (on top of the network path).
                 $current_blog = get_site_by_path($domain, $path, substr_count($current_site->path, '/'));
             }
             // Figure out the current network's main site.
             if (empty($current_site->blog_id)) {
                 if ($current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path) {
                     $current_site->blog_id = $current_blog->blog_id;
                 } elseif (!($current_site->blog_id = wp_cache_get('network:' . $current_site->id . ':main_site', 'site-options'))) {
                     $current_site->blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path));
                     wp_cache_add('network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options');
                 }
             }
             $blog_id = $current_blog->blog_id;
             $public = $current_blog->public;
             if (empty($current_blog->site_id)) {
                 // This dates to [MU134] and shouldn't be relevant anymore,
                 // but it could be possible for arguments passed to insert_blog() etc.
                 $current_blog->site_id = 1;
             }
             $site_id = $current_blog->site_id;
             wp_load_core_site_options($site_id);
         } elseif (version_compare($wp_version, '3.9', '>=')) {
             if (is_admin()) {
                 $path = preg_replace('#(.*)/wp-admin/.*#', '$1/', $path);
             }
             list($path) = explode('?', $path);
             // Are there even two networks installed?
             $one_network = $wpdb->get_row("SELECT * FROM {$wpdb->site} LIMIT 2");
             // [sic]
             if (1 === $wpdb->num_rows) {
                 $current_site = wp_get_network($one_network);
             } elseif (0 === $wpdb->num_rows) {
                 ms_not_installed();
             }
             if (empty($current_site)) {
                 $current_site = get_network_by_path($domain, $path, 1);
             }
             if (empty($current_site)) {
                 ms_not_installed();
             } elseif ($path === $current_site->path) {
                 $current_blog = get_site_by_path($domain, $path);
             } else {
                 // Search the network path + one more path segment (on top of the network path).
                 $current_blog = get_site_by_path($domain, $path, substr_count($current_site->path, '/'));
             }
             // The network declared by the site trumps any constants.
             if ($current_blog && $current_blog->site_id != $current_site->id) {
                 $current_site = wp_get_network($current_blog->site_id);
             }
             // If we don't have a network by now, we have a problem.
             if (empty($current_site)) {
                 ms_not_installed();
             }
             // @todo What if the domain of the network doesn't match the current site?
             $current_site->cookie_domain = $current_site->domain;
             if ('www.' === substr($current_site->cookie_domain, 0, 4)) {
                 $current_site->cookie_domain = substr($current_site->cookie_domain, 4);
             }
             // Figure out the current network's main site.
             if (!isset($current_site->blog_id)) {
                 if ($current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path) {
                     $current_site->blog_id = $current_blog->blog_id;
                 } else {
                     // @todo we should be able to cache the blog ID of a network's main site easily.
                     $current_site->blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path));
                 }
             }
             $blog_id = $current_blog->blog_id;
             $public = $current_blog->public;
             if (empty($current_blog->site_id)) {
                 // This dates to [MU134] and shouldn't be relevant anymore,
                 // but it could be possible for arguments passed to insert_blog() etc.
                 $current_blog->site_id = 1;
             }
             $site_id = $current_blog->site_id;
             wp_load_core_site_options($site_id);
             // Pre WP 3.9
         } else {
             $domain = rtrim($domain, '.');
             $cookie_domain = $domain;
             if (substr($cookie_domain, 0, 4) == 'www.') {
                 $cookie_domain = substr($cookie_domain, 4);
             }
             $path = preg_replace('|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI']);
             $path = str_replace('/wp-admin/', '/', $path);
             $path = preg_replace('|(/[a-z0-9-]+?/).*|', '$1', $path);
             $GLOBALS['current_site'] = wpmu_current_site();
             if (!isset($GLOBALS['current_site']->blog_id) && !empty($GLOBALS['current_site'])) {
                 $GLOBALS['current_site']->blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path));
             }
             $blogname = htmlspecialchars(substr($GLOBALS['_SERVER']['REQUEST_URI'], strlen($path)));
             if (false !== strpos($blogname, '/')) {
                 $blogname = substr($blogname, 0, strpos($blogname, '/'));
             }
             if (false !== strpos($blogname, '?')) {
                 $blogname = substr($blogname, 0, strpos($blogname, '?'));
             }
             $reserved_blognames = array('page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed');
             if ($blogname != '' && !in_array($blogname, $reserved_blognames) && !is_file($blogname)) {
                 $path .= $blogname . '/';
             }
             $GLOBALS['current_blog'] = get_blog_details(array('domain' => $domain, 'path' => $path), false);
             unset($reserved_blognames);
             if ($GLOBALS['current_site'] && !$GLOBALS['current_blog']) {
                 $GLOBALS['current_blog'] = get_blog_details(array('domain' => $GLOBALS['current_site']->domain, 'path' => $GLOBALS['current_site']->path), false);
             }
             $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
         }
         // Emulate a switch_to_blog()
         $table_prefix = $wpdb->get_blog_prefix($current_blog->blog_id);
         $wpdb->set_blog_id($current_blog->blog_id, $current_blog->site_id);
         $_wp_switched_stack = array();
         $switched = false;
         if (!isset($current_site->site_name)) {
             $current_site->site_name = get_site_option('site_name');
             if (!$current_site->site_name) {
                 $current_site->site_name = ucfirst($current_site->domain);
             }
         }
     }
     $this->flush_cache();
     unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
     $GLOBALS['wp_the_query'] = new WP_Query();
     $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
     $GLOBALS['wp'] = new WP();
     // clean out globals to stop them polluting wp and wp_query
     foreach ($GLOBALS['wp']->public_query_vars as $v) {
         unset($GLOBALS[$v]);
     }
     foreach ($GLOBALS['wp']->private_query_vars as $v) {
         unset($GLOBALS[$v]);
     }
     $GLOBALS['wp']->main($parts['query']);
     $wp_roles->reinit();
     $current_user = wp_get_current_user();
     $current_user->for_blog($blog_id);
     // For BuddyPress, James.
     $this->clean_up_global_scope();
     $GLOBALS['bp']->loggedin_user = NULL;
     $GLOBALS['bp']->pages = bp_core_get_directory_pages();
     do_action('bp_init');
 }
/**
 * Add support for a top-level ("root") component.
 *
 * This function originally (pre-1.5) let plugins add support for pages in the
 * root of the install. These root level pages are now handled by actual
 * WordPress pages and this function is now a convenience for compatibility
 * with the new method.
 *
 * @param string $slug The slug of the component being added to the root list.
 */
function bp_core_add_root_component($slug)
{
    $bp = buddypress();
    if (empty($bp->pages)) {
        $bp->pages = bp_core_get_directory_pages();
    }
    $match = false;
    // Check if the slug is registered in the $bp->pages global
    foreach ((array) $bp->pages as $key => $page) {
        if ($key == $slug || $page->slug == $slug) {
            $match = true;
        }
    }
    // Maybe create the add_root array
    if (empty($bp->add_root)) {
        $bp->add_root = array();
    }
    // If there was no match, add a page for this root component
    if (empty($match)) {
        $add_root_items = $bp->add_root();
        $add_root_items[] = $slug;
        $bp->add_root = $add_root_items;
    }
    // Make sure that this component is registered as requiring a top-level directory
    if (isset($bp->{$slug})) {
        $bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;
        $bp->{$slug}->has_directory = true;
    }
}
 /**
  * @group bp_core_activation_notice
  */
 public function test_bp_core_activation_notice_register_activate_pages_created_signup_allowed()
 {
     $bp = buddypress();
     $reset_bp_pages = $bp->pages;
     $reset_admin_notices = $bp->admin->notices;
     add_filter('bp_get_signup_allowed', '__return_true', 999);
     $ac = buddypress()->active_components;
     bp_core_add_page_mappings(array_keys($ac));
     // Reset pages
     $bp->pages = bp_core_get_directory_pages();
     bp_core_activation_notice();
     remove_filter('bp_get_signup_allowed', '__return_true', 999);
     $missing_pages = array();
     foreach (buddypress()->admin->notices as $notice) {
         if (false !== strpos($notice['message'], 'BuddyPress is almost ready')) {
             continue;
         }
         preg_match_all('/<strong>(.+?)<\\/strong>/', $notice['message'], $missing_pages);
     }
     $this->assertEmpty($missing_pages);
     // Reset buddypress() vars
     $bp->pages = $reset_bp_pages;
     $bp->admin->notices = $reset_admin_notices;
 }