Making WordPress.org

Changeset 4142


Ignore:
Timestamp:
09/27/2016 07:00:59 PM (8 years ago)
Author:
coffee2code
Message:

Support Forums: Add support for custom titles.

Restores input for field previously available under bbPress 1.x. Custom titles are already displayed when set. The field is only available to those who can moderate.

Props SergeyBiryukov.
Fixes #1950.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-users.php

    r3880 r4142  
    66
    77    public function __construct() {
     8        // If the user has a custom title, use that instead of the forum role.
    89        add_filter( 'bbp_get_user_display_role', array( $this, 'display_role' ), 10, 2 );
     10
     11        // Add a Custom Title input to user's profile.
     12        add_action( 'bbp_user_edit_after_name', array( $this, 'add_custom_title_input' ) );
     13
     14        // Save Custom Title input value.
     15        add_action( 'personal_options_update', array( $this, 'save_custom_title' ), 10, 2 );
     16        add_action( 'edit_user_profile_update', array( $this, 'save_custom_title' ), 10, 2 );
    917    }
    1018
     
    1220     * If the user has a custom title, use that instead of the forum role.
    1321     *
    14      * @param string $role The user's forum role
    15      * @param int $user_id The user id
    16      * @return string The user's custom forum title, or their forum role
     22     * @param string  $role    The user's forum role.
     23     * @param int     $user_id The user ID.
     24     * @return string The user's custom forum title, or their forum role.
    1725     */
    1826    public function display_role( $role, $user_id ) {
     
    2432        return $role;
    2533    }
     34
     35    /**
     36     * Add a Custom Title input (only available to moderators) to user's profile.
     37     */
     38    public function add_custom_title_input() {
     39        if ( ! current_user_can( 'moderate' ) ) {
     40            return;
     41        }
     42       
     43        ?>
     44        <div>
     45            <label for="title"><?php esc_html_e( 'Custom Title', 'wporg-forums' ); ?></label>
     46            <input type="text" name="title" id="title" value="<?php echo esc_attr( get_user_option( 'title', bbpress()->displayed_user->ID ) ); ?>" class="regular-text" />
     47        </div>
     48    <?php
     49    }
     50
     51    /**
     52     * Save Custom Title input value.
     53     *
     54     * @param int $user_id The user ID.
     55     */
     56    public function save_custom_title( $user_id ) {
     57        if ( ! current_user_can( 'moderate' ) || ! isset( $_POST['title'] ) ) {
     58            return;
     59        }
     60
     61        update_user_option( $user_id, 'title', sanitize_text_field( $_POST['title'] ) );
     62    }
     63
    2664}
Note: See TracChangeset for help on using the changeset viewer.