BP_Messages_Notice

BuddyPress Notices class.

Defined (1)

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

/bp-messages/classes/class-bp-messages-notice.php  
  1. class BP_Messages_Notice { 
  2. /** 
  3. * The notice ID. 
  4. * @var int 
  5. */ 
  6. public $id = null; 
  7.  
  8. /** 
  9. * The subject line for the notice. 
  10. * @var string 
  11. */ 
  12. public $subject; 
  13.  
  14. /** 
  15. * The content of the notice. 
  16. * @var string 
  17. */ 
  18. public $message; 
  19.  
  20. /** 
  21. * The date the notice was created. 
  22. * @var string 
  23. */ 
  24. public $date_sent; 
  25.  
  26. /** 
  27. * Whether the notice is active or not. 
  28. * @var int 
  29. */ 
  30. public $is_active; 
  31.  
  32. /** 
  33. * Constructor. 
  34. * @since 1.0.0 
  35. * @param int|null $id Optional. The ID of the current notice. 
  36. */ 
  37. public function __construct( $id = null ) { 
  38. if ( $id ) { 
  39. $this->id = (int) $id; 
  40. $this->populate(); 
  41.  
  42. /** 
  43. * Populate method. 
  44. * Runs during constructor. 
  45. * @since 1.0.0 
  46. */ 
  47. public function populate() { 
  48. global $wpdb; 
  49.  
  50. $bp = buddypress(); 
  51.  
  52. $notice = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ) ); 
  53.  
  54. if ( $notice ) { 
  55. $this->subject = $notice->subject; 
  56. $this->message = $notice->message; 
  57. $this->date_sent = $notice->date_sent; 
  58. $this->is_active = (int) $notice->is_active; 
  59.  
  60. /** 
  61. * Saves a notice. 
  62. * @since 1.0.0 
  63. * @return bool 
  64. */ 
  65. public function save() { 
  66. global $wpdb; 
  67.  
  68. $bp = buddypress(); 
  69.  
  70. $this->subject = apply_filters( 'messages_notice_subject_before_save', $this->subject, $this->id ); 
  71. $this->message = apply_filters( 'messages_notice_message_before_save', $this->message, $this->id ); 
  72.  
  73. /** 
  74. * Fires before the current message notice item gets saved. 
  75. * Please use this hook to filter the properties above. Each part will be passed in. 
  76. * @since 1.0.0 
  77. * @param BP_Messages_Notice $this Current instance of the message notice item being saved. Passed by reference. 
  78. */ 
  79. do_action_ref_array( 'messages_notice_before_save', array( &$this ) ); 
  80.  
  81. if ( empty( $this->id ) ) { 
  82. $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, %s, %d)", $this->subject, $this->message, $this->date_sent, $this->is_active ); 
  83. } else { 
  84. $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id ); 
  85.  
  86. if ( ! $wpdb->query( $sql ) ) { 
  87. return false; 
  88.  
  89. if ( ! $id = $this->id ) { 
  90. $id = $wpdb->insert_id; 
  91.  
  92. // Now deactivate all notices apart from the new one. 
  93. $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id ) ); 
  94.  
  95. bp_update_user_last_activity( bp_loggedin_user_id(), bp_core_current_time() ); 
  96.  
  97. /** 
  98. * Fires after the current message notice item has been saved. 
  99. * @since 1.0.0 
  100. * @param BP_Messages_Notice $this Current instance of the message item being saved. Passed by reference. 
  101. */ 
  102. do_action_ref_array( 'messages_notice_after_save', array( &$this ) ); 
  103.  
  104. return true; 
  105.  
  106. /** 
  107. * Activates a notice. 
  108. * @since 1.0.0 
  109. * @return bool 
  110. */ 
  111. public function activate() { 
  112. $this->is_active = 1; 
  113. return (bool) $this->save(); 
  114.  
  115. /** 
  116. * Deactivates a notice. 
  117. * @since 1.0.0 
  118. * @return bool 
  119. */ 
  120. public function deactivate() { 
  121. $this->is_active = 0; 
  122. return (bool) $this->save(); 
  123.  
  124. /** 
  125. * Deletes a notice. 
  126. * @since 1.0.0 
  127. * @return bool 
  128. */ 
  129. public function delete() { 
  130. global $wpdb; 
  131.  
  132. /** 
  133. * Fires before the current message item has been deleted. 
  134. * @since 1.0.0 
  135. * @param BP_Messages_Notice $this Current instance of the message notice item being deleted. 
  136. */ 
  137. do_action( 'messages_notice_before_delete', $this ); 
  138.  
  139. $bp = buddypress(); 
  140. $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ); 
  141.  
  142. if ( ! $wpdb->query( $sql ) ) { 
  143. return false; 
  144.  
  145. /** 
  146. * Fires after the current message item has been deleted. 
  147. * @since 2.8.0 
  148. * @param BP_Messages_Notice $this Current instance of the message notice item being deleted. 
  149. */ 
  150. do_action( 'messages_notice_after_delete', $this ); 
  151.  
  152. return true; 
  153.  
  154. /** Static Methods ********************************************************/ 
  155.  
  156. /** 
  157. * Pulls up a list of notices. 
  158. * To get all notices, pass a value of -1 to pag_num. 
  159. * @since 1.0.0 
  160. * @param array $args { 
  161. * Array of parameters. 
  162. * @type int $pag_num Number of notices per page. Defaults to 20. 
  163. * @type int $pag_page The page number. Defaults to 1. 
  164. * } 
  165. * @return object List of notices to display. 
  166. */ 
  167. public static function get_notices( $args = array() ) { 
  168. global $wpdb; 
  169.  
  170. $r = wp_parse_args( $args, array( 
  171. 'pag_num' => 20, // Number of notices per page. 
  172. 'pag_page' => 1 // Page number. 
  173. ) ); 
  174.  
  175. $limit_sql = ''; 
  176. if ( (int) $r['pag_num'] >= 0 ) { 
  177. $limit_sql = $wpdb->prepare( "LIMIT %d, %d", (int) ( ( $r['pag_page'] - 1 ) * $r['pag_num'] ), (int) $r['pag_num'] ); 
  178.  
  179. $bp = buddypress(); 
  180.  
  181. $notices = $wpdb->get_results( "SELECT * FROM {$bp->messages->table_name_notices} ORDER BY date_sent DESC {$limit_sql}" ); 
  182.  
  183. // Integer casting. 
  184. foreach ( (array) $notices as $key => $data ) { 
  185. $notices[ $key ]->id = (int) $notices[ $key ]->id; 
  186. $notices[ $key ]->is_active = (int) $notices[ $key ]->is_active; 
  187.  
  188. /** 
  189. * Filters the array of notices, sorted by date and paginated. 
  190. * @since 2.8.0 
  191. * @param array $r Array of parameters. 
  192. */ 
  193. return apply_filters( 'messages_notice_get_notices', $notices, $r ); 
  194.  
  195. /** 
  196. * Returns the total number of recorded notices. 
  197. * @since 1.0.0 
  198. * @return int 
  199. */ 
  200. public static function get_total_notice_count() { 
  201. global $wpdb; 
  202.  
  203. $bp = buddypress(); 
  204.  
  205. $notice_count = $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->messages->table_name_notices}" ); 
  206.  
  207. /** 
  208. * Filters the total number of notices. 
  209. * @since 2.8.0 
  210. */ 
  211. return (int) apply_filters( 'messages_notice_get_total_notice_count', $notice_count ); 
  212.  
  213. /** 
  214. * Returns the active notice that should be displayed on the front end. 
  215. * @since 1.0.0 
  216. * @return object The BP_Messages_Notice object. 
  217. */ 
  218. public static function get_active() { 
  219. $notice = wp_cache_get( 'active_notice', 'bp_messages' ); 
  220.  
  221. if ( false === $notice ) { 
  222. global $wpdb; 
  223.  
  224. $bp = buddypress(); 
  225.  
  226. $notice_id = $wpdb->get_var( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1" ); 
  227. $notice = new BP_Messages_Notice( $notice_id ); 
  228.  
  229. wp_cache_set( 'active_notice', $notice, 'bp_messages' ); 
  230.  
  231. /** 
  232. * Gives ability to filter the active notice that should be displayed on the front end. 
  233. * @since 2.8.0 
  234. */ 
  235. return apply_filters( 'messages_notice_get_active', $notice );