Completed
Push — master ( fc7440...d56d93 )
by
unknown
19:37
created

bp-notifications-functions.php ➔ bp_notifications_get_unread_notification_count()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 10

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 25
loc 25
rs 8.5806
c 0
b 0
f 0
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 View Code Duplication
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();
0 ignored issues
show
Coding Style introduced by John James Jacoby
Consider using a different name than the parameter $user_id. This often makes code more readable.
Loading history...
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 notifications for a specific user.
167
 *
168
 * @since 1.9.0
169
 *
170
 * @param int    $user_id ID of the user whose notifications are being fetched.
171
 * @param string $format  Format of the returned values. 'string' returns HTML,
172
 *                        while 'object' returns a structured object for parsing.
173
 * @return mixed Object or array on success, false on failure.
174
 */
175
function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
176
177
	// Setup local variables.
178
	$bp = buddypress();
179
180
	// Get notifications (out of the cache, or query if necessary).
181
	$notifications         = bp_notifications_get_all_notifications_for_user( $user_id );
182
	$grouped_notifications = array(); // Notification groups.
183
	$renderable            = array(); // Renderable notifications.
184
185
	// Group notifications by component and component_action and provide totals.
186
	for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
187
		$notification = $notifications[$i];
188
		$grouped_notifications[$notification->component_name][$notification->component_action][] = $notification;
189
	}
190
191
	// Bail if no notification groups.
192
	if ( empty( $grouped_notifications ) ) {
193
		return false;
194
	}
195
196
	// Calculate a renderable output for each notification type.
197
	foreach ( $grouped_notifications as $component_name => $action_arrays ) {
198
199
		// We prefer that extended profile component-related notifications use
200
		// the component_name of 'xprofile'. However, the extended profile child
201
		// object in the $bp object is keyed as 'profile', which is where we need
202
		// to look for the registered notification callback.
203
		if ( 'xprofile' == $component_name ) {
204
			$component_name = 'profile';
205
		}
206
207
		// Skip if group is empty.
208
		if ( empty( $action_arrays ) ) {
209
			continue;
210
		}
211
212
		// Loop through each actionable item and try to map it to a component.
213
		foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) {
214
215
			// Get the number of actionable items.
216
			$action_item_count = count( $component_action_items );
217
218
			// Skip if the count is less than 1.
219
			if ( $action_item_count < 1 ) {
220
				continue;
221
			}
222
223
			// Callback function exists.
224
			if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
225
226
				// Function should return an object.
227
				if ( 'object' === $format ) {
228
229
					// Retrieve the content of the notification using the callback.
230
					$content = call_user_func(
231
						$bp->{$component_name}->notification_callback,
232
						$component_action_name,
233
						$component_action_items[0]->item_id,
234
						$component_action_items[0]->secondary_item_id,
235
						$action_item_count,
236
						'array',
237
						$component_action_items[0]->id
238
					);
239
240
					// Create the object to be returned.
241
					$notification_object = $component_action_items[0];
242
243
					// Minimal backpat with non-compatible notification
244
					// callback functions.
245 View Code Duplication
					if ( is_string( $content ) ) {
246
						$notification_object->content = $content;
247
						$notification_object->href    = bp_loggedin_user_domain();
248
					} else {
249
						$notification_object->content = $content['text'];
250
						$notification_object->href    = $content['link'];
251
					}
252
253
					$renderable[] = $notification_object;
254
255
				// Return an array of content strings.
256
				} else {
257
					$content      = call_user_func( $bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count, 'string', $component_action_items[0]->id );
258
					$renderable[] = $content;
259
				}
260
261
			// @deprecated format_notification_function - 1.5
262
			} elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) {
263
				$renderable[] = call_user_func( $bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count );
264
265
			// Allow non BuddyPress components to hook in.
266
			} else {
267
268
				// The array to reference with apply_filters_ref_array().
269
				$ref_array = array(
270
					$component_action_name,
271
					$component_action_items[0]->item_id,
272
					$component_action_items[0]->secondary_item_id,
273
					$action_item_count,
274
					$format,
275
					$component_action_name, // Duplicated so plugins can check the canonical action name.
276
					$component_name,
277
					$component_action_items[0]->id
278
				);
279
280
				// Function should return an object.
281
				if ( 'object' === $format ) {
282
283
					/**
284
					 * Filters the notification content for notifications created by plugins.
285
					 *
286
					 * If your plugin extends the {@link BP_Component} class, you should use the
287
					 * 'notification_callback' parameter in your extended
288
					 * {@link BP_Component::setup_globals()} method instead.
289
					 *
290
					 * @since 1.9.0
291
					 * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters.
292
					 *
293
					 * @param string $content               Component action. Deprecated. Do not do checks against this! Use
294
					 *                                      the 6th parameter instead - $component_action_name.
295
					 * @param int    $item_id               Notification item ID.
296
					 * @param int    $secondary_item_id     Notification secondary item ID.
297
					 * @param int    $action_item_count     Number of notifications with the same action.
298
					 * @param string $format                Format of return. Either 'string' or 'object'.
299
					 * @param string $component_action_name Canonical notification action.
300
					 * @param string $component_name        Notification component ID.
301
					 * @param int    $id                    Notification ID.
302
					 *
303
					 * @return string|array If $format is 'string', return a string of the notification content.
304
					 *                      If $format is 'object', return an array formatted like:
305
					 *                      array( 'text' => 'CONTENT', 'link' => 'LINK' )
306
					 */
307
					$content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
308
309
					// Create the object to be returned.
310
					$notification_object = $component_action_items[0];
311
312
					// Minimal backpat with non-compatible notification
313
					// callback functions.
314 View Code Duplication
					if ( is_string( $content ) ) {
315
						$notification_object->content = $content;
316
						$notification_object->href    = bp_loggedin_user_domain();
317
					} else {
318
						$notification_object->content = $content['text'];
319
						$notification_object->href    = $content['link'];
320
					}
321
322
					$renderable[] = $notification_object;
323
324
				// Return an array of content strings.
325
				} else {
326
327
					/** This filters is documented in bp-notifications/bp-notifications-functions.php */
328
					$renderable[] = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array );
329
				}
