[ 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;
  28  
  29      /**
  30       * The ID to which the notification relates to within the component.
  31       *
  32       * @var int
  33       */
  34      public $item_id;
  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;
  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;
  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          if ( !empty( $id ) ) {
  87              $this->id = $id;
  88              $this->populate();
  89          }
  90      }
  91  
  92      /**
  93       * Update or insert notification details into the database.
  94       *
  95       * @global wpdb $wpdb WordPress database object.
  96       *
  97       * @return bool Success or failure.
  98       */
  99  	public function save() {
 100          global $wpdb;
 101  
 102          $bp = buddypress();
 103  
 104          // Update.
 105          if ( !empty( $this->id ) ) {
 106              $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 );
 107  
 108          // Save.
 109          } else {
 110              $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 );
 111          }
 112  
 113          if ( !$result = $wpdb->query( $sql ) )
 114              return false;
 115  
 116          $this->id = $wpdb->insert_id;
 117  
 118          return true;
 119      }
 120  
 121      /** Private Methods *******************************************************/
 122  
 123      /**
 124       * Fetches the notification data from the database.
 125       *
 126       * @global wpdb $wpdb WordPress database object.
 127       */
 128  	public function populate() {
 129          global $wpdb;
 130  
 131          $bp = buddypress();
 132  
 133          if ( $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE id = %d", $this->id ) ) ) {
 134              $this->item_id = $notification->item_id;
 135              $this->secondary_item_id = $notification->secondary_item_id;
 136              $this->user_id           = $notification->user_id;
 137              $this->component_name    = $notification->component_name;
 138              $this->component_action  = $notification->component_action;
 139              $this->date_notified     = $notification->date_notified;
 140              $this->is_new            = $notification->is_new;
 141          }
 142      }
 143  
 144      /** Static Methods ********************************************************/
 145  
 146      /**
 147       * Check the access for a user.
 148       *
 149       * @param int $user_id         ID to check access for.
 150       * @param int $notification_id Notification ID to check for.
 151       * @return string
 152       */
 153  	public static function check_access( $user_id, $notification_id ) {
 154          global $wpdb;
 155  
 156          $bp = buddypress();
 157  
 158          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 ) );
 159      }
 160  
 161      /**
 162       * Fetches all the notifications in the database for a specific user.
 163       *
 164       * @global wpdb $wpdb WordPress database object
 165       *
 166       * @static
 167       *
 168       * @param int    $user_id User ID.
 169       * @param string $status 'is_new' or 'all'.
 170       * @return array Associative array
 171       */
 172  	public static function get_all_for_user( $user_id, $status = 'is_new' ) {
 173          global $wpdb;
 174  
 175          $bp = buddypress();
 176  
 177          $is_new = ( 'is_new' === $status )
 178              ? ' AND is_new = 1 '
 179              : '';
 180  
 181          return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE user_id = %d {$is_new}", $user_id ) );
 182      }
 183  
 184      /**
 185       * Delete all the notifications for a user based on the component name and action.
 186       *
 187       * @global wpdb $wpdb WordPress database object.
 188       *
 189       * @static
 190       *
 191       * @param int    $user_id          ID of the user to delet notification for.
 192       * @param string $component_name   Component name.
 193       * @param string $component_action Component action.
 194       * @return mixed
 195       */
 196  	public static function delete_for_user_by_type( $user_id, $component_name, $component_action ) {
 197          global $wpdb;
 198  
 199          $bp = buddypress();
 200  
 201          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 ) );
 202      }
 203  
 204      /**
 205       * Delete all the notifications that have a specific item id, component name and action.
 206       *
 207       * @global wpdb $wpdb WordPress database object.
 208       *
 209       * @static
 210       *
 211       * @param int      $user_id           The ID of the user who the notifications are for.
 212       * @param int      $item_id           The item ID of the notifications we wish to delete.
 213       * @param string   $component_name    The name of the component that the notifications we wish to delete.
 214       * @param string   $component_action  The action of the component that the notifications we wish to delete.
 215       * @param int|bool $secondary_item_id (optional) The secondary item id of the notifications that we wish to
 216       *                                    use to delete.
 217       * @return mixed
 218       */
 219  	public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
 220          global $wpdb;
 221  
 222          $bp = buddypress();
 223  
 224          $secondary_item_sql = !empty( $secondary_item_id )
 225              ? $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id )
 226              : '';
 227  
 228          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 ) );
 229      }
 230  
 231      /**
 232       * Deletes all the notifications sent by a specific user, by component and action.
 233       *
 234       * @global wpdb $wpdb WordPress database object.
 235       *
 236       * @static
 237       *
 238       * @param int    $user_id          The ID of the user whose sent notifications we wish to delete.
 239       * @param string $component_name   The name of the component the notification was sent from.
 240       * @param string $component_action The action of the component the notification was sent from.
 241       * @return mixed
 242       */
 243  	public static function delete_from_user_by_type( $user_id, $component_name, $component_action ) {
 244          global $wpdb;
 245  
 246          $bp = buddypress();
 247  
 248          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 ) );
 249      }
 250  
 251      /**
 252       * Deletes all the notifications for all users by item id, and optional secondary item id,
 253       * and component name and action.
 254       *
 255       * @global wpdb $wpdb WordPress database object.
 256       *
 257       * @static
 258       *
 259       * @param string $item_id           The item id that they notifications are to be for.
 260       * @param string $component_name    The component that the notifications are to be from.
 261       * @param string $component_action  The action that the notifications are to be from.
 262       * @param string $secondary_item_id Optional secondary item id that the notifications are to have.
 263       * @return mixed
 264       */
 265  	public static function delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id ) {
 266          global $wpdb;
 267  
 268          if ( $component_action )
 269              $component_action_sql = $wpdb->prepare( "AND component_action = %s", $component_action );
 270          else
 271              $component_action_sql = '';
 272  
 273          if ( $secondary_item_id )
 274              $secondary_item_sql = $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id );
 275          else
 276              $secondary_item_sql = '';
 277  
 278          $bp = buddypress();
 279  
 280          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 ) );
 281      }
 282  }


Generated: Thu Sep 19 01:01:39 2019 Cross-referenced by PHPXref 0.7.1