Skip to:
Content

BuddyPress.org

Changeset 11153


Ignore:
Timestamp:
09/22/2016 01:28:23 AM (5 years ago)
Author:
boonebgorges
Message:

Accept a $user_id parameter in messages_delete_thread().

Previously, this stack assumed the logged-in user, which made it very
difficult to delete threads for arbitrary users without touching the
database directly.

Props jdgrimes.
Fixes #7235.

Location:
trunk/src/bp-messages
Files:
2 edited

Legend:

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

    r11022 r11153  
    237237
    238238/**
    239  * Delete message thread(s).
     239 * Deletes message thread(s) for a given user.
     240 *
     241 * Note that "deleting" a thread for a user means removing it from the user's
     242 * message boxes. A thread is not deleted from the database until it's been
     243 * "deleted" by all recipients.
     244 *
     245 * @since 2.7.0 The $user_id parameter was added. Previously the current user
     246 *              was always assumed.
    240247 *
    241248 * @param int|array $thread_ids Thread ID or array of thread IDs.
     249 * @param int       $user_id    ID of the user to delete the threads for. Defaults
     250 *                              to the current logged-in user.
    242251 * @return bool True on success, false on failure.
    243252 */
    244 function messages_delete_thread( $thread_ids ) {
     253function messages_delete_thread( $thread_ids, $user_id = 0 ) {
     254
     255    if ( empty( $user_id ) ) {
     256        $user_id = bp_loggedin_user_id();
     257    }
    245258
    246259    /**
     
    248261     *
    249262     * @since 1.5.0
     263     * @since 2.7.0 The $user_id parameter was added.
    250264     *
    251      * @param int|array Thread ID or array of thread IDs that were deleted.
     265     * @param int|array $thread_ids Thread ID or array of thread IDs to be deleted.
     266     * @param int       $user_id    ID of the user the threads are being deleted for.
    252267     */
    253     do_action( 'messages_before_delete_thread', $thread_ids );
     268    do_action( 'messages_before_delete_thread', $thread_ids, $user_id );
    254269
    255270    if ( is_array( $thread_ids ) ) {
    256271        $error = 0;
    257272        for ( $i = 0, $count = count( $thread_ids ); $i < $count; ++$i ) {
    258             if ( ! BP_Messages_Thread::delete( $thread_ids[$i] ) ) {
     273            if ( ! BP_Messages_Thread::delete( $thread_ids[$i], $user_id ) ) {
    259274                $error = 1;
    260275            }
     
    269284         *
    270285         * @since 1.0.0
     286         * @since 2.7.0 The $user_id parameter was added.
    271287         *
    272288         * @param int|array Thread ID or array of thread IDs that were deleted.
     289         * @param int       ID of the user that the threads were deleted for.
    273290         */
    274         do_action( 'messages_delete_thread', $thread_ids );
     291        do_action( 'messages_delete_thread', $thread_ids, $user_id );
    275292
    276293        return true;
    277294    } else {
    278         if ( ! BP_Messages_Thread::delete( $thread_ids ) ) {
     295        if ( ! BP_Messages_Thread::delete( $thread_ids, $user_id ) ) {
    279296            return false;
    280297        }
    281298
    282299        /** This action is documented in bp-messages/bp-messages-functions.php */
    283         do_action( 'messages_delete_thread', $thread_ids );
     300        do_action( 'messages_delete_thread', $thread_ids, $user_id );
    284301
    285302        return true;
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r11028 r11153  
    327327     *
    328328     * @since 1.0.0
     329     * @since 2.7.0 The $user_id parameter was added. Previously the current user
     330     *              was always assumed.
    329331     *
    330332     * @param int $thread_id The message thread ID.
     333     * @param int $user_id The ID of the user in the thread to mark messages as
     334     *                     deleted for. Defaults to the current logged-in user.
     335     *
    331336     * @return bool
    332337     */
    333     public static function delete( $thread_id = 0 ) {
     338    public static function delete( $thread_id = 0, $user_id = 0 ) {
    334339        global $wpdb;
    335340
    336341        $thread_id = (int) $thread_id;
     342        $user_id = (int) $user_id;
     343
     344        if ( empty( $user_id ) ) {
     345            $user_id = bp_loggedin_user_id();
     346        }
    337347
    338348        /**
     
    340350         *
    341351         * @since 2.2.0
     352         * @since 2.7.0 The $user_id parameter was added.
    342353         *
    343354         * @param int $thread_id ID of the thread being deleted.
     355         * @param int $user_id   ID of the user that the thread is being deleted for.
    344356         */
    345         do_action( 'bp_messages_thread_before_mark_delete', $thread_id );
     357        do_action( 'bp_messages_thread_before_mark_delete', $thread_id, $user_id );
    346358
    347359        $bp = buddypress();
    348360
    349361        // Mark messages as deleted
    350         //
    351         // @todo the reliance on bp_loggedin_user_id() sucks for plugins
    352         // refactor this method to accept a $user_id parameter.
    353         $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, bp_loggedin_user_id() ) );
     362        $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) );
    354363
    355364        // Get the message ids in order to pass to the action.
     
    399408         *
    400409         * @since 2.2.0
     410         * @since 2.7.0 The $user_id parameter was added.
    401411         *
    402412         * @param int   $thread_id   ID of the thread being deleted.
    403413         * @param array $message_ids IDs of messages being deleted.
     414         * @param int   $user_id     ID of the user the threads were deleted for.
    404415         */
    405         do_action( 'bp_messages_thread_after_delete', $thread_id, $message_ids );
     416        do_action( 'bp_messages_thread_after_delete', $thread_id, $message_ids, $user_id );
    406417
    407418        return true;
Note: See TracChangeset for help on using the changeset viewer.