bp_messages_star_set_action( array $args = array() )

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


Parameters Parameters

$args

(array) (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()


Top ↑

Return Return

(bool)


Source Source

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

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;
		}
	}
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.