bp-em-activity.php
8.55 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
// This file handles hooks/filter requiring activity stream publications
/**
* bp_em_record_activity()
*
* If the activity stream component is installed, this function will record activity items for your
* component.
*/
function bp_em_record_activity( $args = '' ) {
if ( !function_exists( 'bp_activity_add' ) )
return false;
$defaults = array(
'id' => false,
'user_id' => '',
'action' => '',
'content' => '',
'primary_link' => '',
'component' => 'events-manager',
'type' => false,
'item_id' => false,
'secondary_item_id' => false,
'recorded_time' => gmdate( "Y-m-d H:i:s" ),
'hide_sitewide' => false
);
$r = wp_parse_args( $args, $defaults );
extract( $r );
return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
}
function em_bp_register_activity_actions() {
if ( !bp_is_active( 'events' ) ) {
return false;
}
$bp = buddypress();
// @todo - Figure out why adding a second bp_activity_set_action creates two 'Bookings' actions in backend and different booking status front-end e.g. when friendships created/accepted do not
bp_activity_set_action(
$bp->events->id,
'new_booking',
__( 'Bookings', 'events-manager'),
'em_bp_events_format_activity_action_bookings',
__( 'Bookings', 'events-manager'),
array( 'activity', 'member' )
);
/*
bp_activity_set_action(
$bp->events->id,
'booking_cancelled',
__( 'Booking Cancelled', 'events-manager'),
'em_bp_events_format_activity_action_bookings',
__( 'Bookings', 'events-manager'),
array( 'activity', 'member' )
);
*/
bp_activity_set_action(
$bp->events->id,
'new_event',
__('New Event','events-manager'),
'em_bp_events_format_activity_action_events',
__( 'Events', 'events-manager'),
array( 'activity', 'member' )
);
}
add_action( 'bp_register_activity_actions', 'em_bp_register_activity_actions' );
/**
* Not yet used fully - formats booking-related actions
* @param string $action
* @param object $activity
* @return string
*/
function em_bp_events_format_activity_action_bookings( $action, $activity ) {
return '';
$member_link = bp_core_get_userlink( $activity->user_id );
$EM_Booking = em_get_booking( $activity->item );
$action = '';
switch ($activity->type){
case 'new_booking':
if( $activity->component == 'groups' ){
$action = sprintf(__('%s is attending %s of the group %s.','events-manager'), $member_link, $event_link, $group_link );
}else{
$action = sprintf(__('%s is attending %s.','events-manager'), $member_link, $event_link );
}
break;
case 'cancelled_booking':
if( $activity->component == 'groups' ){
$action = sprintf(__('%s will not be attending %s of group %s anymore.','events-manager'), $user_link, $event_link, $group_link );
}else{
$action = sprintf(__('%s will not be attending %s anymore.','events-manager'), $user_link, $event_link );
}
break;
}
return apply_filters( 'bp_events_format_activity_action_bookings', $action, $activity );
}
/**
* Not yet used fully - formats event-related actions
* @param string $action
* @param object $activity
* @return string
*/
function em_bp_events_format_activity_action_events( $action, $activity ) {
return '';
$member_link = bp_core_get_userlink( $activity->user_id );
$EM_Event = em_get_event( $activity->item_id );
$action = sprintf(__('%s added the event %s','events-manager'), $member_link, $EM_Event->output('#_EVENTLINK') );
return apply_filters( 'bp_events_format_activity_action_events', $action, $activity );
}
/**
* Records new events to the activity stream.
* @param unknown_type $result
* @param unknown_type $EM_Event
* @return unknown
*/
function bp_em_record_activity_event_save( $result, $EM_Event ){
if( $result && $EM_Event->event_status == 1 && $EM_Event->get_previous_status() != 1 ){
$user = get_userdata($EM_Event->event_owner);
$member_link = bp_core_get_user_domain($user->ID);
if( empty($EM_Event->group_id) ){
bp_em_record_activity( array(
'user_id' => $user->ID,
'action' => sprintf(__('%s added the event %s','events-manager'), "<a href='".$member_link."'>".$user->display_name."</a>", $EM_Event->output('#_EVENTLINK') ),
'primary_link' => $EM_Event->output('#_EVENTURL'),
'type' => 'new_event',
'item_id' => $EM_Event->event_id,
'hide_sitewide' => $EM_Event->event_private
));
}else{
//tis a group event
$group = new BP_Groups_Group($EM_Event->group_id);
bp_em_record_activity( array(
'user_id' => $user->ID,
'action' => sprintf(__('%s added the event %s to %s.','events-manager'), "<a href='".$member_link."'>".$user->display_name."</a>", $EM_Event->output('#_EVENTLINK'), '<a href="'.bp_get_group_permalink($group).'">'.bp_get_group_name($group).'</a>' ),
'component' => 'groups',
'type' => 'new_event',
'item_id' => $EM_Event->group_id,
'hide_sitewide' => $EM_Event->event_private
));
}
}
return $result;
}
add_filter('em_event_save','bp_em_record_activity_event_save', 10, 2);
/**
* @param boolean $result
* @param EM_Booking $EM_Booking
* @return boolean
*/
function bp_em_record_activity_booking_save( $result, $EM_Booking ){
/* @todo this isn't good at detecting status changes. */
if( !empty($EM_Booking->event_id) && $result ){
$action_type = 'new_booking';
if( !empty($EM_Booking->last_bp_activity) && $EM_Booking->last_bp_activity == $action_type ) return $result; //prevent duplicates
$EM_Booking->last_bp_activity = $action_type;
$rejected_statuses = array(0,2,3); //these statuses apply to rejected/cancelled bookings
$user = $EM_Booking->get_person();
$member_link = bp_core_get_user_domain($user->ID);
$user_link = "<a href='".$member_link."/'>".$user->display_name."</a>";
$event_link = $EM_Booking->get_event()->output('#_EVENTLINK');
$status = $EM_Booking->booking_status;
$EM_Event = $EM_Booking->get_event();
if( empty($EM_Event->group_id) ){
if( $status == 1 || (!get_option('dbem_bookings_approval') && $status < 2) ){
$action = sprintf(__('%s is attending %s.','events-manager'), $user_link, $event_link );
}elseif( ($EM_Booking->previous_status == 1 || (!get_option('dbem_bookings_approval') && $EM_Booking->previous_status < 2)) && in_array($status, $rejected_statuses) ){
$action = sprintf(__('%s will not be attending %s anymore.','events-manager'), $user_link, $event_link );
//$action_type = 'cancelled_booking';
}
}else{
$group = new BP_Groups_Group($EM_Event->group_id);
$group_link = '<a href="'.bp_get_group_permalink($group).'">'.bp_get_group_name($group).'</a>';
if( $status == 1 || (!get_option('dbem_bookings_approval') && $status < 2) ){
$action = sprintf(__('%s is attending %s of the group %s.','events-manager'), $user_link, $event_link, $group_link );
}elseif( ($EM_Booking->previous_status == 1 || (!get_option('dbem_bookings_approval') && $EM_Booking->previous_status < 2)) && in_array($status, $rejected_statuses) ){
$action = sprintf(__('%s will not be attending %s of group %s anymore.','events-manager'), $user_link, $event_link, $group_link );
//$action_type = 'cancelled_booking';
}
}
if( !empty($action) ){
if( empty($EM_Event->group_id) ){
bp_em_record_activity( array(
'user_id' => $EM_Booking->person->ID,
'action' => $action,
'primary_link' => $EM_Event->output('#_EVENTURL'),
'type' => $action_type,
'item_id' => $EM_Event->event_id,
'secondary_item_id' => $EM_Booking->booking_id,
'hide_sitewide' => $EM_Event->event_private
));
}else{
//tis a group event
bp_em_record_activity( array(
'component' => 'groups',
'user_id' => $EM_Booking->person->ID,
'action' => $action,
'primary_link' => $EM_Event->output('#_EVENTURL'),
'type' => $action_type,
'item_id' => $EM_Event->group_id,
'secondary_item_id' => $EM_Booking->booking_id,
'hide_sitewide' => $EM_Event->event_private
));
}
}
}
return $result;
}
add_filter('em_booking_set_status','bp_em_record_activity_booking_save', 100, 2);
add_filter('em_booking_save','bp_em_record_activity_booking_save', 100, 2);
add_filter('em_booking_delete','bp_em_record_activity_booking_save', 100, 2);