330
			}
331
		}
332
	}
333
334
	// If renderable is empty array, set to false.
335
	if ( empty( $renderable ) ) {
336
		$renderable = false;
337
	}
338
339
	/**
340
	 * Filters the final array of notifications to be displayed for a user.
341
	 *
342
	 * @since 1.6.0
343
	 *
344
	 * @param array|bool $renderable Array of notifications to render or false if no notifications.
345
	 * @param int        $user_id    ID of the user whose notifications are being displayed.
346
	 * @param string     $format     Display format requested for the notifications.
347
	 */
348
	return apply_filters( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
349
}
350
351
/** Delete ********************************************************************/
352
353
/**
354
 * Delete notifications for a user by type.
355
 *
356
 * Used when clearing out notifications for a specific component when the user
357
 * has visited that component.
358
 *
359
 * @since 1.9.0
360
 *
361
 * @param int    $user_id          ID of the user whose notifications are being deleted.
362
 * @param string $component_name   Name of the associated component.
363
 * @param string $component_action Name of the associated action.
364
 * @return int|false True on success, false on failure.
365
 */
366
function bp_notifications_delete_notifications_by_type( $user_id, $component_name, $component_action ) {
367
	return BP_Notifications_Notification::delete( array(
368
		'user_id'          => $user_id,
369
		'component_name'   => $component_name,
370
		'component_action' => $component_action,
371
	) );
372
}
373
374
/**
375
 * Delete notifications for an item ID.
376
 *
377
 * Used when clearing out notifications for a specific component when the user
378
 * has visited that component.
379
 *
380
 * @since 1.9.0
381
 *
382
 * @param int      $user_id           ID of the user whose notifications are being deleted.
383
 * @param int      $item_id           ID of the associated item.
384
 * @param string   $component_name    Name of the associated component.
385
 * @param string   $component_action  Name of the associated action.
386
 * @param int|bool $secondary_item_id ID of the secondary associated item.
387
 * @return int|false True on success, false on failure.
388
 */
