bp_get_the_message_star_action_link( array $args = array() )

Return the link or raw URL for starring or unstarring a message.


Parameters Parameters

$args

(array) (Optional) Array of arguments.

  • 'user_id'
    (int) The user ID. Defaults to the logged-in user ID.
  • 'thread_id'
    (int) The message thread ID. Default: 0. If not zero, this takes precedence over $message_id.
  • 'message_id'
    (int) The individual message ID. If on a single thread page, defaults to the current message ID in the message loop.
  • 'url_only'
    (bool) Whether to return the URL only. If false, returns link with markup. Default: false.
  • 'text_unstar'
    (string) Link text for the 'unstar' action. Only applicable if $url_only is false.
  • 'text_star'
    (string) Link text for the 'star' action. Only applicable if $url_only is false.
  • 'title_unstar'
    (string) Link title for the 'unstar' action. Only applicable if $url_only is false.
  • 'title_star'
    (string) Link title for the 'star' action. Only applicable if $url_only is false.
  • 'title_unstar_thread'
    (string) Link title for the 'unstar' action when displayed in a thread loop. Only applicable if $message_id is set and if $url_only is false.
  • 'title_star_thread'
    (string) Link title for the 'star' action when displayed in a thread loop. Only applicable if $message_id is set and if $url_only is false.

Default value: array()


Top ↑

Return Return

(string)


Source Source

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

	function bp_get_the_message_star_action_link( $args = array() ) {

		// Default user ID.
		$user_id = bp_displayed_user_id()
			? bp_displayed_user_id()
			: bp_loggedin_user_id();

		$r = bp_parse_args( $args, array(
			'user_id'             => (int) $user_id,
			'thread_id'           => 0,
			'message_id'          => (int) bp_get_the_thread_message_id(),
			'url_only'            => false,
			'text_unstar'         => __( 'Unstar',      'buddypress' ),
			'text_star'           => __( 'Star',        'buddypress' ),
			'title_unstar'        => __( 'Starred',     'buddypress' ),
			'title_star'          => __( 'Not starred', 'buddypress' ),
			'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
			'title_star_thread'   => __( 'Star the first message in this thread',      'buddypress' ),
		), 'messages_star_action_link' );

		// Check user ID and determine base user URL.
		switch ( $r['user_id'] ) {

			// Current user.
			case bp_loggedin_user_id() :
				$user_domain = bp_loggedin_user_domain();
				break;

			// Displayed user.
			case bp_displayed_user_id() :
				$user_domain = bp_displayed_user_domain();
				break;

			// Empty or other.
			default :
				$user_domain = bp_core_get_user_domain( $r['user_id'] );
				break;
		}

		// Bail if no user domain was calculated.
		if ( empty( $user_domain ) ) {
			return '';
		}

		// Define local variables.
		$retval = $bulk_attr = '';

		// Thread ID.
		if ( (int) $r['thread_id'] > 0 ) {

			// See if we're in the loop.
			if ( bp_get_message_thread_id() == $r['thread_id'] ) {

				// Grab all message ids.
				$mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' );

				// Make sure order is ASC.
				// Order is DESC when used in the thread loop by default.
				$mids = array_reverse( $mids );

			// Pull up the thread.
			} else {
				$thread = new BP_Messages_Thread( $r['thread_id'] );
				$mids   = wp_list_pluck( $thread->messages, 'id' );
			}

			$is_starred = false;
			$message_id = 0;
			foreach ( $mids as $mid ) {

				// Try to find the first msg that is starred in a thread.
				if ( true === bp_messages_is_message_starred( $mid ) ) {
					$is_starred = true;
					$message_id = $mid;
					break;
				}
			}

			// No star, so default to first message in thread.
			if ( empty( $message_id ) ) {
				$message_id = $mids[0];
			}

			$message_id = (int) $message_id;

			// Nonce.
			$nonce = wp_create_nonce( "bp-messages-star-{$message_id}" );

			if ( true === $is_starred ) {
				$action    = 'unstar';
				$bulk_attr = ' data-star-bulk="1"';
				$retval    = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/';
			} else {
				$action    = 'star';
				$retval    = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
			}

			$title = $r["title_{$action}_thread"];

		// Message ID.
		} else {
			$message_id = (int) $r['message_id'];
			$is_starred = bp_messages_is_message_starred( $message_id );
			$nonce      = wp_create_nonce( "bp-messages-star-{$message_id}" );

			if ( true === $is_starred ) {
				$action = 'unstar';
				$retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/';
			} else {
				$action = 'star';
				$retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
			}

			$title = $r["title_{$action}"];
		}

		/**
		 * Filters the star action URL for starring / unstarring a message.
		 *
		 * @since 2.3.0
		 *
		 * @param string $retval URL for starring / unstarring a message.
		 * @param array  $r      Parsed link arguments. See $args in bp_get_the_message_star_action_link().
		 */
		$retval = esc_url( apply_filters( 'bp_get_the_message_star_action_urlonly', $retval, $r ) );
		if ( true === (bool) $r['url_only'] ) {
			return $retval;
		}

		/**
		 * Filters the star action link, including markup.
		 *
		 * @since 2.3.0
		 *
		 * @param string $retval Link for starring / unstarring a message, including markup.
		 * @param array  $r      Parsed link arguments. See $args in bp_get_the_message_star_action_link().
		 */
		return apply_filters( 'bp_get_the_message_star_action_link', '<a data-bp-tooltip="' . esc_attr( $title ) . '" class="bp-tooltip message-action-' . esc_attr( $action ) . '" data-star-status="' . esc_attr( $action ) .'" data-star-nonce="' . esc_attr( $nonce ) . '"' . $bulk_attr . ' data-message-id="' . esc_attr( (int) $message_id ) . '" href="' . $retval . '" role="button" aria-pressed="false"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r );
	}


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.