Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/09/2011 06:38:28 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Performance improvements and phpdoc to bp-core-catchuri.php. Introduce bp_core_enable_root_profiles() function as wrapper for constant.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-catchuri.php

    r5316 r5319  
    11<?php
     2
     3/**
     4 * BuddyPress URI catcher
     5 *
     6 * Functions for parsing the URI and determining which BuddyPress template file
     7 * to use on-screen.
     8 *
     9 * Based on contributions from: Chris Taylor - http://www.stillbreathing.co.uk/
     10 * Modified for BuddyPress by: Andy Peatling - http://apeatling.wordpress.com/
     11 *
     12 * @package BuddyPress
     13 * @subpackage Core
     14 */
     15
    216// Exit if accessed directly
    317if ( !defined( 'ABSPATH' ) ) exit;
    418
    5 /*
    6 Based on contributions from: Chris Taylor - http://www.stillbreathing.co.uk/
    7 Modified for BuddyPress by: Andy Peatling - http://apeatling.wordpress.com/
    8 */
    9 
    1019/**
    1120 * Analyzes the URI structure and breaks it down into parts for use in code.
    12  * The idea is that BuddyPress can use complete custom friendly URI's without the
    13  * user having to add new re-write rules.
    14  *
    15  * Future custom components would then be able to use their own custom URI structure.
     21 * BuddyPress can use complete custom friendly URI's without the user having to
     22 * add new re-write rules. Custom components are able to use their own custom
     23 * URI structures with very little work.
    1624 *
    1725 * @package BuddyPress Core
    … …  
    3644                return false;
    3745
     46        // Define local variables
     47        $root_profile = $match   = false;
     48        $key_slugs    = $matches = $uri_chunks = array();
     49
    3850        // Fetch all the WP page names for each component
    3951        if ( empty( $bp->pages ) )
    … …  
    4961        $path = apply_filters( 'bp_uri', $path );
    5062
    51         // Take GET variables off the URL to avoid problems,
    52         // they are still registered in the global $_GET variable
    53         if ( $noget = substr( $path, 0, strpos( $path, '?' ) ) )
    54                 $path = $noget;
    55 
    56         // Fetch the current URI and explode each part separated by '/' into an array
     63        // Take GET variables off the URL to avoid problems
     64        $path = strtok( $path, '?' );
     65
     66        // Fetch current URI and explode each part separated by '/' into an array
    5767        $bp_uri = explode( '/', $path );
    5868
    5969        // Loop and remove empties
    60         foreach ( (array)$bp_uri as $key => $uri_chunk )
    61                 if ( empty( $bp_uri[$key] ) ) unset( $bp_uri[$key] );
    62 
    63         // Running off blog other than root
     70        foreach ( (array)$bp_uri as $key => $uri_chunk ) {
     71                if ( empty( $bp_uri[$key] ) ) {
     72                        unset( $bp_uri[$key] );
     73                }
     74        }
     75
     76        // If running off blog other than root, any subdirectory names must be
     77        // removed from $bp_uri. This includes two cases:
     78        //
     79        //    1. when WP is installed in a subdirectory,
     80        //    2. when BP is running on secondary blog of a subdirectory
     81        //       multisite installation. Phew!
    6482        if ( is_multisite() && !is_subdomain_install() && ( bp_is_multiblog_mode() || 1 != bp_get_root_blog_id() ) ) {
    6583
    66                 // Any subdirectory names must be removed from $bp_uri.
    67                 // This includes two cases: (1) when WP is installed in a subdirectory,
    68                 // and (2) when BP is running on secondary blog of a subdirectory
    69                 // multisite installation. Phew!
    70                 if ( $chunks = explode( '/', $current_blog->path ) ) {
     84                // Blow chunks
     85                $chunks = explode( '/', $current_blog->path );
     86
     87                // If chunks exist...
     88                if ( !empty( $chunks ) ) {
     89
     90                        // ...loop through them...
    7191                        foreach( $chunks as $key => $chunk ) {
    7292                                $bkey = array_search( $chunk, $bp_uri );
    7393
    74                                 if ( $bkey !== false ) {
     94                                // ...and unset offending keys
     95                                if ( false !== $bkey ) {
    7596                                        unset( $bp_uri[$bkey] );
    7697                                }
    … …  
    80101                }
    81102        }
    82 
    83         // Set the indexes, these are incresed by one if we are not on a VHOST install
    84         $component_index = 0;
    85         $action_index    = $component_index + 1;
    86103
    87104        // Get site path items
    … …  
    178195
    179196        // URLs with BP_ENABLE_ROOT_PROFILES enabled won't be caught above
    180         if ( empty( $matches ) && defined( 'BP_ENABLE_ROOT_PROFILES' ) && BP_ENABLE_ROOT_PROFILES ) {
     197        if ( empty( $matches ) && bp_core_enable_root_profiles() ) {
    181198
    182199                // Make sure there's a user corresponding to $bp_uri[0]
    … …  
    296313
    297314/**
     315 * Are root profiles enabled and allowed
     316 *
     317 * @since BuddyPress (1.6)
     318 * @return bool True if yes, false if no
     319 */
     320function bp_core_enable_root_profiles() {
     321       
     322        $retval = false;
     323
     324        if ( defined( 'BP_ENABLE_ROOT_PROFILES' ) && ( true == BP_ENABLE_ROOT_PROFILES ) )
     325                $retval = true;
     326       
     327        return apply_filters( 'bp_core_enable_root_profiles', $retval );
     328}
     329
     330/**
    298331 * bp_core_load_template()
    299332 *
    … …  
    310343 */
    311344function bp_core_load_template( $templates ) {
    312         global $post, $bp, $wpdb, $wp_query;
    313 
    314         // Determine if the root object WP page exists for this request (TODO: is there an API function for this?
    315         if ( !empty( $bp->unfiltered_uri_offset ) && !$page_exists = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) ) )
     345        global $post, $bp, $wp_query;
     346
     347        // Bail if there is no offset
     348        if ( empty( $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) )
     349                return false;
     350
     351        // Determine if the root object WP page exists for this request
     352        if ( !get_page_by_path( $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) )
    316353                return false;
    317354
    318355        // Set the root object as the current wp_query-ied item
    319356        $object_id = 0;
    320         foreach ( (array)$bp->pages as $page ) {
    321                 if ( isset( $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) && ( $page->name == $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) ) {
     357        foreach ( (array) $bp->pages as $page ) {
     358                if ( $page->name == $bp->unfiltered_uri[$bp->unfiltered_uri_offset] ) {
    322359                        $object_id = $page->id;
    323360                }
    … …  
    331368        }
    332369
     370        // Define local variables
     371        $located_template   = false;
     372        $filtered_templates = array();
     373
    333374        // Fetch each template and add the php suffix
    334         foreach ( (array)$templates as $template )
     375        foreach ( (array) $templates as $template )
    335376                $filtered_templates[] = $template . '.php';
    336377
    337378        // Filter the template locations so that plugins can alter where they are located
    338         if ( $located_template = apply_filters( 'bp_located_template', locate_template( (array) $filtered_templates, false ), $filtered_templates ) ) {
     379        $located_template = apply_filters( 'bp_located_template', locate_template( (array) $filtered_templates, false ), $filtered_templates );
     380        if ( !empty( $located_template ) ) {
     381
    339382                // Template was located, lets set this as a valid page and not a 404.
    340383                status_header( 200 );
    … …  
    405448
    406449        // Apply filters to these variables
    407         $mode           = apply_filters( 'bp_no_access_mode', $mode, $root, $redirect, $message );
    408         $redirect       = apply_filters( 'bp_no_access_redirect', $redirect, $root, $message, $mode );
    409         $root           = trailingslashit( apply_filters( 'bp_no_access_root', $root, $redirect, $message, $mode ) );
    410         $message        = apply_filters( 'bp_no_access_message', $message, $root, $redirect, $mode );
     450        $mode           = apply_filters( 'bp_no_access_mode',     $mode,     $root,     $redirect, $message );
     451        $redirect       = apply_filters( 'bp_no_access_redirect', $redirect, $root,     $message,  $mode    );
     452        $root           = apply_filters( 'bp_no_access_root',     $root,     $redirect, $message,  $mode    );
     453        $message        = apply_filters( 'bp_no_access_message',  $message,  $root,     $redirect, $mode    );
     454        $root       = trailingslashit( $root );
    411455
    412456        switch ( $mode ) {
Note: See TracChangeset for help on using the changeset viewer.