Skip to:
Content

BuddyPress.org

Changeset 14061


Ignore:
Timestamp:
10/28/2024 05:27:43 PM (4 months ago)
Author:
espellcaste
Message:

Improve deleted-user private messages in the web and REST API.

Private messages from a deleted user are properly anonymized in both the web and REST API.

Props imath, niftythree.

Closes https://github.com/buddypress/buddypress/pull/384
Fixes #9160

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-template.php

    r13897 r14061  
    199199        global $messages_template;
    200200
     201        $last_user_id = $messages_template->thread->last_sender_id;
     202        $user         = get_userdata( (int) $last_user_id );
     203
     204        if ( 'sentbox' !== bp_current_action() && ( empty( $user ) || ! $user->exists() ) ) {
     205            $thread_excerpt = esc_html__( '[deleted]', 'buddypress' );
     206        } else {
     207            $thread_excerpt = wp_strip_all_tags( bp_create_excerpt( $messages_template->thread->last_message_content, 75 ) );
     208        }
     209
    201210        /**
    202211         * Filters the excerpt of the current thread in the loop.
     
    204213         * @since 1.0.0
    205214         *
    206          * @param string $value Excerpt of the current thread in the loop.
    207          */
    208         return apply_filters( 'bp_get_message_thread_excerpt', wp_strip_all_tags( bp_create_excerpt( $messages_template->thread->last_message_content, 75 ) ) );
     215         * @param string $thread_excerpt Excerpt of the current thread in the loop.
     216         */
     217        return apply_filters( 'bp_get_message_thread_excerpt', $thread_excerpt );
    209218    }
    210219
     
    243252        global $messages_template;
    244253
     254        $last_user_id = $messages_template->thread->last_sender_id;
     255        $user         = get_userdata( (int) $last_user_id );
     256
     257        if ( ( empty( $user ) || ! $user->exists() ) ) {
     258            $last_message_content = esc_html__( '[deleted]', 'buddypress' );
     259        } else {
     260            $last_message_content = $messages_template->thread->last_message_content;
     261        }
     262
    245263        /**
    246264         * Filters the content of the last message in the thread.
     
    250268         * @param string $last_message_content Content of the last message in the thread.
    251269         */
    252         return apply_filters( 'bp_get_message_thread_content', $messages_template->thread->last_message_content );
     270        return apply_filters( 'bp_get_message_thread_content', $last_message_content );
    253271    }
    254272
     
    257275 */
    258276function bp_message_thread_from() {
    259     // Esaping is made in `bp_core_get_userlink()` && in `bp_core_get_user_displayname()`.
     277    // Escaping is made in `bp_core_get_userlink()` && in `bp_core_get_user_displayname()`.
    260278    // phpcs:ignore WordPress.Security.EscapeOutput
    261279    echo bp_get_message_thread_from();
     
    271289        global $messages_template;
    272290
     291        $userlink = bp_core_get_userlink( $messages_template->thread->last_sender_id );
     292
     293        if ( empty( $userlink ) ) {
     294            $userlink = esc_html__( 'Deleted User', 'buddypress' );
     295        }
     296
    273297        /**
    274298         * Filters the link to the page of the current thread's last author.
     
    276300         * @since 1.0.0
    277301         *
    278          * @param string $value Link to the page of the current thread's last author.
    279          */
    280         return apply_filters( 'bp_get_message_thread_from', bp_core_get_userlink( $messages_template->thread->last_sender_id ) );
     302         * @param string $userlink Link to the page of the current thread's last author.
     303         */
     304        return apply_filters( 'bp_get_message_thread_from', $userlink );
    281305    }
    282306
     
    304328         * @since 1.0.0
    305329         *
    306          * @param string $value HTML links to the pages of the current thread's recipients.
     330         * @param string $recipients_links HTML links to the pages of the current thread's recipients.
    307331         */
    308332        return apply_filters( 'bp_message_thread_to', BP_Messages_Thread::get_recipient_links( $messages_template->thread->recipients ) );
     
    22342258        global $thread_template;
    22352259
     2260        $sender_link = bp_core_get_userlink( $thread_template->message->sender_id, false, true );
     2261
     2262        if ( empty( $sender_link ) ) {
     2263            $sender_link = '';
     2264        }
     2265
    22362266        /**
    22372267         * Filters the link to the sender of the current message.
     
    22392269         * @since 1.1.0
    22402270         *
    2241          * @param string $value Link to the sender of the current message.
    2242          */
    2243         return apply_filters( 'bp_get_the_thread_message_sender_link', bp_core_get_userlink( $thread_template->message->sender_id, false, true ) );
     2271         * @param string $sender_link Link to the sender of the current message.
     2272         */
     2273        return apply_filters( 'bp_get_the_thread_message_sender_link', $sender_link );
    22442274    }
    22452275
  • trunk/src/bp-messages/classes/class-bp-messages-box-template.php

    r13414 r14061  
    343343     */
    344344    public function the_message_thread() {
    345 
    346345        $this->in_the_loop = true;
    347346        $this->thread      = $this->next_thread();
  • trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php

    r14058 r14061  
    158158        $retval = array();
    159159        foreach ( (array) $messages_box->threads as $thread ) {
     160            $messages_box->the_message_thread();
     161
    160162            $retval[] = $this->prepare_response_for_collection(
    161163                $this->prepare_item_for_response( $thread, $request )
     
    167169
    168170        /**
    169          * Fires after a thread is fetched via the REST API.
    170          *
    171          * @since 15.0.0
    172          *
    173          * @param BP_Messages_Box_Template  $messages_box Fetched thread.
     171         * Fires after threads are fetched via the REST API.
     172         *
     173         * @since 15.0.0
     174         *
     175         * @param BP_Messages_Box_Template  $messages_box Messages box
    174176         * @param WP_REST_Response          $response     The response data.
    175177         * @param WP_REST_Request           $request      The request sent to the API.
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r14036 r14061  
    11951195     *
    11961196     * @param array $recipients Array containing the message recipients (array of objects).
    1197      * @return string String of message recipent userlinks.
     1197     * @return string String of message recipient userlinks.
    11981198     */
    11991199    public static function get_recipient_links( $recipients ) {
     
    12161216        }
    12171217
    1218         return implode( ', ', (array) $recipient_links );
     1218        return implode( ', ', $recipient_links );
    12191219    }
    12201220
  • trunk/src/bp-templates/bp-nouveau/buddypress/common/js-templates/messages/index.php

    r13820 r14061  
    131131    <# if ( ! data.recipientsCount ) { #>
    132132        <div class="thread-from">
    133             <a class="user-link" href="{{data.sender_link}}">
    134                 <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
    135                 <span class="bp-screen-reader-text"><?php esc_html_e( 'From:', 'buddypress' ); ?></span>
    136                 <span class="user-name">{{data.sender_name}}</span>
    137             </a>
     133            <# if ( data.sender_link ) { #>
     134                <a class="user-link" href="{{data.sender_link}}">
     135                    <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
     136                    <span class="bp-screen-reader-text"><?php esc_html_e( 'From:', 'buddypress' ); ?></span>
     137                    <span class="user-name">{{data.sender_name}}</span>
     138                </a>
     139            <# } else { #>
     140                <div class="user-link">
     141                    <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
     142                    <span class="bp-screen-reader-text"><?php esc_html_e( 'From:', 'buddypress' ); ?></span>
     143                    <span class="user-name">{{data.sender_name}}</span>
     144                </div>
     145            <# } #>
    138146        </div>
    139147    <# } else {
     
    141149        #>
    142150        <div class="thread-to">
    143             <a class="user-link" href="{{recipient.user_link}}">
    144                 <img class="avatar" src="{{{recipient.avatar}}}" alt="" />
    145                 <span class="bp-screen-reader-text"><?php esc_html_e( 'To:', 'buddypress' ); ?></span>
    146                 <span class="user-name">{{recipient.user_name}}</span>
    147             </a>
     151            <# if ( recipient.user_link ) { #>
     152                <a class="user-link" href="{{recipient.user_link}}">
     153                    <img class="avatar" src="{{{recipient.avatar}}}" alt="" />
     154                    <span class="bp-screen-reader-text"><?php esc_html_e( 'To:', 'buddypress' ); ?></span>
     155                    <span class="user-name">{{recipient.user_name}}</span>
     156                </a>
     157            <# } else { #>
     158                <div class="user-link">
     159                    <img class="avatar" src="{{{recipient.avatar}}}" alt="" />
     160                    <span class="bp-screen-reader-text"><?php esc_html_e( 'To:', 'buddypress' ); ?></span>
     161                    <span class="user-name">{{recipient.user_name}}</span>
     162                </div>
     163            <# } #>
    148164
    149165            <# if ( data.toOthers ) { #>
     
    274290        <# } #>
    275291
    276         <a href="{{data.sender_link}}" class="user-link">
    277             <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
    278             <strong>{{data.sender_name}}</strong>
    279         </a>
     292        <# if ( data.sender_link ) { #>
     293            <a href="{{data.sender_link}}" class="user-link">
     294                <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
     295                <strong>{{data.sender_name}}</strong>
     296            </a>
     297        <# } else { #>
     298            <div class="user-link">
     299                <img class="avatar" src="{{{data.sender_avatar}}}" alt="" />
     300                <strong>{{data.sender_name}}</strong>
     301            </div>
     302        <# } #>
    280303
    281304        <time datetime="{{data.date.toISOString()}}" class="activity">{{data.display_date}}</time>
    282305
    283         <div class="actions">
    284             <# if ( undefined !== data.star_link ) { #>
     306        <# if ( undefined !== data.star_link ) { #>
     307            <div class="actions">
    285308
    286309                <button type="button" class="message-action-unstar bp-tooltip bp-icons <# if ( false === data.is_starred ) { #>bp-hide<# } #>" data-bp-star-link="{{data.star_link}}" data-bp-action="unstar" data-bp-tooltip="<?php esc_attr_e( 'Unstar Message', 'buddypress' ); ?>">
     
    292315                </button>
    293316
    294             <# } #>
    295         </div>
     317            </div>
     318        <# } #>
    296319
    297320        <# if ( data.afterMeta ) { #>
  • trunk/src/bp-templates/bp-nouveau/includes/messages/ajax.php

    r13897 r14061  
    163163        wp_send_json_error( $response );
    164164    }
     165}
     166
     167/**
     168 * Returns recipient's data for the Backbone UI.
     169 *
     170 * @since 15.0.0
     171 *
     172 * @param integer $user_id The User ID.
     173 * @return array
     174 */
     175function bp_nouveau_ajax_get_message_recipients_data( $user_id ) {
     176    $user_link   = bp_core_get_userlink( $user_id, false, true );
     177    $avatar_args = array(
     178        'item_id' => $user_id,
     179        'object'  => 'user',
     180        'type'    => 'thumb',
     181        'width'   => 28,
     182        'height'  => 28,
     183        'html'    => false,
     184    );
     185
     186    if ( ! $user_link ) {
     187        return array(
     188            'avatar'    => esc_url( bp_core_avatar_default( 'gravatar', $avatar_args ) ),
     189            'user_link' => '',
     190            'user_name' => '',
     191        );
     192    }
     193
     194    return array(
     195        'avatar'    => esc_url( bp_core_fetch_avatar( $avatar_args ) ),
     196        'user_link' => esc_url( $user_link ),
     197        'user_name' => esc_html( bp_core_get_user_displayname( $user_id ) ),
     198    );
     199}
     200
     201/**
     202 * Returns sender's data for the Backbone UI.
     203 *
     204 * @since 15.0.0
     205 *
     206 * @param integer $user_id The User ID.
     207 * @return array
     208 */
     209function bp_nouveau_ajax_get_message_sender_data( $user_id ) {
     210    $sender_link = bp_core_get_userlink( $user_id, false, true );
     211    $avatar_args = array(
     212        'item_id' => $user_id,
     213        'object'  => 'user',
     214        'type'    => 'thumb',
     215        'width'   => 32,
     216        'height'  => 32,
     217        'html'    => false,
     218    );
     219
     220    if ( ! $sender_link ) {
     221        return array(
     222            'name'   => esc_html__( 'Deleted User', 'buddypress' ),
     223            'link'   => $sender_link,
     224            'avatar' => esc_url( bp_core_avatar_default( 'gravatar', $avatar_args ) ),
     225        );
     226    }
     227
     228    return array(
     229        'name'   => esc_html( bp_core_get_user_displayname( $user_id ) ),
     230        'link'   => esc_url( $sender_link ),
     231        'avatar' => esc_url( bp_core_fetch_avatar( $avatar_args ) ),
     232    );
    165233}
    166234
     
    333401    while ( bp_message_threads() ) : bp_message_thread();
    334402        $last_message_id = (int) $messages_template->thread->last_message_id;
     403        $sender_data     = bp_nouveau_ajax_get_message_sender_data( $messages_template->thread->last_sender_id );
    335404
    336405        $threads->threads[ $i ] = array(
     
    341410            'content'       => do_shortcode( bp_get_message_thread_content() ),
    342411            'unread'        => bp_message_thread_has_unread(),
    343             'sender_name'   => bp_core_get_user_displayname( $messages_template->thread->last_sender_id ),
    344             'sender_link'   => bp_core_get_userlink( $messages_template->thread->last_sender_id, false, true ),
    345             'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
    346                 'item_id' => $messages_template->thread->last_sender_id,
    347                 'object'  => 'user',
    348                 'type'    => 'thumb',
    349                 'width'   => 32,
    350                 'height'  => 32,
    351                 'html'    => false,
    352             ) ) ),
     412            'sender_name'   => $sender_data['name'],
     413            'sender_link'   => $sender_data['link'],
     414            'sender_avatar' => $sender_data['avatar'],
    353415            'count'         => bp_get_message_thread_total_count(),
    354416            'date'          => strtotime( bp_get_message_thread_last_post_date_raw() ) * 1000,
     
    358420        if ( is_array( $messages_template->thread->recipients ) ) {
    359421            foreach ( $messages_template->thread->recipients as $recipient ) {
    360                 $threads->threads[ $i ]['recipients'][] = array(
    361                     'avatar' => esc_url( bp_core_fetch_avatar( array(
    362                         'item_id' => $recipient->user_id,
    363                         'object'  => 'user',
    364                         'type'    => 'thumb',
    365                         'width'   => 28,
    366                         'height'  => 28,
    367                         'html'    => false,
    368                     ) ) ),
    369                     'user_link' => bp_core_get_userlink( $recipient->user_id, false, true ),
    370                     'user_name' => bp_members_get_user_slug( $recipient->user_id ),
    371                 );
     422                $threads->threads[ $i ]['recipients'][] = bp_nouveau_ajax_get_message_recipients_data( $recipient->user_id );
    372423            }
    373424        }
     
    421472    $bp->current_action = $reset_action;
    422473
    423     // Return the successfull reply.
     474    // Return the successful reply.
    424475    wp_send_json_success( $threads );
    425476}
     
    508559        if ( is_array( $thread_template->thread->recipients ) ) {
    509560            foreach ( $thread_template->thread->recipients as $recipient ) {
    510                 $thread->thread['recipients'][] = array(
    511                     'avatar' => esc_url( bp_core_fetch_avatar( array(
    512                         'item_id' => $recipient->user_id,
    513                         'object'  => 'user',
    514                         'type'    => 'thumb',
    515                         'width'   => 28,
    516                         'height'  => 28,
    517                         'html'    => false,
    518                     ) ) ),
    519                     'user_link' => bp_core_get_userlink( $recipient->user_id, false, true ),
    520                     'user_name' => bp_members_get_user_slug( $recipient->user_id ),
    521                 );
     561                $thread->thread['recipients'][] = bp_nouveau_ajax_get_message_recipients_data( $recipient->user_id );
    522562            }
    523563        }
     
    528568
    529569    while ( bp_thread_messages() ) : bp_thread_the_message();
     570        $sender_data = bp_nouveau_ajax_get_message_sender_data( bp_get_the_thread_message_sender_id() );
     571
    530572        $thread->messages[ $i ] = array(
    531573            'id'            => bp_get_the_thread_message_id(),
    532574            'content'       => do_shortcode( bp_get_the_thread_message_content() ),
    533575            'sender_id'     => bp_get_the_thread_message_sender_id(),
    534             'sender_name'   => esc_html( bp_get_the_thread_message_sender_name() ),
    535             'sender_link'   => bp_get_the_thread_message_sender_link(),
    536             'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
    537                 'item_id' => bp_get_the_thread_message_sender_id(),
    538                 'object'  => 'user',
    539                 'type'    => 'thumb',
    540                 'width'   => 32,
    541                 'height'  => 32,
    542                 'html'    => false,
    543             ) ) ),
     576            'sender_name'   => $sender_data['name'],
     577            'sender_link'   => $sender_data['link'],
     578            'sender_avatar' => $sender_data['avatar'],
    544579            'date'          => bp_get_the_thread_message_date_sent() * 1000,
    545580            'display_date'  => bp_get_the_thread_message_time_since(),
Note: See TracChangeset for help on using the changeset viewer.