WordPress.org

Making WordPress.org

Changeset 10585


Ignore:
Timestamp:
01/15/2021 05:55:03 AM (4 months ago)
Author:
dd32
Message:

User Tweaks: Use the user nicename for blocked users, rather than the contents of the display_name field which may be set to something not display friendly.

See #5578.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/wporg-user-tweaks.php

    r10238 r10585  
    11<?php
     2namespace WordPressdotorg\User_Tweaks;
     3use WP_User;
     4use add_filter;
     5
    26/**
    37 * Plugin Name: W.org User Tweaks
     
    59
    610// Some users have an empty display_name field, which shouldn't happen but does due to sanitization.
    7 add_filter( 'user_display_name', function( $name, $user_id ) {
     11add_filter( 'user_display_name', function( $name, $user_id, $context ) {
     12    if ( 'edit' === $context ) {
     13        return $name;
     14    }
     15
    816    if ( '' === $name ) {
    917        $name = get_user_by( 'id', $user_id )->user_nicename;
    1018    }
    1119
     20    $name = maybe_replace_blocked_user_name( $name, $user_id );
     21
    1222    return $name;
    13 }, 1, 2 );
     23}, 1, 3 );
    1424
    1525// bbPress skips user filtering and does it's own
    16 add_filter( 'bbp_get_displayed_user_field', function( $value, $field ) {
    17     if ( 'display_name' === $field && '' === $value ) {
    18         $value = bbpress()->displayed_user->user_nicename;
     26add_filter( 'bbp_get_displayed_user_field', function( $value, $field, $filter ) {
     27    if ( 'edit' === $filter ) {
     28        return $value;
     29    }
     30
     31    if ( 'display_name' === $field ) {
     32        if ( '' === $value ) {
     33            $value = bbpress()->displayed_user->user_nicename;
     34        }
     35
     36        $value = maybe_replace_blocked_user_name( $value, bbpress()->displayed_user );
    1937    }
    2038
    2139    return $value;
    22 }, 1, 2 );
     40}, 1, 3 );
    2341
    2442/**
     
    3452    }
    3553
     54    // Intentionally not used, as this will override in edit contexts too.
     55    //$wp_user->display_name = maybe_replace_blocked_user_name( $wp_user->display_name, $wp_user );
     56
    3657    return $allcaps;
    3758}, 1, 4 );
     59
     60/**
     61 * Use the nicename field for blocked users.
     62 */
     63function maybe_replace_blocked_user_name( $name, $user ) {
     64    if ( ! defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) ) {
     65        return $name;
     66    }
     67
     68    $user_id = is_object( $user ) ? $user->ID : $user;
     69    if ( ! $user_id ) {
     70        return $name;
     71    }
     72
     73    if ( ! ( $user instanceof WP_User ) ) {
     74        $user = get_user_by( 'id', $user );
     75    }
     76
     77    if ( ! $user ) {
     78        return $name;
     79    }
     80
     81    $user->for_site( WPORG_SUPPORT_FORUMS_BLOGID );
     82
     83    /* Cannot use $user->has_cap( 'bbp_blocked' ), as we may be within a capability filter. */
     84    if ( !empty( $user->roles ) && in_array( 'bbp_blocked', $user->roles ) ) {
     85        return $user->user_nicename;
     86    }
     87
     88    return $name;
     89}
Note: See TracChangeset for help on using the changeset viewer.