Making WordPress.org

Changeset 10594


Ignore:
Timestamp:
01/19/2021 04:38:56 AM (6 years ago)
Author:
dd32
Message:

Support Forums: Display user notes (and allow them to be added from) the forum user edit screen.

See #4691.
Fixes #4192.

File:
1 edited

Legend:

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

    r10580 r10594  
    2222                add_action( 'wp_enqueue_scripts',                   array( $this, 'enqueue_scripts' ) );
    2323
    24                 add_action( 'bbp_post_request',                     array( $this, 'add_user_note_request' ) );
     24                add_action( 'bbp_post_request',                     array( $this, 'add_user_note_request' ), 0 ); // Low priority to get below bbp_edit_user_handler()
    2525                add_action( 'bbp_get_request',                      array( $this, 'delete_user_note_request' ) );
    2626
     
    3131                add_action( 'bbp_theme_before_reply_content',       array( $this, 'display_user_notes_in_content' ) );
    3232                add_action( 'bbp_template_after_user_profile',      array( $this, 'display_user_notes_in_profile' ) );
     33                add_action( 'bbp_user_edit_after',                  array( $this, 'display_user_notes_in_profile_edit' ) );
    3334        }
    3435
     
    5051         */
    5152        public function add_user_note_request( $action = '' ) {
    52                 if ( 'wporg_bbp_add_user_note' !== $action || ! current_user_can( 'moderate' ) ) {
    53                         return;
    54                 }
     53                if (
     54                        ! current_user_can( 'moderate' ) ||
     55                        ! in_array( $action, [ 'bbp-update-user', 'wporg_bbp_add_user_note' ] )
     56                ) {
     57                        return;
     58                }
     59
     60                $should_redirect = 'wporg_bbp_add_user_note' === $action;
    5561
    5662                $user_id   = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0;
     
    6571
    6672                // Make sure our nonces are in order.
    67                 if ( ! bbp_verify_nonce_request( sprintf( 'wporg-bbp-add-user-note_%d', $user_id ) ) ) {
     73                if ( ! bbp_verify_nonce_request( sprintf( 'wporg-bbp-add-user-note_%d', $user_id ), '_notenonce' ) ) {
    6874                        return;
    6975                }
     
    7177                $this->add_user_note( $user_id, $note_text, $post_id, $note_id );
    7278
    73                 $redirect_url = set_url_scheme( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
    74 
    75                 // Redirect to clear form data.
    76                 bbp_redirect( $redirect_url );
     79                if ( $should_redirect ) {
     80                        $redirect_url = set_url_scheme( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     81
     82                        // Redirect to clear form data.
     83                        bbp_redirect( $redirect_url );
     84                }
    7785        }
    7886
     
    130138                }
    131139
    132                 update_user_meta( $user_id, self::META, $user_notes );
     140                if ( update_user_meta( $user_id, self::META, $user_notes ) ) {
     141                        // Clear internal cache.
     142                        unset( $this->user_notes[ $user_id ] );
     143                }
    133144
    134145                return true;
     
    153164
    154165                // Make sure our nonces are in order.
    155                 if ( ! bbp_verify_nonce_request( sprintf( 'wporg-bbp-delete-user-note_%d_%d', $user_id, $note_id ) ) ) {
     166                if ( ! bbp_verify_nonce_request( sprintf( 'wporg-bbp-delete-user-note_%d_%d', $user_id, $note_id ), '_notenonce' ) ) {
    156167                        return;
    157168                }
     
    159170                $this->delete_user_note( $user_id, $note_id );
    160171
    161                 $redirect_url = remove_query_arg( array( 'action', 'user_id', 'note_id', '_wpnonce' ) );
     172                $redirect_url = remove_query_arg( array( 'action', 'user_id', 'note_id', '_notenonce' ) );
    162173
    163174                // Redirect to clear URL.
     
    200211         *
    201212         * @param int $user_id User ID. Defaults to the current post author.
     213         * @param bool $display_add_note_form Whether to show the Add New Note form.
    202214         * @return array {
    203215         *     Array of user notes.
     
    208220         * }
    209221         */
    210         public function get_user_notes( $user_id = 0 ) {
     222        public function get_user_notes( $user_id = 0, $display_add_note_form = true ) {
    211223                if ( ! $user_id ) {
    212224                        $user_id = get_the_author_meta( 'ID' );
     
    225237                $note_id   = isset( $_GET['note_id'] ) ? (int) $_GET['note_id'] : 0;
    226238                $edit_note = isset( $user_notes[ $note_id ] );
     239
     240                // Don't display the new note form when editing a note.
     241                if ( $edit_note ) {
     242                        $display_add_note_form = false;
     243                }
    227244
    228245                $this->user_notes[ $user_id ] = (object) array(
     
    280297                                                        'note_id' => $key,
    281298                                                ), $redirect_on_delete ),
    282                                                 sprintf( 'wporg-bbp-delete-user-note_%d_%d', $user_id, $key )
     299                                                sprintf( 'wporg-bbp-delete-user-note_%d_%d', $user_id, $key ),
     300                                                '_notenonce'
    283301                                        ) ),
    284302                                        __( 'Delete', 'wporg-forums' )
     
    308326                }
    309327
    310                 if ( ! $edit_note ) {
     328                if ( $display_add_note_form ) {
    311329                        ob_start();
    312330                        $this->display_note_form( $user_id );
     
    324342         *
    325343         * @param int $user_id User ID. Default 0.
     344         * @param bool $display_add_note_form Whether to show the add new note form. Default true.
    326345         * @return string User notes output.
    327346         */
    328         public function get_user_notes_html( $user_id = 0 ) {
    329                 $user_notes = $this->get_user_notes( $user_id )->html;
     347        public function get_user_notes_html( $user_id = 0, $display_add_note_form = true ) {
     348                $user_notes = $this->get_user_notes( $user_id, $display_add_note_form )->html;
    330349
    331350                if ( ! bbp_is_single_user_profile() ) {
     
    420439                ?>
    421440                <form action="###POST_PERMALINK###" method="post" class="wporg-bbp-add-user-note">
    422                         <?php wp_nonce_field( sprintf( 'wporg-bbp-add-user-note_%d', $user_id ) ); ?>
     441                        <?php wp_nonce_field( sprintf( 'wporg-bbp-add-user-note_%d', $user_id ), '_notenonce' ); ?>
    423442                        <input type="hidden" name="action" value="wporg_bbp_add_user_note">
    424443                        <input type="hidden" name="user_id" value="<?php echo esc_attr( $user_id ); ?>">
     
    537556        }
    538557
     558       
     559        /**
     560         * Displays existing notes and the form for adding a new note in user edit profile.
     561         */
     562        public function display_user_notes_in_profile_edit() {
     563                if ( ! current_user_can( 'moderate' ) ) {
     564                        return;
     565                }
     566
     567                $user_id = bbp_get_displayed_user_id();
     568
     569                // Only super admins can see notes on the current user.
     570                if ( ! is_super_admin() && $user_id == get_current_user_id() ) {
     571                        return;
     572                }
     573
     574                // Only keymasters can see notes on moderators.
     575                if ( user_can( $user_id, 'moderate' ) && ! current_user_can( 'keep_gate' ) ) {
     576                        return;
     577                }
     578                ?>
     579                <div class="wporg-bbp-user-notes">
     580                        <h2 id="user-notes" class="entry-title"><?php esc_html_e( 'User Notes', 'wporg-forums' ); ?></h2>
     581                        <div class="bbp-user-section">
     582                                <?php echo $this->get_user_notes_html( $user_id, false ); ?>
     583
     584                                <div class="wporg-bbp-add-user-note">
     585                                        <?php wp_nonce_field( sprintf( 'wporg-bbp-add-user-note_%d', $user_id ), '_notenonce' ); ?>
     586                                        <input type="hidden" name="user_id" value="<?php echo esc_attr( $user_id ); ?>">
     587                                        <label for="wporg-bbp-user-note-text" class=""><?php esc_html_e( 'Add your note', 'wporg-forums' ); ?></label><br>
     588                                        <textarea name="note_text" id="wporg-bbp-user-note-text" cols="40" rows="5"><?php echo esc_textarea( $note_text ); ?></textarea>
     589                                </div>
     590                        </div>
     591                </div>
     592                <?php
     593        }
     594
    539595}
Note: See TracChangeset for help on using the changeset viewer.