WordPress.org

Make WordPress Core

Ticket #12295: 0001-Users-Add-filter-to-hide-fields-from-user-page.patch

File 0001-Users-Add-filter-to-hide-fields-from-user-page.patch, 9.4 KB (added by jvcdk, 2 years ago)

Patch in git format

  • devops/www/wp-admin/includes/user.php

    From 33ecff33ea7d54717d872767e9d0eb03d2219960 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?J=C3=B8rn=20V=2E=20Christensen?= <dev@mettle.dk>
    Date: Sat, 15 Feb 2020 13:56:38 +0100
    Subject: [PATCH] Users: Add filter to hide fields from user page.
    
    This patch hides the following fields from the edit-user page and new-user page:
      * nickname
      * user_url
      * description
    
    It also prevents them from being updated. In case of nickname, it tries to keep
    the old value. If that is not set (should not happen) it defaults back to
    user_login (since it is a required field).
    ---
     devops/www/wp-admin/includes/user.php | 18 +++++++--
     devops/www/wp-admin/user-edit.php     | 57 +++++++++++++++++----------
     devops/www/wp-admin/user-new.php      |  3 ++
     devops/www/wp-includes/user.php       | 35 ++++++++++++++++
     4 files changed, 90 insertions(+), 23 deletions(-)
    
    diff --git a/devops/www/wp-admin/includes/user.php b/devops/www/wp-admin/includes/user.php
    index e4af054..1a9b6dc 100644
    a b function edit_user( $user_id = 0 ) { 
    7777                }
    7878        }
    7979
     80        $activeFields = wp_get_user_profile_active_fields();
    8081        if ( isset( $_POST['email'] ) ) {
    8182                $user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
    8283        }
    83         if ( isset( $_POST['url'] ) ) {
     84        if ( isset( $_POST['url'] ) && true === $activeFields['user_url'] ) {
    8485                if ( empty( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
    8586                        $user->user_url = '';
    8687                } else {
    function edit_user( $user_id = 0 ) { 
    9596        if ( isset( $_POST['last_name'] ) ) {
    9697                $user->last_name = sanitize_text_field( $_POST['last_name'] );
    9798        }
    98         if ( isset( $_POST['nickname'] ) ) {
     99
     100        if ( isset( $_POST['nickname'] ) && true === $activeFields['nickname'] ) {
    99101                $user->nickname = sanitize_text_field( $_POST['nickname'] );
    100102        }
     103        else {
     104                $nickname = "";
     105                if($user_id) {
     106                        $nickname = get_user_meta( $user_id, 'nickname', true );
     107                }
     108                if(empty($nickname)) {
     109                        $nickname = $user->user_login;
     110                }
     111                $user->nickname = $nickname;
     112        }
    101113        if ( isset( $_POST['display_name'] ) ) {
    102114                $user->display_name = sanitize_text_field( $_POST['display_name'] );
    103115        }
    104116
    105         if ( isset( $_POST['description'] ) ) {
     117        if ( isset( $_POST['description'] ) && true === $activeFields['description'] ) {
    106118                $user->description = trim( $_POST['description'] );
    107119        }
    108120
  • devops/www/wp-admin/user-edit.php

    diff --git a/devops/www/wp-admin/user-edit.php b/devops/www/wp-admin/user-edit.php
    index 10e39e8..e90b22d 100644
    a b switch ( $action ) { 
    185185                $sessions = WP_Session_Tokens::get_instance( $profileuser->ID );
    186186
    187187                include( ABSPATH . 'wp-admin/admin-header.php' );
     188
     189                $fieldsToShow = wp_get_user_profile_active_fields();
    188190                ?>
    189191
    190192                <?php if ( ! IS_PROFILE_PAGE && is_super_admin( $profileuser->ID ) && current_user_can( 'manage_network_options' ) ) { ?>
    endif; //!IS_PROFILE_PAGE 
    451453        <td><input type="text" name="last_name" id="last_name" value="<?php echo esc_attr( $profileuser->last_name ); ?>" class="regular-text" /></td>
    452454</tr>
    453455
    454 <tr class="user-nickname-wrap">
    455         <th><label for="nickname"><?php _e( 'Nickname' ); ?> <span class="description"><?php _e( '(required)' ); ?></span></label></th>
    456         <td><input type="text" name="nickname" id="nickname" value="<?php echo esc_attr( $profileuser->nickname ); ?>" class="regular-text" /></td>
    457 </tr>
    458 
     456<?php
     457if( true === $fieldsToShow['nickname'] ) {
     458?>
     459        <tr class="user-nickname-wrap">
     460                <th><label for="nickname"><?php _e( 'Nickname' ); ?> <span class="description"><?php _e( '(required)' ); ?></span></label></th>
     461                <td><input type="text" name="nickname" id="nickname" value="<?php echo esc_attr( $profileuser->nickname ); ?>" class="regular-text" /></td>
     462        </tr>
     463<?php
     464}
     465?>
    459466<tr class="user-display-name-wrap">
    460467        <th><label for="display_name"><?php _e( 'Display name publicly as' ); ?></label></th>
    461468        <td>
    462469                <select name="display_name" id="display_name">
    463470                <?php
    464471                        $public_display                     = array();
    465                         $public_display['display_nickname'] = $profileuser->nickname;
     472                        if ( true === $fieldsToShow['nickname'] ) {
     473                                $public_display['display_nickname'] = $profileuser->nickname;
     474                        }
    466475                        $public_display['display_username'] = $profileuser->user_login;
    467476
    468477                if ( ! empty( $profileuser->first_name ) ) {
    endif; //!IS_PROFILE_PAGE 
    534543        </td>
    535544        </tr>
    536545
    537         <tr class="user-url-wrap">
    538         <th><label for="url"><?php _e( 'Website' ); ?></label></th>
    539         <td><input type="url" name="url" id="url" value="<?php echo esc_attr( $profileuser->user_url ); ?>" class="regular-text code" /></td>
    540         </tr>
    541 
    542                 <?php
     546        <?php
     547        if( true === $fieldsToShow['user_url'] ) {
     548        ?>
     549                <tr class="user-url-wrap">
     550                <th><label for="url"><?php _e( 'Website' ); ?></label></th>
     551                <td><input type="url" name="url" id="url" value="<?php echo esc_attr( $profileuser->user_url ); ?>" class="regular-text code" /></td>
     552                </tr>
     553        <?php
     554        }
    543555                foreach ( wp_get_user_contact_methods( $profileuser ) as $name => $desc ) {
    544556                        ?>
    545557        <tr class="user-<?php echo $name; ?>-wrap">
    endif; //!IS_PROFILE_PAGE 
    565577                ?>
    566578        </table>
    567579
    568         <h2><?php IS_PROFILE_PAGE ? _e( 'About Yourself' ) : _e( 'About the user' ); ?></h2>
     580        <?php
     581        if(true === $fieldsToShow['description']) {
     582        ?>
     583                <h2><?php IS_PROFILE_PAGE ? _e( 'About Yourself' ) : _e( 'About the user' ); ?></h2>
    569584
    570 <table class="form-table" role="presentation">
    571 <tr class="user-description-wrap">
    572         <th><label for="description"><?php _e( 'Biographical Info' ); ?></label></th>
    573         <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
    574         <p class="description"><?php _e( 'Share a little biographical information to fill out your profile. This may be shown publicly.' ); ?></p></td>
    575 </tr>
     585                <table class="form-table" role="presentation">
     586                <tr class="user-description-wrap">
     587                        <th><label for="description"><?php _e( 'Biographical Info' ); ?></label></th>
     588                        <td><textarea name="description" id="description" rows="5" cols="30"><?php echo $profileuser->description; // textarea_escaped ?></textarea>
     589                        <p class="description"><?php _e( 'Share a little biographical information to fill out your profile. This may be shown publicly.' ); ?></p></td>
     590                </tr>
    576591
    577                 <?php if ( get_option( 'show_avatars' ) ) : ?>
     592        <?php
     593        }
     594                if ( get_option( 'show_avatars' ) ) : ?>
    578595<tr class="user-profile-picture">
    579596        <th><?php _e( 'Profile Picture' ); ?></th>
    580597        <td>
  • devops/www/wp-admin/user-new.php

    diff --git a/devops/www/wp-admin/user-new.php b/devops/www/wp-admin/user-new.php
    index d8c76e6..e5fe54b 100644
    a b if ( current_user_can( 'create_users' ) ) { 
    475475        $new_user_send_notification = $creating && ! isset( $_POST['send_user_notification'] ) ? false : true;
    476476        $new_user_ignore_pass       = $creating && isset( $_POST['noconfirmation'] ) ? wp_unslash( $_POST['noconfirmation'] ) : '';
    477477
     478        $fieldsToShow = wp_get_user_profile_active_fields();
    478479        ?>
    479480<table class="form-table" role="presentation">
    480481        <tr class="form-field form-required">
    if ( current_user_can( 'create_users' ) ) { 
    494495                <th scope="row"><label for="last_name"><?php _e( 'Last Name' ); ?> </label></th>
    495496                <td><input name="last_name" type="text" id="last_name" value="<?php echo esc_attr( $new_user_lastname ); ?>" /></td>
    496497        </tr>
     498        <?php if ( $true === $fieldsToShow['user_url'] ) { ?>
    497499        <tr class="form-field">
    498500                <th scope="row"><label for="url"><?php _e( 'Website' ); ?></label></th>
    499501                <td><input name="url" type="url" id="url" class="code" value="<?php echo esc_attr( $new_user_uri ); ?>" /></td>
    500502        </tr>
     503        <?php } ?>
    501504        <tr class="form-field form-required user-pass1-wrap">
    502505                <th scope="row">
    503506                        <label for="pass1">
  • devops/www/wp-includes/user.php

    diff --git a/devops/www/wp-includes/user.php b/devops/www/wp-includes/user.php
    index 505048e..fb40bfd 100644
    a b function get_current_user_id() { 
    444444        return ( isset( $user->ID ) ? (int) $user->ID : 0 );
    445445}
    446446
     447/**
     448 * Get an array of profile fields are active (shown and can be updated).
     449 *
     450 * @internal Fires the filter user_profile_active_fields.
     451 *
     452 * @since TDB
     453 *
     454 * @return array Input array with hidden elements set to false.
     455 */
     456function wp_get_user_profile_active_fields() {
     457        $keys = array('nickname', 'user_url', 'description');
     458        $fieldsToShow = array_fill_keys($keys, true);
     459
     460        /**
     461         * Filter on what user profile fields are active (shown and can be updated).
     462         *
     463         * List of available keys: 'nickname', 'user_url', 'description'.
     464         *
     465         * @since TBD
     466         *
     467         * @param array Array key => true of default elements shown.
     468         * @return array Input array with hidden elements set to false.
     469         */
     470        $fieldsToShowFiltered = apply_filters('user_profile_active_fields', $fieldsToShow);
     471        return array_merge($fieldsToShow, $fieldsToShowFiltered);
     472}
     473
    447474/**
    448475 * Retrieve user option that can be either per Site or per Network.
    449476 *
    function wp_update_user( $userdata ) { 
    19782005                $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
    19792006        }
    19802007
     2008        // Skip fields marked as non-active
     2009        $activeFields = wp_get_user_profile_active_fields();
     2010        foreach ( wp_get_user_profile_active_fields() as $key => $value ) {
     2011                if( true !== $value ) {
     2012                        unset($userdata[$key]);
     2013                }
     2014        }
     2015
    19812016        clean_user_cache( $user_obj );
    19822017
    19832018        // Merge old and new fields with new fields overwriting old ones.