Making WordPress.org

Changeset 5659


Ignore:
Timestamp:
07/12/2017 02:53:16 PM (8 years ago)
Author:
SergeyBiryukov
Message:

Support Forums, User Notes: Cache notes output to a static variable, following the idea from the original patch, to avoid repeated template function calls.

See #2272.

File:
1 edited

Legend:

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

    r5658 r5659  
    2323        add_action( 'bbp_get_request',                      array( $this, 'delete_user_note' ) );
    2424
    25         add_action( 'bbp_theme_after_topic_author_details', array( $this, 'add_user_notes_toggle_link' ) );
    26         add_action( 'bbp_theme_after_reply_author_details', array( $this, 'add_user_notes_toggle_link' ) );
     25        add_action( 'bbp_theme_after_topic_author_details', array( $this, 'display_user_notes_toggle_link' ) );
     26        add_action( 'bbp_theme_after_reply_author_details', array( $this, 'display_user_notes_toggle_link' ) );
    2727
    2828        add_action( 'bbp_theme_before_topic_content',       array( $this, 'display_user_notes_in_content' ) );
     
    172172     *
    173173     * @param int $user_id User ID. Defaults to the current post author.
    174      * @return array Array of user notes.
     174     * @return array {
     175     *     Array of user notes.
     176     *
     177     *     @type int    $count User notes count.
     178     *     @type array  $raw   Array of raw user notes data.
     179     *     @type string $html  User notes output.
     180     * }
    175181     */
    176182    public function get_user_notes( $user_id = 0 ) {
     
    189195        }
    190196
    191         $this->user_notes[ $user_id ] = $user_notes;
    192 
    193         return $user_notes;
    194     }
    195 
    196     /**
    197      * Adds toggle link for notes to the author area of a post.
    198      */
    199     public function add_user_notes_toggle_link() {
    200         if ( ! current_user_can( 'moderate' ) ) {
    201             return;
    202         }
    203 
    204         $user_id = get_the_author_meta( 'ID' );
    205         $post_id = get_the_ID();
    206 
    207         // Only keymasters can see notes on moderators.
    208         if ( user_can( $user_id, 'moderate' ) && ! current_user_can( 'keep_gate' ) ) {
    209             return;
    210         }
    211 
    212         printf( '<div class="wporg-bbp-user-notes-toggle"><a href="#" data-post-id="%d">%s</a></div>',
    213             esc_attr( $post_id ),
    214             esc_html(
    215                 /* translators: %d: user notes count */
    216                 sprintf( __( 'User Notes (%d)', 'wporg-forums' ),
    217                     count( $this->get_user_notes( $user_id ) )
    218                 )
    219             )
     197        $note_id   = isset( $_GET['note_id'] ) ? (int) $_GET['note_id'] : 0;
     198        $edit_note = isset( $user_notes[ $note_id ] );
     199
     200        $this->user_notes[ $user_id ] = (object) array(
     201            'count' => count( $user_notes ),
     202            'raw'   => $user_notes,
     203            'html'  => '',
    220204        );
    221     }
    222 
    223     /**
    224      * Retrieves permalink to the post associated with a note.
    225      *
    226      * If the note is not associated with a particular post, returns a link
    227      * to user profile.
    228      *
    229      * @param int $post_id Post ID. Default 0.
    230      * @param int $user_id User ID. Default 0.
    231      * @param int $site_id Site ID. Default 0.
    232      * @return string Post permalink or user profile URL.
    233      */
    234     public function get_user_note_post_permalink( $post_id = 0, $user_id = 0, $site_id = 0 ) {
    235         switch_to_blog( $site_id );
    236 
    237         $post_type = $post_id ? get_post_type( $post_id ) : '';
    238 
    239         if ( 'topic' === $post_type ) {
    240             $permalink = bbp_get_topic_permalink( $post_id ) . '#post-' . (int) $post_id;
    241             $permalink = add_query_arg( array(
    242                 'view'            => 'all',
    243                 'show_user_notes' => $post_id,
    244             ), $permalink );
    245         } elseif ( 'reply' === $post_type ) {
    246             $permalink = bbp_get_reply_url( $post_id );
    247             $permalink = add_query_arg( array(
    248                 'view'            => 'all',
    249                 'show_user_notes' => $post_id,
    250             ), $permalink );
    251         } else {
    252             $permalink = bbp_get_user_profile_url( $user_id ) . '#user-notes';
    253         }
    254 
    255         restore_current_blog();
    256 
    257         return $permalink;
    258     }
    259 
    260     /**
    261      * Displays notes for a particular user and a form for adding a new note.
    262      *
    263      * @param int $user_id User ID. Defaults to the current post author.
    264      */
    265     public function display_user_notes( $user_id = 0 ) {
    266         $note_id    = isset( $_GET['note_id'] ) ? (int) $_GET['note_id'] : 0;
    267         $user_notes = $this->get_user_notes( $user_id );
    268         $edit_note  = isset( $user_notes[ $note_id ] );
    269 
    270         $current_post_type = get_post_type();
    271205
    272206        foreach ( $user_notes as $key => $note ) {
     
    276210
    277211            $note_meta = array(
    278                 /* translators: 1: user note author's display name, 2: link to post, 3: date, 4: time */
    279                 'author' => sprintf( __( 'By %1$s on <a href="%2$s">%3$s at %4$s</a>', 'wporg-forums' ),
     212                'author' => sprintf(
     213                    /* translators: 1: user note author's display name, 2: link to post, 3: date, 4: time */
     214                    __( 'By %1$s on <a href="%2$s">%3$s at %4$s</a>', 'wporg-forums' ),
    280215                    sprintf( '<a href="%s">%s</a>',
    281216                        esc_url( get_home_url( $post_site_id, "/users/{$note->moderator}/" ) ),
     
    323258            }
    324259
    325             printf( '<div class="bbp-template-notice warning"><p>%s</p> %s</div>' . "\n",
     260            $this->user_notes[ $user_id ]->html .= sprintf(
     261                '<div class="bbp-template-notice warning"><p>%s</p> %s</div>' . "\n",
    326262                wp_kses( $note->text, array( 'a' => array( 'href' => true ) ) ),
    327263                sprintf( '<p class="wporg-bbp-user-note-meta">%s</p>' . "\n",
     
    331267
    332268            if ( $edit_note && $key == $note_id ) {
     269                ob_start();
    333270                $this->display_note_form( $user_id );
     271                $this->user_notes[ $user_id ]->html .= ob_get_clean();
    334272            }
    335273        }
    336274
    337275        if ( ! $user_notes ) {
    338             printf( '<div class="bbp-template-notice info"><p>%s</p></div>',
     276            $this->user_notes[ $user_id ]->html .= sprintf(
     277                '<div class="bbp-template-notice info"><p>%s</p></div>',
    339278                esc_html__( 'No notes have been added for this user.', 'wporg-forums' )
    340279            );
     
    342281
    343282        if ( ! $edit_note ) {
     283            ob_start();
    344284            $this->display_note_form( $user_id );
    345         }
     285            $this->user_notes[ $user_id ]->html .= ob_get_clean();
     286        }
     287
     288        return $this->user_notes[ $user_id ];
     289    }
     290
     291    /**
     292     * Retrieves permalink to the post associated with a note.
     293     *
     294     * If the note is not associated with a particular post, returns a link
     295     * to user profile.
     296     *
     297     * @param int $post_id Post ID. Default 0.
     298     * @param int $user_id User ID. Default 0.
     299     * @param int $site_id Site ID. Default 0.
     300     * @return string Post permalink or user profile URL.
     301     */
     302    public function get_user_note_post_permalink( $post_id = 0, $user_id = 0, $site_id = 0 ) {
     303        switch_to_blog( $site_id );
     304
     305        $post_type = $post_id ? get_post_type( $post_id ) : '';
     306
     307        if ( 'topic' === $post_type ) {
     308            $permalink = bbp_get_topic_permalink( $post_id ) . '#post-' . (int) $post_id;
     309            $permalink = add_query_arg( array(
     310                'view'            => 'all',
     311                'show_user_notes' => $post_id,
     312            ), $permalink );
     313        } elseif ( 'reply' === $post_type ) {
     314            $permalink = bbp_get_reply_url( $post_id );
     315            $permalink = add_query_arg( array(
     316                'view'            => 'all',
     317                'show_user_notes' => $post_id,
     318            ), $permalink );
     319        } else {
     320            $permalink = bbp_get_user_profile_url( $user_id ) . '#user-notes';
     321        }
     322
     323        restore_current_blog();
     324
     325        return $permalink;
    346326    }
    347327
     
    363343
    364344        $note_id    = isset( $_GET['note_id'] ) ? (int) $_GET['note_id'] : 0;
    365         $user_notes = $this->get_user_notes( $user_id );
     345        $user_notes = $this->get_user_notes( $user_id )->raw;
    366346
    367347        if ( isset( $user_notes[ $note_id ] ) ) {
     
    395375
    396376    /**
     377     * Displays toggle link for notes to the author area of a post.
     378     */
     379    public function display_user_notes_toggle_link() {
     380        if ( ! current_user_can( 'moderate' ) ) {
     381            return;
     382        }
     383
     384        $user_id = get_the_author_meta( 'ID' );
     385        $post_id = get_the_ID();
     386
     387        // Only keymasters can see notes on moderators.
     388        if ( user_can( $user_id, 'moderate' ) && ! current_user_can( 'keep_gate' ) ) {
     389            return;
     390        }
     391
     392        printf( '<div class="wporg-bbp-user-notes-toggle"><a href="#" data-post-id="%d">%s</a></div>',
     393            esc_attr( $post_id ),
     394            esc_html(
     395                /* translators: %d: user notes count */
     396                sprintf( __( 'User Notes (%d)', 'wporg-forums' ),
     397                    $this->get_user_notes( $user_id )->count
     398                )
     399            )
     400        );
     401    }
     402
     403    /**
    397404     * Displays existing notes and the form for adding a new note before post content
    398405     * in topics or replies.
     
    419426        ?>
    420427        <div class="<?php echo esc_attr( $class ); ?>" id="wporg-bbp-user-notes-<?php echo esc_attr( $post_id ); ?>">
    421             <?php $this->display_user_notes( $user_id ); ?>
     428            <?php echo $this->get_user_notes( $user_id )->html; ?>
    422429        </div>
    423430        <?php
     
    442449            <h2 id="user-notes" class="entry-title"><?php esc_html_e( 'User Notes', 'wporg-forums' ); ?></h2>
    443450            <div class="bbp-user-section">
    444                 <?php $this->display_user_notes( $user_id ); ?>
     451                <?php echo $this->get_user_notes( $user_id )->html; ?>
    445452            </div>
    446453        </div>
Note: See TracChangeset for help on using the changeset viewer.