Skip to:
Content

BuddyPress.org

Ticket #6325: 6325.01.patch

File 6325.01.patch, 2.0 KB (added by r-a-y, 9 years ago)

Refreshed with 'template_redirect' fix from comment:23 and includes basic unit test.

  • src/bp-core/bp-core-catchuri.php

     
    535535}
    536536
    537537/**
     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' );
     576
     577/**
    538578 * Catch unauthorized access to certain BuddyPress pages and redirect accordingly.
    539579 *
    540580 * @since 1.5.0
  • tests/phpunit/testcases/routing/members.php

     
    108108                $this->go_to( bp_get_members_directory_permalink() . 'type/foo/' );
    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}