Skip to:
Content

BuddyPress.org

Changeset 2863 for trunk/bp-core.php


Ignore:
Timestamp:
03/22/2010 11:34:23 AM (15 years ago)
Author:
apeatling
Message:

Added new install/upgrade wizard. Removed root components and replaced them with actual WordPress pages. Testing on WordPress vhost/novhost and root profile support still to do.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core.php

    r2842 r2863  
    11<?php
    2 
    3 /* Define the current version number for checking if DB tables are up to date. */
    4 define( 'BP_CORE_DB_VERSION', '1800' );
    52
    63/***
     
    4643    require ( BP_PLUGIN_DIR . '/bp-core/bp-core-adminbar.php' );
    4744
    48 /* Define the slug for member pages and the members directory (e.g. domain.com/[members] ) */
    49 if ( !defined( 'BP_MEMBERS_SLUG' ) )
    50     define( 'BP_MEMBERS_SLUG', 'members' );
    51 
    52 /* Define the slug for the register/signup page */
    53 if ( !defined( 'BP_REGISTER_SLUG' ) )
    54     define( 'BP_REGISTER_SLUG', 'register' );
    55 
    56 /* Define the slug for the activation page */
    57 if ( !defined( 'BP_ACTIVATION_SLUG' ) )
    58     define( 'BP_ACTIVATION_SLUG', 'activate' );
    59 
    60 /* Define the slug for the search page */
    61 if ( !defined( 'BP_SEARCH_SLUG' ) )
    62     define( 'BP_SEARCH_SLUG', 'search' );
    63 
    6445/* Register BuddyPress themes contained within the bp-theme folder */
    6546if ( function_exists( 'register_theme_directory') )
     
    8768    global $bp, $wpdb;
    8869    global $current_user, $current_component, $current_action, $current_blog;
    89     global $displayed_user_id;
     70    global $displayed_user_id, $bp_pages;
    9071    global $action_variables;
    9172
     
    9576    $bp->root_domain = bp_core_get_root_domain();
    9677
     78    /* Contains an array of all the active components. The key is the slug, value the internal ID of the component */
     79    $bp->active_components = array();
     80
     81    /* The names of the core WordPress pages used to display BuddyPress content */
     82    $bp->pages = $bp_pages;
     83
     84    /* Set up the members id and active components entry */
     85    $bp->members->id = 'members';
     86    $bp->members->slug = $bp->pages->members->slug;
     87    $bp->active_components[$bp->members->slug] = $bp->members->id;
     88
    9789    /* The user ID of the user who is currently logged in. */
    9890    $bp->loggedin_user->id = $current_user->ID;
     
    133125    /* The default component to use if none are set and someone visits: http://domain.com/members/andy */
    134126    if ( !defined( 'BP_DEFAULT_COMPONENT' ) ) {
    135         if ( defined( 'BP_ACTIVITY_SLUG' ) )
    136             $bp->default_component = BP_ACTIVITY_SLUG;
     127        if ( isset( $bp->pages->activity ) )
     128            $bp->default_component = $bp->pages->activity->name;
    137129        else
    138             $bp->default_component = 'profile';
     130            $bp->default_component = $bp->pages->profile->name;
    139131    } else {
    140132        $bp->default_component = BP_DEFAULT_COMPONENT;
     
    149141    /* Sets up the array container for the component options navigation rendered by bp_get_options_nav() */
    150142    $bp->bp_options_nav = array();
    151 
    152     /* Contains an array of all the active components. The key is the slug, value the internal ID of the component */
    153     $bp->active_components = array();
    154143
    155144    /* Fetches the default Gravatar image to use if the user/group/blog has no avatar or gravatar */
     
    181170
    182171/**
    183  * bp_core_setup_root_uris()
    184  *
    185  * Adds the core URIs that should run in the root of the installation.
    186  *
    187  * For example: http://example.org/search/ or http://example.org/members/
    188  *
    189  * @package BuddyPress Core
    190  * @uses bp_core_add_root_component() Adds a slug to the root components global variable.
    191  */
    192 function bp_core_setup_root_uris() {
    193     /* Add core root components */
    194     bp_core_add_root_component( BP_MEMBERS_SLUG );
    195     bp_core_add_root_component( BP_REGISTER_SLUG );
    196     bp_core_add_root_component( BP_ACTIVATION_SLUG );
    197     bp_core_add_root_component( BP_SEARCH_SLUG );
    198 }
    199 add_action( 'plugins_loaded', 'bp_core_setup_root_uris', 2 );
    200 
    201 
    202 /**
    203  * bp_core_install()
    204  *
    205  * Installs the core DB tables for BuddyPress.
    206  *
    207  * @package BuddyPress Core
     172 * bp_core_define_slugs()
     173 *
     174 * Define the slugs used for BuddyPress pages, based on the slugs of the WP pages used.
     175 * These can be overridden manually by defining these slugs in wp-config.php.
     176 *
     177 * @package BuddyPress Core Core
    208178 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
    209  * @global $wpdb WordPress DB access object.
    210  * @uses dbDelta() Performs a table creation, or upgrade based on what already exists in the DB.
    211  * @uses bp_core_add_illegal_names() Adds illegal blog names to the WP settings
    212  */
    213 function bp_core_install() {
    214     global $wpdb, $bp;
    215 
    216     if ( !empty($wpdb->charset) )
    217         $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    218 
    219     $sql[] = "CREATE TABLE {$bp->core->table_name_notifications} (
    220                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    221                 user_id bigint(20) NOT NULL,
    222                 item_id bigint(20) NOT NULL,
    223                 secondary_item_id bigint(20),
    224                 component_name varchar(75) NOT NULL,
    225                 component_action varchar(75) NOT NULL,
    226                 date_notified datetime NOT NULL,
    227                 is_new bool NOT NULL DEFAULT 0,
    228                 KEY item_id (item_id),
    229                 KEY secondary_item_id (secondary_item_id),
    230                 KEY user_id (user_id),
    231                 KEY is_new (is_new),
    232                 KEY component_name (component_name),
    233                 KEY component_action (component_action),
    234                 KEY useritem (user_id,is_new)
    235                ) {$charset_collate};";
    236 
    237     require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );
    238     dbDelta( $sql );
    239 
    240     /* Add names of root components to the banned blog list to avoid conflicts */
    241     if ( bp_core_is_multisite() )
    242         bp_core_add_illegal_names();
    243 
    244     update_site_option( 'bp-core-db-version', BP_CORE_DB_VERSION );
    245 }
    246 
    247 /**
    248  * bp_core_check_installed()
    249  *
    250  * Checks to make sure the database tables are set up for the core component.
    251  *
    252  * @package BuddyPress Core
    253  * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
    254  * @global $wpdb WordPress DB access object.
    255  * @global $current_user WordPress global variable containing current logged in user information
     179 */
     180function bp_core_define_slugs() {
     181    global $bp;
     182
     183    if ( !defined( 'BP_MEMBERS_SLUG' ) )
     184        define( 'BP_MEMBERS_SLUG', $bp->pages->members->slug );
     185
     186    if ( !defined( 'BP_REGISTER_SLUG' ) )
     187        define( 'BP_REGISTER_SLUG', $bp->pages->register->slug );
     188
     189    if ( !defined( 'BP_ACTIVATION_SLUG' ) )
     190        define( 'BP_ACTIVATION_SLUG', $bp->pages->activate->slug );
     191}
     192add_action( 'bp_setup_globals', 'bp_core_define_slugs' );
     193
     194function bp_core_get_page_names() {
     195    global $wpdb;
     196
     197    $page_ids = get_site_option( 'bp-pages' );
     198
     199    if ( empty( $page_ids ) )
     200        return false;
     201
     202    $page_ids_sql = implode( ',', (array)$page_ids );
     203    $page_names = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM {$wpdb->posts} WHERE ID IN ({$page_ids_sql}) " ) );
     204    $pages = new stdClass;
     205
     206    foreach ( (array)$page_ids as $key => $page_id ) {
     207        foreach ( (array)$page_names as $page_name ) {
     208            if ( $page_name->ID == $page_id ) {
     209                $pages->{$key}->name = $page_name->post_name;
     210                $pages->{$key}->id = $page_name->ID;
     211
     212                $slug[] = $page_name->post_name;
     213
     214                /* Get the slug */
     215                while ( $page_name->post_parent != 0 ) {
     216                    $parent = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$wpdb->posts} WHERE ID = %d", $page_name->post_parent ) );
     217                    $slug[] = $parent[0]->post_name;
     218                    $page_name->post_parent = $parent[0]->post_parent;
     219                }
     220
     221                $pages->{$key}->slug = implode( '/', array_reverse( (array)$slug ) );
     222            }
     223
     224            unset( $slug );
     225        }
     226    }
     227
     228
     229    return apply_filters( 'bp_core_get_page_names', $pages );
     230}
     231
     232/**
     233 * bp_core_admin_menu_init()
     234 *
     235 * Initializes the wp-admin area "BuddyPress" menus and sub menus.
     236 *
     237 * @package BuddyPress Core
    256238 * @uses is_site_admin() returns true if the current user is a site admin, false if not
    257  * @uses get_site_option() fetches the value for a meta_key in the wp_sitemeta table
    258  * @uses bp_core_install() runs the installation of DB tables for the core component
    259  */
    260 function bp_core_check_installed() {
    261     global $wpdb, $bp;
    262 
     239 */
     240function bp_core_admin_menu_init() {
    263241    if ( !is_site_admin() )
    264242        return false;
    265243
    266     require ( BP_PLUGIN_DIR . '/bp-core/bp-core-admin.php' );
    267 
    268     /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
    269     if ( get_site_option( 'bp-core-db-version' ) < BP_CORE_DB_VERSION )
    270         bp_core_install();
    271 }
    272 add_action( 'admin_menu', 'bp_core_check_installed' );
     244    require ( BP_PLUGIN_DIR . '/bp-core/admin/bp-core-admin.php' );
     245}
     246add_action( 'admin_menu', 'bp_core_admin_menu_init' );
    273247
    274248/**
     
    288262
    289263    /* Add the administration tab under the "Site Admin" tab for site administrators */
    290     bp_core_add_admin_menu_page( array(
     264    $hook = bp_core_add_admin_menu_page( array(
    291265        'menu_title' => __( 'BuddyPress', 'buddypress' ),
    292266        'page_title' => __( 'BuddyPress', 'buddypress' ),
    293267        'access_level' => 10, 'file' => 'bp-general-settings',
    294         'function' => 'bp_core_admin_settings',
     268        'function' => 'bp_core_admin_dashboard',
    295269        'position' => 2
    296270    ) );
    297271
    298     add_submenu_page( 'bp-general-settings', __( 'General Settings', 'buddypress'), __( 'General Settings', 'buddypress' ), 'manage_options', 'bp-general-settings', 'bp_core_admin_settings' );
    299     add_submenu_page( 'bp-general-settings', __( 'Component Setup', 'buddypress'), __( 'Component Setup', 'buddypress' ), 'manage_options', 'bp-component-setup', 'bp_core_admin_component_setup' );
     272    add_submenu_page( 'bp-general-settings', __( 'BuddyPress Dashboard', 'buddypress' ), __( 'Dashboard', 'buddypress' ), 'manage_options', 'bp-general-settings', 'bp_core_admin_dashboard' );
     273    add_submenu_page( 'bp-general-settings', __( 'Settings', 'buddypress' ), __( 'Settings', 'buddypress' ), 'manage_options', 'bp-settings', 'bp_core_admin_settings' );
     274
     275    /* Add a hook for css/js */
     276    add_action( "admin_print_styles-$hook", 'bp_core_add_admin_menu_styles' );
    300277}
    301278add_action( 'admin_menu', 'bp_core_add_admin_menu' );
    302279
    303 /**
    304  * bp_core_is_root_component()
    305  *
    306  * Checks to see if a component's URL should be in the root, not under a member page:
    307  * eg: http://domain.com/groups/the-group NOT http://domain.com/members/andy/groups/the-group
    308  *
    309  * @package BuddyPress Core
    310  * @return true if root component, else false.
    311  */
    312 function bp_core_is_root_component( $component_name ) {
    313     global $bp;
    314 
    315     return in_array( $component_name, $bp->root_components );
    316 }
    317280
    318281/**
     
    395358    global $bp;
    396359
    397     if ( is_null( $bp->displayed_user->id ) && $bp->current_component == BP_MEMBERS_SLUG ) {
     360    if ( is_null( $bp->displayed_user->id ) && $bp->current_component == $bp->members->slug ) {
    398361        $bp->is_directory = true;
    399362
     
    507470add_action( 'wp', 'bp_core_action_delete_user', 3 );
    508471
    509 
    510472/********************************************************************************
    511473 * Business Functions
     
    564526        /* If we are using a members slug, include it. */
    565527        if ( !defined( 'BP_ENABLE_ROOT_PROFILES' ) )
    566             $domain = $bp->root_domain . '/' . BP_MEMBERS_SLUG . '/' . $username . '/';
     528            $domain = $bp->root_domain . '/' . $bp->members->slug . '/' . $username . '/';
    567529        else
    568530            $domain = $bp->root_domain . '/' . $username . '/';
     
    689651     * default subnav function so we can display a directory or something else.
    690652     */
    691     if ( bp_core_is_root_component( $slug ) && !$bp->displayed_user->id )
     653    if ( !$bp->displayed_user->id )
    692654        return;
    693655
     
    916878}
    917879
    918 /**
    919  * bp_core_load_template()
    920  *
    921  * Uses the bp_catch_uri function to load a specific template file with fallback support.
    922  *
    923  * Example:
    924  *   bp_core_load_template( 'profile/edit-profile' );
    925  * Loads:
    926  *   wp-content/themes/[activated_theme]/profile/edit-profile.php
     880
     881/**
     882 * bp_core_get_random_member()
     883 *
     884 * Returns the user_id for a user based on their username.
    927885 *
    928886 * @package BuddyPress Core
     
    932890 * @return int the user ID of the matched user.
    933891 */
    934 function bp_core_load_template( $template, $skip_blog_check = false ) {
    935     return bp_catch_uri( $template, $skip_blog_check );
    936 }
    937 
    938 /**
    939  * bp_core_add_root_component()
    940  *
    941  * Adds a component to the $bp->root_components global.
    942  * Any component that runs in the "root" of an install should be added.
    943  * The "root" as in, it can or always runs outside of the /members/username/ path.
    944  *
    945  * Example of a root component:
    946  *  Groups: http://domain.com/groups/group-name
    947  *          http://community.domain.com/groups/group-name
    948  *          http://domain.com/wpmu/groups/group-name
    949  *
    950  * Example of a component that is NOT a root component:
    951  *  Friends: http://domain.com/members/andy/friends
    952  *           http://community.domain.com/members/andy/friends
    953  *           http://domain.com/wpmu/members/andy/friends
    954  *
    955  * @package BuddyPress Core
    956  * @param $slug str The slug of the component
    957  * @global $bp BuddyPress global settings
    958  */
    959 function bp_core_add_root_component( $slug ) {
    960     global $bp;
    961 
    962     $bp->root_components[] = $slug;
    963 }
    964 
    965 /**
    966  * bp_core_get_random_member()
     892function bp_core_get_random_member() {
     893    global $bp, $wpdb;
     894
     895    if ( isset( $_GET['random-member'] ) ) {
     896        $user = bp_core_get_users( array( 'type' => 'random', 'per_page' => 1 ) );
     897        bp_core_redirect( bp_core_get_user_domain( $user['users'][0]->id ) );
     898    }
     899}
     900add_action( 'wp', 'bp_core_get_random_member' );
     901
     902/**
     903 * bp_core_get_userid()
    967904 *
    968905 * Returns the user_id for a user based on their username.
     
    974911 * @return int the user ID of the matched user.
    975912 */
    976 function bp_core_get_random_member() {
    977     global $bp, $wpdb;
    978 
    979     if ( isset( $_GET['random-member'] ) ) {
    980         $user = bp_core_get_users( array( 'type' => 'random', 'per_page' => 1 ) );
    981         bp_core_redirect( bp_core_get_user_domain( $user['users'][0]->id ) );
    982     }
    983 }
    984 add_action( 'wp', 'bp_core_get_random_member' );
    985 
    986 /**
    987  * bp_core_get_userid()
    988  *
    989  * Returns the user_id for a user based on their username.
     913function bp_core_get_userid( $username ) {
     914    global $wpdb;
     915
     916    if ( !empty( $username ) )
     917        return apply_filters( 'bp_core_get_userid', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_login = %s", $username ) ) );
     918}
     919
     920/**
     921 * bp_core_get_userid_from_nicename()
     922 *
     923 * Returns the user_id for a user based on their user_nicename.
    990924 *
    991925 * @package BuddyPress Core
     
    995929 * @return int the user ID of the matched user.
    996930 */
    997 function bp_core_get_userid( $username ) {
     931function bp_core_get_userid_from_nicename( $user_nicename ) {
    998932    global $wpdb;
    999933
    1000     if ( !empty( $username ) )
    1001         return apply_filters( 'bp_core_get_userid', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_login = %s", $username ) ) );
    1002 }
     934    if ( empty( $user_nicename ) )
     935        return false;
     936
     937    return apply_filters( 'bp_core_get_userid_from_nicename', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_nicename = %s", $user_nicename ) ) );
     938}
     939
    1003940
    1004941/**
     
    17181655            switch ( $search_which ) {
    17191656                case 'members': default:
    1720                     $slug = BP_MEMBERS_SLUG;
     1657                    $slug = $bp->members->slug;
    17211658                    $var = '/?s=';
    17221659                    break;
     
    20401977remove_action( 'plugins_loaded', 'catch_nonexistant_blogs' );
    20411978
     1979
     1980/* DEPRECATED FUNCTIONS ****/
     1981
     1982/**
     1983 * bp_core_add_root_component()
     1984 *
     1985 * This function originally let plugins add support for pages in the root of the install.
     1986 * These pages are now handled by actual WordPress pages so this function is deprecated.
     1987 * It now simply facilitates backwards compatibility by adding a WP page if the plugin has not been
     1988 * updated to do so.
     1989 *
     1990 * @package BuddyPress Core
     1991 * @param $slug str The slug of the component
     1992 * @global $bp BuddyPress global settings
     1993 */
     1994function bp_core_add_root_component( $slug ) {
     1995    global $bp, $bp_pages;
     1996
     1997    if ( empty( $bp_pages ) )
     1998        $bp_pages = bp_core_get_page_names();
     1999
     2000    $match = false;
     2001
     2002    /* Check if the slug is registered in the $bp->pages global */
     2003    foreach ( (array)$bp_pages as $key => $page ) {
     2004        if ( $key == $slug || $page->slug == $slug )
     2005            $match = true;
     2006    }
     2007
     2008    /* If there was no match, add a page for this root component */
     2009    if ( empty( $match ) ) {
     2010        $bp->add_root[] = $slug;
     2011        add_action( 'init', 'bp_core_create_root_component_page' );
     2012    }
     2013}
     2014
     2015function bp_core_create_root_component_page() {
     2016    global $bp;
     2017
     2018    $new_page_ids = array();
     2019
     2020    var_dump( $bp->add_root );
     2021
     2022    foreach ( (array)$bp->add_root as $slug )
     2023        $new_page_ids[$slug] = wp_insert_post( array( 'post_title' => ucwords( $slug ), 'post_status' => 'publish', 'post_type' => 'page' ) );
     2024
     2025    $page_ids = get_site_option( 'bp-pages' );
     2026    $page_ids = (array) $page_ids;
     2027    $page_ids = array_merge( (array) $new_page_ids, (array) $page_ids );
     2028    update_site_option( 'bp-pages', $page_ids );
     2029}
     2030
     2031function bp_core_is_root_component( $component_name ) {
     2032    global $bp;
     2033
     2034    foreach ( (array) $bp->pages as $key => $page ) {
     2035        if ( $key == $component_name || $page->slug == $component_name )
     2036            return true;
     2037    }
     2038
     2039    return false;
     2040}
     2041
    20422042?>
Note: See TracChangeset for help on using the changeset viewer.