Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/02/2024 01:34:07 PM (2 years ago)
Author:
espellcaste
Message:

BP REST API: Anonymise message data for deleted users.

Messages from deleted users are anonymised properly.

Props niftythree.

See #9160

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php

    r14035 r14037  
    129129                // Include the meta_query for starred messages.
    130130                if ( 'starred' === $args['box'] ) {
    131                         $args['meta_query'] = array( // phpcs:ignore
     131                        $args['meta_query'] = array(
    132132                                array(
    133133                                        'key'   => 'starred_by_user',
    … …  
    472472                }
    473473
    474                 // By default use the last message.
     474                // By default, use the last message.
    475475                $message_id = $thread->last_message_id;
    476476                if ( $request->get_param( 'message_id' ) ) {
    … …  
    822822         */
    823823        public function prepare_message_for_response( $message, $request ) {
     824                $user         = bp_rest_get_user( $message->sender_id );
     825                $deleted_user = ! $user instanceof WP_User;
     826                $content      = $deleted_user
     827                        ? esc_html__( '[deleted]', 'buddypress' )
     828                        : $message->message;
     829
    824830                $data = array(
    825                         'id'        => (int) $message->id,
    826                         'thread_id' => (int) $message->thread_id,
    827                         'sender_id' => (int) $message->sender_id,
    828                         'subject'   => array(
     831                        'id'            => (int) $message->id,
     832                        'thread_id'     => (int) $message->thread_id,
     833                        'sender_id'     => (int) $message->sender_id,
     834                        'subject'       => array(
    829835                                'raw'      => $message->subject,
    830836                                'rendered' => apply_filters( 'bp_get_message_thread_subject', $message->subject ),
    831837                        ),
    832                         'message'   => array(
    833                                 'raw'      => $message->message,
    834                                 'rendered' => apply_filters( 'bp_get_the_thread_message_content', $message->message ),
     838                        'message'       => array(
     839                                'raw'      => $content,
     840                                'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
    835841                        ),
    836                         'date_sent' => bp_rest_prepare_date_response( $message->date_sent ),
     842                        'date_sent'     => bp_rest_prepare_date_response( $message->date_sent, get_date_from_gmt( $message->date_sent ) ),
     843                        'date_sent_gmt' => bp_rest_prepare_date_response( $message->date_sent ),
    837844                );
    838845
    … …  
    875882                $display_name = '';
    876883                $user_info    = get_userdata( (int) $recipient->user_id );
    877 
    878                 if ( $user_info instanceof WP_User && ! empty( $user_info->display_name ) ) {
    879                         $display_name = (string) $user_info->display_name;
     884                $user_exists  = $user_info instanceof WP_User;
     885
     886                if ( $user_exists && ! empty( $user_info->display_name ) ) {
     887                        $display_name = $user_info->display_name;
     888                }
     889
     890                if ( false === $user_exists ) {
     891                        $display_name = esc_html__( 'Deleted User', 'buddypress' );
    880892                }
    881893
    882894                $data = array(
    883895                        'id'           => (int) $recipient->id,
    884                         'is_deleted'   => (int) $recipient->is_deleted,
     896                        'is_deleted'   => $recipient->is_deleted || ! $user_exists,
    885897                        'name'         => $display_name,
    886                         'sender_only'  => (int) $recipient->sender_only,
     898                        'sender_only'  => (bool) $recipient->sender_only,
    887899                        'thread_id'    => (int) $recipient->thread_id,
    888900                        'unread_count' => (int) $recipient->unread_count,
    889901                        'user_id'      => (int) $recipient->user_id,
    890                         'user_link'    => esc_url( bp_members_get_user_url( $recipient->user_id ) ),
     902                        'user_link'    => $user_exists ? esc_url( bp_members_get_user_url( $recipient->user_id ) ) : '',
    891903                );
    892904
    … …  
    926938         */
    927939        public function prepare_item_for_response( $thread, $request ) {
    928                 $excerpt = '';
     940                $user_exists = function ( $user_id ) {
     941                        $user = bp_rest_get_user( $user_id );
     942
     943                        return $user instanceof WP_User;
     944                };
     945
     946                $deleted_user = false === $user_exists( $thread->last_sender_id );
     947                $raw_excerpt  = '';
     948
    929949                if ( isset( $thread->last_message_content ) ) {
    930                         $excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
    931                 }
     950                        $raw_excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
     951                }
     952
     953                $deleted_text = esc_html__( '[deleted]', 'buddypress' );
     954
     955                $content = $deleted_user
     956                        ? $deleted_text
     957                        : $thread->last_message_content;
     958
     959                $excerpt = $deleted_user
     960                        ? $deleted_text
     961                        : $raw_excerpt;
    932962
    933963                $data = array(
    … …  
    944974                        ),
    945975                        'message'        => array(
    946                                 'raw'      => $thread->last_message_content,
    947                                 'rendered' => apply_filters( 'bp_get_message_thread_content', $thread->last_message_content ),
     976                                'raw'      => $content,
     977                                'rendered' => apply_filters( 'bp_get_the_thread_message_content', $content ),
    948978                        ),
    949979                        'date'           => bp_rest_prepare_date_response( $thread->last_message_date, get_date_from_gmt( $thread->last_message_date ) ),
    950980                        'date_gmt'       => bp_rest_prepare_date_response( $thread->last_message_date ),
    951                         'unread_count'   => ! empty( $thread->unread_count ) ? absint( $thread->unread_count ) : 0,
    952                         'sender_ids'     => (array) isset( $thread->sender_ids ) ? $thread->sender_ids : array(),
     981                        'unread_count'   => (int) $thread->unread_count,
     982                        'sender_ids'     => wp_parse_id_list( array_values( $thread->sender_ids ) ),
    953983                        'recipients'     => array(),
    954984                        'messages'       => array(),
    … …  
    966996
    967997                // Pluck starred message ids.
    968                 $data['starred_message_ids'] = array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) );
     998                $data['starred_message_ids'] = wp_parse_id_list(
     999                        array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) )
     1000                );
    9691001
    9701002                $context  = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
    … …  
    10111043                // Add star links for each message of the thread.
    10121044                if ( is_user_logged_in() && bp_is_active( 'messages', 'star' ) ) {
    1013                         $starred_base = $base . bp_get_messages_starred_slug() . '/';
     1045                        $starred_base              = $base . bp_get_messages_starred_slug() . '/';
     1046                        $links['starred-messages'] = array();
    10141047
    10151048                        foreach ( $thread->messages as $message ) {
    1016                                 $links[ $message->id ] = array(
     1049                                $links['star-messages'][ $message->id ] = array(
    10171050                                        'href' => rest_url( $starred_base . $message->id ),
    10181051                                );
    … …  
    12501283        public function get_item_schema() {
    12511284                if ( is_null( $this->schema ) ) {
    1252                         $this->schema = array(
     1285                        $schema = array(
    12531286                                '$schema'    => 'http://json-schema.org/draft-04/schema#',
    12541287                                'title'      => 'bp_messages',
    … …  
    13841417                                                'type'        => 'array',
    13851418                                                'items'       => array(
    1386                                                         'type' => 'object',
     1419                                                        'type'       => 'object',
     1420                                                        'properties' => array(
     1421                                                                'id'           => array(
     1422                                                                        'description' => __( 'ID of the recipient.', 'buddypress' ),
     1423                                                                        'type'        => 'integer',
     1424                                                                        'context'     => array( 'view', 'edit' ),
     1425                                                                        'readonly'    => true,
     1426                                                                ),
     1427                                                                'thread_id'    => array(
     1428                                                                        'description' => __( 'Thread ID.', 'buddypress' ),
     1429                                                                        'type'        => 'integer',
     1430                                                                        'context'     => array( 'view', 'edit' ),
     1431                                                                        'readonly'    => true,
     1432                                                                ),
     1433                                                                'user_id'      => array(
     1434                                                                        'description' => __( 'The user ID of the recipient.', 'buddypress' ),
     1435                                                                        'type'        => 'integer',
     1436                                                                        'context'     => array( 'view', 'edit' ),
     1437                                                                        'readonly'    => true,
     1438                                                                ),
     1439                                                                'unread_count' => array(
     1440                                                                        'description' => __( 'The unread count for the recipient.', 'buddypress' ),
     1441                                                                        'type'        => 'integer',
     1442                                                                        'context'     => array( 'view', 'edit' ),
     1443                                                                        'readonly'    => true,
     1444                                                                ),
     1445                                                                'is_deleted'   => array(
     1446                                                                        'description' => __( 'Status of the recipient.', 'buddypress' ),
     1447                                                                        'type'        => 'boolean',
     1448                                                                        'context'     => array( 'view', 'edit' ),
     1449                                                                        'readonly'    => true,
     1450                                                                ),
     1451                                                                'sender_only'  => array(
     1452                                                                        'description' => __( 'If recipient is the only sender.', 'buddypress' ),
     1453                                                                        'type'        => 'boolean',
     1454                                                                        'context'     => array( 'view', 'edit' ),
     1455                                                                        'readonly'    => true,
     1456                                                                ),
     1457                                                                'name'         => array(
     1458                                                                        'description' => __( 'Name of the recipient.', 'buddypress' ),
     1459                                                                        'type'        => 'string',
     1460                                                                        'context'     => array( 'view', 'edit' ),
     1461                                                                        'readonly'    => true,
     1462                                                                ),
     1463                                                                'user_link'    => array(
     1464                                                                        'description' => __( 'The link of the recipient.', 'buddypress' ),
     1465                                                                        'type'        => 'string',
     1466                                                                        'context'     => array( 'view', 'edit' ),
     1467                                                                        'readonly'    => true,
     1468                                                                ),
     1469                                                        ),
    13871470                                                ),
    13881471                                        ),
    … …  
    13991482                                ),
    14001483                        );
     1484
     1485                        if ( true === buddypress()->avatar->show_avatars ) {
     1486                                $avatar_properties = array();
     1487
     1488                                $avatar_properties['full'] = array(
     1489                                        /* translators: 1: Full avatar width in pixels. 2: Full avatar height in pixels */
     1490                                        'description' => sprintf( __( 'Avatar URL with full image size (%1$d x %2$d pixels).', 'buddypress' ), number_format_i18n( bp_core_avatar_full_width() ), number_format_i18n( bp_core_avatar_full_height() ) ),
     1491                                        'type'        => 'string',
     1492                                        'format'      => 'uri',
     1493                                        'context'     => array( 'view', 'edit' ),
     1494                                );
     1495
     1496                                $avatar_properties['thumb'] = array(
     1497                                        /* translators: 1: Thumb avatar width in pixels. 2: Thumb avatar height in pixels */
     1498                                        'description' => sprintf( __( 'Avatar URL with thumb image size (%1$d x %2$d pixels).', 'buddypress' ), number_format_i18n( bp_core_avatar_thumb_width() ), number_format_i18n( bp_core_avatar_thumb_height() ) ),
     1499                                        'type'        => 'string',
     1500                                        'format'      => 'uri',
     1501                                        'context'     => array( 'view', 'edit' ),
     1502                                );
     1503
     1504                                $schema['properties']['recipients']['items']['properties']['avatar_urls'] = array(
     1505                                        'description' => __( 'Avatar URLs for the recipient.', 'buddypress' ),
     1506                                        'type'        => 'object',
     1507                                        'context'     => array( 'view', 'edit' ),
     1508                                        'readonly'    => true,
     1509                                        'properties'  => $avatar_properties,
     1510                                );
     1511                        }
     1512
     1513                        $this->schema = $schema;
    14011514                }
    14021515
Note: See TracChangeset for help on using the changeset viewer.