389 View Code Duplication
function bp_notifications_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
390
	return BP_Notifications_Notification::delete( array(
391
		'user_id'           => $user_id,
392
		'item_id'           => $item_id,
393
		'secondary_item_id' => $secondary_item_id,
394
		'component_name'    => $component_name,
395
		'component_action'  => $component_action,
396
	) );
397
}
398
399
/**
400
 * Delete all notifications by type.
401
 *
402
 * Used when clearing out notifications for an entire component.
403
 *
404
 * @since 1.9.0
405
 *
406
 * @param int         $item_id           ID of the user whose notifications are being deleted.
407
 * @param string      $component_name    Name of the associated component.
408
 * @param string|bool $component_action  Optional. Name of the associated action.
409
 * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
410
 * @return int|false True on success, false on failure.
411
 */
412 View Code Duplication
function bp_notifications_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) {
413
	return BP_Notifications_Notification::delete( array(
414
		'item_id'           => $item_id,
415
		'secondary_item_id' => $secondary_item_id,
416
		'component_name'    => $component_name,
417
		'component_action'  => $component_action,
418
	) );
419
}
420
421
/**
422
 * Delete all notifications from a user.
423
 *
424
 * Used when clearing out all notifications for a user, when deleted or spammed.
425
 *
426
 * @todo This function assumes that items with the user_id in the item_id slot
427
 *       are associated with that user. However, this will only be true with
428
 *       certain components (such as Friends). Use with caution!
429
 *
430
 * @since 1.9.0
431
 *
432
 * @param int    $user_id          ID of the user whose associated items are being deleted.
433
 * @param string $component_name   Name of the associated component.
434
 * @param string $component_action Name of the associated action.
435
 * @return int|false True on success, false on failure.
436
 */
437
function bp_notifications_delete_notifications_from_user( $user_id, $component_name, $component_action ) {
438
	return BP_Notifications_Notification::delete( array(
439
		'item_id'           => $user_id,
440
		'component_name'    => $component_name,
441
		'component_action'  => $component_action,
442
	) );
443
}
444
445
/**
446
 * Delete a user's notifications when the user is deleted.
447
 *
448
 * @since 2.5.0
449
 *
450
 * @param int $user_id ID of the user who is about to be deleted.
451
 * @return int|false The number of rows deleted, or false on error.
452
 */
453
function bp_notifications_delete_notifications_on_user_delete( $user_id ) {
454
	return BP_Notifications_Notification::delete( array(
455
		'user_id'           => $user_id,
456
		'item_id'           => false,
457
		'secondary_item_id' => false,
458
		'component_action'  => false,
459
		'component_name'    => false,
460
	) );
461
}
462
add_action( 'wpmu_delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
463
add_action( 'delete_user', 'bp_notifications_delete_notifications_on_user_delete' );
464
465
/** Mark **********************************************************************/
466
467
/**
468
 * Mark notifications read/unread for a user by type.
469
 *
470
 * Used when clearing out notifications for a specific component when the user
471
 * has visited that component.
472
 *
473
 * @since 1.9.0
474
 *
475
 * @param int      $user_id          ID of the user whose notifications are being deleted.
476
 * @param string   $component_name   Name of the associated component.
477
 * @param string   $component_action Name of the associated action.
478
 * @param int|bool $is_new           0 for read, 1 for unread.
479
 * @return int|false True on success, false on failure.
480
 */
481
function bp_notifications_mark_notifications_by_type( $user_id, $component_name, $component_action, $is_new = false ) {
482
	return BP_Notifications_Notification::update(
483
		array(
484
			'is_new' => $is_new
485
		),
486
		array(
487
			'user_id'          => $user_id,
488
			'component_name'   => $component_name,
489
			'component_action' => $component_action
490
		)
491
	);
492
}
493
494
/**
495
 * Mark notifications read/unread for an item ID.
496
 *
497
 * Used when clearing out notifications for a specific component when the user
498
 * has visited that component.
499
 *
500
 * @since 1.9.0
501
 *
502
 * @param int      $user_id           ID of the user whose notifications are being deleted.
503
 * @param int      $item_id           ID of the associated item.
504
 * @param string   $component_name    Name of the associated component.
505
 * @param string   $component_action  Name of the associated action.
506
 * @param int|bool $secondary_item_id ID of the secondary associated item.
507
 * @param int|bool $is_new            0 for read, 1 for unread.
508
 * @return int|false True on success, false on failure.
509
 */
510 View Code Duplication
function bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false, $is_new = false ) {
511
	return BP_Notifications_Notification::update(
512
		array(
513
			'is_new' => $is_new
514
		),
515
		array(
516
			'user_id'           => $user_id,
517
			'item_id'           => $item_id,
518
			'secondary_item_id' => $secondary_item_id,
519
			'component_name'    => $component_name,
520
			'component_action'  => $component_action
521
		)
522
	);
523
}
524
525
/**
526
 * Mark all notifications read/unread by type.
527
 *
528
 * Used when clearing out notifications for an entire component.
529
 *
530
 * @since 1.9.0
531
 *
532
 * @param int         $item_id           ID of the user whose notifications are being deleted.
533
 * @param string      $component_name    Name of the associated component.
534
 * @param string|bool $component_action  Optional. Name of the associated action.
535
 * @param int|bool    $secondary_item_id Optional. ID of the secondary associated item.
536
 * @param int|bool    $is_new            0 for read, 1 for unread.
537
 * @return int|false True on success, false on failure.
538
 */
