Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/10/2011 10:48:38 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Fix bug in bp_core_set_uri_globals() where partial slug matches could override exact slugs based on the component load order, by manually checking for exact matches first, and looking for partials if no exact match is found.

Also attempt to better assign the $bp->current_component global. Previously we set it to be the root_slug and did extra work to figure out the component name; now we set the component name/key based on the match, and rely on that to set the current_component.

File:
1 edited

Legend:

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

    r4091 r4096  
    3434    $bp->action_variables = $bp->displayed_user->id = '';
    3535
     36    // Only catch URI's on the root blog if we are not running
     37    // on multiple blogs
    3638    if ( !defined( 'BP_ENABLE_MULTIBLOG' ) && is_multisite() ) {
    37         // Only catch URI's on the root blog if we are not running
    38         // on multiple blogs
    3939        if ( BP_ROOT_BLOG != (int) $current_blog->blog_id )
    4040            return false;
     
    5151        $path = esc_url( $_SERVER['REQUEST_URI'] );
    5252
     53    // Filter the path
    5354    $path = apply_filters( 'bp_uri', $path );
    5455
     
    9293
    9394    // Take empties off the end of path
    94     if ( empty( $paths[count($paths) - 1] ) )
     95    if ( empty( $paths[count( $paths ) - 1] ) )
    9596        array_pop( $paths );
    9697
     
    99100        array_shift( $paths );
    100101
    101     foreach ( (array)$bp_uri as $key => $uri_chunk ) {
    102         if ( in_array( $uri_chunk, $paths )) {
     102    // Unset URI indices if they intersect with the paths
     103    foreach ( (array) $bp_uri as $key => $uri_chunk ) {
     104        if ( in_array( $uri_chunk, $paths ) ) {
    103105            unset( $bp_uri[$key] );
    104106        }
     
    108110    $bp_uri = array_merge( array(), $bp_uri );
    109111
    110     // If a component is set to the front page, force its name into $bp_uri so that $current_component is populated
     112    // If a component is set to the front page, force its name into $bp_uri
     113    // so that $current_component is populated
    111114    if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && empty( $bp_uri ) ) {
    112115        $post = get_post( get_option( 'page_on_front' ) );
     
    115118    }
    116119
     120    // Keep the unfiltered URI safe
    117121    $bp_unfiltered_uri = $bp_uri;
    118122
    119     // Loop through each page in the global
     123    // Get slugs of pages into array
    120124    foreach ( (array) $bp->pages as $page_key => $bp_page ) {
    121 
    122         // Look for a match (check members first)
    123         if ( in_array( $bp_page->name, (array) $bp_uri ) ) {
    124 
    125             // Match found, now match the slug to make sure.
    126             $uri_chunks = explode( '/', $bp_page->slug );
    127 
    128             // Loop through uri_chunks
    129             foreach ( (array) $uri_chunks as $key => $uri_chunk ) {
    130 
    131                 // Make sure chunk is in the correct position
    132                 if ( !empty( $bp_uri[$key] ) && ( $bp_uri[$key] == $uri_chunk ) ) {
    133                     $matches[] = 1;
    134 
    135                 // No match
    136                 } else {
    137                     $matches[] = 0;
     125        $key_slugs[$page_key] = trailingslashit( '/' . $bp_page->slug );
     126    }
     127
     128    // Loop through page slugs and look for exact match to path
     129    foreach ( $key_slugs as $key => $slug ) {
     130        if ( $slug == $path ) {
     131            $match      = $bp->pages->{$key};
     132            $match->key = $key;
     133            $matches[]  = 1;
     134            break;
     135        }
     136    }
     137
     138    // No exact match, so look for partials
     139    if ( empty( $match ) ) {
     140
     141        // Loop through each page in the $bp->pages global
     142        foreach ( (array) $bp->pages as $page_key => $bp_page ) {
     143
     144            // Look for a match (check members first)
     145            if ( in_array( $bp_page->name, (array) $bp_uri ) ) {
     146
     147                // Match found, now match the slug to make sure.
     148                $uri_chunks = explode( '/', $bp_page->slug );
     149
     150                // Loop through uri_chunks
     151                foreach ( (array) $uri_chunks as $key => $uri_chunk ) {
     152
     153                    // Make sure chunk is in the correct position
     154                    if ( !empty( $bp_uri[$key] ) && ( $bp_uri[$key] == $uri_chunk ) ) {
     155                        $matches[] = 1;
     156
     157                    // No match
     158                    } else {
     159                        $matches[] = 0;
     160                    }
    138161                }
     162
     163                // Have a match
     164                if ( !in_array( 0, (array) $matches ) ) {
     165                    $match      = $bp_page;
     166                    $match->key = $page_key;
     167                    break;
     168                };
     169
     170                // Unset matches
     171                unset( $matches );
    139172            }
    140173
    141             // Have a match
    142             if ( !in_array( 0, (array) $matches ) ) {
    143                 $match      = $bp_page;
    144                 $match->key = $page_key;
    145                 break;
    146             };
    147 
    148             // Unset matches
    149             unset( $matches );
    150         }
    151 
    152         // Unset uri chunks
    153         unset( $uri_chunks );
     174            // Unset uri chunks
     175            unset( $uri_chunks );
     176        }
    154177    }
    155178
    156179    // Search doesn't have an associated page, so we check for it separately
    157     if ( !empty( $bp_uri[0] ) && BP_SEARCH_SLUG == $bp_uri[0] )
     180    if ( !empty( $bp_uri[0] ) && ( BP_SEARCH_SLUG == $bp_uri[0] ) )
    158181        $matches[] = 1;
    159182
     
    163186
    164187    // Find the offset
     188    $slug       = !empty ( $match ) ? explode( '/', $match->slug ) : '';
    165189    $uri_offset = 0;
    166     $slug       = explode( '/', $match->slug );
    167 
    168     if ( !empty( $slug ) && 1 != count( $slug ) ) {
     190
     191    // Rejig the offset
     192    if ( !empty( $slug ) && ( 1 < count( $slug ) ) ) {
    169193        array_pop( $slug );
    170194        $uri_offset = count( $slug );
     
    174198    $bp_unfiltered_uri_offset = $uri_offset;
    175199
    176     // This is a members page so lets check if we have a displayed member
    177     if ( isset( $match->key ) && 'members' == $match->key ) {
    178         if ( !empty( $bp_uri[$uri_offset + 1] ) ) {
    179             if ( defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) )
    180                 $bp->displayed_user->id = (int) bp_core_get_userid( urldecode( $bp_uri[$uri_offset + 1] ) );
    181             else
    182                 $bp->displayed_user->id = (int) bp_core_get_userid_from_nicename( urldecode( $bp_uri[$uri_offset + 1] ) );
    183 
    184             $uri_offset = $uri_offset + 2;
    185 
    186             // Remove everything from the URI up to the offset and take it from there.
    187             for ( $i = 0; $i < $uri_offset; $i++ )
    188                 unset( $bp_uri[$i] );
    189 
    190             $bp->current_component = isset( $bp_uri[$uri_offset] ) ? $bp_uri[$uri_offset] : '';
    191         }
    192     }
    193 
    194     // Reset the keys by merging with an empty array
    195     $bp_uri = array_merge( array(), $bp_uri );
    196 
    197     // Set the current component
    198     if ( empty( $bp->current_component ) ) {
    199         for ( $i = 0; $i <= $uri_offset; $i++ ) {
    200             if ( !empty( $bp_uri[$i] ) ) {
    201                 $bp->current_component .= $bp_uri[$i];
    202 
    203                 if ( $i != $uri_offset )
    204                     $bp->current_component .= '/';
     200    // We have an exact match
     201    if ( isset( $match->key ) ) {
     202
     203        // Set current component to matched key
     204        $bp->current_component = $match->key;
     205
     206        // If members component, do more work to find the actual component
     207        if ( 'members' == $match->key ) {
     208
     209            // Viewing a specific user
     210            if ( !empty( $bp_uri[$uri_offset + 1] ) ) {
     211
     212                // Switch the displayed_user based on cmpatbility mode
     213                if ( defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) )
     214                    $bp->displayed_user->id = (int) bp_core_get_userid( urldecode( $bp_uri[$uri_offset + 1] ) );
     215                else
     216                    $bp->displayed_user->id = (int) bp_core_get_userid_from_nicename( urldecode( $bp_uri[$uri_offset + 1] ) );
     217
     218                // Bump the offset
     219                if ( isset( $bp_uri[$uri_offset + 2] ) ) {
     220                    $bp_uri                = array_merge( array(), array_slice( $bp_uri, $uri_offset + 2 ) );
     221                    $bp->current_component = $bp_uri[0];
     222
     223                // No component, so default will be picked later
     224                } else {
     225                    $bp_uri                = array_merge( array(), array_slice( $bp_uri, $uri_offset + 2 ) );
     226                    $bp->current_component = '';
     227                }
     228               
     229                // Reset the offset
     230                $uri_offset = 0;
    205231            }
    206232        }
    207     } else {
    208         $i = 1;
    209233    }
    210234
    211235    // Set the current action
    212     $bp->current_action = isset( $bp_uri[$i] ) ? $bp_uri[$i] : '';
    213 
    214     // Unset the current_component and action from action_variables
    215     for ( $j = 0; $j <= $i; $j++ )
    216         unset( $bp_uri[$j] );
     236    $bp->current_action = isset( $bp_uri[$uri_offset + 1] ) ? $bp_uri[$uri_offset + 1] : '';
     237
     238    // Slice the rest of the $bp_uri array and reset offset
     239    $bp_uri      = array_slice( $bp_uri, $uri_offset + 2 );
     240    $uri_offset  = 0;
    217241
    218242    // Set the entire URI as the action variables, we will unset the current_component and action in a second
Note: See TracChangeset for help on using the changeset viewer.