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