539 View Code Duplication
function bp_notifications_mark_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false, $is_new = false ) {
540
	return BP_Notifications_Notification::update(
541
		array(
542
			'is_new' => $is_new
543
		),
544
		array(
545
			'item_id'           => $item_id,
546
			'secondary_item_id' => $secondary_item_id,
547
			'component_name'    => $component_name,
548
			'component_action'  => $component_action
549
		)
550
	);
551
}
552
553
/**
554
 * Mark all notifications read/unread from a user.
555
 *
556
 * Used when clearing out all notifications for a user, when deleted or spammed.
557
 *
558
 * @todo This function assumes that items with the user_id in the item_id slot
559
 *       are associated with that user. However, this will only be true with
560
 *       certain components (such as Friends). Use with caution!
561
 *
562
 * @since 1.9.0
563
 *
564
 * @param int      $user_id          ID of the user whose associated items are being deleted.
565
 * @param string   $component_name   Name of the associated component.
566
 * @param string   $component_action Name of the associated action.
567
 * @param int|bool $is_new           0 for read, 1 for unread.
568
 * @return int|false True on success, false on failure.
569
 */
570
function bp_notifications_mark_notifications_from_user( $user_id, $component_name, $component_action, $is_new = false ) {
571
	return BP_Notifications_Notification::update(
572
		array(
573
			'is_new' => $is_new
574
		),
575
		array(
576
			'item_id'          => $user_id,
577
			'component_name'   => $component_name,
578
			'component_action' => $component_action
579
		)
580
	);
581
}
582
583
/** Helpers *******************************************************************/
584
585
/**
586
 * Check if a user has access to a specific notification.
587
 *
588
 * Used before deleting a notification for a user.
589
 *
590
 * @since 1.9.0
591
 *
592
 * @param int $user_id         ID of the user being checked.
593
 * @param int $notification_id ID of the notification being checked.
594
 * @return bool True if the notification belongs to the user, otherwise false.
595
 */
596
function bp_notifications_check_notification_access( $user_id, $notification_id ) {
597
	return (bool) BP_Notifications_Notification::check_access( $user_id, $notification_id );
598
}
599
600
/**
601
 * Get a count of unread notification items for a user.
602
 *
603
 * @since 1.9.0
604
 *
605
 * @param int $user_id ID of the user whose unread notifications are being
606
 *                     counted.
607
 * @return int Unread notification count.
608
 */
