Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/13/2012 02:00:50 PM (12 years ago)
Author:
boonebgorges
Message:

Some canonical URL reconfiguration:

  • Introduces bp_get_canonical_url() and bp_get_requested_url(), to abstract some commonly used canonical logic
  • Provides an easy way for plugins to access the current canonical URL, using bp_get_canonical_url(). Fixes #4141
File:
1 edited

Legend:

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

    r5982 r5985  
    531531 *   ['component'] may be set
    532532 * @see bp_core_new_nav_item() where $bp->canonical_stack['action'] may be set
     533 * @uses bp_get_canonical_url()
     534 * @uses bp_get_requested_url()
    533535 */
    534536function bp_redirect_canonical() {
     
    545547
    546548        // build the URL in the address bar
    547         $requested_url  = is_ssl() ? 'https://' : 'http://';
    548         $requested_url .= $_SERVER['HTTP_HOST'];
    549         $requested_url .= $_SERVER['REQUEST_URI'];
     549        $requested_url  = bp_get_requested_url();
    550550
    551551        // Stash query args
    552552        $url_stack      = explode( '?', $requested_url );
    553553        $req_url_clean  = $url_stack[0];
    554 
    555         // Build the canonical URL out of the redirect stack
    556         if ( isset( $bp->canonical_stack['base_url'] ) ) {
    557             $url_stack[0] = $bp->canonical_stack['base_url'];
    558         }
    559 
    560         if ( isset( $bp->canonical_stack['component'] ) ) {
    561             $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
    562         }
    563 
    564         if ( isset( $bp->canonical_stack['action'] ) ) {
    565             $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
    566         }
    567 
    568         if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
    569             foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
    570                 $url_stack[0] = trailingslashit( $url_stack[0] . $av );
    571             }
    572         }
    573 
    574         // Add trailing slash
    575         $url_stack[0] = trailingslashit( $url_stack[0] );
     554        $query_args     = isset( $url_stack[1] ) ? $url_stack[1] : '';
     555
     556        $canonical_url  = bp_get_canonical_url();
    576557
    577558        // Only redirect if we've assembled a URL different from the request
    578         if ( $url_stack[0] !== $req_url_clean ) {
     559        if ( $canonical_url !== $req_url_clean ) {
    579560
    580561            // Template messages have been deleted from the cookie by this point, so
     
    587568            }
    588569
    589             bp_core_redirect( implode( '?', $url_stack ), 301 );
     570            if ( !empty( $query_args ) ) {
     571                $canonical_url .= '?' . $query_args;
     572            }
     573
     574            bp_core_redirect( $canonical_url, 301 );
    590575        }
    591576    }
     
    598583 */
    599584function bp_rel_canonical() {
    600     // Build the URL in the address bar
    601     $requested_url  = is_ssl() ? 'https://' : 'http://';
    602     $requested_url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    603 
    604     // Stash query args
    605     $url_stack      = explode( '?', $requested_url );
    606 
    607     // Build the canonical URL out of the redirect stack
    608     if ( isset( $bp->canonical_stack['base_url'] ) )
    609         $url_stack[0] = $bp->canonical_stack['base_url'];
    610 
    611     if ( isset( $bp->canonical_stack['component'] ) )
    612         $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
    613 
    614     if ( isset( $bp->canonical_stack['action'] ) )
    615         $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
    616 
    617     if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
    618         foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
    619             $url_stack[0] = trailingslashit( $url_stack[0] . $av );
    620         }
    621     }
    622 
    623     // Add trailing slash
    624     $url_stack[0] = trailingslashit( $url_stack[0] );
     585    $canonical_url = bp_get_canonical_url();
    625586
    626587    // Output rel=canonical tag
    627     echo "<link rel='canonical' href='" . esc_attr( $url_stack[0] ) . "' />\n";
     588    echo "<link rel='canonical' href='" . esc_attr( $canonical_url ) . "' />\n";
     589}
     590
     591/**
     592 * Returns the canonical URL of the current page
     593 *
     594 * @since BuddyPress (1.6)
     595 * @uses apply_filters() Filter bp_get_canonical_url to modify return value
     596 * @param array $args
     597 * @return string
     598 */
     599function bp_get_canonical_url( $args = array() ) {
     600    global $bp;
     601
     602    // For non-BP content, return the requested url, and let WP do the work
     603    if ( bp_is_blog_page() ) {
     604        return bp_get_requested_url();
     605    }
     606
     607    $defaults = array(
     608        'include_query_args' => false // Include URL arguments, eg ?foo=bar&foo2=bar2
     609    );
     610    $r = wp_parse_args( $args, $defaults );
     611    extract( $r );
     612
     613    if ( empty( $bp->canonical_stack['canonical_url'] ) ) {
     614        // Build the URL in the address bar
     615        $requested_url  = bp_get_requested_url();
     616
     617        // Stash query args
     618        $url_stack      = explode( '?', $requested_url );
     619
     620        // Build the canonical URL out of the redirect stack
     621        if ( isset( $bp->canonical_stack['base_url'] ) )
     622            $url_stack[0] = $bp->canonical_stack['base_url'];
     623
     624        if ( isset( $bp->canonical_stack['component'] ) )
     625            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
     626
     627        if ( isset( $bp->canonical_stack['action'] ) )
     628            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
     629
     630        if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
     631            foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
     632                $url_stack[0] = trailingslashit( $url_stack[0] . $av );
     633            }
     634        }
     635
     636        // Add trailing slash
     637        $url_stack[0] = trailingslashit( $url_stack[0] );
     638
     639        // Stash in the $bp global
     640        $bp->canonical_stack['canonical_url'] = implode( '?', $url_stack );
     641    }
     642
     643    $canonical_url = $bp->canonical_stack['canonical_url'];
     644
     645    if ( !$include_query_args ) {
     646        $canonical_url = array_pop( array_reverse( explode( '?', $canonical_url ) ) );
     647    }
     648
     649    return apply_filters( 'bp_get_canonical_url', $canonical_url, $args );
     650}
     651
     652/**
     653 * Returns the URL as requested on the current page load by the user agent
     654 *
     655 * @since BuddyPress (1.6)
     656 * @return string
     657 */
     658function bp_get_requested_url() {
     659    global $bp;
     660
     661    if ( empty( $bp->canonical_stack['requested_url'] ) ) {
     662        $bp->canonical_stack['requested_url']  = is_ssl() ? 'https://' : 'http://';
     663        $bp->canonical_stack['requested_url'] .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     664    }
     665
     666    return $bp->canonical_stack['requested_url'];
    628667}
    629668
Note: See TracChangeset for help on using the changeset viewer.