[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-core-notification.php (source)

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * BP_Core_Notification is deprecated.
  15   *
  16   * Use BP_Notifications_Notification instead.
  17   *
  18   * @deprecated since 1.9.0
  19   */
  20  class BP_Core_Notification {
  21  
  22      /**
  23       * The notification id.
  24       *
  25       * @var int
  26       */
  27      public $id = 0;
  28  
  29      /**
  30       * The ID to which the notification relates to within the component.
  31       *
  32       * @var int
  33       */
  34      public $item_id = 0;
  35  
  36      /**
  37       * The secondary ID to which the notification relates to within the component.
  38       *
  39       * @var int
  40       */
  41      public $secondary_item_id = null;
  42  
  43      /**
  44       * The user ID for who the notification is for.
  45       *
  46       * @var int
  47       */
  48      public $user_id = 0;
  49  
  50      /**
  51       * The name of the component that the notification is for.
  52       *
  53       * @var string
  54       */
  55      public $component_name = '';
  56  
  57      /**
  58       * The action within the component which the notification is related to.
  59       *
  60       * @var string
  61       */
  62      public $component_action = '';
  63  
  64      /**
  65       * The date the notification was created.
  66       *
  67       * @var string
  68       */
  69      public $date_notified = '';
  70  
  71      /**
  72       * Is the notification new or has it already been read.
  73       *
  74       * @var boolean
  75       */
  76      public $is_new = false;
  77  
  78      /** Public Methods ********************************************************/
  79  
  80      /**
  81       * Constructor
  82       *
  83       * @param int $id ID for the notification.
  84       */
  85  	public function __construct( $id = 0 ) {
  86  
  87          // Bail if no ID
  88          if ( empty( $id ) ) {
  89              return;
  90          }
  91  
  92          $this->id = absint( $id );
  93          $this->populate();
  94      }
  95  
  96      /**
  97       * Update or insert notification details into the database.
  98       *
  99       * @global wpdb $wpdb WordPress database object.
 100       *
 101       * @return bool Success or failure.
 102       */
 103  	public function save() {
 104          global $wpdb;
 105  
 106          $bp = buddypress();
 107  
 108          // Update.
 109          if ( ! empty( $this->id ) ) {
 110              $query = "UPDATE {$bp->notifications->table_name} 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";
 111              $sql   = $wpdb->prepare( $query, $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new, $this->id );
 112  
 113          // Save.
 114          } else {
 115              $query = "INSERT INTO {$bp->notifications->table_name} ( item_id, secondary_item_id, user_id, component_name, component_action, date_notified, is_new ) VALUES ( %d, %d, %d, %s, %s, %s, %d )";
 116              $sql   = $wpdb->prepare( $query, $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new );
 117          }
 118  
 119          $result = $wpdb->query( $sql );
 120  
 121          if ( empty( $result ) || is_wp_error( $result ) ) {
 122              return false;
 123          }
 124  
 125          $this->id = $wpdb->insert_id;
 126  
 127          return true;
 128      }
 129  
 130      /** Private Methods *******************************************************/
 131  
 132      /**
 133       * Fetches the notification data from the database.
 134       *
 135       * @global wpdb $wpdb WordPress database object.
 136       */
 137  	public function populate() {
 138          global $wpdb;
 139  
 140          $bp = buddypress();
 141  
 142          $query   = "SELECT * FROM {$bp->notifications->table_name} WHERE id = %d";
 143          $prepare = $wpdb->prepare( $query, $this->id );
 144          $result  = $wpdb->get_row( $prepare );
 145  
 146          if ( ! empty( $result ) ) {
 147              $this->item_id = $result->item_id;
 148              $this->secondary_item_id = $result->secondary_item_id;
 149              $this->user_id           = $result->user_id;
 150              $this->component_name    = $result->component_name;
 151              $this->component_action  = $result->component_action;
 152              $this->date_notified     = $result->date_notified;
 153              $this->is_new            = $result->is_new;
 154          }
 155      }
 156  
 157      /** Static Methods ********************************************************/
 158  
 159      /**
 160       * Check the access for a user.
 161       *
 162       * @param int $user_id         ID to check access for.
 163       * @param int $notification_id Notification ID to check for.
 164       * @return string
 165       */
 166  	public static function check_access( $user_id = 0, $notification_id = 0 ) {
 167          global $wpdb;
 168  
 169          $bp = buddypress();
 170  
 171          $query   = "SELECT COUNT(id) FROM {$bp->notifications->table_name} WHERE id = %d AND user_id = %d";
 172          $prepare = $wpdb->prepare( $query, $notification_id, $user_id );
 173          $result  = $wpdb->get_var( $prepare );
 174  
 175          return $result;
 176      }
 177  
 178      /**
 179       * Fetches all the notifications in the database for a specific user.
 180       *
 181       * @global wpdb $wpdb WordPress database object
 182       *
 183       * @static
 184       *
 185       * @param int    $user_id User ID.
 186       * @param string $status 'is_new' or 'all'.
 187       * @return array Associative array
 188       */
 189  	public static function get_all_for_user( $user_id, $status = 'is_new' ) {
 190          global $wpdb;
 191  
 192          $bp = buddypress();
 193  
 194          $is_new = ( 'is_new' === $status )
 195              ? ' AND is_new = 1 '
 196              : '';
 197  
 198          $query   = "SELECT * FROM {$bp->notifications->table_name} WHERE user_id = %d {$is_new}";
 199          $prepare = $wpdb->prepare( $query, $user_id );
 200          $result  = $wpdb->get_results( $prepare );
 201  
 202          return $result;
 203      }
 204  
 205      /**
 206       * Delete all the notifications for a user based on the component name and action.
 207       *
 208       * @global wpdb $wpdb WordPress database object.
 209       *
 210       * @static
 211       *
 212       * @param int    $user_id          ID of the user to delet notification for.
 213       * @param string $component_name   Component name.
 214       * @param string $component_action Component action.
 215       * @return mixed
 216       */
 217  	public static function delete_for_user_by_type( $user_id, $component_name, $component_action ) {
 218          global $wpdb;
 219  
 220          $bp = buddypress();
 221  
 222          $query   = "DELETE FROM {$bp->notifications->table_name} WHERE user_id = %d AND component_name = %s AND component_action = %s";
 223          $prepare = $wpdb->prepare( $query, $user_id, $component_name, $component_action );
 224          $result  = $wpdb->query( $prepare );
 225  
 226          return $result;
 227      }
 228  
 229      /**
 230       * Delete all the notifications that have a specific item id, component name and action.
 231       *
 232       * @global wpdb $wpdb WordPress database object.
 233       *
 234       * @static
 235       *
 236       * @param int      $user_id           The ID of the user who the notifications are for.
 237       * @param int      $item_id           The item ID of the notifications we wish to delete.
 238       * @param string   $component_name    The name of the component that the notifications we wish to delete.
 239       * @param string   $component_action  The action of the component that the notifications we wish to delete.
 240       * @param int      $secondary_item_id (optional) The secondary item id of the notifications that we wish to
 241       *                                    use to delete.
 242       * @return mixed
 243       */
 244  	public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = 0 ) {
 245          global $wpdb;
 246  
 247          $bp = buddypress();
 248  
 249          $secondary_item_sql = ! empty( $secondary_item_id )
 250              ? $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id )
 251              : '';
 252  
 253          $query   = "DELETE FROM {$bp->notifications->table_name} WHERE user_id = %d AND item_id = %d AND component_name = %s AND component_action = %s{$secondary_item_sql}";
 254          $prepare = $wpdb->prepare( $query, $user_id, $item_id, $component_name, $component_action );
 255          $result  = $wpdb->query( $prepare );
 256  
 257          return $result;
 258      }
 259  
 260      /**
 261       * Deletes all the notifications sent by a specific user, by component and action.
 262       *
 263       * @global wpdb $wpdb WordPress database object.
 264       *
 265       * @static
 266       *
 267       * @param int    $user_id          The ID of the user whose sent notifications we wish to delete.
 268       * @param string $component_name   The name of the component the notification was sent from.
 269       * @param string $component_action The action of the component the notification was sent from.
 270       * @return mixed
 271       */
 272  	public static function delete_from_user_by_type( $user_id, $component_name, $component_action ) {
 273          global $wpdb;
 274  
 275          $bp = buddypress();
 276  
 277          $query   = "DELETE FROM {$bp->notifications->table_name} WHERE item_id = %d AND component_name = %s AND component_action = %s";
 278          $prepare = $wpdb->prepare( $query, $user_id, $component_name, $component_action );
 279          $result  = $wpdb->query( $prepare );
 280  
 281          return $result;
 282      }
 283  
 284      /**
 285       * Deletes all the notifications for all users by item id, and optional secondary item id,
 286       * and component name and action.
 287       *
 288       * @global wpdb $wpdb WordPress database object.
 289       *
 290       * @static
 291       *
 292       * @param int    $item_id           The item id that they notifications are to be for.
 293       * @param string $component_name    The component that the notifications are to be from.
 294       * @param string $component_action  The action that the notifications are to be from.
 295       * @param int    $secondary_item_id Optional secondary item id that the notifications are to have.
 296       * @return mixed
 297       */
 298  	public static function delete_all_by_type( $item_id, $component_name, $component_action = '', $secondary_item_id = 0 ) {
 299          global $wpdb;
 300  
 301          $component_action_sql = ! empty( $component_action )
 302              ? $wpdb->prepare( "AND component_action = %s", $component_action )
 303              : '';
 304  
 305          $secondary_item_sql = ! empty( $secondary_item_id )
 306              ? $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id )
 307              : '';
 308  
 309          $bp = buddypress();
 310  
 311          $query   = "DELETE FROM {$bp->notifications->table_name} WHERE item_id = %d AND component_name = %s {$component_action_sql} {$secondary_item_sql}";
 312          $prepare = $wpdb->prepare( $query, $item_id, $component_name );
 313          $result  = $wpdb->query( $prepare );
 314  
 315          return $result;
 316      }
 317  }


Generated: Fri Oct 4 01:00:58 2024 Cross-referenced by PHPXref 0.7.1