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' => ''
);        


Usage

  1. if ( !function_exists( 'bp_get_the_message_star_action_link' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-messages/bp-messages-star.php'; 
  3.  
  4. // Array of arguments. 
  5. $args = array( 
  6. 'user_id' => to the logged-in user ID, 
  7. 'thread_id' => 0, 
  8. 'message_id' => 0, 
  9. 'url_only' => null, 
  10. 'text_unstar' => '', 
  11. 'text_star' => '', 
  12. 'title_unstar' => '', 
  13. 'title_star' => '', 
  14. 'title_unstar_thread' => '' 
  15. ); 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = bp_get_the_message_star_action_link($args); 
  19.  

Defined (1)

The function is defined in the following location(s).

/bp-messages/bp-messages-star.php  
  1. function bp_get_the_message_star_action_link( $args = array() ) { 
  2.  
  3. // Default user ID. 
  4. $user_id = bp_displayed_user_id() 
  5.  
  6. $r = bp_parse_args( $args, array( 
  7. 'user_id' => (int) $user_id,  
  8. 'thread_id' => 0,  
  9. 'message_id' => (int) bp_get_the_thread_message_id(),  
  10. 'url_only' => false,  
  11. 'text_unstar' => __( 'Unstar', buddypress ),  
  12. 'text_star' => __( 'Star', buddypress ),  
  13. 'title_unstar' => __( 'Starred', buddypress ),  
  14. 'title_star' => __( 'Not starred', buddypress ),  
  15. 'title_unstar_thread' => __( 'Remove all starred messages in this thread', buddypress ),  
  16. 'title_star_thread' => __( 'Star the first message in this thread', buddypress ),  
  17. ), 'messages_star_action_link' ); 
  18.  
  19. // Check user ID and determine base user URL. 
  20. switch ( $r['user_id'] ) { 
  21.  
  22. // Current user. 
  23. $user_domain = bp_loggedin_user_domain(); 
  24. break; 
  25.  
  26. // Displayed user. 
  27. $user_domain = bp_displayed_user_domain(); 
  28. break; 
  29.  
  30. // Empty or other. 
  31. default : 
  32. $user_domain = bp_core_get_user_domain( $r['user_id'] ); 
  33. break; 
  34.  
  35. // Bail if no user domain was calculated. 
  36. if ( empty( $user_domain ) ) { 
  37. return ''; 
  38.  
  39. // Define local variables. 
  40. $retval = $bulk_attr = ''; 
  41.  
  42. // Thread ID. 
  43. if ( (int) $r['thread_id'] > 0 ) { 
  44.  
  45. // See if we're in the loop. 
  46. if ( bp_get_message_thread_id() == $r['thread_id'] ) { 
  47.  
  48. // Grab all message ids. 
  49. $mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' ); 
  50.  
  51. // Make sure order is ASC. 
  52. // Order is DESC when used in the thread loop by default. 
  53. $mids = array_reverse( $mids ); 
  54.  
  55. // Pull up the thread. 
  56. } else { 
  57. $thread = new BP_Messages_Thread( $r['thread_id'] ); 
  58. $mids = wp_list_pluck( $thread->messages, 'id' ); 
  59.  
  60. $is_starred = false; 
  61. $message_id = 0; 
  62. foreach ( $mids as $mid ) { 
  63.  
  64. // Try to find the first msg that is starred in a thread. 
  65. if ( true === bp_messages_is_message_starred( $mid ) ) { 
  66. $is_starred = true; 
  67. $message_id = $mid; 
  68. break; 
  69.  
  70. // No star, so default to first message in thread. 
  71. if ( empty( $message_id ) ) { 
  72. $message_id = $mids[0]; 
  73.  
  74. $message_id = (int) $message_id; 
  75.  
  76. // Nonce. 
  77. $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 
  78.  
  79. if ( true === $is_starred ) { 
  80. $action = 'unstar'; 
  81. $bulk_attr = ' data-star-bulk="1"'; 
  82. $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/'; 
  83. } else { 
  84. $action = 'star'; 
  85. $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 
  86.  
  87. $title = $r["title_{$action}_thread"]; 
  88.  
  89. // Message ID. 
  90. } else { 
  91. $message_id = (int) $r['message_id']; 
  92. $is_starred = bp_messages_is_message_starred( $message_id ); 
  93. $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 
  94.  
  95. if ( true === $is_starred ) { 
  96. $action = 'unstar'; 
  97. $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/'; 
  98. } else { 
  99. $action = 'star'; 
  100. $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 
  101.  
  102. $title = $r["title_{$action}"]; 
  103.  
  104. /** 
  105. * Filters the star action URL for starring / unstarring a message. 
  106. * @since 2.3.0 
  107. * @param string $retval URL for starring / unstarring a message. 
  108. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 
  109. */ 
  110. if ( true === (bool) $r['url_only'] ) { 
  111. return $retval; 
  112.  
  113. /** 
  114. * Filters the star action link, including markup. 
  115. * @since 2.3.0 
  116. * @param string $retval Link for starring / unstarring a message, including markup. 
  117. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 
  118. */ 
  119. 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 );