BP_Core_Notification

BP_Core_Notification is deprecated.

Defined (1)

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

/bp-core/classes/class-bp-core-notification.php  
  1. class BP_Core_Notification { 
  2.  
  3. /** 
  4. * The notification id. 
  5. * @var int 
  6. */ 
  7. public $id; 
  8.  
  9. /** 
  10. * The ID to which the notification relates to within the component. 
  11. * @var int 
  12. */ 
  13. public $item_id; 
  14.  
  15. /** 
  16. * The secondary ID to which the notification relates to within the component. 
  17. * @var int 
  18. */ 
  19. public $secondary_item_id = null; 
  20.  
  21. /** 
  22. * The user ID for who the notification is for. 
  23. * @var int 
  24. */ 
  25. public $user_id; 
  26.  
  27. /** 
  28. * The name of the component that the notification is for. 
  29. * @var string 
  30. */ 
  31. public $component_name; 
  32.  
  33. /** 
  34. * The action within the component which the notification is related to. 
  35. * @var string 
  36. */ 
  37. public $component_action; 
  38.  
  39. /** 
  40. * The date the notification was created. 
  41. * @var string 
  42. */ 
  43. public $date_notified; 
  44.  
  45. /** 
  46. * Is the notification new or has it already been read. 
  47. * @var boolean 
  48. */ 
  49. public $is_new; 
  50.  
  51. /** Public Methods ********************************************************/ 
  52.  
  53. /** 
  54. * Constructor 
  55. * @param int $id ID for the notification. 
  56. */ 
  57. public function __construct( $id = 0 ) { 
  58. if ( !empty( $id ) ) { 
  59. $this->id = $id; 
  60. $this->populate(); 
  61.  
  62. /** 
  63. * Update or insert notification details into the database. 
  64. * @global wpdb $wpdb WordPress database object. 
  65. * @return bool Success or failure. 
  66. */ 
  67. public function save() { 
  68. global $wpdb; 
  69.  
  70. $bp = buddypress(); 
  71.  
  72. // Update. 
  73. if ( !empty( $this->id ) ) { 
  74. $sql = $wpdb->prepare( "UPDATE {$bp->core->table_name_notifications} SET item_id = %d, secondary_item_id = %d, user_id = %d, component_name = %s, component_action = %d, date_notified = %s, is_new = %d ) WHERE id = %d", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new, $this->id ); 
  75.  
  76. // Save. 
  77. } else { 
  78. $sql = $wpdb->prepare( "INSERT INTO {$bp->core->table_name_notifications} ( item_id, secondary_item_id, user_id, component_name, component_action, date_notified, is_new ) VALUES ( %d, %d, %d, %s, %s, %s, %d )", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new ); 
  79.  
  80. if ( !$result = $wpdb->query( $sql ) ) 
  81. return false; 
  82.  
  83. $this->id = $wpdb->insert_id; 
  84.  
  85. return true; 
  86.  
  87. /** Private Methods *******************************************************/ 
  88.  
  89. /** 
  90. * Fetches the notification data from the database. 
  91. * @global wpdb $wpdb WordPress database object. 
  92. */ 
  93. public function populate() { 
  94. global $wpdb; 
  95.  
  96. $bp = buddypress(); 
  97.  
  98. if ( $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE id = %d", $this->id ) ) ) { 
  99. $this->item_id = $notification->item_id; 
  100. $this->secondary_item_id = $notification->secondary_item_id; 
  101. $this->user_id = $notification->user_id; 
  102. $this->component_name = $notification->component_name; 
  103. $this->component_action = $notification->component_action; 
  104. $this->date_notified = $notification->date_notified; 
  105. $this->is_new = $notification->is_new; 
  106.  
  107. /** Static Methods ********************************************************/ 
  108.  
  109. /** 
  110. * Check the access for a user. 
  111. * @param int $user_id ID to check access for. 
  112. * @param int $notification_id Notification ID to check for. 
  113. * @return string 
  114. */ 
  115. public static function check_access( $user_id, $notification_id ) { 
  116. global $wpdb; 
  117.  
  118. $bp = buddypress(); 
  119.  
  120. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->core->table_name_notifications} WHERE id = %d AND user_id = %d", $notification_id, $user_id ) ); 
  121.  
  122. /** 
  123. * Fetches all the notifications in the database for a specific user. 
  124. * @global wpdb $wpdb WordPress database object 
  125. * @static 
  126. * @param int $user_id User ID. 
  127. * @param string $status 'is_new' or 'all'. 
  128. * @return array Associative array 
  129. */ 
  130. public static function get_all_for_user( $user_id, $status = 'is_new' ) { 
  131. global $wpdb; 
  132.  
  133. $bp = buddypress(); 
  134.  
  135. $is_new = ( 'is_new' === $status ) 
  136. ? ' AND is_new = 1 ' 
  137. : ''; 
  138.  
  139. return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE user_id = %d {$is_new}", $user_id ) ); 
  140.  
  141. /** 
  142. * Delete all the notifications for a user based on the component name and action. 
  143. * @global wpdb $wpdb WordPress database object. 
  144. * @static 
  145. * @param int $user_id ID of the user to delet notification for. 
  146. * @param string $component_name Component name. 
  147. * @param string $component_action Component action. 
  148. * @return mixed 
  149. */ 
  150. public static function delete_for_user_by_type( $user_id, $component_name, $component_action ) { 
  151. global $wpdb; 
  152.  
  153. $bp = buddypress(); 
  154.  
  155. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE user_id = %d AND component_name = %s AND component_action = %s", $user_id, $component_name, $component_action ) ); 
  156.  
  157. /** 
  158. * Delete all the notifications that have a specific item id, component name and action. 
  159. * @global wpdb $wpdb WordPress database object. 
  160. * @static 
  161. * @param int $user_id The ID of the user who the notifications are for. 
  162. * @param int $item_id The item ID of the notifications we wish to delete. 
  163. * @param string $component_name The name of the component that the notifications we wish to delete. 
  164. * @param string $component_action The action of the component that the notifications we wish to delete. 
  165. * @param int|bool $secondary_item_id (optional) The secondary item id of the notifications that we wish to 
  166. * use to delete. 
  167. * @return mixed 
  168. */ 
  169. public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) { 
  170. global $wpdb; 
  171.  
  172. $bp = buddypress(); 
  173.  
  174. $secondary_item_sql = !empty( $secondary_item_id ) 
  175. ? $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id ) 
  176. : ''; 
  177.  
  178. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE user_id = %d AND item_id = %d AND component_name = %s AND component_action = %s{$secondary_item_sql}", $user_id, $item_id, $component_name, $component_action ) ); 
  179.  
  180. /** 
  181. * Deletes all the notifications sent by a specific user, by component and action. 
  182. * @global wpdb $wpdb WordPress database object. 
  183. * @static 
  184. * @param int $user_id The ID of the user whose sent notifications we wish to delete. 
  185. * @param string $component_name The name of the component the notification was sent from. 
  186. * @param string $component_action The action of the component the notification was sent from. 
  187. * @return mixed 
  188. */ 
  189. public static function delete_from_user_by_type( $user_id, $component_name, $component_action ) { 
  190. global $wpdb; 
  191.  
  192. $bp = buddypress(); 
  193.  
  194. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE item_id = %d AND component_name = %s AND component_action = %s", $user_id, $component_name, $component_action ) ); 
  195.  
  196. /** 
  197. * Deletes all the notifications for all users by item id, and optional secondary item id,  
  198. * and component name and action. 
  199. * @global wpdb $wpdb WordPress database object. 
  200. * @static 
  201. * @param string $item_id The item id that they notifications are to be for. 
  202. * @param string $component_name The component that the notifications are to be from. 
  203. * @param string $component_action The action that the notifications are to be from. 
  204. * @param string $secondary_item_id Optional secondary item id that the notifications are to have. 
  205. * @return mixed 
  206. */ 
  207. public static function delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id ) { 
  208. global $wpdb; 
  209.  
  210. if ( $component_action ) 
  211. $component_action_sql = $wpdb->prepare( "AND component_action = %s", $component_action ); 
  212. else 
  213. $component_action_sql = ''; 
  214.  
  215. if ( $secondary_item_id ) 
  216. $secondary_item_sql = $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id ); 
  217. else 
  218. $secondary_item_sql = ''; 
  219.  
  220. $bp = buddypress(); 
  221.  
  222. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE item_id = %d AND component_name = %s {$component_action_sql} {$secondary_item_sql}", $item_id, $component_name ) );