-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp-custom-notifications.php
111 lines (87 loc) · 3.31 KB
/
bp-custom-notifications.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
class BP_Custom_Notifications_Comment_Component extends BP_Component {
/**
* Initial component setup.
*/
public function __construct() {
parent::start(
// Unique component ID
'wctpa_notifications_comment',
// Used by BP when listing components (eg in the Dashboard)
__( 'Enable Custom Post Comment Notifications', 'wctpa_notifications_comment' )
);
}
/**
* Set up component data, as required by BP.
*/
public function setup_globals( $args = array() ) {
global $bp;
$wctpa_notifications_comments_slug = 'wctpa_notifications_comment';
parent::setup_globals( array(
'id' => 'wctpa_notifications_comment',
'slug' => $wctpa_notifications_comments_slug, // used for building URLs
'notification_callback' => 'wctpa_format_notifications_comments'
) );
/* Register this in the active components array */
$bp->active_components[$wctpa_notifications_comments_slug] = 'wctpa_notifications_comment';
}
/**
* Set up component navigation, and register display callbacks.
*/
public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
return false;
}
/**
* Set up display screen logic.
*
* We are using BP's plugins.php template as a wrapper, which is
* the easiest technique for compatibility with themes.
*/
public function screen_function() {
return false;
}
}
/**
* Bootstrap the component.
*/
function wctpa_init() {
buddypress()->wctpa_notifications_comment = new BP_Custom_Notifications_Comment_Component();
}
add_action( 'bp_loaded', 'wctpa_init' );
/**
* Format the BuddyBar/Toolbar notifications for the custom component
*
* @since BuddyPress (1.0)
* @param string $action The kind of notification being rendered
* @param int $item_id The primary item id
* @param int $secondary_item_id The secondary item id
* @param int $total_items The total number of custom-related notifications waiting for the user
* @param string $format 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar
*/
function wctpa_format_notifications_comments( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
if ( 'new_comment' === $action ) {
$link = get_permalink( $item_id );
$title = __( 'New Comment On '.get_the_title( $item_id ), 'buddypress' );
if ( (int) $total_items > 1 ) {
$text = sprintf( __('You have %d new comments', 'buddypress' ), (int) $total_items );
$filter = 'wctpa_bp_notifications_multiple_new_comment_notification';
} else {
if ( !empty( $secondary_item_id ) ) {
$text = sprintf( __('You have %d new comment on %s', 'buddypress' ), (int) $total_items, get_the_title( $item_id ) );
} else {
$text = sprintf( __('You have %d new comment', 'buddypress' ), (int) $total_items );
}
$filter = 'wctpa_bp_notifications_single_new_comment_notification';
}
}
if ( 'string' === $format ) {
$return = apply_filters( $filter, '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $link, $item_id, $secondary_item_id );
} else {
$return = apply_filters( $filter, array(
'text' => $text,
'link' => $link
), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
}
do_action( 'wctpa_format_notifications_comments', $action, $item_id, $secondary_item_id, $total_items );
return $return;
}