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