/bp-messages/classes/class-bp-messages-notice.php

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