609 View Code Duplication
function bp_notifications_get_unread_notification_count( $user_id = 0 ) {
610
	if ( empty( $user_id ) ) {
611
		$user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
0 ignored issues
show
Coding Style introduced by John James Jacoby
Consider using a different name than the parameter $user_id. This often makes code more readable.
Loading history...
612
	}
613
614
	$count = wp_cache_get( $user_id, 'bp_notifications_unread_count' );
615
	if ( false === $count ) {
616
		$count = BP_Notifications_Notification::get_total_count( array(
617
			'user_id' => $user_id,
618
			'is_new'  => true,
619
		) );
620
		wp_cache_set( $user_id, $count, 'bp_notifications_unread_count' );
621
	}
622
623
	/**
624
	 * Filters the count of unread notification items for a user.
625
	 *
626
	 * @since 1.9.0
627
	 * @since 2.7.0 Added user ID parameter.
628
	 *
629
	 * @param int $count   Count of unread notification items for a user.
630
	 * @param int $user_id User ID for notifications count.
631
	 */
632
	return apply_filters( 'bp_notifications_get_total_notification_count', (int) $count, $user_id );
633
}
634
635
/**
636
 * Return an array of component names that are currently active and have
637
 * registered Notifications callbacks.
638
 *
639
 * @since 1.9.1
640
 *
641
 * @see http://buddypress.trac.wordpress.org/ticket/5300
642
 *
643
 * @return array $component_names Array of registered components.
644
 */
645
function bp_notifications_get_registered_components() {
646
647
	// Load BuddyPress.
648
	$bp = buddypress();
649
650
	// Setup return value.
651
	$component_names = array();
652
653
	// Get the active components.
654
	$active_components = array_keys( $bp->active_components );
655
656
	// Loop through components, look for callbacks, add to return value.
657
	foreach ( $active_components as $component ) {
658
		if ( !empty( $bp->$component->notification_callback ) ) {
659
			$component_names[] = $component;
660
		}
661
		// The extended profile component is identified in the active_components array as 'xprofile'.
662
		// However, the extended profile child object has the key 'profile' in the $bp object.
663
		if ( 'xprofile' == $component && ! empty( $bp->profile->notification_callback ) ) {
664
			$component_names[] = $component;
665
		}
666
	}
667
668
	/**
669
	 * Filters active components with registered notifications callbacks.
670
	 *
671
	 * @since 1.9.1
672
	 *
673
	 * @param array $component_names   Array of registered component names.
674
	 * @param array $active_components Array of active components.
675
	 */
676
	return apply_filters( 'bp_notifications_get_registered_components', $component_names, $active_components );
677
}
678
679
/** Meta **********************************************************************/
680
681
/**
682
 * Delete a meta entry from the DB for a notification item.
683
 *
684
 * @since 2.3.0
685
 *
686
 * @global object $wpdb WordPress database access object.
687
 *
688
 * @param int    $notification_id ID of the notification item whose metadata is being deleted.
689
 * @param string $meta_key        Optional. The key of the metadata being deleted. If
690
 *                                omitted, all metadata associated with the notification
691
 *                                item will be deleted.
692
 * @param string $meta_value      Optional. If present, the metadata will only be
693
 *                                deleted if the meta_value matches this parameter.
694
 * @param bool   $delete_all      Optional. If true, delete matching metadata entries
695
 *                                for all objects, ignoring the specified object_id. Otherwise,
696
 *                                only delete matching metadata entries for the specified
697
 *                                notification item. Default: false.
698
 * @return bool                   True on success, false on failure.
699
 */
