Skip to:
Content

bbPress.org

Changeset 5815


Ignore:
Timestamp:
07/13/2015 01:13:46 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Users: Use bbp_get_user_profile_url() to avoid code duplication.

This commit removes duplicated code used to build user profile URLs & eliminates several superfluous string replacements. It works with pretty & unpretty permalinks, and plays nicely with the BuddyPress extension for linking to a member's forum sections.

Fixes #2831.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/template.php

    r5770 r5815  
    354354
    355355        // Allow early overriding of the profile URL to cut down on processing
    356         $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', (int) $user_id );
     356        $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', $user_id );
    357357        if ( is_string( $early_profile_url ) ) {
    358358            return $early_profile_url;
     
    361361        // Pretty permalinks
    362362        if ( $wp_rewrite->using_permalinks() ) {
    363             $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%';
    364363
    365364            // Get username if not passed
     
    368367            }
    369368
    370             $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
    371             $url = home_url( user_trailingslashit( $url ) );
     369            $url = trailingslashit( $wp_rewrite->root . bbp_get_user_slug() ) . $user_nicename;
     370            $url = user_trailingslashit( $url );
     371            $url = home_url( $url );
    372372
    373373        // Unpretty permalinks
    374374        } else {
    375             $url = add_query_arg( array( bbp_get_user_rewrite_id() => $user_id ), home_url( '/' ) );
     375            $url = add_query_arg( array(
     376                bbp_get_user_rewrite_id() => $user_id
     377            ), home_url( '/' ) );
    376378        }
    377379
     
    436438     * @param string $user_nicename Optional. User nicename
    437439     * @uses bbp_get_user_id() To get user id
     440     * @uses bbp_get_user_profile_url() To get the user profile url
    438441     * @uses WP_Rewrite::using_permalinks() To check if the blog is using
    439442     *                                       permalinks
     
    447450        global $wp_rewrite;
    448451
    449         $bbp     = bbpress();
    450452        $user_id = bbp_get_user_id( $user_id );
    451453        if ( empty( $user_id ) ) {
     
    453455        }
    454456
     457        // Allow early overriding of the profile edit URL to cut down on processing
     458        $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_edit_url', $user_id );
     459        if ( is_string( $early_profile_url ) ) {
     460            return $early_profile_url;
     461        }
     462
     463        // Get user profile URL
     464        $profile_url = bbp_get_user_profile_url( $user_id, $user_nicename );
     465
    455466        // Pretty permalinks
    456467        if ( $wp_rewrite->using_permalinks() ) {
    457             $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . $bbp->user_id . '%/' . $bbp->edit_id;
    458 
    459             // Get username if not passed
    460             if ( empty( $user_nicename ) ) {
    461                 $user = get_userdata( $user_id );
    462                 if ( ! empty( $user->user_nicename ) ) {
    463                     $user_nicename = $user->user_nicename;
    464                 }
    465             }
    466 
    467             $url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url );
    468             $url = home_url( user_trailingslashit( $url ) );
     468            $url = trailingslashit( $profile_url ) . 'edit';
     469            $url = user_trailingslashit( $url );
     470            $url = home_url( $url );
    469471
    470472        // Unpretty permalinks
    471473        } else {
    472             $url = add_query_arg( array( $bbp->user_id => $user_id, $bbp->edit_id => '1' ), home_url( '/' ) );
     474            $url = add_query_arg( array(
     475                bbp_get_edit_rewrite_id() => '1'
     476            ), $profile_url );
    473477        }
    474478
     
    825829
    826830        // Allow early overriding of the profile URL to cut down on processing
    827         $early_profile_url = apply_filters( 'bbp_pre_get_favorites_permalink', (int) $user_id );
     831        $early_profile_url = apply_filters( 'bbp_pre_get_favorites_permalink', $user_id );
    828832        if ( is_string( $early_profile_url ) ) {
    829833            return $early_profile_url;
    830834        }
    831835
     836        // Get user profile URL
     837        $profile_url = bbp_get_user_profile_url( $user_id );
     838
    832839        // Pretty permalinks
    833840        if ( $wp_rewrite->using_permalinks() ) {
    834             $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/%' . bbp_get_user_favorites_rewrite_id() . '%';
    835             $user = get_userdata( $user_id );
    836             if ( ! empty( $user->user_nicename ) ) {
    837                 $user_nicename = $user->user_nicename;
    838             } else {
    839                 $user_nicename = $user->user_login;
    840             }
    841             $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
    842             $url = str_replace( '%' . bbp_get_user_favorites_rewrite_id() . '%', bbp_get_user_favorites_slug(), $url );
    843             $url = home_url( user_trailingslashit( $url ) );
     841            $url = trailingslashit( $profile_url ) . bbp_get_user_favorites_rewrite_id();
     842            $url = user_trailingslashit( $url );
     843            $url = home_url( $url );
    844844
    845845        // Unpretty permalinks
    846846        } else {
    847847            $url = add_query_arg( array(
    848                 bbp_get_user_rewrite_id()           => $user_id,
    849848                bbp_get_user_favorites_rewrite_id() => bbp_get_user_favorites_slug(),
    850             ), home_url( '/' ) );
     849            ), $profile_url );
    851850        }
    852851
     
    919918
    920919        // No link if you can't edit yourself
    921         if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
     920        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    922921            return false;
    923922        }
     
    990989
    991990        // Allow early overriding of the profile URL to cut down on processing
    992         $early_profile_url = apply_filters( 'bbp_pre_get_subscriptions_permalink', (int) $user_id );
     991        $early_profile_url = apply_filters( 'bbp_pre_get_subscriptions_permalink', $user_id );
    993992        if ( is_string( $early_profile_url ) ) {
    994993            return $early_profile_url;
    995994        }
    996995
     996        // Get user profile URL
     997        $profile_url = bbp_get_user_profile_url( $user_id );
     998
    997999        // Pretty permalinks
    9981000        if ( $wp_rewrite->using_permalinks() ) {
    999             $url  = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/%' . bbp_get_user_subscriptions_rewrite_id() . '%';
    1000             $user = get_userdata( $user_id );
    1001             if ( ! empty( $user->user_nicename ) ) {
    1002                 $user_nicename = $user->user_nicename;
    1003             } else {
    1004                 $user_nicename = $user->user_login;
    1005             }
    1006             $url = str_replace( '%' . bbp_get_user_rewrite_id()               . '%', $user_nicename,                    $url );
    1007             $url = str_replace( '%' . bbp_get_user_subscriptions_rewrite_id() . '%', bbp_get_user_subscriptions_slug(), $url );
    1008             $url = home_url( user_trailingslashit( $url ) );
     1001            $url = trailingslashit( $profile_url ) . bbp_get_user_subscriptions_slug();
     1002            $url = user_trailingslashit( $url );
     1003            $url = home_url( $url );
    10091004
    10101005        // Unpretty permalinks
    10111006        } else {
    10121007            $url = add_query_arg( array(
    1013                 bbp_get_user_rewrite_id()           => $user_id,
    10141008                bbp_get_user_subscriptions_rewrite_id() => bbp_get_user_subscriptions_slug(),
    1015             ), home_url( '/' ) );
     1009            ), $profile_url );
    10161010        }
    10171011
     
    10871081
    10881082        // No link if you can't edit yourself
    1089         if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
     1083        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    10901084            return false;
    10911085        }
     
    14361430
    14371431        // Allow early overriding of the profile URL to cut down on processing
    1438         $early_url = apply_filters( 'bbp_pre_get_user_topics_created_url', (int) $user_id );
     1432        $early_url = apply_filters( 'bbp_pre_get_user_topics_created_url', $user_id );
    14391433        if ( is_string( $early_url ) ) {
    14401434            return $early_url;
    14411435        }
    14421436
     1437        // Get user profile URL
     1438        $profile_url = bbp_get_user_profile_url( $user_id );
     1439
    14431440        // Pretty permalinks
    14441441        if ( $wp_rewrite->using_permalinks() ) {
    1445             $url  = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/' . bbp_get_topic_archive_slug();
    1446             $user = get_userdata( $user_id );
    1447             if ( ! empty( $user->user_nicename ) ) {
    1448                 $user_nicename = $user->user_nicename;
    1449             } else {
    1450                 $user_nicename = $user->user_login;
    1451             }
    1452             $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
    1453             $url = home_url( user_trailingslashit( $url ) );
     1442            $url = trailingslashit( $profile_url ) . bbp_get_topic_archive_slug();
     1443            $url = user_trailingslashit( $url );
     1444            $url = home_url( $url );
    14541445
    14551446        // Unpretty permalinks
    14561447        } else {
    14571448            $url = add_query_arg( array(
    1458                 bbp_get_user_rewrite_id()        => $user_id,
    14591449                bbp_get_user_topics_rewrite_id() => '1',
    1460             ), home_url( '/' ) );
     1450            ), $profile_url );
    14611451        }
    14621452
     
    14981488
    14991489        // Allow early overriding of the profile URL to cut down on processing
    1500         $early_url = apply_filters( 'bbp_pre_get_user_replies_created_url', (int) $user_id );
     1490        $early_url = apply_filters( 'bbp_pre_get_user_replies_created_url', $user_id );
    15011491        if ( is_string( $early_url ) ) {
    15021492            return $early_url;
    15031493        }
    15041494
     1495        // Get user profile URL
     1496        $profile_url = bbp_get_user_profile_url( $user_id );
     1497
    15051498        // Pretty permalinks
    15061499        if ( $wp_rewrite->using_permalinks() ) {
    1507             $url  = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/' . bbp_get_reply_archive_slug();
    1508             $user = get_userdata( $user_id );
    1509             if ( ! empty( $user->user_nicename ) ) {
    1510                 $user_nicename = $user->user_nicename;
    1511             } else {
    1512                 $user_nicename = $user->user_login;
    1513             }
    1514             $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
    1515             $url = home_url( user_trailingslashit( $url ) );
     1500            $url = trailingslashit( $profile_url ) . bbp_get_reply_archive_slug();
     1501            $url = user_trailingslashit( $url );
     1502            $url = home_url( $url );
    15161503
    15171504        // Unpretty permalinks
    15181505        } else {
    15191506            $url = add_query_arg( array(
    1520                 bbp_get_user_rewrite_id()         => $user_id,
    15211507                bbp_get_user_replies_rewrite_id() => '1',
    1522             ), home_url( '/' ) );
     1508            ), $profile_url );
    15231509        }
    15241510
Note: See TracChangeset for help on using the changeset viewer.