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


Generated: Sun Sep 22 01:01:44 2019 Cross-referenced by PHPXref 0.7.1