[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
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 = isset( $content['text'] ) ? $content['text'] : ''; 237 $notification_object->href = isset( $content['link'] ) ? $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 449 /** 450 * Deletes user notifications data on the 'delete_user' hook. 451 * 452 * @since 6.0.0 453 * 454 * @param int $user_id The ID of the deleted user. 455 */ 456 function bp_notifications_delete_notifications_on_delete_user( $user_id ) { 457 if ( ! bp_remove_user_data_on_delete_user_hook( 'notifications', $user_id ) ) { 458 return; 459 } 460 461 bp_notifications_delete_notifications_on_user_delete( $user_id ); 462 } 463 464 add_action( 'delete_user', 'bp_notifications_delete_notifications_on_delete_user' ); 465 466 /** Mark **********************************************************************/ 467 468 /** 469 * Mark notifications read/unread for a user by type. 470 * 471 * Used when clearing out notifications for a specific component when the user 472 * has visited that component. 473 * 474 * @since 1.9.0 475 * 476 * @param int $user_id ID of the user whose notifications are being deleted. 477 * @param string $component_name Name of the associated component. 478 * @param string $component_action Name of the associated action. 479 * @param int|bool $is_new 0 for read, 1 for unread. 480 * @return int|false True on success, false on failure. 481 */ 482 function bp_notifications_mark_notifications_by_type( $user_id, $component_name, $component_action, $is_new = false ) { 483 return BP_Notifications_Notification::update( 484 array( 485 'is_new' => $is_new 486 ), 487 array( 488 'user_id' => $user_id, 489 'component_name' => $component_name, 490 'component_action' => $component_action 491 ) 492 ); 493 } 494 495 /** 496 * Mark notifications read/unread for an item ID. 497 * 498 * Used when clearing out notifications for a specific component when the user 499 * has visited that component. 500 * 501 * @since 1.9.0 502 * 503 * @param int $user_id ID of the user whose notifications are being deleted. 504 * @param int $item_id ID of the associated item. 505 * @param string $component_name Name of the associated component. 506 * @param string $component_action Name of the associated action. 507 * @param int|bool $secondary_item_id ID of the secondary associated item. 508 * @param int|bool $is_new 0 for read, 1 for unread. 509 * @return int|false True on success, false on failure. 510 */ 511 function bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false, $is_new = false ) { 512 return BP_Notifications_Notification::update( 513 array( 514 'is_new' => $is_new 515 ), 516 array( 517 'user_id' => $user_id, 518 'item_id' => $item_id, 519 'secondary_item_id' => $secondary_item_id, 520 'component_name' => $component_name, 521 'component_action' => $component_action 522 ) 523 ); 524 } 525 526 /** 527 * Mark all notifications read/unread by type. 528 * 529 * Used when clearing out notifications for an entire component. 530 * 531 * @since 1.9.0 532 * 533 * @param int $item_id ID of the user whose notifications are being deleted. 534 * @param string $component_name Name of the associated component. 535 * @param string|bool $component_action Optional. Name of the associated action. 536 * @param int|bool $secondary_item_id Optional. ID of the secondary associated item. 537 * @param int|bool $is_new 0 for read, 1 for unread. 538 * @return int|false True on success, false on failure. 539 */ 540 function bp_notifications_mark_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false, $is_new = false ) { 541 return BP_Notifications_Notification::update( 542 array( 543 'is_new' => $is_new 544 ), 545 array( 546 'item_id' => $item_id, 547 'secondary_item_id' => $secondary_item_id, 548 'component_name' => $component_name, 549 'component_action' => $component_action 550 ) 551 ); 552 } 553 554 /** 555 * Mark all notifications read/unread from a user. 556 * 557 * Used when clearing out all notifications for a user, when deleted or spammed. 558 * 559 * @todo This function assumes that items with the user_id in the item_id slot 560 * are associated with that user. However, this will only be true with 561 * certain components (such as Friends). Use with caution! 562 * 563 * @since 1.9.0 564 * 565 * @param int $user_id ID of the user whose associated items are being deleted. 566 * @param string $component_name Name of the associated component. 567 * @param string $component_action Name of the associated action. 568 * @param int|bool $is_new 0 for read, 1 for unread. 569 * @return int|false True on success, false on failure. 570 */ 571 function bp_notifications_mark_notifications_from_user( $user_id, $component_name, $component_action, $is_new = false ) { 572 return BP_Notifications_Notification::update( 573 array( 574 'is_new' => $is_new 575 ), 576 array( 577 'item_id' => $user_id, 578 'component_name' => $component_name, 579 'component_action' => $component_action 580 ) 581 ); 582 } 583 584 /** Helpers *******************************************************************/ 585 586 /** 587 * Check if a user has access to a specific notification. 588 * 589 * Used before deleting a notification for a user. 590 * 591 * @since 1.9.0 592 * 593 * @param int $user_id ID of the user being checked. 594 * @param int $notification_id ID of the notification being checked. 595 * @return bool True if the notification belongs to the user, otherwise false. 596 */ 597 function bp_notifications_check_notification_access( $user_id, $notification_id ) { 598 return (bool) BP_Notifications_Notification::check_access( $user_id, $notification_id ); 599 } 600 601 /** 602 * Get a count of unread notification items for a user. 603 * 604 * @since 1.9.0 605 * 606 * @param int $user_id ID of the user whose unread notifications are being 607 * counted. 608 * @return int Unread notification count. 609 */ 610 function bp_notifications_get_unread_notification_count( $user_id = 0 ) { 611 if ( empty( $user_id ) ) { 612 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 613 } 614 615 $count = wp_cache_get( $user_id, 'bp_notifications_unread_count' ); 616 if ( false === $count ) { 617 $count = BP_Notifications_Notification::get_total_count( array( 618 'user_id' => $user_id, 619 'is_new' => true, 620 ) ); 621 wp_cache_set( $user_id, $count, 'bp_notifications_unread_count' ); 622 } 623 624 /** 625 * Filters the count of unread notification items for a user. 626 * 627 * @since 1.9.0 628 * @since 2.7.0 Added user ID parameter. 629 * 630 * @param int $count Count of unread notification items for a user. 631 * @param int $user_id User ID for notifications count. 632 */ 633 return apply_filters( 'bp_notifications_get_total_notification_count', (int) $count, $user_id ); 634 } 635 636 /** 637 * Return an array of component names that are currently active and have 638 * registered Notifications callbacks. 639 * 640 * @since 1.9.1 641 * 642 * @see http://buddypress.trac.wordpress.org/ticket/5300 643 * 644 * @return array $component_names Array of registered components. 645 */ 646 function bp_notifications_get_registered_components() { 647 648 // Load BuddyPress. 649 $bp = buddypress(); 650 651 // Setup return value. 652 $component_names = array(); 653 654 // Get the active components. 655 $active_components = array_keys( $bp->active_components ); 656 657 // Loop through components, look for callbacks, add to return value. 658 foreach ( $active_components as $component ) { 659 if ( !empty( $bp->$component->notification_callback ) ) { 660 $component_names[] = $component; 661 } 662 // The extended profile component is identified in the active_components array as 'xprofile'. 663 // However, the extended profile child object has the key 'profile' in the $bp object. 664 if ( 'xprofile' == $component && ! empty( $bp->profile->notification_callback ) ) { 665 $component_names[] = $component; 666 } 667 } 668 669 /** 670 * Filters active components with registered notifications callbacks. 671 * 672 * @since 1.9.1 673 * 674 * @param array $component_names Array of registered component names. 675 * @param array $active_components Array of active components. 676 */ 677 return apply_filters( 'bp_notifications_get_registered_components', $component_names, $active_components ); 678 } 679 680 /** 681 * Catch and route the 'settings' notifications screen. 682 * 683 * This is currently unused. 684 * 685 * @since 1.9.0 686 */ 687 function bp_notifications_screen_settings() {} 688 689 /** Meta **********************************************************************/ 690 691 /** 692 * Delete a meta entry from the DB for a notification item. 693 * 694 * @since 2.3.0 695 * 696 * @global object $wpdb WordPress database access object. 697 * 698 * @param int $notification_id ID of the notification item whose metadata is being deleted. 699 * @param string $meta_key Optional. The key of the metadata being deleted. If 700 * omitted, all metadata associated with the notification 701 * item will be deleted. 702 * @param string $meta_value Optional. If present, the metadata will only be 703 * deleted if the meta_value matches this parameter. 704 * @param bool $delete_all Optional. If true, delete matching metadata entries 705 * for all objects, ignoring the specified object_id. Otherwise, 706 * only delete matching metadata entries for the specified 707 * notification item. Default: false. 708 * @return bool True on success, false on failure. 709 */ 710 function bp_notifications_delete_meta( $notification_id, $meta_key = '', $meta_value = '', $delete_all = false ) { 711 712 // Legacy - if no meta_key is passed, delete all for the item. 713 if ( empty( $meta_key ) ) { 714 $all_meta = bp_notifications_get_meta( $notification_id ); 715 $keys = ! empty( $all_meta ) 716 ? array_keys( $all_meta ) 717 : array(); 718 719 // With no meta_key, ignore $delete_all. 720 $delete_all = false; 721 } else { 722 $keys = array( $meta_key ); 723 } 724 725 $retval = true; 726 727 add_filter( 'query', 'bp_filter_metaid_column_name' ); 728 foreach ( $keys as $key ) { 729 $retval = delete_metadata( 'notification', $notification_id, $key, $meta_value, $delete_all ); 730 } 731 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 732 733 return $retval; 734 } 735 736 /** 737 * Get metadata for a given notification item. 738 * 739 * @since 2.3.0 740 * 741 * @param int $notification_id ID of the notification item whose metadata is being requested. 742 * @param string $meta_key Optional. If present, only the metadata matching 743 * that meta key will be returned. Otherwise, all metadata for the 744 * notification item will be fetched. 745 * @param bool $single Optional. If true, return only the first value of the 746 * specified meta_key. This parameter has no effect if meta_key is not 747 * specified. Default: true. 748 * @return mixed The meta value(s) being requested. 749 */ 750 function bp_notifications_get_meta( $notification_id = 0, $meta_key = '', $single = true ) { 751 add_filter( 'query', 'bp_filter_metaid_column_name' ); 752 $retval = get_metadata( 'notification', $notification_id, $meta_key, $single ); 753 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 754 755 /** 756 * Filters the metadata for a specified notification item. 757 * 758 * @since 2.3.0 759 * 760 * @param mixed $retval The meta values for the notification item. 761 * @param int $notification_id ID of the notification item. 762 * @param string $meta_key Meta key for the value being requested. 763 * @param bool $single Whether to return one matched meta key row or all. 764 */ 765 return apply_filters( 'bp_notifications_get_meta', $retval, $notification_id, $meta_key, $single ); 766 } 767 768 /** 769 * Update a piece of notification meta. 770 * 771 * @since 1.2.0 772 * 773 * @param int $notification_id ID of the notification item whose metadata is being 774 * updated. 775 * @param string $meta_key Key of the metadata being updated. 776 * @param mixed $meta_value Value to be set. 777 * @param mixed $prev_value Optional. If specified, only update existing 778 * metadata entries with the specified value. 779 * Otherwise, update all entries. 780 * @return bool|int Returns false on failure. On successful 781 * update of existing metadata, returns true. On 782 * successful creation of new metadata, returns 783 * the integer ID of the new metadata row. 784 */ 785 function bp_notifications_update_meta( $notification_id, $meta_key, $meta_value, $prev_value = '' ) { 786 add_filter( 'query', 'bp_filter_metaid_column_name' ); 787 $retval = update_metadata( 'notification', $notification_id, $meta_key, $meta_value, $prev_value ); 788 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 789 790 return $retval; 791 } 792 793 /** 794 * Add a piece of notification metadata. 795 * 796 * @since 2.3.0 797 * 798 * @param int $notification_id ID of the notification item. 799 * @param string $meta_key Metadata key. 800 * @param mixed $meta_value Metadata value. 801 * @param bool $unique Optional. Whether to enforce a single metadata value 802 * for the given key. If true, and the object already has a value for 803 * the key, no change will be made. Default: false. 804 * @return int|bool The meta ID on successful update, false on failure. 805 */ 806 function bp_notifications_add_meta( $notification_id, $meta_key, $meta_value, $unique = false ) { 807 add_filter( 'query', 'bp_filter_metaid_column_name' ); 808 $retval = add_metadata( 'notification', $notification_id, $meta_key, $meta_value, $unique ); 809 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 810 811 return $retval; 812 } 813 814 /** 815 * Finds and exports personal data associated with an email address from the Notifications tables. 816 * 817 * @since 4.0.0 818 * 819 * @param string $email_address The users email address. 820 * @param int $page Batch number. 821 * @return array An array of personal data. 822 */ 823 function bp_notifications_personal_data_exporter( $email_address, $page ) { 824 $number = 50; 825 826 $email_address = trim( $email_address ); 827 828 $data_to_export = array(); 829 830 $user = get_user_by( 'email', $email_address ); 831 832 if ( ! $user ) { 833 return array( 834 'data' => array(), 835 'done' => true, 836 ); 837 } 838 839 $notifications = BP_Notifications_Notification::get( array( 840 'is_new' => null, 841 'per_page' => $number, 842 'page' => $page, 843 'user_id' => $user->ID, 844 'order' => 'DESC', 845 ) ); 846 847 $user_data_to_export = array(); 848 849 foreach ( $notifications as $notification ) { 850 if ( 'xprofile' === $notification->component_name ) { 851 $component_name = 'profile'; 852 } else { 853 $component_name = $notification->component_name; 854 } 855 856 // Format notifications. 857 if ( isset( buddypress()->{$component_name}->notification_callback ) && is_callable( buddypress()->{$component_name}->notification_callback ) ) { 858 $content = call_user_func( buddypress()->{$component_name}->notification_callback, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1, 'string', $notification->id ); 859 } else { 860 /* 861 * Compile an array of data to send to filter. 862 * 863 * Note that a null value is passed in the slot filled by `total_count` in 864 * other filter contexts. We don't have enough info here to pass a `total_count`. 865 */ 866 $ref_array = array( 867 $notification->component_action, 868 $notification->item_id, 869 $notification->secondary_item_id, 870 null, 871 'string', 872 $notification->component_action, 873 $component_name, 874 $notification->id, 875 ); 876 877 /** This filter is documented in bp-notifications/bp-notifications-functions.php */ 878 $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array ); 879 } 880 881 $item_data = array( 882 array( 883 'name' => __( 'Notification Content', 'buddypress' ), 884 'value' => $content, 885 ), 886 array( 887 'name' => __( 'Notification Date', 'buddypress' ), 888 'value' => $notification->date_notified, 889 ), 890 array( 891 'name' => __( 'Status', 'buddypress' ), 892 'value' => $notification->is_new ? __( 'Unread', 'buddypress' ) : __( 'Read', 'buddypress' ), 893 ), 894 ); 895 896 $data_to_export[] = array( 897 'group_id' => 'bp_notifications', 898 'group_label' => __( 'Notifications', 'buddypress' ), 899 'item_id' => "bp-notifications-{$notification->id}", 900 'data' => $item_data, 901 ); 902 } 903 904 // Tell core if we have more items to process. 905 $done = count( $notifications ) < $number; 906 907 return array( 908 'data' => $data_to_export, 909 'done' => $done, 910 ); 911 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Feb 25 01:01:43 2021 | Cross-referenced by PHPXref 0.7.1 |