bp_get_the_message_star_action_link
Return the link or raw URL for starring or unstarring a message.
Description
(string) bp_get_the_message_star_action_link( (array) $args = array() );
Returns (string)
Parameters (1)
- 0. $args — Optional. (array) =>
array()
- Array of arguments.
Options
- user_id (int) =>
to the logged-in user ID
The user ID.
- thread_id (int) =>
0
The message thread ID. Default: 0. If not zero, this takes precedence over
$message_id
. - message_id (int) =>
0
The individual message ID. If on a single thread page, defaults to the current message ID in the message loop.
- url_only (bool) =>
null
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.
array( /** * The user ID. * * @type int * @default to the logged-in user ID */ 'user_id' => to the logged-in user ID, /** * The message thread ID. Default: 0. If not zero, this takes precedence over $message_id. * * @type int */ 'thread_id' => 0, /** * The individual message ID. If on a single thread page, defaults to the current message ID in the * message loop. * * @type int */ 'message_id' => 0, /** * Whether to return the URL only. If false, returns link with markup. Default: false. * * @type bool * @default null */ 'url_only' => null, /** * Link text for the 'unstar' action. Only applicable if $url_only is false. * * @type string * @default '' */ 'text_unstar' => '', /** * Link text for the 'star' action. Only applicable if $url_only is false. * * @type string * @default '' */ 'text_star' => '', /** * Link title for the 'unstar' action. Only applicable if $url_only is false. * * @type string * @default '' */ 'title_unstar' => '', /** * Link title for the 'star' action. Only applicable if $url_only is false. * * @type string * @default '' */ 'title_star' => '', /** * 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. * * @type string * @default '' */ 'title_unstar_thread' => '' );
…
- user_id (int) =>
Usage
if ( !function_exists( 'bp_get_the_message_star_action_link' ) ) { require_once '/bp-messages/bp-messages-star.php'; } // Array of arguments. $args = array( 'user_id' => to the logged-in user ID, 'thread_id' => 0, 'message_id' => 0, 'url_only' => null, 'text_unstar' => '', 'text_star' => '', 'title_unstar' => '', 'title_star' => '', 'title_unstar_thread' => '' ); // NOTICE! Understand what this does before running. $result = bp_get_the_message_star_action_link($args);
Defined (1)
The function is defined in the following location(s).
- /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 title="' . esc_attr( $title ) . '" class="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 . '"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r );
- }