Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/07/2023 04:28:08 AM (14 months ago)
Author:
imath
Message:

BP Rewrites: Introduce the bp_members_get_user_url() function

As many member URLs are built concatenating bp_core_get_user_domain()
with URL chunks, the safer way to make sure developers update the way they
build their member URLs in favor of using BP Rewrites is:

  1. to deprecate this function
  2. create a new function bp_members_get_user_url() which is a wrapper of

bp_rewrites_get_url()

  1. replace all bp_core_get_user_domain() occurrences by

bp_members_get_user_url()

This commit also deprecates bp_core_get_username() in favor of the new
bp_members_get_user_slug() function and updates PHPUnit tests.

Props r-a-y, johnjamesjacoby, boonebgorges

Closes https://github.com/buddypress/buddypress/pull/70
See #4954

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-functions.php

    r13432 r13433  
    140140
    141141/**
    142  * Return the domain for the passed user: e.g. http://example.com/members/andy/.
    143  *
    144  * @since 1.0.0
    145  *
    146  * @param int         $user_id       The ID of the user.
    147  * @param string|bool $user_nicename Optional. user_nicename of the user.
    148  * @param string|bool $user_login    Optional. user_login of the user.
    149  * @return string
    150  */
    151 function bp_core_get_user_domain( $user_id = 0, $user_nicename = false, $user_login = false ) {
    152 
    153     if ( empty( $user_id ) ) {
    154         return;
    155     }
    156 
    157     $username = bp_core_get_username( $user_id, $user_nicename, $user_login );
    158 
    159     if ( bp_is_username_compatibility_mode() ) {
    160         $username = rawurlencode( $username );
    161     }
    162 
    163     $after_domain = bp_core_enable_root_profiles() ? $username : bp_get_members_root_slug() . '/' . $username;
    164     $domain       = trailingslashit( bp_get_root_domain() . '/' . $after_domain );
    165 
    166     // Don't use this filter.  Subject to removal in a future release.
    167     // Use the 'bp_core_get_user_domain' filter instead.
    168     $domain = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login );
     142 * Return the Mmbers single item's URL.
     143 *
     144 * @since 12.0.0
     145 *
     146 * @param integer $user_id  The user ID.
     147 * @param array   $action {
     148 *     An array of arguments. Optional.
     149 *
     150 *     @type string $single_item_component        The component slug the action is relative to.
     151 *     @type string $single_item_action           The slug of the action to perform.
     152 *     @type array  $single_item_action_variables An array of additional informations about the action to perform.
     153 * }
     154 * @return string The URL built for the BP Rewrites URL parser.
     155 */
     156function bp_members_get_user_url( $user_id = 0, $path_chunks = array() ) {
     157    $url  = '';
     158    $slug = bp_members_get_user_slug( $user_id );
     159
     160    if ( $slug ) {
     161        if ( bp_is_username_compatibility_mode() ) {
     162            $slug = rawurlencode( $slug );
     163        }
     164
     165        $supported_chunks = array_fill_keys( array( 'single_item_component', 'single_item_action', 'single_item_action_variables' ), true );
     166        $path_chunks      = bp_parse_args(
     167            array_intersect_key( $path_chunks, $supported_chunks ),
     168            array(
     169                'component_id' => 'members',
     170                'single_item'  => $slug,
     171            )
     172        );
     173
     174        $url = bp_rewrites_get_url( $path_chunks );
     175    }
    169176
    170177    /**
     
    172179     *
    173180     * @since 1.0.1
     181     * @deprecated 12.0.0
    174182     *
    175183     * @param string $domain        Domain for the passed user.
     
    178186     * @param string $user_login    User login of the passed user.
    179187     */
    180     return apply_filters( 'bp_core_get_user_domain', $domain, $user_id, $user_nicename, $user_login );
     188    $url = apply_filters_deprecated( 'bp_core_get_user_domain', array( $url, $user_id, false, false ), '12.0.0', 'bp_members_get_user_url' );
     189
     190    /**
     191     * Filters the domain for the passed user.
     192     *
     193     * @since 12.0.0
     194     *
     195     * @param string  $url      The user url.
     196     * @param integer $user_id  The user ID.
     197     * @param string  $slug     The user slug.
     198     * @param array   $path_chunks {
     199     *     An array of arguments. Optional.
     200     *
     201     *     @type string $single_item_component        The component slug the action is relative to.
     202     *     @type string $single_item_action           The slug of the action to perform.
     203     *     @type array  $single_item_action_variables An array of additional informations about the action to perform.
     204     * }
     205     */
     206    return apply_filters( 'bp_members_get_user_url', $url, $user_id, $slug, $path_chunks );
    181207}
    182208
     
    274300
    275301/**
    276  * Return the username for a user based on their user id.
    277  *
    278  * This function is sensitive to the BP_ENABLE_USERNAME_COMPATIBILITY_MODE,
    279  * so it will return the user_login or user_nicename as appropriate.
    280  *
    281  * @since 1.0.0
    282  *
    283  * @param int         $user_id       User ID to check.
    284  * @param string|bool $user_nicename Optional. user_nicename of user being checked.
    285  * @param string|bool $user_login    Optional. user_login of user being checked.
    286  * @return string The username of the matched user or an empty string if no user is found.
    287  */
    288 function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login = false ) {
    289 
    290     if ( ! $user_nicename && ! $user_login ) {
    291         // Pull an audible and maybe use the login over the nicename.
    292         if ( bp_is_username_compatibility_mode() ) {
    293             $username = get_the_author_meta( 'login', $user_id );
    294         } else {
    295             $username = get_the_author_meta( 'nicename', $user_id );
    296         }
     302 * Returns the members single item (member) slug.
     303 *
     304 * @since 12.0.0
     305 *
     306 * @param integer $user_id The User ID.
     307 * @return string The member slug.
     308 */
     309function bp_members_get_user_slug( $user_id = 0 ) {
     310    $bp  = buddypress();
     311    $lug = '';
     312
     313    $prop = 'user_nicename';
     314    if ( bp_is_username_compatibility_mode() ) {
     315        $prop = 'user_login';
     316    }
     317
     318    if ( (int) bp_loggedin_user_id() === (int) $user_id ) {
     319        $slug = isset( $bp->loggedin_user->userdata->{$prop} ) ? $bp->loggedin_user->userdata->{$prop} : null;
     320    } elseif ( (int) bp_displayed_user_id() === (int) $user_id ) {
     321        $slug = isset( $bp->displayed_user->userdata->{$prop} ) ? $bp->displayed_user->userdata->{$prop} : null;
    297322    } else {
    298         $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename;
     323        $user = get_user_by( 'id', $user_id );
     324
     325        if ( $user instanceof WP_User ) {
     326            $slug = $user->{$prop};
     327        }
    299328    }
    300329
     
    303332     *
    304333     * @since 1.0.1
    305      *
    306      * @param string $username Username determined by user ID.
    307      */
    308     return apply_filters( 'bp_core_get_username', $username );
     334     * @deprecated 12.0.0
     335     *
     336     * @param string $slug Username determined by user ID.
     337     */
     338    $slug = apply_filters_deprecated( 'bp_core_get_username', array( $slug ), '12.0.0', 'bp_members_get_user_slug' );
     339
     340    /**
     341     * Filter here to edit the user's slug.
     342     *
     343     * @since 12.0.0
     344     *
     345     * @param string $slug     The user's slug.
     346     * @param integer $user_id The user ID.
     347     */
     348    return apply_filters( 'bp_members_get_user_slug', $slug, $user_id );
    309349}
    310350
     
    381421    }
    382422
    383     if ( !$url = bp_core_get_user_domain( $user_id ) ) {
     423    if ( !$url = bp_members_get_user_url( $user_id ) ) {
    384424        return false;
    385425    }
     
    33163356    }
    33173357
    3318     $profile_url = bp_core_get_user_domain( $user_id );
     3358    $profile_url = bp_members_get_user_url( $user_id );
    33193359
    33203360    /**
Note: See TracChangeset for help on using the changeset viewer.