[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Functions related to starring private messages. 4 * 5 * @package BuddyPress 6 * @subpackage MessagesStar 7 * @since 2.3.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** UTILITY **************************************************************/ 14 15 /** 16 * Return the starred messages slug. Defaults to 'starred'. 17 * 18 * @since 2.3.0 19 * 20 * @return string 21 */ 22 function bp_get_messages_starred_slug() { 23 /** 24 * Filters the starred message slug. 25 * 26 * @since 2.3.0 27 * 28 * @param string 29 */ 30 return sanitize_title( apply_filters( 'bp_get_messages_starred_slug', 'starred' ) ); 31 } 32 33 /** 34 * Function to determine if a message ID is starred. 35 * 36 * @since 2.3.0 37 * 38 * @param int $mid The message ID. Please note that this isn't the message thread ID. 39 * @param int $user_id The user ID. 40 * @return bool 41 */ 42 function bp_messages_is_message_starred( $mid = 0, $user_id = 0 ) { 43 if ( empty( $user_id ) ) { 44 $user_id = bp_displayed_user_id(); 45 } 46 47 if ( empty( $mid ) ) { 48 return false; 49 } 50 51 $starred = array_flip( (array) bp_messages_get_meta( $mid, 'starred_by_user', false ) ); 52 53 if ( isset( $starred[$user_id] ) ) { 54 return true; 55 } else { 56 return false; 57 } 58 } 59 60 /** 61 * Output the link or raw URL for starring or unstarring a message. 62 * 63 * @since 2.3.0 64 * 65 * @param array $args See bp_get_the_message_star_action_link() for full documentation. 66 */ 67 function bp_the_message_star_action_link( $args = array() ) { 68 echo bp_get_the_message_star_action_link( $args ); 69 } 70 /** 71 * Return the link or raw URL for starring or unstarring a message. 72 * 73 * @since 2.3.0 74 * 75 * @param array $args { 76 * Array of arguments. 77 * @type int $user_id The user ID. Defaults to the logged-in user ID. 78 * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over 79 * $message_id. 80 * @type int $message_id The individual message ID. If on a single thread page, defaults to the 81 * current message ID in the message loop. 82 * @type bool $url_only Whether to return the URL only. If false, returns link with markup. 83 * Default: false. 84 * @type string $text_unstar Link text for the 'unstar' action. Only applicable if $url_only is false. 85 * @type string $text_star Link text for the 'star' action. Only applicable if $url_only is false. 86 * @type string $title_unstar Link title for the 'unstar' action. Only applicable if $url_only is false. 87 * @type string $title_star Link title for the 'star' action. Only applicable if $url_only is false. 88 * @type string $title_unstar_thread Link title for the 'unstar' action when displayed in a thread loop. 89 * Only applicable if $message_id is set and if $url_only is false. 90 * @type string $title_star_thread Link title for the 'star' action when displayed in a thread loop. 91 * Only applicable if $message_id is set and if $url_only is false. 92 * } 93 * @return string 94 */ 95 function bp_get_the_message_star_action_link( $args = array() ) { 96 97 // Default user ID. 98 $user_id = bp_displayed_user_id() 99 ? bp_displayed_user_id() 100 : bp_loggedin_user_id(); 101 102 $r = bp_parse_args( $args, array( 103 'user_id' => (int) $user_id, 104 'thread_id' => 0, 105 'message_id' => (int) bp_get_the_thread_message_id(), 106 'url_only' => false, 107 'text_unstar' => __( 'Unstar', 'buddypress' ), 108 'text_star' => __( 'Star', 'buddypress' ), 109 'title_unstar' => __( 'Starred', 'buddypress' ), 110 'title_star' => __( 'Not starred', 'buddypress' ), 111 'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ), 112 'title_star_thread' => __( 'Star the first message in this thread', 'buddypress' ), 113 ), 'messages_star_action_link' ); 114 115 // Check user ID and determine base user URL. 116 switch ( $r['user_id'] ) { 117 118 // Current user. 119 case bp_loggedin_user_id() : 120 $user_domain = bp_loggedin_user_domain(); 121 break; 122 123 // Displayed user. 124 case bp_displayed_user_id() : 125 $user_domain = bp_displayed_user_domain(); 126 break; 127 128 // Empty or other. 129 default : 130 $user_domain = bp_core_get_user_domain( $r['user_id'] ); 131 break; 132 } 133 134 // Bail if no user domain was calculated. 135 if ( empty( $user_domain ) ) { 136 return ''; 137 } 138 139 // Define local variables. 140 $retval = $bulk_attr = ''; 141 142 // Thread ID. 143 if ( (int) $r['thread_id'] > 0 ) { 144 145 // See if we're in the loop. 146 if ( bp_get_message_thread_id() == $r['thread_id'] ) { 147 148 // Grab all message ids. 149 $mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' ); 150 151 // Make sure order is ASC. 152 // Order is DESC when used in the thread loop by default. 153 $mids = array_reverse( $mids ); 154 155 // Pull up the thread. 156 } else { 157 $thread = new BP_Messages_Thread( $r['thread_id'] ); 158 $mids = wp_list_pluck( $thread->messages, 'id' ); 159 } 160 161 $is_starred = false; 162 $message_id = 0; 163 foreach ( $mids as $mid ) { 164 165 // Try to find the first msg that is starred in a thread. 166 if ( true === bp_messages_is_message_starred( $mid ) ) { 167 $is_starred = true; 168 $message_id = $mid; 169 break; 170 } 171 } 172 173 // No star, so default to first message in thread. 174 if ( empty( $message_id ) ) { 175 $message_id = $mids[0]; 176 } 177 178 $message_id = (int) $message_id; 179 180 // Nonce. 181 $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 182 183 if ( true === $is_starred ) { 184 $action = 'unstar'; 185 $bulk_attr = ' data-star-bulk="1"'; 186 $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/'; 187 } else { 188 $action = 'star'; 189 $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 190 } 191 192 $title = $r["title_{$action}_thread"]; 193 194 // Message ID. 195 } else { 196 $message_id = (int) $r['message_id']; 197 $is_starred = bp_messages_is_message_starred( $message_id ); 198 $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 199 200 if ( true === $is_starred ) { 201 $action = 'unstar'; 202 $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/'; 203 } else { 204 $action = 'star'; 205 $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 206 } 207 208 $title = $r["title_{$action}"]; 209 } 210 211 /** 212 * Filters the star action URL for starring / unstarring a message. 213 * 214 * @since 2.3.0 215 * 216 * @param string $retval URL for starring / unstarring a message. 217 * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 218 */ 219 $retval = esc_url( apply_filters( 'bp_get_the_message_star_action_urlonly', $retval, $r ) ); 220 if ( true === (bool) $r['url_only'] ) { 221 return $retval; 222 } 223 224 /** 225 * Filters the star action link, including markup. 226 * 227 * @since 2.3.0 228 * 229 * @param string $retval Link for starring / unstarring a message, including markup. 230 * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 231 */ 232 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 ); 233 } 234 235 /** 236 * Save or delete star message meta according to a message's star status. 237 * 238 * @since 2.3.0 239 * 240 * @param array $args { 241 * Array of arguments. 242 * @type string $action The star action. Either 'star' or 'unstar'. Default: 'star'. 243 * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over 244 * $message_id. 245 * @type int $message_id The indivudal message ID to star or unstar. Default: 0. 246 * @type int $user_id The user ID. Defaults to the logged-in user ID. 247 * @type bool $bulk Whether to mark all messages in a thread as a certain action. Only relevant 248 * when $action is 'unstar' at the moment. Default: false. 249 * } 250 * @return bool 251 */ 252 function bp_messages_star_set_action( $args = array() ) { 253 $r = wp_parse_args( $args, array( 254 'action' => 'star', 255 'thread_id' => 0, 256 'message_id' => 0, 257 'user_id' => bp_displayed_user_id(), 258 'bulk' => false 259 ) ); 260 261 // Set thread ID. 262 if ( ! empty( $r['thread_id'] ) ) { 263 $thread_id = (int) $r['thread_id']; 264 } else { 265 $thread_id = messages_get_message_thread_id( $r['message_id'] ); 266 } 267 if ( empty( $thread_id ) ) { 268 return false; 269 } 270 271 // Check if user has access to thread. 272 if( ! messages_check_thread_access( $thread_id, $r['user_id'] ) ) { 273 return false; 274 } 275 276 $is_starred = bp_messages_is_message_starred( $r['message_id'], $r['user_id'] ); 277 278 // Star. 279 if ( 'star' == $r['action'] ) { 280 if ( true === $is_starred ) { 281 return true; 282 } else { 283 bp_messages_add_meta( $r['message_id'], 'starred_by_user', $r['user_id'] ); 284 return true; 285 } 286 // Unstar. 287 } else { 288 // Unstar one message. 289 if ( false === $r['bulk'] ) { 290 if ( false === $is_starred ) { 291 return true; 292 } else { 293 bp_messages_delete_meta( $r['message_id'], 'starred_by_user', $r['user_id'] ); 294 return true; 295 } 296 297 // Unstar all messages in a thread. 298 } else { 299 $thread = new BP_Messages_Thread( $thread_id ); 300 $mids = wp_list_pluck( $thread->messages, 'id' ); 301 302 foreach ( $mids as $mid ) { 303 if ( true === bp_messages_is_message_starred( $mid, $r['user_id'] ) ) { 304 bp_messages_delete_meta( $mid, 'starred_by_user', $r['user_id'] ); 305 } 306 } 307 308 return true; 309 } 310 } 311 } 312 313 /** HOOKS ****************************************************************/ 314 315 /** 316 * Enqueues the dashicons font. 317 * 318 * The dashicons font is used for the star / unstar icon. 319 * 320 * @since 2.3.0 321 */ 322 function bp_messages_star_enqueue_scripts() { 323 if ( ! bp_is_user_messages() ) { 324 return; 325 } 326 327 wp_enqueue_style( 'dashicons' ); 328 } 329 add_action( 'bp_enqueue_scripts', 'bp_messages_star_enqueue_scripts' ); 330 331 /** 332 * Add the "Add star" and "Remove star" options to the bulk management list. 333 * 334 * @since 2.3.0 335 */ 336 function bp_messages_star_bulk_management_dropdown() { 337 ?> 338 339 <option value="star"><?php _e( 'Add star', 'buddypress' ); ?></option> 340 <option value="unstar"><?php _e( 'Remove star', 'buddypress' ); ?></option> 341 342 <?php 343 } 344 add_action( 'bp_messages_bulk_management_dropdown', 'bp_messages_star_bulk_management_dropdown', 1 ); 345 346 /** 347 * Add CSS class for the current message depending on starred status. 348 * 349 * @since 2.3.0 350 * 351 * @param array $retval Current CSS classes. 352 * @return array 353 */ 354 function bp_messages_star_message_css_class( $retval = array() ) { 355 if ( true === bp_messages_is_message_starred( bp_get_the_thread_message_id() ) ) { 356 $status = 'starred'; 357 } else { 358 $status = 'not-starred'; 359 } 360 361 // Add css class based on star status for the current message. 362 $retval[] = "message-{$status}"; 363 364 return $retval; 365 } 366 add_filter( 'bp_get_the_thread_message_css_class', 'bp_messages_star_message_css_class' ); 367 368 /** 369 * Filter message threads by those starred by the logged-in user. 370 * 371 * @since 2.3.0 372 * 373 * @param array $r Current message thread arguments. 374 * @return array $r Array of starred message threads. 375 */ 376 function bp_messages_filter_starred_message_threads( $r = array() ) { 377 $r['box'] = 'starred'; 378 $r['meta_query'] = array( array( 379 'key' => 'starred_by_user', 380 'value' => $r['user_id'] 381 ) ); 382 383 return $r; 384 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jan 17 01:01:36 2021 | Cross-referenced by PHPXref 0.7.1 |