Skip to:
Content

BuddyPress.org

Changeset 10791


Ignore:
Timestamp:
05/19/2016 06:04:47 PM (9 years ago)
Author:
r-a-y
Message:

URI: Support members shortlink redirection.

This commit redirects /members/me/* to /members/LOGGEDIN_USER_SLUG/*.
If root profiles is enabled, then this redirects /me/* to
/LOGGEDIN_USER_SLUG/*. If a user is logged out, /me/ redirects to the
login page and when properly authenticated, will redirect back to the
logged-in user's page.

This will allow devs to create dynamic links to a user's profile, which is
useful for all sorts of things -- nav menus, support links, etc.

Props r-a-y, dcavins.

Fixes #6325.

Location:
trunk
Files:
2 edited

Legend:

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

    r10714 r10791  
    534534    }
    535535}
     536
     537/**
     538 * Members user shortlink redirector.
     539 *
     540 * Redirects x.com/members/me/* to x.com/members/{LOGGED_IN_USER_SLUG}/*
     541 *
     542 * @since 2.6.0
     543 *
     544 * @param string $member_slug The current member slug.
     545 */
     546function bp_core_members_shortlink_redirector( $member_slug ) {
     547    /**
     548     * Shortlink slug to redirect to logged-in user.
     549     *
     550     * x.com/members/me/* will redirect to x.com/members/{LOGGED_IN_USER_SLUG}/*
     551     *
     552     * @since 2.6.0
     553     *
     554     * @var string $slug Defaults to 'me'.
     555     */
     556    $me_slug = apply_filters( 'bp_core_members_shortlink_slug', 'me' );
     557
     558    // Check if we're on our special shortlink slug. If not, bail.
     559    if ( $me_slug !== $member_slug ) {
     560        return $member_slug;
     561    }
     562
     563    // If logged out, redirect user to login.
     564    if ( false === is_user_logged_in() ) {
     565        // Add our login redirector hook.
     566        add_action( 'template_redirect', 'bp_core_no_access', 0 );
     567
     568        return $member_slug;
     569    }
     570
     571    $user = wp_get_current_user();
     572
     573    return bp_core_get_username( $user->ID, $user->user_nicename, $user->user_login );
     574}
     575add_filter( 'bp_core_set_uri_globals_member_slug', 'bp_core_members_shortlink_redirector' );
    536576
    537577/**
  • trunk/tests/phpunit/testcases/routing/members.php

    r9914 r10791  
    109109        $this->assertTrue( is_404() );
    110110    }
     111
     112    /**
     113     * @ticket BP6325
     114     */
     115    function test_members_shortlink_redirector() {
     116        $shortlink_member_slug = 'me';
     117
     118        $this->go_to( bp_get_members_directory_permalink() . $shortlink_member_slug );
     119
     120        $this->assertSame( get_current_user_id(), bp_displayed_user_id() );
     121    }
    111122}
Note: See TracChangeset for help on using the changeset viewer.