[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/classes/ -> class-bp-messages-notice.php (source)

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


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1