Skip to:
Content

BuddyPress.org

Changeset 9926


Ignore:
Timestamp:
06/11/2015 02:19:29 AM (10 years ago)
Author:
johnjamesjacoby
Message:

Messages: Updates to bp_get_the_message_star_action_link():

  • Set default $user_id so links work on displayed users
  • Use switch vs. if/else to make $user_domain calculation more obvious
  • Bail if $user_domain cannot be calculated
  • Cast a few variables to avoid non-integer issues cascading into other methods
  • Strict boolean comparisons to ensure proper execution
  • Escape output of a few HTML attributes

This change allows for the action-link used to star a message to use the correct URL for the user_id that's passed into it. This is particularly helpful for capable community moderators that can view other user's Messages, so the links point to the correct URLs for the correct user.

See #6489. (2.3 branch, for 2.3.2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/src/bp-messages/bp-messages-star.php

    r9923 r9926  
    8989     */
    9090    function bp_get_the_message_star_action_link( $args = array() ) {
     91
     92        // Default user ID
     93        $user_id = bp_displayed_user_id()
     94            ? bp_displayed_user_id()
     95            : bp_loggedin_user_id();
     96
    9197        $r = bp_parse_args( $args, array(
    92             'user_id'      => bp_loggedin_user_id(),
    93             'thread_id'    => 0,
    94             'message_id'   => (int) bp_get_the_thread_message_id(),
    95             'url_only'     => false,
    96             'text_unstar'  => __( 'Unstar', 'buddypress' ),
    97             'text_star'    => __( 'Star', 'buddypress' ),
    98             'title_unstar' => __( 'Starred', 'buddypress' ),
    99             'title_star'   => __( 'Not starred', 'buddypress' ),
     98            'user_id'             => (int) $user_id,
     99            'thread_id'           => 0,
     100            'message_id'          => (int) bp_get_the_thread_message_id(),
     101            'url_only'            => false,
     102            'text_unstar'         => __( 'Unstar',      'buddypress' ),
     103            'text_star'           => __( 'Star',        'buddypress' ),
     104            'title_unstar'        => __( 'Starred',    'buddypress' ),
     105            'title_star'          => __( 'Not starred', 'buddypress' ),
    100106            'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
    101             'title_star_thread'   => __( 'Star the first message in this thread', 'buddypress' ),
     107            'title_star_thread'   => __( 'Star the first message in this thread',      'buddypress' ),
    102108        ), 'messages_star_action_link' );
    103109
    104         $retval = $bulk_attr = '';
    105 
    106         if ( 0 === $r['user_id'] ) {
    107             return $retval;
     110        // Check user ID and determine base user URL
     111        switch ( $r['user_id'] ) {
     112
     113            // Current user
     114            case bp_loggedin_user_id() :
     115                $user_domain = bp_loggedin_user_domain();
     116                break;
     117
     118            // Displayed user
     119            case bp_displayed_user_id() :
     120                $user_domain = bp_displayed_user_domain();
     121                break;
     122
     123            // Empty or other
     124            default :
     125                $user_domain = bp_core_get_user_domain( $r['user_id'] );
     126                break;
    108127        }
    109128
    110         // get user domain
    111         if ( $r['user_id'] == bp_loggedin_user_id() ) {
    112             $user_domain = bp_loggedin_user_domain();
    113         } elseif ( $r['user_id'] == bp_displayed_user_domain() ) {
    114             $user_domain = bp_displayed_user_domain();
    115         } else {
    116             $user_domain = bp_core_get_user_domain( $r['user_id'] );
     129        // Bail if no user domain was calculated
     130        if ( empty( $user_domain ) ) {
     131            return '';
    117132        }
    118133
    119134        // thread ID
    120135        if ( (int) $r['thread_id'] > 0 ) {
     136
    121137            // see if we're in the loop
    122138            if ( bp_get_message_thread_id() == $r['thread_id'] ) {
     139
    123140                // grab all message ids
    124141                $mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' );
     
    130147            // pull up the thread
    131148            } else {
    132                 $thread = new BP_Messages_thread( $r['thread_id'] );
    133                 $mids = wp_list_pluck( $thread->messages, 'id' );
     149                $thread = new BP_Messages_Thread( $r['thread_id'] );
     150                $mids   = wp_list_pluck( $thread->messages, 'id' );
    134151            }
    135152
     
    137154            $message_id = 0;
    138155            foreach ( $mids as $mid ) {
     156
    139157                // try to find the first msg that is starred in a thread
    140158                if ( true === bp_messages_is_message_starred( $mid ) ) {
     
    150168            }
    151169
     170            $message_id = (int) $message_id;
     171
    152172            // nonce
    153173            $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" );
    154174
    155             if ( $is_starred ) {
    156                 $action = 'unstar';
     175            if ( true === $is_starred ) {
     176                $action    = 'unstar';
    157177                $bulk_attr = ' data-star-bulk="1"';
    158                 $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/';
     178                $retval    = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/';
    159179            } else {
    160                 $action = 'star';
    161                 $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
     180                $action    = 'star';
     181                $retval    = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
    162182            }
    163183
     
    170190            $nonce      = wp_create_nonce( "bp-messages-star-{$message_id}" );
    171191
    172             if ( $is_starred ) {
     192            if ( true === $is_starred ) {
    173193                $action = 'unstar';
    174194                $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/';
     
    202222         * @param array  $r      Parsed link arguments. See $args in bp_get_the_message_star_action_link().
    203223         */
    204         return apply_filters( 'bp_get_the_message_star_action_link', '<a title="' . esc_attr( $title ) . '" class="message-action-' . $action . '" data-star-status="' . $action .'" data-star-nonce="' . $nonce . '"' . $bulk_attr . ' data-message-id="' . esc_attr( (int) $message_id ) . '" href="' . $retval . '"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r );
     224        return apply_filters( 'bp_get_the_message_star_action_link', '<a title="' . esc_attr( $title ) . '" class="message-action-' . esc_attr( $action ) . '" data-star-status="' . esc_attr( $action ) .'" data-star-nonce="' . esc_attr( $nonce ) . '"' . $bulk_attr . ' data-message-id="' . esc_attr( (int) $message_id ) . '" href="' . $retval . '"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r );
    205225    }
    206226
Note: See TracChangeset for help on using the changeset viewer.