bp_messages_star_set_action( array $args = array() )

Save or delete star message meta according to a message’s star status.

Description

Parameters

$args

(Optional) Array of arguments.

  • 'action'
    (string) The star action. Either 'star' or 'unstar'. Default: 'star'.
  • 'thread_id'
    (int) The message thread ID. Default: 0. If not zero, this takes precedence over $message_id.
  • 'message_id'
    (int) The indivudal message ID to star or unstar. Default: 0.
  • 'user_id'
    (int) The user ID. Defaults to the logged-in user ID.
  • 'bulk'
    (bool) Whether to mark all messages in a thread as a certain action. Only relevant when $action is 'unstar' at the moment. Default: false.

Default value: array()

Return

(bool)

Source

File: bp-messages/bp-messages-star.php

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function bp_messages_star_set_action( $args = array() ) {
    $r = wp_parse_args( $args, array(
        'action'     => 'star',
        'thread_id'  => 0,
        'message_id' => 0,
        'user_id'    => bp_displayed_user_id(),
        'bulk'       => false
    ) );
 
    // Set thread ID.
    if ( ! empty( $r['thread_id'] ) ) {
        $thread_id = (int) $r['thread_id'];
    } else {
        $thread_id = messages_get_message_thread_id( $r['message_id'] );
    }
    if ( empty( $thread_id ) ) {
        return false;
    }
 
    // Check if user has access to thread.
    if( ! messages_check_thread_access( $thread_id, $r['user_id'] ) ) {
        return false;
    }
 
    $is_starred = bp_messages_is_message_starred( $r['message_id'], $r['user_id'] );
 
    // Star.
    if ( 'star' == $r['action'] ) {
        if ( true === $is_starred ) {
            return true;
        } else {
            bp_messages_add_meta( $r['message_id'], 'starred_by_user', $r['user_id'] );
            return true;
        }
    // Unstar.
    } else {
        // Unstar one message.
        if ( false === $r['bulk'] ) {
            if ( false === $is_starred ) {
                return true;
            } else {
                bp_messages_delete_meta( $r['message_id'], 'starred_by_user', $r['user_id'] );
                return true;
            }
 
        // Unstar all messages in a thread.
        } else {
            $thread = new BP_Messages_Thread( $thread_id );
            $mids = wp_list_pluck( $thread->messages, 'id' );
 
            foreach ( $mids as $mid ) {
                if ( true === bp_messages_is_message_starred( $mid, $r['user_id'] ) ) {
                    bp_messages_delete_meta( $mid, 'starred_by_user', $r['user_id'] );
                }
            }
 
            return true;
        }
    }
}

Changelog

Changelog
Version Description
BuddyPress 2.3.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.