Skip to:
Content

BuddyPress.org

Ticket #7096: 7096.02.diff

File 7096.02.diff, 20.1 KB (added by dcavins, 8 years ago)

More complete code review.

  • src/bp-messages/bp-messages-actions.php

    diff --git src/bp-messages/bp-messages-actions.php src/bp-messages/bp-messages-actions.php
    index e145f85..eb77706 100644
    function messages_action_conversation() { 
    247247                bp_core_redirect( bp_displayed_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id . '/' );
    248248        }
    249249
    250         // Mark message read.
    251         messages_mark_thread_read( $thread_id );
     250        /*
     251         * Mark message read, but only run on the logged-in user's profile.
     252         * If an admin visits a thread, it shouldn't change the read status.
     253         */
     254        if ( bp_is_my_profile() ) {
     255                messages_mark_thread_read( $thread_id );
     256        }
    252257
    253258        /**
    254259         * Fires after processing a view request for a single message thread.
    function bp_messages_action_mark_read() { 
    319324        }
    320325
    321326        // Check access to the message and mark as read.
    322         if ( messages_check_thread_access( $id ) ) {
     327        if ( messages_check_thread_access( $id ) || bp_current_user_can( 'bp_moderate' ) ) {
    323328                messages_mark_thread_read( $id );
    324329                bp_core_add_message( __( 'Message marked as read.', 'buddypress' ) );
    325330        } else {
    function bp_messages_action_mark_unread() { 
    360365        }
    361366
    362367        // Check access to the message and mark unread.
    363         if ( messages_check_thread_access( $id ) ) {
     368        if ( messages_check_thread_access( $id ) || bp_current_user_can( 'bp_moderate' ) ) {
    364369                messages_mark_thread_unread( $id );
    365370                bp_core_add_message( __( 'Message marked unread.', 'buddypress' ) );
    366371        } else {
    function bp_messages_action_bulk_manage() { 
    404409
    405410        // Make sure the user has access to all notifications before managing them.
    406411        foreach ( $messages as $message ) {
    407                 if ( ! messages_check_thread_access( $message ) ) {
     412                if ( ! messages_check_thread_access( $message ) && ! bp_current_user_can( 'bp_moderate' ) ) {
    408413                        bp_core_add_message( __( 'There was a problem managing your messages.', 'buddypress' ), 'error' );
    409414                        bp_core_redirect( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' );
    410415                }
  • src/bp-messages/bp-messages-cache.php

    diff --git src/bp-messages/bp-messages-cache.php src/bp-messages/bp-messages-cache.php
    index 434c3a8..6883c96 100644
    add_action( 'messages_message_after_save', 'bp_messages_clear_cache_on_message_s 
    7474 *
    7575 * @param int|array $thread_ids If single thread, the thread ID.
    7676 *                              Otherwise, an array of thread IDs.
     77 * @param int       $user_id    ID of the user that the threads were deleted for.
    7778 */
    78 function bp_messages_clear_cache_on_message_delete( $thread_ids ) {
     79function bp_messages_clear_cache_on_message_delete( $thread_ids, $user_id ) {
    7980        // Delete thread and thread recipient cache.
    8081        foreach( (array) $thread_ids as $thread_id ) {
    8182                wp_cache_delete( $thread_id, 'bp_messages_threads' );
    function bp_messages_clear_cache_on_message_delete( $thread_ids ) { 
    8384        }
    8485
    8586        // Delete unread count for logged-in user.
    86         wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     87        wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    8788}
    88 add_action( 'messages_delete_thread', 'bp_messages_clear_cache_on_message_delete' );
     89add_action( 'messages_delete_thread', 'bp_messages_clear_cache_on_message_delete', 10, 2 );
    8990
    9091/**
    9192 * Invalidate cache for notices.
  • src/bp-messages/bp-messages-functions.php

    diff --git src/bp-messages/bp-messages-functions.php src/bp-messages/bp-messages-functions.php
    index 16e70b8..f78ddcf 100644
    function messages_send_notice( $subject, $message ) { 
    254254function messages_delete_thread( $thread_ids, $user_id = 0 ) {
    255255
    256256        if ( empty( $user_id ) ) {
    257                 $user_id = bp_loggedin_user_id();
     257                $user_id =
     258                        bp_displayed_user_id() ?
     259                        bp_displayed_user_id() :
     260                        bp_loggedin_user_id();
    258261        }
    259262
    260263        /**
    function messages_remove_callback_values() { 
    380383 * @return int
    381384 */
    382385function messages_get_unread_count( $user_id = 0 ) {
    383         if ( empty( $user_id ) ) {
    384                 $user_id = bp_loggedin_user_id();
    385         }
    386 
    387386        return BP_Messages_Thread::get_inbox_count( $user_id );
    388387}
    389388
  • src/bp-messages/bp-messages-notifications.php

    diff --git src/bp-messages/bp-messages-notifications.php src/bp-messages/bp-messages-notifications.php
    index 4a6b025..f6d2284 100644
    add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification' 
    171171function bp_messages_screen_conversation_mark_notifications() {
    172172        global $thread_template;
    173173
     174        /*
     175         * Only run on the logged-in user's profile.
     176         * If an admin visits a thread, it shouldn't change the read status.
     177         */
     178        if ( ! bp_is_my_profile() ) {
     179                return;
     180        }
     181
    174182        // Get unread PM notifications for the user.
    175183        $new_pm_notifications = BP_Notifications_Notification::get( array(
    176184                'user_id'           => bp_loggedin_user_id(),
  • src/bp-messages/bp-messages-star.php

    diff --git src/bp-messages/bp-messages-star.php src/bp-messages/bp-messages-star.php
    index 2b4e667..54e06c8 100644
    function bp_get_messages_starred_slug() { 
    4141 */
    4242function bp_messages_is_message_starred( $mid = 0, $user_id = 0 ) {
    4343        if ( empty( $user_id ) ) {
    44                 $user_id = bp_loggedin_user_id();
     44                $user_id = bp_displayed_user_id();
    4545        }
    4646
    4747        if ( empty( $mid ) ) {
    function bp_messages_star_set_action( $args = array() ) { 
    254254                'action'     => 'star',
    255255                'thread_id'  => 0,
    256256                'message_id' => 0,
    257                 'user_id'    => bp_loggedin_user_id(),
     257                'user_id'    => bp_displayed_user_id(),
    258258                'bulk'       => false
    259259        ) );
    260260
    function bp_messages_star_action_handler() { 
    397397        ) );
    398398
    399399        // Redirect back to previous screen.
    400         $redirect = wp_get_referer() ? wp_get_referer() : bp_loggedin_user_domain() . bp_get_messages_slug();
     400        $redirect = wp_get_referer() ? wp_get_referer() : bp_displayed_user_domain() . bp_get_messages_slug();
    401401        bp_core_redirect( $redirect );
    402402        die();
    403403}
  • src/bp-messages/bp-messages-template.php

    diff --git src/bp-messages/bp-messages-template.php src/bp-messages/bp-messages-template.php
    index c89d691..aa7de6e 100644
    function bp_has_message_threads( $args = array() ) { 
    5858
    5959        // User ID
    6060        // @todo displayed user for moderators that get this far?
    61         $user_id = bp_loggedin_user_id();
     61        $user_id = bp_displayed_user_id();
    6262
    6363        // Search Terms.
    6464        $search_terms = isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '';
    function bp_message_thread_view_link( $thread_id = 0 ) { 
    304304                 * @param string $value     Permalink of a particular thread.
    305305                 * @param int    $thread_id ID of the thread.
    306306                 */
    307                 return apply_filters( 'bp_get_message_thread_view_link', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id ), $thread_id );
     307                return apply_filters( 'bp_get_message_thread_view_link', trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id ), $thread_id );
    308308        }
    309309
    310310/**
    function bp_message_thread_delete_link() { 
    329329                 * @param string $value URL for deleting the current thread.
    330330                 * @param string $value Text indicating action being executed.
    331331                 */
    332                 return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/delete/' . $messages_template->thread->thread_id ), 'messages_delete_thread' ) );
     332                return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/delete/' . $messages_template->thread->thread_id ), 'messages_delete_thread' ) );
    333333        }
    334334
    335335/**
    function bp_the_message_thread_mark_unread_url() { 
    361361                );
    362362
    363363                // Base unread URL.
    364                 $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/unread' );
     364                $url = trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/unread' );
    365365
    366366                // Add the args to the URL.
    367367                $url = add_query_arg( $args, $url );
    function bp_the_message_thread_mark_read_url() { 
    408408                );
    409409
    410410                // Base read URL.
    411                 $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/read' );
     411                $url = trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/read' );
    412412
    413413                // Add the args to the URL.
    414414                $url = add_query_arg( $args, $url );
    function bp_message_thread_avatar( $args = '' ) { 
    696696
    697697/**
    698698 * Output the unread messages count for the current inbox.
     699 *
     700 * @since 2.6.x Added $user_id argument.
     701 *
     702 * @param int $user_id The user ID.
     703 *
     704 * @return int $unread_count Total inbox unread count for user.
    699705 */
    700 function bp_total_unread_messages_count() {
    701         echo bp_get_total_unread_messages_count();
     706function bp_total_unread_messages_count( $user_id = 0 ) {
     707        echo bp_get_total_unread_messages_count( $user_id );
    702708}
    703709        /**
    704710         * Get the unread messages count for the current inbox.
    705711         *
    706          * @return int
     712         * @since 2.6.x Added $user_id argument.
     713         *
     714         * @param int $user_id The user ID.
     715         *
     716         * @return int $unread_count Total inbox unread count for user.
    707717         */
    708         function bp_get_total_unread_messages_count() {
     718        function bp_get_total_unread_messages_count( $user_id = 0 ) {
    709719
    710720                /**
    711721                 * Filters the unread messages count for the current inbox.
    function bp_total_unread_messages_count() { 
    714724                 *
    715725                 * @param int $value Unread messages count for the current inbox.
    716726                 */
    717                 return apply_filters( 'bp_get_total_unread_messages_count', BP_Messages_Thread::get_inbox_count() );
     727                return apply_filters( 'bp_get_total_unread_messages_count', BP_Messages_Thread::get_inbox_count( $user_id ) );
    718728        }
    719729
    720730/**
    function bp_messages_form_action() { 
    824834                 *
    825835                 * @param string $value The form action.
    826836                 */
    827                 return apply_filters( 'bp_get_messages_form_action', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' . bp_action_variable( 0 ) ) );
     837                return apply_filters( 'bp_get_messages_form_action', trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' . bp_action_variable( 0 ) ) );
    828838        }
    829839
    830840/**
    function bp_message_notice_delete_link() { 
    11531163                 * @param string $value URL for deleting the current notice.
    11541164                 * @param string $value Text indicating action being executed.
    11551165                 */
    1156                 return apply_filters( 'bp_get_message_notice_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/delete/' . $messages_template->thread->id, 'messages_delete_thread' ) );
     1166                return apply_filters( 'bp_get_message_notice_delete_link', wp_nonce_url( bp_displayed_user_domain() . bp_get_messages_slug() . '/notices/delete/' . $messages_template->thread->id, 'messages_delete_thread' ) );
    11571167        }
    11581168
    11591169/**
    function bp_message_activate_deactivate_link() { 
    11711181                global $messages_template;
    11721182
    11731183                if ( 1 === (int) $messages_template->thread->is_active ) {
    1174                         $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/deactivate/' . $messages_template->thread->id ), 'messages_deactivate_notice' );
     1184                        $link = wp_nonce_url( trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/notices/deactivate/' . $messages_template->thread->id ), 'messages_deactivate_notice' );
    11751185                } else {
    1176                         $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/activate/' . $messages_template->thread->id ), 'messages_activate_notice' );
     1186                        $link = wp_nonce_url( trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() . '/notices/activate/' . $messages_template->thread->id ), 'messages_activate_notice' );
    11771187                }
    11781188
    11791189                /**
    function bp_the_thread_recipients_list() { 
    16341644                                }
    16351645
    16361646                                $recipient_links[] = $recipient_link;
     1647                        } else {
     1648                                $recipient_links[] = __( 'you', 'buddypress' );
    16371649                        }
    16381650                }
    16391651
     1652                // Convert to natural language.
     1653                $size   = count( $recipient_links );
     1654                $string = '';
     1655
     1656                $recipient_links[ $size - 1 ] = _x( 'and ', 'Joining word in a list of message recipients', 'buddypress' ) .  $recipient_links[ $size - 1 ];
     1657
     1658                if ( $size >= 3 ) {
     1659                        $recipient_links = implode( ', ', $recipient_links );
     1660                } else {
     1661                        $recipient_links = implode( ' ', $recipient_links );
     1662                }
     1663
    16401664                /**
    16411665                 * Filters the HTML links to the profiles of recipients in the current thread.
    16421666                 *
    function bp_the_thread_recipients_list() { 
    16441668                 *
    16451669                 * @param string $value Comma-separated list of recipient HTML links for current thread.
    16461670                 */
    1647                 return apply_filters( 'bp_get_the_thread_recipients_list', implode( ', ', $recipient_links ) );
     1671                return apply_filters( 'bp_get_the_thread_recipients_list', $recipient_links );
    16481672        }
    16491673
    16501674/**
    function bp_the_thread_delete_link() { 
    19191943                 * @param string $value URL for deleting the current thread.
    19201944                 * @param string $value Text indicating action being executed.
    19211945                 */
    1922                 return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox/delete/' . bp_get_the_thread_id(), 'messages_delete_thread' ) );
     1946                return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( bp_displayed_user_domain() . bp_get_messages_slug() . '/inbox/delete/' . bp_get_the_thread_id(), 'messages_delete_thread' ) );
    19231947        }
    19241948
    19251949/**
  • src/bp-messages/classes/class-bp-messages-component.php

    diff --git src/bp-messages/classes/class-bp-messages-component.php src/bp-messages/classes/class-bp-messages-component.php
    index baf5207..7049563 100644
    class BP_Messages_Component extends BP_Component { 
    145145
    146146                // Only grab count if we're on a user page and current user has access.
    147147                if ( bp_is_user() && bp_user_has_access() ) {
    148                         $count    = bp_get_total_unread_messages_count();
     148                        $count    = bp_get_total_unread_messages_count( bp_displayed_user_id() );
    149149                        $class    = ( 0 === $count ) ? 'no-count' : 'count';
    150150                        $nav_name = sprintf(
    151151                                /* translators: %s: Unread message count for the current user */
    class BP_Messages_Component extends BP_Component { 
    204204                        'user_has_access' => $access
    205205                );
    206206
    207                 $sub_nav[] = array(
    208                         'name'            => __( 'Compose', 'buddypress' ),
    209                         'slug'            => 'compose',
    210                         'parent_url'      => $messages_link,
    211                         'parent_slug'     => $slug,
    212                         'screen_function' => 'messages_screen_compose',
    213                         'position'        => 30,
    214                         'user_has_access' => $access
    215                 );
     207                // Show certain screens only if the current user is the displayed user.
     208                if ( bp_is_my_profile() ) {
    216209
    217                 if ( bp_current_user_can( 'bp_moderate' ) ) {
     210                        // Show "Compose" on the logged-in user's profile only.
    218211                        $sub_nav[] = array(
    219                                 'name'            => __( 'Notices', 'buddypress' ),
    220                                 'slug'            => 'notices',
     212                                'name'            => __( 'Compose', 'buddypress' ),
     213                                'slug'            => 'compose',
    221214                                'parent_url'      => $messages_link,
    222215                                'parent_slug'     => $slug,
    223                                 'screen_function' => 'messages_screen_notices',
    224                                 'position'        => 90,
    225                                 'user_has_access' => true
     216                                'screen_function' => 'messages_screen_compose',
     217                                'position'        => 30,
     218                                'user_has_access' => $access
    226219                        );
     220
     221                        /*
     222                         * Show "Notices" on the logged-in user's profile only
     223                         * and then only if the user can create notices.
     224                         */
     225                        if ( bp_current_user_can( 'bp_moderate' ) ) {
     226                                $sub_nav[] = array(
     227                                        'name'            => __( 'Notices', 'buddypress' ),
     228                                        'slug'            => 'notices',
     229                                        'parent_url'      => $messages_link,
     230                                        'parent_slug'     => $slug,
     231                                        'screen_function' => 'messages_screen_notices',
     232                                        'position'        => 90,
     233                                        'user_has_access' => true
     234                                );
     235                        }
    227236                }
    228237
    229238                parent::setup_nav( $main_nav, $sub_nav );
    class BP_Messages_Component extends BP_Component { 
    243252                        $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
    244253
    245254                        // Unread message count.
    246                         $count = messages_get_unread_count();
     255                        $count = messages_get_unread_count( bp_loggedin_user_id() );
    247256                        if ( !empty( $count ) ) {
    248257                                $title = sprintf(
    249258                                        /* translators: %s: Unread message count for the current user */
  • src/bp-messages/classes/class-bp-messages-thread.php

    diff --git src/bp-messages/classes/class-bp-messages-thread.php src/bp-messages/classes/class-bp-messages-thread.php
    index 7b5356d..c868d55 100644
    class BP_Messages_Thread { 
    144144                        $order = 'ASC';
    145145                }
    146146
     147                $user_id =
     148                        bp_displayed_user_id() ?
     149                        bp_displayed_user_id() :
     150                        bp_loggedin_user_id();
     151
    147152                // Merge $args with our defaults.
    148153                $r = wp_parse_args( $args, array(
    149                         'user_id'           => bp_loggedin_user_id(),
     154                        'user_id'           => $user_id,
    150155                        'update_meta_cache' => true
    151156                ) );
    152157
    class BP_Messages_Thread { 
    613618        public static function mark_as_read( $thread_id = 0 ) {
    614619                global $wpdb;
    615620
     621                $user_id =
     622                        bp_displayed_user_id() ?
     623                        bp_displayed_user_id() :
     624                        bp_loggedin_user_id();
     625
    616626                $bp     = buddypress();
    617                 $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id ) );
     627                $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) );
    618628
    619629                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    620                 wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     630                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    621631
    622632                /**
    623633                 * Fires when messages thread was marked as read.
    class BP_Messages_Thread { 
    643653        public static function mark_as_unread( $thread_id = 0 ) {
    644654                global $wpdb;
    645655
     656                $user_id =
     657                        bp_displayed_user_id() ?
     658                        bp_displayed_user_id() :
     659                        bp_loggedin_user_id();
     660
    646661                $bp     = buddypress();
    647                 $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id ) );
     662                $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) );
    648663
    649664                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    650                 wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     665                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    651666
    652667                /**
    653668                 * Fires when messages thread was marked as unread.
  • src/bp-templates/bp-legacy/buddypress/members/single/messages/messages-loop.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/single/messages/messages-loop.php src/bp-templates/bp-legacy/buddypress/members/single/messages/messages-loop.php
    index 4b37ead..148d5f3 100644
    do_action( 'bp_before_member_messages_loop' ); ?> 
    5050         */
    5151        do_action( 'bp_before_member_messages_threads' ); ?>
    5252
    53         <form action="<?php echo bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() ?>/bulk-manage/" method="post" id="messages-bulk-management">
     53        <form action="<?php echo bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() ?>/bulk-manage/" method="post" id="messages-bulk-management">
    5454
    5555                <table id="message-threads" class="messages-notices">
    5656
  • src/bp-templates/bp-legacy/buddypress/members/single/messages/single.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/single/messages/single.php src/bp-templates/bp-legacy/buddypress/members/single/messages/single.php
    index 1b31bbe..deae005 100644
     
    3535
    3636                                <?php else : ?>
    3737
    38                                         <?php printf( __( 'Conversation between %s and you.', 'buddypress' ), bp_get_thread_recipients_list() ); ?>
     38                                        <?php printf( __( 'Conversation between %s.', 'buddypress' ), bp_get_thread_recipients_list() ); ?>
    3939
    4040                                <?php endif; ?>
    4141