Skip to:
Content

BuddyPress.org

Ticket #6325: 6325.03.patch

File 6325.03.patch, 3.5 KB (added by r-a-y, 3 years ago)
  • 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        // Convert to utf-8 for localization.
     559        if ( true === function_exists( 'iconv_get_encoding' ) ) {
     560                $me_slug = iconv( iconv_get_encoding( 'internal_encoding' ), "UTF-8//TRANSLIT", $me_slug );
     561
     562        // This might not be accurate...
     563        } else {
     564                $me_slug = htmlentities( $me_slug, ENT_COMPAT | ENT_HTML401 );
     565        }
     566
     567        // Ensure our custom slug is URL-encoded.
     568        $me_slug = html_entity_decode( $me_slug, ENT_COMPAT | ENT_HTML401, 'utf-8' );
     569        $me_slug = rawurlencode( $me_slug );
     570
     571        // Check if we're on our special shortlink slug. If not, bail.
     572        if ( $me_slug !== $member_slug ) {
     573                return $member_slug;
     574        }
     575
     576        // If logged out, redirect user to login.
     577        if ( false === is_user_logged_in() ) {
     578                // Add our login redirector hook.
     579                add_action( 'template_redirect', 'bp_core_no_access', 0 );
     580
     581                return $member_slug;
     582        }
     583
     584        $user = wp_get_current_user();
     585
     586        return bp_core_get_username( $user->ID, $user->user_nicename, $user->user_login );
     587}
     588add_filter( 'bp_core_set_uri_globals_member_slug', 'bp_core_members_shortlink_redirector' );
     589
     590/**
    538591 * Catch unauthorized access to certain BuddyPress pages and redirect accordingly.
    539592 *
    540593 * @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_custom_member_slug_with_accents_in_shortlink_redirector() {
     116                // Emulate our custom slug as seen through bp_core_set_uri_globals().
     117                $custom_member_slug = 'regard%C3%A9';
     118
     119                add_filter( 'bp_core_members_shortlink_slug', array( $this, 'custom_member_slug_with_accents' ) );
     120
     121                $this->go_to( bp_get_members_directory_permalink() . $custom_member_slug );
     122
     123                remove_filter( 'bp_core_members_shortlink_slug', array( $this, 'custom_member_slug_with_accents' ) );
     124
     125                $this->assertSame( get_current_user_id(), bp_displayed_user_id() );
     126        }
     127
     128        /**
     129         * @ticket BP6325
     130         */
     131        function test_custom_member_slug_with_entity_in_shortlink_redirector() {
     132                // Emulate our custom slug as seen through bp_core_set_uri_globals().
     133                $custom_member_slug = 'regard%C3%A9';
     134
     135                add_filter( 'bp_core_members_shortlink_slug', array( $this, 'custom_member_slug_with_entities' ) );
     136
     137                $this->go_to( bp_get_members_directory_permalink() . $custom_member_slug );
     138
     139                remove_filter( 'bp_core_members_shortlink_slug', array( $this, 'custom_member_slug_with_entities' ) );
     140
     141                $this->assertSame( get_current_user_id(), bp_displayed_user_id() );
     142        }
     143
     144        function custom_member_slug_with_entities() {
     145                return 'regardé';
     146        }
     147
     148        function custom_member_slug_with_accents() {
     149                return 'regardé';
     150        }
    111151}