Make WordPress Core

Ticket #42900: gender-to-user-profile.diff

File gender-to-user-profile.diff, 5.6 KB (added by yoavf, 7 years ago)

User profile gender field, and get_user_gender()

  • src/wp-admin/includes/user.php

    diff --git a/src/wp-admin/includes/user.php b/src/wp-admin/includes/user.php
    index 269d64fb26..f3efd1daf1 100644
    a b function edit_user( $user_id = 0 ) { 
    9696                $user->description = trim( $_POST['description'] );
    9797        }
    9898
     99        if ( isset( $_POST['gender'] ) ) {
     100                $user->gender = in_array( $_POST['gender'], array( 'unknown', 'female', 'male' ), true ) ? $_POST['gender'] : 'unknown';
     101        }
     102
    99103        foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) {
    100104                if ( isset( $_POST[ $method ] ) ) {
    101105                        $user->$method = sanitize_text_field( $_POST[ $method ] );
  • src/wp-admin/user-edit.php

    diff --git a/src/wp-admin/user-edit.php b/src/wp-admin/user-edit.php
    index 7e443c42c3..2b50ae815d 100644
    a b if ( is_multisite() && is_network_admin() && ! IS_PROFILE_PAGE && current_user_c 
    638638</tr>
    639639<?php endif; ?>
    640640
     641<tr class="user-gender-wrap">
     642    <th><label for="gender"><?php _e( 'What pronoun do you prefer?' ); ?></label></th>
     643    <td>
     644        <p>
     645            <?php
     646                $gender = get_user_meta( $user_id, 'gender', true );
     647                if ( ! $gender ) {
     648                    $gender = 'unknown';
     649                }
     650            ?>
     651            <label><input name="gender" type="radio" value="female" <?php checked( 'female', $gender ); ?> /> <?php _e( 'Female: She published a new post.' ); ?></label><br />
     652            <label><input name="gender" type="radio" value="male" <?php checked( 'male', $gender ); ?> /> <?php _e( 'Male: He published a new post.' ); ?></label><br />
     653            <label><input name="gender" type="radio" value="unknown" <?php checked( 'unknown', $gender ); ?> /> <?php _e( 'Other/Unknown: They published a new post.' ); ?></label>
     654        </p>
     655        <p class="description">
     656            <?php _e( 'This preference will allow translations of WordPress to address you and to mention you to others using the appropriate grammatical gender.' ); ?><br />
     657            <?php _e( 'If "Other/Unknown" is chosen, WordPress will use gender-neutral words whenever possible.' ); ?>
     658        </p>
     659    </td>
     660</tr>
     661
    641662<?php
    642663if ( IS_PROFILE_PAGE && count( $sessions->get_all() ) === 1 ) :
    643664?>
  • src/wp-includes/class-wp-user.php

    diff --git a/src/wp-includes/class-wp-user.php b/src/wp-includes/class-wp-user.php
    index f67780f1f4..4abbad63b8 100644
    a b  
    3232 * @property string $spam
    3333 * @property string $deleted
    3434 * @property string $locale
     35 * @property string $gender
    3536 * @property string $rich_editing
    3637 * @property string $syntax_highlighting
    3738 */
  • src/wp-includes/l10n.php

    diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
    index 5f4ba59ec3..22c271ca37 100644
    a b function get_user_locale( $user_id = 0 ) { 
    104104        return $locale ? $locale : get_locale();
    105105}
    106106
     107/**
     108 * Retrieves the gender of a user for use in translations.
     109 *
     110 * If the user has a gender set to a valid non-empty string then it will be
     111 * returned. Otherwise it returns 'unknown'.
     112 *
     113 * @since 5.0.0
     114 *
     115 * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.
     116 * @return string The gender of the user.
     117 */
     118function get_user_gender( $user_id = 0 ) {
     119        $user = false;
     120        if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
     121                $user = wp_get_current_user();
     122        } elseif ( $user_id instanceof WP_User ) {
     123                $user = $user_id;
     124        } elseif ( $user_id && is_numeric( $user_id ) ) {
     125                $user = get_user_by( 'id', $user_id );
     126        }
     127
     128        if ( ! $user ) {
     129                return 'unknown';
     130        }
     131
     132        $gender = $user->gender;
     133        return $gender ? $gender : 'unknown';
     134}
     135
    107136/**
    108137 * Retrieve the translation of $text.
    109138 *
  • src/wp-includes/user.php

    diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
    index 9a685b9b36..aad6026826 100644
    a b function validate_username( $username ) { 
    14691469 *                                             to build the second part of the user's display name
    14701470 *                                             if `$display_name` is not specified.
    14711471 *     @type string      $description          The user's biographical description.
     1472 *     @type string      $gender               The user's chosen gender.
    14721473 *     @type string|bool $rich_editing         Whether to enable the rich-editor for the user.
    14731474 *                                             False if not empty.
    14741475 *     @type string|bool $syntax_highlighting  Whether to enable the rich code editor for the user.
    function wp_insert_user( $userdata ) { 
    16841685         */
    16851686        $meta['description'] = apply_filters( 'pre_user_description', $description );
    16861687
     1688        $meta['gender'] = empty( $userdata['gender'] ) ? 'unknown' : $userdata['gender'];
     1689
    16871690        $meta['rich_editing'] = empty( $userdata['rich_editing'] ) ? 'true' : $userdata['rich_editing'];
    16881691
    16891692        $meta['syntax_highlighting'] = empty( $userdata['syntax_highlighting'] ) ? 'true' : $userdata['syntax_highlighting'];
    function wp_insert_user( $userdata ) { 
    17741777         *     @type string   $first_name           The user's first name.
    17751778         *     @type string   $last_name            The user's last name.
    17761779         *     @type string   $description          The user's description.
     1780         *     @type string   $gender               The user's gender.
    17771781         *     @type bool     $rich_editing         Whether to enable the rich-editor for the user. False if not empty.
    17781782         *     @type bool     $syntax_highlighting  Whether to enable the rich code editor for the user. False if not empty.
    17791783         *     @type bool     $comment_shortcuts    Whether to enable keyboard shortcuts for the user. Default false.