/bp-messages/bp-messages-star.php

  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. * Function to determine if a message ID is starred. 
  34. * 
  35. * @since 2.3.0 
  36. * 
  37. * @param int $mid The message ID. Please note that this isn't the message thread ID. 
  38. * @param int $user_id The user ID. 
  39. * @return bool 
  40. */ 
  41. function bp_messages_is_message_starred( $mid = 0, $user_id = 0 ) { 
  42. if ( empty( $user_id ) ) { 
  43. $user_id = bp_loggedin_user_id(); 
  44.  
  45. if ( empty( $mid ) ) { 
  46. return false; 
  47.  
  48. $starred = array_flip( (array) bp_messages_get_meta( $mid, 'starred_by_user', false ) ); 
  49.  
  50. if ( isset( $starred[$user_id] ) ) { 
  51. return true; 
  52. } else { 
  53. return false; 
  54.  
  55. /** 
  56. * Output the link or raw URL for starring or unstarring a message. 
  57. * 
  58. * @since 2.3.0 
  59. * 
  60. * @param array $args See bp_get_the_message_star_action_link() for full documentation. 
  61. */ 
  62. function bp_the_message_star_action_link( $args = array() ) { 
  63. echo bp_get_the_message_star_action_link( $args ); 
  64. /** 
  65. * Return the link or raw URL for starring or unstarring a message. 
  66. * 
  67. * @since 2.3.0 
  68. * 
  69. * @param array $args { 
  70. * Array of arguments. 
  71. * @type int $user_id The user ID. Defaults to the logged-in user ID. 
  72. * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over 
  73. * $message_id. 
  74. * @type int $message_id The individual message ID. If on a single thread page, defaults to the 
  75. * current message ID in the message loop. 
  76. * @type bool $url_only Whether to return the URL only. If false, returns link with markup. 
  77. * Default: false. 
  78. * @type string $text_unstar Link text for the 'unstar' action. Only applicable if $url_only is false. 
  79. * @type string $text_star Link text for the 'star' action. Only applicable if $url_only is false. 
  80. * @type string $title_unstar Link title for the 'unstar' action. Only applicable if $url_only is false. 
  81. * @type string $title_star Link title for the 'star' action. Only applicable if $url_only is false. 
  82. * @type string $title_unstar_thread Link title for the 'unstar' action when displayed in a thread loop. 
  83. * Only applicable if $message_id is set and if $url_only is false. 
  84. * @type string $title_star_thread Link title for the 'star' action when displayed in a thread loop. 
  85. * Only applicable if $message_id is set and if $url_only is false. 
  86. * } 
  87. * @return string 
  88. */ 
  89. function bp_get_the_message_star_action_link( $args = array() ) { 
  90.  
  91. // Default user ID. 
  92. $user_id = bp_displayed_user_id() 
  93. ? bp_displayed_user_id() 
  94. : bp_loggedin_user_id(); 
  95.  
  96. $r = bp_parse_args( $args, array( 
  97. 'user_id' => (int) $user_id,  
  98. 'thread_id' => 0,  
  99. 'message_id' => (int) bp_get_the_thread_message_id(),  
  100. 'url_only' => false,  
  101. 'text_unstar' => __( 'Unstar', 'buddypress' ),  
  102. 'text_star' => __( 'Star', 'buddypress' ),  
  103. 'title_unstar' => __( 'Starred', 'buddypress' ),  
  104. 'title_star' => __( 'Not starred', 'buddypress' ),  
  105. 'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),  
  106. 'title_star_thread' => __( 'Star the first message in this thread', 'buddypress' ),  
  107. ), 'messages_star_action_link' ); 
  108.  
  109. // Check user ID and determine base user URL. 
  110. switch ( $r['user_id'] ) { 
  111.  
  112. // Current user. 
  113. case bp_loggedin_user_id() : 
  114. $user_domain = bp_loggedin_user_domain(); 
  115. break; 
  116.  
  117. // Displayed user. 
  118. case bp_displayed_user_id() : 
  119. $user_domain = bp_displayed_user_domain(); 
  120. break; 
  121.  
  122. // Empty or other. 
  123. default : 
  124. $user_domain = bp_core_get_user_domain( $r['user_id'] ); 
  125. break; 
  126.  
  127. // Bail if no user domain was calculated. 
  128. if ( empty( $user_domain ) ) { 
  129. return ''; 
  130.  
  131. // Define local variables. 
  132. $retval = $bulk_attr = ''; 
  133.  
  134. // Thread ID. 
  135. if ( (int) $r['thread_id'] > 0 ) { 
  136.  
  137. // See if we're in the loop. 
  138. if ( bp_get_message_thread_id() == $r['thread_id'] ) { 
  139.  
  140. // Grab all message ids. 
  141. $mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' ); 
  142.  
  143. // Make sure order is ASC. 
  144. // Order is DESC when used in the thread loop by default. 
  145. $mids = array_reverse( $mids ); 
  146.  
  147. // Pull up the thread. 
  148. } else { 
  149. $thread = new BP_Messages_Thread( $r['thread_id'] ); 
  150. $mids = wp_list_pluck( $thread->messages, 'id' ); 
  151.  
  152. $is_starred = false; 
  153. $message_id = 0; 
  154. foreach ( $mids as $mid ) { 
  155.  
  156. // Try to find the first msg that is starred in a thread. 
  157. if ( true === bp_messages_is_message_starred( $mid ) ) { 
  158. $is_starred = true; 
  159. $message_id = $mid; 
  160. break; 
  161.  
  162. // No star, so default to first message in thread. 
  163. if ( empty( $message_id ) ) { 
  164. $message_id = $mids[0]; 
  165.  
  166. $message_id = (int) $message_id; 
  167.  
  168. // Nonce. 
  169. $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 
  170.  
  171. if ( true === $is_starred ) { 
  172. $action = 'unstar'; 
  173. $bulk_attr = ' data-star-bulk="1"'; 
  174. $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/'; 
  175. } else { 
  176. $action = 'star'; 
  177. $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 
  178.  
  179. $title = $r["title_{$action}_thread"]; 
  180.  
  181. // Message ID. 
  182. } else { 
  183. $message_id = (int) $r['message_id']; 
  184. $is_starred = bp_messages_is_message_starred( $message_id ); 
  185. $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" ); 
  186.  
  187. if ( true === $is_starred ) { 
  188. $action = 'unstar'; 
  189. $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/'; 
  190. } else { 
  191. $action = 'star'; 
  192. $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; 
  193.  
  194. $title = $r["title_{$action}"]; 
  195.  
  196. /** 
  197. * Filters the star action URL for starring / unstarring a message. 
  198. * 
  199. * @since 2.3.0 
  200. * 
  201. * @param string $retval URL for starring / unstarring a message. 
  202. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 
  203. */ 
  204. $retval = esc_url( apply_filters( 'bp_get_the_message_star_action_urlonly', $retval, $r ) ); 
  205. if ( true === (bool) $r['url_only'] ) { 
  206. return $retval; 
  207.  
  208. /** 
  209. * Filters the star action link, including markup. 
  210. * 
  211. * @since 2.3.0 
  212. * 
  213. * @param string $retval Link for starring / unstarring a message, including markup. 
  214. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). 
  215. */ 
  216. 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 ); 
  217.  
  218. /** 
  219. * Save or delete star message meta according to a message's star status. 
  220. * 
  221. * @since 2.3.0 
  222. * 
  223. * @param array $args { 
  224. * Array of arguments. 
  225. * @type string $action The star action. Either 'star' or 'unstar'. Default: 'star'. 
  226. * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over 
  227. * $message_id. 
  228. * @type int $message_id The indivudal message ID to star or unstar. Default: 0. 
  229. * @type int $user_id The user ID. Defaults to the logged-in user ID. 
  230. * @type bool $bulk Whether to mark all messages in a thread as a certain action. Only relevant 
  231. * when $action is 'unstar' at the moment. Default: false. 
  232. * } 
  233. * @return bool 
  234. */ 
  235. function bp_messages_star_set_action( $args = array() ) { 
  236. $r = wp_parse_args( $args, array( 
  237. 'action' => 'star',  
  238. 'thread_id' => 0,  
  239. 'message_id' => 0,  
  240. 'user_id' => bp_loggedin_user_id(),  
  241. 'bulk' => false 
  242. ) ); 
  243.  
  244. // Set thread ID. 
  245. if ( ! empty( $r['thread_id'] ) ) { 
  246. $thread_id = (int) $r['thread_id']; 
  247. } else { 
  248. $thread_id = messages_get_message_thread_id( $r['message_id'] ); 
  249. if ( empty( $thread_id ) ) { 
  250. return false; 
  251.  
  252. // Check if user has access to thread. 
  253. if( ! messages_check_thread_access( $thread_id, $r['user_id'] ) ) { 
  254. return false; 
  255.  
  256. $is_starred = bp_messages_is_message_starred( $r['message_id'], $r['user_id'] ); 
  257.  
  258. // Star. 
  259. if ( 'star' == $r['action'] ) { 
  260. if ( true === $is_starred ) { 
  261. return true; 
  262. } else { 
  263. bp_messages_add_meta( $r['message_id'], 'starred_by_user', $r['user_id'] ); 
  264. return true; 
  265. // Unstar. 
  266. } else { 
  267. // Unstar one message. 
  268. if ( false === $r['bulk'] ) { 
  269. if ( false === $is_starred ) { 
  270. return true; 
  271. } else { 
  272. bp_messages_delete_meta( $r['message_id'], 'starred_by_user', $r['user_id'] ); 
  273. return true; 
  274.  
  275. // Unstar all messages in a thread. 
  276. } else { 
  277. $thread = new BP_Messages_Thread( $thread_id ); 
  278. $mids = wp_list_pluck( $thread->messages, 'id' ); 
  279.  
  280. foreach ( $mids as $mid ) { 
  281. if ( true === bp_messages_is_message_starred( $mid, $r['user_id'] ) ) { 
  282. bp_messages_delete_meta( $mid, 'starred_by_user', $r['user_id'] ); 
  283.  
  284. return true; 
  285.  
  286. /** SCREENS **************************************************************/ 
  287.  
  288. /** 
  289. * Screen handler to display a user's "Starred" private messages page. 
  290. * 
  291. * @since 2.3.0 
  292. */ 
  293. function bp_messages_star_screen() { 
  294. add_action( 'bp_template_content', 'bp_messages_star_content' ); 
  295.  
  296. /** 
  297. * Fires right before the loading of the "Starred" messages box. 
  298. * 
  299. * @since 2.3.0 
  300. */ 
  301. do_action( 'bp_messages_screen_star' ); 
  302.  
  303. bp_core_load_template( 'members/single/plugins' ); 
  304.  
  305. /** 
  306. * Screen content callback to display a user's "Starred" messages page. 
  307. * 
  308. * @since 2.3.0 
  309. */ 
  310. function bp_messages_star_content() { 
  311. // Add our message thread filter. 
  312. add_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' ); 
  313.  
  314. // Load the message loop template part. 
  315. bp_get_template_part( 'members/single/messages/messages-loop' ); 
  316.  
  317. // Remove our filter. 
  318. remove_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' ); 
  319.  
  320. /** 
  321. * Filter message threads by those starred by the logged-in user. 
  322. * 
  323. * @since 2.3.0 
  324. * 
  325. * @param array $r Current message thread arguments. 
  326. * @return array $r Array of starred message threads. 
  327. */ 
  328. function bp_messages_filter_starred_message_threads( $r = array() ) { 
  329. $r['box'] = 'starred'; 
  330. $r['meta_query'] = array( array( 
  331. 'key' => 'starred_by_user',  
  332. 'value' => $r['user_id'] 
  333. ) ); 
  334.  
  335. return $r; 
  336.  
  337. /** ACTIONS **************************************************************/ 
  338.  
  339. /** 
  340. * Action handler to set a message's star status for those not using JS. 
  341. * 
  342. * @since 2.3.0 
  343. */ 
  344. function bp_messages_star_action_handler() { 
  345. if ( ! bp_is_user_messages() ) { 
  346. return; 
  347.  
  348. if ( false === ( bp_is_current_action( 'unstar' ) || bp_is_current_action( 'star' ) ) ) { 
  349. return; 
  350.  
  351. if ( ! wp_verify_nonce( bp_action_variable( 1 ), 'bp-messages-star-' . bp_action_variable( 0 ) ) ) { 
  352. wp_die( "Oops! That's a no-no!" ); 
  353.  
  354. // Check capability. 
  355. if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) { 
  356. return; 
  357.  
  358. // Mark the star. 
  359. bp_messages_star_set_action( array( 
  360. 'action' => bp_current_action(),  
  361. 'message_id' => bp_action_variable(),  
  362. 'bulk' => (bool) bp_action_variable( 2 ) 
  363. ) ); 
  364.  
  365. // Redirect back to previous screen. 
  366. $redirect = wp_get_referer() ? wp_get_referer() : bp_loggedin_user_domain() . bp_get_messages_slug(); 
  367. bp_core_redirect( $redirect ); 
  368. die(); 
  369. add_action( 'bp_actions', 'bp_messages_star_action_handler' ); 
  370.  
  371. /** 
  372. * Bulk manage handler to set the star status for multiple messages. 
  373. * 
  374. * @since 2.3.0 
  375. */ 
  376. function bp_messages_star_bulk_manage_handler() { 
  377. if ( empty( $_POST['messages_bulk_nonce' ] ) ) { 
  378. return; 
  379.  
  380. // Check the nonce. 
  381. if ( ! wp_verify_nonce( $_POST['messages_bulk_nonce'], 'messages_bulk_nonce' ) ) { 
  382. return; 
  383.  
  384. // Check capability. 
  385. if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) { 
  386. return; 
  387.  
  388. $action = ! empty( $_POST['messages_bulk_action'] ) ? $_POST['messages_bulk_action'] : ''; 
  389. $threads = ! empty( $_POST['message_ids'] ) ? $_POST['message_ids'] : ''; 
  390. $threads = wp_parse_id_list( $threads ); 
  391.  
  392. // Bail if action doesn't match our star actions or no IDs. 
  393. if ( false === in_array( $action, array( 'star', 'unstar' ), true ) || empty( $threads ) ) { 
  394. return; 
  395.  
  396. // It's star time! 
  397. switch ( $action ) { 
  398. case 'star' : 
  399. $count = count( $threads ); 
  400.  
  401. // If we're starring a thread, we only star the first message in the thread. 
  402. foreach ( $threads as $thread ) { 
  403. $thread = new BP_Messages_thread( $thread ); 
  404. $mids = wp_list_pluck( $thread->messages, 'id' ); 
  405.  
  406. bp_messages_star_set_action( array( 
  407. 'action' => 'star',  
  408. 'message_id' => $mids[0],  
  409. ) ); 
  410.  
  411. bp_core_add_message( sprintf( _n( '%s message was successfully starred', '%s messages were successfully starred', $count, 'buddypress' ), $count ) ); 
  412. break; 
  413.  
  414. case 'unstar' : 
  415. $count = count( $threads ); 
  416.  
  417. foreach ( $threads as $thread ) { 
  418. bp_messages_star_set_action( array( 
  419. 'action' => 'unstar',  
  420. 'thread_id' => $thread,  
  421. 'bulk' => true 
  422. ) ); 
  423.  
  424. bp_core_add_message( sprintf( _n( '%s message was successfully unstarred', '%s messages were successfully unstarred', $count, 'buddypress' ), $count ) ); 
  425. break; 
  426.  
  427. // Redirect back to message box. 
  428. bp_core_redirect( bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' ); 
  429. die(); 
  430. add_action( 'bp_actions', 'bp_messages_star_bulk_manage_handler', 5 ); 
  431.  
  432. /** HOOKS ****************************************************************/ 
  433.  
  434. /** 
  435. * Enqueues the dashicons font. 
  436. * 
  437. * The dashicons font is used for the star / unstar icon. 
  438. * 
  439. * @since 2.3.0 
  440. */ 
  441. function bp_messages_star_enqueue_scripts() { 
  442. if ( ! bp_is_user_messages() ) { 
  443. return; 
  444.  
  445. wp_enqueue_style( 'dashicons' ); 
  446. add_action( 'bp_enqueue_scripts', 'bp_messages_star_enqueue_scripts' ); 
  447.  
  448. /** 
  449. * Add the "Add star" and "Remove star" options to the bulk management list. 
  450. * 
  451. * @since 2.3.0 
  452. */ 
  453. function bp_messages_star_bulk_management_dropdown() { 
  454. ?> 
  455.  
  456. <option value="star"><?php _e( 'Add star', 'buddypress' ); ?></option> 
  457. <option value="unstar"><?php _e( 'Remove star', 'buddypress' ); ?></option> 
  458.  
  459. <?php 
  460. add_action( 'bp_messages_bulk_management_dropdown', 'bp_messages_star_bulk_management_dropdown', 1 ); 
  461.  
  462. /** 
  463. * Add CSS class for the current message depending on starred status. 
  464. * 
  465. * @since 2.3.0 
  466. * 
  467. * @param array $retval Current CSS classes. 
  468. * @return array 
  469. */ 
  470. function bp_messages_star_message_css_class( $retval = array() ) { 
  471. if ( true === bp_messages_is_message_starred( bp_get_the_thread_message_id() ) ) { 
  472. $status = 'starred'; 
  473. } else { 
  474. $status = 'not-starred'; 
  475.  
  476. // Add css class based on star status for the current message. 
  477. $retval[] = "message-{$status}"; 
  478.  
  479. return $retval; 
  480. add_filter( 'bp_get_the_thread_message_css_class', 'bp_messages_star_message_css_class' ); 
.