700 View Code Duplication
function bp_notifications_delete_meta( $notification_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
701
702
	// Legacy - if no meta_key is passed, delete all for the item.
703
	if ( empty( $meta_key ) ) {
704
		$all_meta = bp_notifications_get_meta( $notification_id );
705
		$keys     = ! empty( $all_meta )
706
			? array_keys( $all_meta )
707
			: array();
708
709
		// With no meta_key, ignore $delete_all.
710
		$delete_all = false;
0 ignored issues
show
Coding Style introduced by John James Jacoby
Consider using a different name than the parameter $delete_all. This often makes code more readable.
Loading history...
711
	} else {
712
		$keys = array( $meta_key );
713
	}
714
715
	$retval = true;
716
717
	add_filter( 'query', 'bp_filter_metaid_column_name' );
718
	foreach ( $keys as $key ) {
719
		$retval = delete_metadata( 'notification', $notification_id, $key, $meta_value, $delete_all );
720
	}
721
	remove_filter( 'query', 'bp_filter_metaid_column_name' );
722
723
	return $retval;
724
}
725
726
/**
727
 * Get metadata for a given notification item.
728
 *
729
 * @since 2.3.0
730
 *
731
 * @param int    $notification_id ID of the notification item whose metadata is being requested.
732
 * @param string $meta_key        Optional. If present, only the metadata matching
733
 *                                that meta key will be returned. Otherwise, all metadata for the
734
 *                                notification item will be fetched.
735
 * @param bool   $single          Optional. If true, return only the first value of the
736
 *                                specified meta_key. This parameter has no effect if meta_key is not
737
 *                                specified. Default: true.
738
 * @return mixed                  The meta value(s) being requested.
739
 */
740 View Code Duplication
function bp_notifications_get_meta( $notification_id = 0, $meta_key = '', $single = true ) {
741
	add_filter( 'query', 'bp_filter_metaid_column_name' );
742
	$retval = get_metadata( 'notification', $notification_id, $meta_key, $single );
743
	remove_filter( 'query', 'bp_filter_metaid_column_name' );
744
745
	/**
746
	 * Filters the metadata for a specified notification item.
747
	 *
748
	 * @since 2.3.0
749
	 *
750
	 * @param mixed  $retval          The meta values for the notification item.
751
	 * @param int    $notification_id ID of the notification item.
752
	 * @param string $meta_key        Meta key for the value being requested.
753
	 * @param bool   $single          Whether to return one matched meta key row or all.
754
	 */
755
	return apply_filters( 'bp_notifications_get_meta', $retval, $notification_id, $meta_key, $single );
756
}
757
758
/**
759
 * Update a piece of notification meta.
760
 *
761
 * @since 1.2.0
762
 *
763
 * @param  int    $notification_id ID of the notification item whose metadata is being
764
 *                                 updated.
765
 * @param  string $meta_key        Key of the metadata being updated.
766
 * @param  mixed  $meta_value      Value to be set.
767
 * @param  mixed  $prev_value      Optional. If specified, only update existing
768
 *                                 metadata entries with the specified value.
769
 *                                 Otherwise, update all entries.
770
 * @return bool|int                Returns false on failure. On successful
771
 *                                 update of existing metadata, returns true. On
772
 *                                 successful creation of new metadata,  returns
773
 *                                 the integer ID of the new metadata row.
774
 */
775 View Code Duplication
function bp_notifications_update_meta( $notification_id, $meta_key, $meta_value, $prev_value = '' ) {
776
	add_filter( 'query', 'bp_filter_metaid_column_name' );
777
	$retval = update_metadata( 'notification', $notification_id, $meta_key, $meta_value, $prev_value );
778
	remove_filter( 'query', 'bp_filter_metaid_column_name' );
779
780
	return $retval;
781
}
782
783
/**
784
 * Add a piece of notification metadata.
785
 *
786
 * @since 2.3.0
787
 *
788
 * @param int    $notification_id ID of the notification item.
789
 * @param string $meta_key        Metadata key.
790
 * @param mixed  $meta_value      Metadata value.
791
 * @param bool   $unique          Optional. Whether to enforce a single metadata value
792
 *                                for the given key. If true, and the object already has a value for
793
 *                                the key, no change will be made. Default: false.
794
 * @return int|bool               The meta ID on successful update, false on failure.
795
 */
796 View Code Duplication
function bp_notifications_add_meta( $notification_id, $meta_key, $meta_value, $unique = false ) {
797
	add_filter( 'query', 'bp_filter_metaid_column_name' );
798
	$retval = add_metadata( 'notification', $notification_id, $meta_key, $meta_value, $unique );
799
	remove_filter( 'query', 'bp_filter_metaid_column_name' );
800
801
	return $retval;
802
}
803