[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-notifications/ -> bp-notifications-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Member Notifications Functions.
   4   *
   5   * Functions and filters used in the Notifications component.
   6   *
   7   * @package BuddyPress
   8   * @subpackage NotificationsFunctions
   9   * @since 1.9.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Add a notification for a specific user, from a specific component.
  17   *
  18   * @since 1.9.0
  19   *
  20   * @param array $args {
  21   *     Array of arguments describing the notification. All are optional.
  22   *     @type int    $user_id           ID of the user to associate the notification with.
  23   *     @type int    $item_id           ID of the item to associate the notification with.
  24   *     @type int    $secondary_item_id ID of the secondary item to associate the
  25   *                                     notification with.
  26   *     @type string $component_name    Name of the component to associate the
  27   *                                     notification with.
  28   *     @type string $component_action  Name of the action to associate the
  29   *                                     notification with.
  30   *     @type string $date_notified     Timestamp for the notification.
  31   * }
  32   * @return int|bool ID of the newly created notification on success, false
  33   *                  on failure.
  34   */
  35  function bp_notifications_add_notification( $args = array() ) {
  36  
  37      $r = bp_parse_args( $args, array(
  38          'user_id'           => 0,
  39          'item_id'           => 0,
  40          'secondary_item_id' => 0,
  41          'component_name'    => '',
  42          'component_action'  => '',
  43          'date_notified'     => bp_core_current_time(),
  44          'is_new'            => 1,
  45          'allow_duplicate'   => false,
  46      ), 'notifications_add_notification' );
  47  
  48      // Check for existing duplicate notifications.
  49      if ( ! $r['allow_duplicate'] ) {
  50          // Date_notified, allow_duplicate don't count toward
  51          // duplicate status.
  52          $existing = BP_Notifications_Notification::get( array(
  53              'user_id'           => $r['user_id'],
  54              'item_id'           => $r['item_id'],
  55              'secondary_item_id' => $r['secondary_item_id'],
  56              'component_name'    => $r['component_name'],
  57              'component_action'  => $r['component_action'],
  58              'is_new'            => $r['is_new'],
  59          ) );
  60  
  61          if ( ! empty( $existing ) ) {
  62              return false;
  63          }
  64      }
  65  
  66      // Setup the new notification.
  67      $notification                    = new BP_Notifications_Notification;
  68      $notification->user_id           = $r['user_id'];
  69      $notification->item_id           = $r['item_id'];
  70      $notification->secondary_item_id = $r['secondary_item_id'];
  71      $notification->component_name    = $r['component_name'];
  72      $notification->component_action  = $r['component_action'];
  73      $notification->date_notified     = $r['date_notified'];
  74      $notification->is_new            = $r['is_new'];
  75  
  76      // Save the new notification.
  77      return $notification->save();
  78  }
  79  
  80  /**
  81   * Get a specific notification by its ID.
  82   *
  83   * @since 1.9.0
  84   *
  85   * @param int $id ID of the notification.
  86   * @return BP_Notifications_Notification Notification object for ID specified.
  87   */
  88  function bp_notifications_get_notification( $id ) {
  89      return new BP_Notifications_Notification( $id );
  90  }
  91  
  92  /**
  93   * Delete a specific notification by its ID.
  94   *
  95   * @since 1.9.0
  96   *
  97   * @param int $id ID of the notification to delete.
  98   * @return false|int True on success, false on failure.
  99   */
 100  function bp_notifications_delete_notification( $id ) {
 101      if ( ! bp_notifications_check_notification_access( bp_displayed_user_id(), $id ) ) {
 102          return false;
 103      }
 104  
 105      return BP_Notifications_Notification::delete( array( 'id' => $id ) );
 106  }
 107  
 108  /**
 109   * Mark notification read/unread for a user by ID.
 110   *
 111   * Used when clearing out notifications for a specific notification item.
 112   *
 113   * @since 1.9.0
 114   *
 115   * @param int      $id     ID of the notification.
 116   * @param int|bool $is_new 0 for read, 1 for unread.
 117   * @return false|int True on success, false on failure.
 118   */
 119  function bp_notifications_mark_notification( $id, $is_new = false ) {
 120      if ( ! bp_notifications_check_notification_access( bp_displayed_user_id(), $id ) ) {
 121          return false;
 122      }
 123  
 124      return BP_Notifications_Notification::update(
 125          array( 'is_new' => $is_new ),
 126          array( 'id'     => $id     )
 127      );
 128  }
 129  
 130  /**
 131   * Get all notifications for a user and cache them.
 132   *
 133   * @since 2.1.0
 134   *
 135   * @param int $user_id ID of the user whose notifications are being fetched.
 136   * @return array $notifications Array of notifications for user.
 137   */
 138  function bp_notifications_get_all_notifications_for_user( $user_id = 0 ) {
 139  
 140      // Default to displayed user if no ID is passed.
 141      if ( empty( $user_id ) ) {
 142          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
 143      }
 144  
 145      // Get notifications out of the cache, or query if necessary.
 146      $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
 147      if ( false === $notifications ) {
 148          $notifications = BP_Notifications_Notification::get( array(
 149              'user_id' => $user_id
 150          ) );
 151          wp_cache_set( 'all_for_user_' . $user_id, $notifications, 'bp_notifications' );
 152      }
 153  
 154      /**
 155       * Filters all notifications for a user.
 156       *
 157       * @since 2.1.0
 158       *
 159       * @param array $notifications Array of notifications for user.
 160       * @param int   $user_id       ID of the user being fetched.
 161       */
 162      return apply_filters( 'bp_notifications_get_all_notifications_for_user', $notifications, $user_id );
 163  }
 164  
 165  /**
 166   * Get a user's unread notifications, grouped by component and action.
 167   *
 168   * This function returns a list of notifications collapsed by component + action.
 169   * See BP_Notifications_Notification::get_grouped_notifications_for_user() for
 170   * more details.
 171   *
 172   * @since 3.0.0
 173   *
 174   * @param int $user_id ID of the user whose notifications are being fetched.
 175   * @return array $notifications
 176   */
 177  function bp_notifications_get_grouped_notifications_for_user( $user_id = 0 ) {
 178      if ( empty( $user_id ) ) {
 179          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
 180      }
 181  
 182      $notifications = wp_cache_get( $user_id, 'bp_notifications_grouped_notifications' );
 183      if ( false === $notifications ) {
 184          $notifications = BP_Notifications_Notification::get_grouped_notifications_for_user( $user_id );
 185          wp_cache_set( $user_id, $notifications, 'bp_notifications_grouped_notifications' );
 186      }
 187  
 188      return $notifications;
 189  }
 190  
 191  /**
 192   * Get notifications for a specific user.
 193   *
 194   * @since 1.9.0
 195   *
 196   * @param int    $user_id ID of the user whose notifications are being fetched.
 197   * @param string $format  Format of the returned values. 'string' returns HTML,
 198   *                        while 'object' returns a structured object for parsing.
 199   * @return mixed Object or array on success, false on failure.
 200   */
 201  function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
 202      $bp = buddypress();
 203  
 204      $notifications = bp_notifications_get_grouped_notifications_for_user( $user_id );
 205  
 206      // Calculate a renderable output for each notification type.
 207      foreach ( $notifications as $notification_item ) {
 208  
 209          $component_name = $notification_item->component_name;
 210          // We prefer that extended profile component-related notifications use
 211          // the component_name of 'xprofile'. However, the extended profile child
 212          // object in the $bp object is keyed as 'profile', which is where we need
 213          // to look for the registered notification callback.
 214          if ( 'xprofile' == $notification_item->component_name ) {
 215              $component_name = 'profile';
 216          }
 217  
 218          // Callback function exists.
 219          if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
 220  
 221              // Function should return an object.
 222              if ( 'object' === $format ) {
 223  
 224                  // Retrieve the content of the notification using the callback.
 225                  $content = call_user_func( $bp->{$component_name}->notification_callback, $notification_item->component_action, $notification_item->item_id, $notification_item->secondary_item_id, $notification_item->total_count, 'array', $notification_item->id );
 226  
 227                  // Create the object to be returned.
 228                  $notification_object = $notification_item;
 229  
 230                  // Minimal backpat with non-compatible notification
 231                  // callback functions.
 232                  if ( is_string( $content ) ) {
 233                      $notification_object->content = $content;
 234                      $notification_object->href    = bp_loggedin_user_domain();
 235                  } else {
 236                      $notification_object->content = $content['text'];
 237                      $notification_object->href    = $content['link'];
 238                  }
 239  
 240                  $renderable[] = $notification_object;
 241  
 242                  // Return an array of content strings.
 243              } else {
 244                  $content      = call_user_func( $bp->{$component_name}->notification_callback, $notification_item->component_action, $notification_item->item_id, $notification_item->secondary_item_id, $notification_item->total_count, 'string', $notification_item->id );
 245                  $renderable[] = $content;
 246              }
 247  
 248              // @deprecated format_notification_function - 1.5
 249          } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) {
 250              $renderable[] = call_user_func( $bp->{$component_name}->notification_callback, $notification_item->component_action, $notification_item->item_id, $notification_item->secondary_item_id, $notification_item->total_count );
 251  
 252              // Allow non BuddyPress components to hook in.
 253          } else {
 254  
 255              // The array to reference with apply_filters_ref_array().
 256              $ref_array = array(
 257                  $notification_item->component_action,
 258                  $notification_item->item_id,
 259                  $notification_item->secondary_item_id,
 260                  $notification_item->total_count,
 261                  $format,
 262                  $notification_item->component_action, // Duplicated so plugins can check the canonical action name.
 263                  $component_name,
 264                  $notification_item->id,
 265              );
 266  
 267              // Function should return an object.
 268              if ( 'object' === $format ) {
 269  
 270                  /**
 271                   * Filters the notification content for notifications created by plugins.
 272                   * If your plugin extends the {@link BP_Component} class, you should use the
 273                   * 'notification_callback' parameter in your extended
 274                   * {@link BP_Component::setup_globals()} method instead.
 275                   *
 276                   * @since 1.9.0
 277                   * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters.
 278                   *
 279                   * @param string $content               Component action. Deprecated. Do not do checks against this! Use
 280                   *                                      the 6th parameter instead - $component_action_name.
 281                   * @param int    $item_id               Notification item ID.
 282                   * @param int    $secondary_item_id     Notification secondary item ID.
 283                   * @param int    $action_item_count     Number of notifications with the same action.
 284                   * @param string $format                Format of return. Either 'string' or 'object'.
 285                   * @param string $component_action_name Canonical notification action.
 286                   * @param string $component_name        Notification component ID.
 287                   * @param int    $id                    Notification ID.
 288                   *
 289                   * @return string|array If $format is 'string', return a string of the notification content.
 290                   *                      If $format is 'object', return an array formatted like:
 291                   *                      array( 'text' => 'CONTENT', 'link' => 'LINK' )
 292                   */
 293                  $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
 294  
 295                  // Create the object to be returned.
 296                  $notification_object = $notification_item;
 297  
 298                  // Minimal backpat with non-compatible notification
 299                  // callback functions.
 300                  if ( is_string( $content ) ) {
 301                      $notification_object->content = $content;
 302                      $notification_object->href    = bp_loggedin_user_domain();
 303                  } else {
 304                      $notification_object->content = $content['text'];
 305                      $notification_object->href    = $content['link'];
 306                  }
 307  
 308                  $renderable[] = $notification_object;
 309  
 310                  // Return an array of content strings.
 311              } else {
 312  
 313                  /** This filters is documented in bp-notifications/bp-notifications-functions.php */
 314                  $renderable[] = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
 315              }
 316          }
 317      }
 318  
 319      // If renderable is empty array, set to false.
 320      if ( empty( $renderable ) ) {
 321          $renderable = false;
 322      }
 323  
 324      /**
 325       * Filters the final array of notifications to be displayed for a user.
 326       *
 327       * @since 1.6.0
 328       *
 329       * @param array|bool $renderable Array of notifications to render or false if no notifications.
 330       * @param int        $user_id    ID of the user whose notifications are being displayed.
 331       * @param string     $format     Display format requested for the notifications.
 332       */
 333      return apply_filters( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
 334  }
 335  
 336  /** Delete ********************************************************************/
 337  
 338  /**
 339   * Delete notifications for a user by type.
 340   *
 341   * Used when clearing out notifications for a specific component when the user
 342   * has visited that component.
 343   *
 344   * @since 1.9.0
 345   *
 346   * @param int    $user_id          ID of the user whose notifications are being deleted.
 347   * @param string $component_name   Name of the associated component.
 348   * @param string $component_action Name of the associated action.
 349   * @return int|false True on success, false on failure.
 350   */
 351  function bp_notifications_delete_notifications_by_type( $user_id, $component_name, $component_action ) {
 352      return BP_Notifications_Notification::delete( array(
 353          'user_id'          => $user_id,
 354          'component_name'   => $component_name,
 355          'component_action' => $component_action,
 356      ) );
 357  }
 358  
 359  /**
 360   * Delete notifications for an item ID.
 361   *
 362   * Used when clearing out notifications for a specific component when the user
 363   * has visited that component.
 364   *
 365   * @since 1.9.0
 366   *
 367   * @param int      $user_id           ID of the user whose notifications are being deleted.
 368   * @param int      $item_id           ID of the associated item.
 369   * @param string   $component_name    Name of the associated component.
 370   * @param string   $component_action  Name of the associated action.
 371   * @param int|bool $secondary_item_id ID of the secondary associated item.
 372   * @return int|false True on success, false on failure.
 373   */
 374  function bp_notifications_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
 375      return BP_Notifications_Notification::delete( array(
 376          'user_id'           => $user_id,
 377          'item_id'           => $item_id,
 378          'secondary_item_id' => $secondary_item_id,
 379          'component_name'    => $component_name,
 380          'component_action'  => $component_action,
 381      ) );
 382  }
 383  
 384  /**
 385   * Delete all notifications by type.
 386   *
 387   * Used when clearing out notifications for an entire component.
 388   *
 389   * @since 1.9.0
 390   *
 391   * @param int         $item_id           ID of the user whose notifications are being deleted.
 392   * @param string      $component_name    Name of the associated component.
 393   * @param string|bool $component_action  Optional. Name of the associated action.
 394   * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
 395   * @return int|false True on success, false on failure.
 396   */
 397  function bp_notifications_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) {
 398      return BP_Notifications_Notification::delete( array(
 399          'item_id'           => $item_id,
 400          'secondary_item_id' => $secondary_item_id,
 401          'component_name'    => $component_name,
 402          'component_action'  => $component_action,
 403      ) );
 404  }
 405  
 406  /**
 407   * Delete all notifications from a user.
 408   *
 409   * Used when clearing out all notifications for a user, when deleted or spammed.
 410   *
 411   * @todo This function assumes that items with the user_id in the item_id slot
 412   *       are associated with that user. However, this will only be true with
 413   *       certain components (such as Friends). Use with caution!
 414   *
 415   * @since 1.9.0
 416   *
 417   * @param int    $user_id          ID of the user whose associated items are being deleted.
 418   * @param string $component_name   Name of the associated component.
 419   * @param string $component_action Name of the associated action.
 420   * @return int|false True on success, false on failure.
 421   */
 422  function bp_notifications_delete_notifications_from_user( $user_id, $component_name, $component_action ) {
 423      return BP_Notifications_Notification::delete( array(
 424          'item_id'           => $user_id,
 425          'component_name'    => $component_name,
 426          'component_action'  => $component_action,
 427      ) );
 428  }
 429  
 430  /**
 431   * Delete a user's notifications when the user is deleted.
 432   *
 433   * @since 2.5.0
 434   *
 435   * @param int $user_id ID of the user who is about to be deleted.
 436   * @return int|false The number of rows deleted, or false on error.
 437   */
 438  function bp_notifications_delete_notifications_on_user_delete( $user_id ) {
 439      return BP_Notifications_Notification::delete( array(
 440          'user_id'           => $user_id,
 441          'item_id'           => false,
 442          'secondary_item_id' => false,
 443          'component_action'  => false,
 444          'component_name'    => false,
 445      ) );
 446  }
 447  add_action( 'wpmu_delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
 448  add_action( 'delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
 449  
 450  /** Mark **********************************************************************/
 451  
 452  /**
 453   * Mark notifications read/unread for a user by type.
 454   *
 455   * Used when clearing out notifications for a specific component when the user
 456   * has visited that component.
 457   *
 458   * @since 1.9.0
 459   *
 460   * @param int      $user_id          ID of the user whose notifications are being deleted.
 461   * @param string   $component_name   Name of the associated component.
 462   * @param string   $component_action Name of the associated action.
 463   * @param int|bool $is_new           0 for read, 1 for unread.
 464   * @return int|false True on success, false on failure.
 465   */
 466  function bp_notifications_mark_notifications_by_type( $user_id, $component_name, $component_action, $is_new = false ) {
 467      return BP_Notifications_Notification::update(
 468          array(
 469              'is_new' => $is_new
 470          ),
 471          array(
 472              'user_id'          => $user_id,
 473              'component_name'   => $component_name,
 474              'component_action' => $component_action
 475          )
 476      );
 477  }
 478  
 479  /**
 480   * Mark notifications read/unread for an item ID.
 481   *
 482   * Used when clearing out notifications for a specific component when the user
 483   * has visited that component.
 484   *
 485   * @since 1.9.0
 486   *
 487   * @param int      $user_id           ID of the user whose notifications are being deleted.
 488   * @param int      $item_id           ID of the associated item.
 489   * @param string   $component_name    Name of the associated component.
 490   * @param string   $component_action  Name of the associated action.
 491   * @param int|bool $secondary_item_id ID of the secondary associated item.
 492   * @param int|bool $is_new            0 for read, 1 for unread.
 493   * @return int|false True on success, false on failure.
 494   */
 495  function bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false, $is_new = false ) {
 496      return BP_Notifications_Notification::update(
 497          array(
 498              'is_new' => $is_new
 499          ),
 500          array(
 501              'user_id'           => $user_id,
 502              'item_id'           => $item_id,
 503              'secondary_item_id' => $secondary_item_id,
 504              'component_name'    => $component_name,
 505              'component_action'  => $component_action
 506          )
 507      );
 508  }
 509  
 510  /**
 511   * Mark all notifications read/unread by type.
 512   *
 513   * Used when clearing out notifications for an entire component.
 514   *
 515   * @since 1.9.0
 516   *
 517   * @param int         $item_id           ID of the user whose notifications are being deleted.
 518   * @param string      $component_name    Name of the associated component.
 519   * @param string|bool $component_action  Optional. Name of the associated action.
 520   * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
 521   * @param int|bool    $is_new            0 for read, 1 for unread.
 522   * @return int|false True on success, false on failure.
 523   */
 524  function bp_notifications_mark_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false, $is_new = false ) {
 525      return BP_Notifications_Notification::update(
 526          array(
 527              'is_new' => $is_new
 528          ),
 529          array(
 530              'item_id'           => $item_id,
 531              'secondary_item_id' => $secondary_item_id,
 532              'component_name'    => $component_name,
 533              'component_action'  => $component_action
 534          )
 535      );
 536  }
 537  
 538  /**
 539   * Mark all notifications read/unread from a user.
 540   *
 541   * Used when clearing out all notifications for a user, when deleted or spammed.
 542   *
 543   * @todo This function assumes that items with the user_id in the item_id slot
 544   *       are associated with that user. However, this will only be true with
 545   *       certain components (such as Friends). Use with caution!
 546   *
 547   * @since 1.9.0
 548   *
 549   * @param int      $user_id          ID of the user whose associated items are being deleted.
 550   * @param string   $component_name   Name of the associated component.
 551   * @param string   $component_action Name of the associated action.
 552   * @param int|bool $is_new           0 for read, 1 for unread.
 553   * @return int|false True on success, false on failure.
 554   */
 555  function bp_notifications_mark_notifications_from_user( $user_id, $component_name, $component_action, $is_new = false ) {
 556      return BP_Notifications_Notification::update(
 557          array(
 558              'is_new' => $is_new
 559          ),
 560          array(
 561              'item_id'          => $user_id,
 562              'component_name'   => $component_name,
 563              'component_action' => $component_action
 564          )
 565      );
 566  }
 567  
 568  /** Helpers *******************************************************************/
 569  
 570  /**
 571   * Check if a user has access to a specific notification.
 572   *
 573   * Used before deleting a notification for a user.
 574   *
 575   * @since 1.9.0
 576   *
 577   * @param int $user_id         ID of the user being checked.
 578   * @param int $notification_id ID of the notification being checked.
 579   * @return bool True if the notification belongs to the user, otherwise false.
 580   */
 581  function bp_notifications_check_notification_access( $user_id, $notification_id ) {
 582      return (bool) BP_Notifications_Notification::check_access( $user_id, $notification_id );
 583  }
 584  
 585  /**
 586   * Get a count of unread notification items for a user.
 587   *
 588   * @since 1.9.0
 589   *
 590   * @param int $user_id ID of the user whose unread notifications are being
 591   *                     counted.
 592   * @return int Unread notification count.
 593   */
 594  function bp_notifications_get_unread_notification_count( $user_id = 0 ) {
 595      if ( empty( $user_id ) ) {
 596          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
 597      }
 598  
 599      $count = wp_cache_get( $user_id, 'bp_notifications_unread_count' );
 600      if ( false === $count ) {
 601          $count = BP_Notifications_Notification::get_total_count( array(
 602              'user_id' => $user_id,
 603              'is_new'  => true,
 604          ) );
 605          wp_cache_set( $user_id, $count, 'bp_notifications_unread_count' );
 606      }
 607  
 608      /**
 609       * Filters the count of unread notification items for a user.
 610       *
 611       * @since 1.9.0
 612       * @since 2.7.0 Added user ID parameter.
 613       *
 614       * @param int $count   Count of unread notification items for a user.
 615       * @param int $user_id User ID for notifications count.
 616       */
 617      return apply_filters( 'bp_notifications_get_total_notification_count', (int) $count, $user_id );
 618  }
 619  
 620  /**
 621   * Return an array of component names that are currently active and have
 622   * registered Notifications callbacks.
 623   *
 624   * @since 1.9.1
 625   *
 626   * @see http://buddypress.trac.wordpress.org/ticket/5300
 627   *
 628   * @return array $component_names Array of registered components.
 629   */
 630  function bp_notifications_get_registered_components() {
 631  
 632      // Load BuddyPress.
 633      $bp = buddypress();
 634  
 635      // Setup return value.
 636      $component_names = array();
 637  
 638      // Get the active components.
 639      $active_components = array_keys( $bp->active_components );
 640  
 641      // Loop through components, look for callbacks, add to return value.
 642      foreach ( $active_components as $component ) {
 643          if ( !empty( $bp->$component->notification_callback ) ) {
 644              $component_names[] = $component;
 645          }
 646          // The extended profile component is identified in the active_components array as 'xprofile'.
 647          // However, the extended profile child object has the key 'profile' in the $bp object.
 648          if ( 'xprofile' == $component && ! empty( $bp->profile->notification_callback ) ) {
 649              $component_names[] = $component;
 650          }
 651      }
 652  
 653      /**
 654       * Filters active components with registered notifications callbacks.
 655       *
 656       * @since 1.9.1
 657       *
 658       * @param array $component_names   Array of registered component names.
 659       * @param array $active_components Array of active components.
 660       */
 661      return apply_filters( 'bp_notifications_get_registered_components', $component_names, $active_components );
 662  }
 663  
 664  /**
 665   * Catch and route the 'settings' notifications screen.
 666   *
 667   * This is currently unused.
 668   *
 669   * @since 1.9.0
 670   */
 671  function bp_notifications_screen_settings() {}
 672  
 673  /** Meta **********************************************************************/
 674  
 675  /**
 676   * Delete a meta entry from the DB for a notification item.
 677   *
 678   * @since 2.3.0
 679   *
 680   * @global object $wpdb WordPress database access object.
 681   *
 682   * @param int    $notification_id ID of the notification item whose metadata is being deleted.
 683   * @param string $meta_key        Optional. The key of the metadata being deleted. If
 684   *                                omitted, all metadata associated with the notification
 685   *                                item will be deleted.
 686   * @param string $meta_value      Optional. If present, the metadata will only be
 687   *                                deleted if the meta_value matches this parameter.
 688   * @param bool   $delete_all      Optional. If true, delete matching metadata entries
 689   *                                for all objects, ignoring the specified object_id. Otherwise,
 690   *                                only delete matching metadata entries for the specified
 691   *                                notification item. Default: false.
 692   * @return bool                   True on success, false on failure.
 693   */
 694  function bp_notifications_delete_meta( $notification_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
 695  
 696      // Legacy - if no meta_key is passed, delete all for the item.
 697      if ( empty( $meta_key ) ) {
 698          $all_meta = bp_notifications_get_meta( $notification_id );
 699          $keys     = ! empty( $all_meta )
 700              ? array_keys( $all_meta )
 701              : array();
 702  
 703          // With no meta_key, ignore $delete_all.
 704          $delete_all = false;
 705      } else {
 706          $keys = array( $meta_key );
 707      }
 708  
 709      $retval = true;
 710  
 711      add_filter( 'query', 'bp_filter_metaid_column_name' );
 712      foreach ( $keys as $key ) {
 713          $retval = delete_metadata( 'notification', $notification_id, $key, $meta_value, $delete_all );
 714      }
 715      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 716  
 717      return $retval;
 718  }
 719  
 720  /**
 721   * Get metadata for a given notification item.
 722   *
 723   * @since 2.3.0
 724   *
 725   * @param int    $notification_id ID of the notification item whose metadata is being requested.
 726   * @param string $meta_key        Optional. If present, only the metadata matching
 727   *                                that meta key will be returned. Otherwise, all metadata for the
 728   *                                notification item will be fetched.
 729   * @param bool   $single          Optional. If true, return only the first value of the
 730   *                                specified meta_key. This parameter has no effect if meta_key is not
 731   *                                specified. Default: true.
 732   * @return mixed                  The meta value(s) being requested.
 733   */
 734  function bp_notifications_get_meta( $notification_id = 0, $meta_key = '', $single = true ) {
 735      add_filter( 'query', 'bp_filter_metaid_column_name' );
 736      $retval = get_metadata( 'notification', $notification_id, $meta_key, $single );
 737      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 738  
 739      /**
 740       * Filters the metadata for a specified notification item.
 741       *
 742       * @since 2.3.0
 743       *
 744       * @param mixed  $retval          The meta values for the notification item.
 745       * @param int    $notification_id ID of the notification item.
 746       * @param string $meta_key        Meta key for the value being requested.
 747       * @param bool   $single          Whether to return one matched meta key row or all.
 748       */
 749      return apply_filters( 'bp_notifications_get_meta', $retval, $notification_id, $meta_key, $single );
 750  }
 751  
 752  /**
 753   * Update a piece of notification meta.
 754   *
 755   * @since 1.2.0
 756   *
 757   * @param  int    $notification_id ID of the notification item whose metadata is being
 758   *                                 updated.
 759   * @param  string $meta_key        Key of the metadata being updated.
 760   * @param  mixed  $meta_value      Value to be set.
 761   * @param  mixed  $prev_value      Optional. If specified, only update existing
 762   *                                 metadata entries with the specified value.
 763   *                                 Otherwise, update all entries.
 764   * @return bool|int                Returns false on failure. On successful
 765   *                                 update of existing metadata, returns true. On
 766   *                                 successful creation of new metadata,  returns
 767   *                                 the integer ID of the new metadata row.
 768   */
 769  function bp_notifications_update_meta( $notification_id, $meta_key, $meta_value, $prev_value = '' ) {
 770      add_filter( 'query', 'bp_filter_metaid_column_name' );
 771      $retval = update_metadata( 'notification', $notification_id, $meta_key, $meta_value, $prev_value );
 772      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 773  
 774      return $retval;
 775  }
 776  
 777  /**
 778   * Add a piece of notification metadata.
 779   *
 780   * @since 2.3.0
 781   *
 782   * @param int    $notification_id ID of the notification item.
 783   * @param string $meta_key        Metadata key.
 784   * @param mixed  $meta_value      Metadata value.
 785   * @param bool   $unique          Optional. Whether to enforce a single metadata value
 786   *                                for the given key. If true, and the object already has a value for
 787   *                                the key, no change will be made. Default: false.
 788   * @return int|bool               The meta ID on successful update, false on failure.
 789   */
 790  function bp_notifications_add_meta( $notification_id, $meta_key, $meta_value, $unique = false ) {
 791      add_filter( 'query', 'bp_filter_metaid_column_name' );
 792      $retval = add_metadata( 'notification', $notification_id, $meta_key, $meta_value, $unique );
 793      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 794  
 795      return $retval;
 796  }
 797  
 798  /**
 799   * Finds and exports personal data associated with an email address from the Notifications tables.
 800   *
 801   * @since 4.0.0
 802   *
 803   * @param string $email_address  The users email address.
 804   * @param int    $page           Batch number.
 805   * @return array An array of personal data.
 806   */
 807  function bp_notifications_personal_data_exporter( $email_address, $page ) {
 808      $number = 50;
 809  
 810      $email_address = trim( $email_address );
 811  
 812      $data_to_export = array();
 813  
 814      $user = get_user_by( 'email', $email_address );
 815  
 816      if ( ! $user ) {
 817          return array(
 818              'data' => array(),
 819              'done' => true,
 820          );
 821      }
 822  
 823      $notifications = BP_Notifications_Notification::get( array(
 824          'is_new'   => null,
 825          'per_page' => $number,
 826          'page'     => $page,
 827          'user_id'  => $user->ID,
 828          'order'    => 'DESC',
 829      ) );
 830  
 831      $user_data_to_export = array();
 832  
 833      foreach ( $notifications as $notification ) {
 834          if ( 'xprofile' === $notification->component_name ) {
 835              $component_name = 'profile';
 836          } else {
 837              $component_name = $notification->component_name;
 838          }
 839  
 840          // Format notifications.
 841          if ( isset( buddypress()->{$component_name}->notification_callback ) && is_callable( buddypress()->{$component_name}->notification_callback ) ) {
 842              $content = call_user_func( buddypress()->{$component_name}->notification_callback, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1, 'string', $notification->id );
 843          } else {
 844              /*
 845               * Compile an array of data to send to filter.
 846               *
 847               * Note that a null value is passed in the slot filled by `total_count` in
 848               * other filter contexts. We don't have enough info here to pass a `total_count`.
 849               */
 850              $ref_array = array(
 851                  $notification->component_action,
 852                  $notification->item_id,
 853                  $notification->secondary_item_id,
 854                  null,
 855                  'string',
 856                  $notification->component_action,
 857                  $component_name,
 858                  $notification->id,
 859              );
 860  
 861              /** This filter is documented in bp-notifications/bp-notifications-functions.php */
 862              $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
 863          }
 864  
 865          $item_data = array(
 866              array(
 867                  'name'  => __( 'Notification Content', 'buddypress' ),
 868                  'value' => $content,
 869              ),
 870              array(
 871                  'name'  => __( 'Notification Date', 'buddypress' ),
 872                  'value' => $notification->date_notified,
 873              ),
 874              array(
 875                  'name'  => __( 'Status', 'buddypress' ),
 876                  'value' => $notification->is_new ? __( 'Unread', 'buddypress' ) : __( 'Read', 'buddypress' ),
 877              ),
 878          );
 879  
 880          $data_to_export[] = array(
 881              'group_id'    => 'bp_notifications',
 882              'group_label' => __( 'Notifications', 'buddypress' ),
 883              'item_id'     => "bp-notifications-{$notification->id}",
 884              'data'        => $item_data,
 885          );
 886      }
 887  
 888      // Tell core if we have more items to process.
 889      $done = count( $notifications ) < $number;
 890  
 891      return array(
 892          'data' => $data_to_export,
 893          'done' => $done,
 894      );
 895  }


Generated: Tue Nov 19 01:01:36 2019 Cross-referenced by PHPXref 0.7.1