Skip to:
Content

BuddyPress.org


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/bp-core-catchuri.php

    r2695 r2863  
    2828function bp_core_set_uri_globals() {
    2929    global $current_component, $current_action, $action_variables;
    30     global $displayed_user_id;
     30    global $displayed_user_id, $bp_pages;
    3131    global $is_member_page;
    32     global $bp_unfiltered_uri;
     32    global $bp_unfiltered_uri, $bp_unfiltered_uri_offset;
    3333    global $bp, $current_blog;
     34
     35    /* Fetch all the WP page names for each component */
     36    if ( empty( $bp_pages ) )
     37        $bp_pages = bp_core_get_page_names();
    3438
    3539    if ( !defined( 'BP_ENABLE_MULTIBLOG' ) && bp_core_is_multisite() ) {
     
    6973    $action_index = $component_index + 1;
    7074
    71     // If this is a WordPress page, return from the function.
    72     if ( is_page( $bp_uri[$component_index] ) )
    73         return false;
    74 
    7575    /* Get site path items */
    7676    $paths = explode( '/', bp_core_get_site_path() );
     
    9494    $bp_unfiltered_uri = $bp_uri;
    9595
    96     /* If we are under anything with a members slug, set the correct globals */
    97     if ( $bp_uri[0] == BP_MEMBERS_SLUG ) {
    98         $is_member_page = true;
    99         $is_root_component = true;
    100     }
    101 
    102     /* Catch a member page and set the current member ID */
    103     if ( !defined( 'BP_ENABLE_ROOT_PROFILES' ) ) {
    104         if ( ( $bp_uri[0] == BP_MEMBERS_SLUG && !empty( $bp_uri[1] ) ) || in_array( 'wp-load.php', $bp_uri ) ) {
    105             // We are within a member page, set up user id globals
    106             $displayed_user_id = bp_core_get_displayed_userid( $bp_uri[1] );
    107 
    108             unset($bp_uri[0]);
    109             unset($bp_uri[1]);
    110 
    111             /* Reset the keys by merging with an empty array */
    112             $bp_uri = array_merge( array(), $bp_uri );
    113         }
    114     } else {
    115         if ( get_userdatabylogin( $bp_uri[0] ) || in_array( 'wp-load.php', $bp_uri ) ) {
    116             $is_member_page = true;
    117             $is_root_component = true;
    118 
    119             // We are within a member page, set up user id globals
    120             $displayed_user_id = bp_core_get_displayed_userid( $bp_uri[0] );
    121 
    122             unset($bp_uri[0]);
    123 
    124             /* Reset the keys by merging with an empty array */
    125             $bp_uri = array_merge( array(), $bp_uri );
    126         }
    127     }
    128 
    129     if ( !isset($is_root_component) )
    130         $is_root_component = in_array( $bp_uri[0], $bp->root_components );
    131 
    132     if ( 'no' == VHOST && !$is_root_component ) {
    133         $component_index++;
    134         $action_index++;
    135     }
     96    /* Find a match within registered BuddyPress controlled WP pages (check members first) */
     97    foreach ( (array)$bp_pages as $page_key => $bp_page ) {
     98        if ( in_array( $bp_page->name, (array)$bp_uri ) ) {
     99            /* Match found, now match the slug to make sure. */
     100            $uri_chunks = explode( '/', $bp_page->slug );
     101
     102            foreach ( (array)$uri_chunks as $key => $uri_chunk ) {
     103                if ( $bp_uri[$key] == $uri_chunk ) {
     104                    $matches[] = 1;
     105                } else {
     106                    $matches[] = 0;
     107                }
     108            }
     109
     110            if ( !in_array( 0, (array) $matches ) ) {
     111                $match = $bp_page;
     112                $match->key = $page_key;
     113                break;
     114            };
     115
     116            unset( $matches );
     117        }
     118
     119        unset( $uri_chunks );
     120    }
     121
     122    /* This is not a BuddyPress page, so just return. */
     123    if ( in_array( 0, (array) $matches ) )
     124        return false;
     125
     126    /* Find the offset */
     127    $uri_offset = 0;
     128    $slug = explode( '/', $match->slug );
     129
     130    if ( !empty( $slug ) && 1 != count( $slug ) ) {
     131        array_pop( $slug );
     132        $uri_offset = count( $slug );
     133    }
     134
     135    /* Global the unfiltered offset to use in bp_core_load_template() */
     136    $bp_unfiltered_uri_offset = $uri_offset;
     137
     138    /* This is a members page so lets check if we have a displayed member */
     139    if ( 'members' == $match->key ) {
     140        if ( !empty( $bp_uri[$uri_offset + 1] ) ) {
     141            if ( defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) )
     142                $displayed_user_id = (int) bp_core_get_userid( $bp_uri[$uri_offset + 1] );
     143            else
     144                $displayed_user_id = (int) bp_core_get_userid_from_nicename( $bp_uri[$uri_offset + 1] );
     145
     146            $uri_offset = $uri_offset + 2;
     147
     148            /* Remove everything from the URI up to the offset and take it from there. */
     149            for ( $i = 0; $i < $uri_offset; $i++ ) {
     150                unset( $bp_uri[$i] );
     151            }
     152
     153            $current_component = $bp_uri[$uri_offset];
     154        }
     155    }
     156
     157    /* Reset the keys by merging with an empty array */
     158    $bp_uri = array_merge( array(), $bp_uri );
    136159
    137160    /* Set the current component */
    138     $current_component = $bp_uri[$component_index];
     161    if ( empty( $current_component ) ) {
     162        for ( $i = 0; $i <= $uri_offset; $i++ ) {
     163            if ( !empty( $bp_uri[$i] ) ) {
     164                $current_component .= $bp_uri[$i];
     165
     166                if ( $i != $uri_offset )
     167                    $current_component .= '/';
     168            }
     169        }
     170    } else
     171        $i = 1;
    139172
    140173    /* Set the current action */
    141     $current_action = $bp_uri[$action_index];
     174    $current_action = $bp_uri[$i];
     175
     176    /* Unset the current_component and action from action_variables */
     177    for ( $j = 0; $j <= $i; $j++ )
     178        unset( $bp_uri[$j] );
    142179
    143180    /* Set the entire URI as the action variables, we will unset the current_component and action in a second */
    144181    $action_variables = $bp_uri;
    145182
    146     /* Unset the current_component and action from action_variables */
    147     unset($action_variables[$component_index]);
    148     unset($action_variables[$action_index]);
    149 
    150183    /* Remove the username from action variables if this is not a VHOST install */
    151184    if ( 'no' == VHOST && !$is_root_component )
    152         array_shift($action_variables);
     185        array_shift($bp_uri);
    153186
    154187    /* Reset the keys by merging with an empty array */
    155188    $action_variables = array_merge( array(), $action_variables );
    156189
    157     //var_dump($current_component, $current_action, $action_variables); die;
     190    //var_dump($current_component, $current_action, $bp_uri);
    158191}
    159192add_action( 'plugins_loaded', 'bp_core_set_uri_globals', 3 );
    160193
    161194/**
    162  * bp_catch_uri()
    163  *
    164  * Takes either a single page name or array of page names and
    165  * loads the first template file that can be found.
    166  *
    167  * Please don't call this function directly anymore, use: bp_core_load_template()
     195 * bp_core_load_template()
     196 *
     197 * Load a specific template file with fallback support.
     198 *
     199 * Example:
     200 *   bp_core_load_template( 'members/index' );
     201 * Loads:
     202 *   wp-content/themes/[activated_theme]/members/index.php
    168203 *
    169204 * @package BuddyPress Core
    170  * @global $bp_path BuddyPress global containing the template file names to use.
    171  * @param $pages Template file names to use.
    172  * @uses add_action() Hooks a function on to a specific action
     205 * @param $username str Username to check.
     206 * @global $wpdb WordPress DB access object.
     207 * @return false on no match
     208 * @return int the user ID of the matched user.
    173209 */
    174 function bp_catch_uri( $pages, $skip_blog_check = false ) {
    175     global $bp_path, $bp_skip_blog_check;
    176 
    177     $bp_skip_blog_check = $skip_blog_check;
    178 
    179     $bp_path = $pages;
    180 
    181     if ( !bp_is_blog_page() ) {
    182         remove_action( 'template_redirect', 'redirect_canonical' );
    183     }
    184     add_action( 'template_redirect', 'bp_core_do_catch_uri', 2 );
    185 }
    186 
    187 /**
    188  * bp_core_do_catch_uri()
    189  *
    190  * Loads the first template file found based on the $bp_path global.
    191  *
    192  * @package BuddyPress Core
    193  * @global $bp_path BuddyPress global containing the template file names to use.
    194  */
    195 function bp_core_do_catch_uri() {
    196     global $bp_path, $bp, $wpdb;
    197     global $current_blog, $bp_skip_blog_check;
    198     global $bp_no_status_set;
    199     global $wp_query;
    200 
    201     /* Can be a single template or an array of templates */
    202     $templates = $bp_path;
    203 
    204     /* Don't hijack any URLs on blog pages */
    205     if ( bp_is_blog_page() ) {
    206         if ( !$bp_skip_blog_check )
    207             return false;
    208     } else {
    209         $wp_query->is_home = false;
    210     }
    211 
    212     /* Make sure this is not reported as a 404 */
    213     if ( !$bp_no_status_set ) {
    214         status_header( 200 );
    215         $wp_query->is_404 = false;
    216         $wp_query->is_page = true;
    217     }
    218 
     210function bp_core_load_template( $templates ) {
     211    global $bp, $wpdb, $wp_query, $bp_unfiltered_uri, $bp_unfiltered_uri_offset;
     212
     213    /* Determine if the root object WP page exists for this request (is there an API function for this?)*/
     214    if ( !$page_exists = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $bp_unfiltered_uri[$bp_unfiltered_uri_offset] ) ) )
     215        return false;
     216
     217    /* Set the root object as the current wp_query-ied item */
     218    foreach ( $bp->pages as $page ) {
     219        if ( $page->name == $bp_unfiltered_uri[$bp_unfiltered_uri_offset] )
     220            $object_id = $page->id;
     221    }
     222
     223    $wp_query->queried_object = &get_post( $object_id );
     224    $wp_query->queried_object_id = $object_id;
     225
     226    /* Fetch each template and add the php suffix */
    219227    foreach ( (array)$templates as $template )
    220228        $filtered_templates[] = $template . '.php';
    221229
     230    /* Filter the template locations so that plugins can alter where they are located */
    222231    if ( $located_template = apply_filters( 'bp_located_template', locate_template( (array) $filtered_templates, false ), $filtered_templates ) ) {
     232        /* Template was located, lets set this as a valid page and not a 404. */
     233        status_header( 200 );
     234        $wp_query->is_page = true;
     235        $wp_query->is_404 = false;
     236
    223237        load_template( apply_filters( 'bp_load_template', $located_template ) );
    224     } else {
    225         if ( $located_template = locate_template( array( '404.php' ) ) ) {
    226             status_header( 404 );
    227             load_template( $located_template );
    228         } else
    229             bp_core_redirect( $bp->root_domain );
    230     }
     238    }
     239
     240    /* Kill any other output after this. */
    231241    die;
    232242}
    233 
    234 function bp_core_catch_no_access() {
    235     global $bp, $bp_path, $bp_unfiltered_uri, $bp_no_status_set;
    236 
    237     // If bp_core_redirect() and $bp_no_status_set is true,
    238     // we are redirecting to an accessable page, so skip this check.
    239     if ( $bp_no_status_set )
    240         return false;
    241 
    242     /* If this user has been marked as a spammer and the logged in user is not a site admin, redirect. */
    243     if ( isset( $bp->displayed_user->id ) && bp_core_is_user_spammer( $bp->displayed_user->id ) ) {
    244         if ( !is_site_admin() )
    245             bp_core_redirect( $bp->root_domain );
    246         else
    247             bp_core_add_message( __( 'This user has been marked as a spammer. Only site admins can view this profile.', 'buddypress' ), 'error' );
    248     }
    249 
    250     // If this user does not exist, redirect to the root domain.
    251     if ( !$bp->displayed_user->id && $bp_unfiltered_uri[0] == BP_MEMBERS_SLUG && isset($bp_unfiltered_uri[1]) )
    252         bp_core_redirect( $bp->root_domain );
    253 
    254     // If the template file doesn't exist, redirect to the root domain.
    255     if ( !bp_is_blog_page() && !file_exists( apply_filters( 'bp_located_template', locate_template( array( $bp_path . '.php' ), false ), array( $bp_path . '.php' ) ) ) )
    256         bp_core_redirect( $bp->root_domain );
    257 
    258     if ( !$bp_path && !bp_is_blog_page() ) {
    259         if ( is_user_logged_in() ) {
    260             wp_redirect( $bp->root_domain );
    261         } else {
    262             wp_redirect( site_url( 'wp-login.php?redirect_to=' . site_url() . $_SERVER['REQUEST_URI'] ) );
    263         }
    264     }
    265 }
    266 add_action( 'wp', 'bp_core_catch_no_access' );
    267243
    268244/**
Note: See TracChangeset for help on using the changeset viewer.