bp-activity-actions.php 26 KB
Newer Older
root's avatar
root committed
1
2
3
4
5
6
7
8
9
10
11
12
<?php

/**
 * Action functions are exactly the same as screen functions, however they do
 * not have a template screen associated with them. Usually they will send the
 * user back to the default screen after execution.
 *
 * @package BuddyPress
 * @subpackage ActivityActions
 */

// Exit if accessed directly
ale's avatar
ale committed
13
defined( 'ABSPATH' ) || exit;
root's avatar
root committed
14
15

/**
lechuck's avatar
lechuck committed
16
 * Allow core components and dependent plugins to register activity actions.
root's avatar
root committed
17
 *
ale's avatar
ale committed
18
 * @since BuddyPress (1.2.0)
root's avatar
root committed
19
20
21
22
 *
 * @uses do_action() To call 'bp_register_activity_actions' hook.
 */
function bp_register_activity_actions() {
ale's avatar
ale committed
23
24
25
26
27
28

	/**
	 * Fires on bp_init to allow core components and dependent plugins to register activity actions.
	 *
	 * @since BuddyPress (1.2.0)
	 */
root's avatar
root committed
29
30
31
32
33
	do_action( 'bp_register_activity_actions' );
}
add_action( 'bp_init', 'bp_register_activity_actions', 8 );

/**
lechuck's avatar
lechuck committed
34
 * Catch and route requests for single activity item permalinks.
root's avatar
root committed
35
 *
ale's avatar
ale committed
36
 * @since BuddyPress (1.2.0)
root's avatar
root committed
37
38
39
40
41
42
43
44
45
 *
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses bp_action_variable()
 * @uses bp_activity_get_specific()
 * @uses bp_is_active()
 * @uses bp_core_get_user_domain()
 * @uses groups_get_group()
 * @uses bp_get_group_permalink()
lechuck's avatar
lechuck committed
46
 * @uses apply_filters_ref_array() To call the 'bp_activity_permalink_redirect_url' hook.
root's avatar
root committed
47
48
49
 * @uses bp_core_redirect()
 * @uses bp_get_root_domain()
 *
lechuck's avatar
lechuck committed
50
 * @return bool False on failure.
root's avatar
root committed
51
52
53
54
 */
function bp_activity_action_permalink_router() {

	// Not viewing activity
lechuck's avatar
lechuck committed
55
	if ( ! bp_is_activity_component() || ! bp_is_current_action( 'p' ) )
root's avatar
root committed
56
57
58
		return false;

	// No activity to display
lechuck's avatar
lechuck committed
59
	if ( ! bp_action_variable( 0 ) || ! is_numeric( bp_action_variable( 0 ) ) )
root's avatar
root committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		return false;

	// Get the activity details
	$activity = bp_activity_get_specific( array( 'activity_ids' => bp_action_variable( 0 ), 'show_hidden' => true ) );

	// 404 if activity does not exist
	if ( empty( $activity['activities'][0] ) ) {
		bp_do_404();
		return;
	} else {
		$activity = $activity['activities'][0];
	}

	// Do not redirect at default
	$redirect = false;

	// Redirect based on the type of activity
lechuck's avatar
lechuck committed
77
	if ( bp_is_active( 'groups' ) && $activity->component == buddypress()->groups->id ) {
root's avatar
root committed
78
79

		// Activity is a user update
lechuck's avatar
lechuck committed
80
		if ( ! empty( $activity->user_id ) ) {
root's avatar
root committed
81
82
83
84
85
86
87
88
89
90
91
92
			$redirect = bp_core_get_user_domain( $activity->user_id, $activity->user_nicename, $activity->user_login ) . bp_get_activity_slug() . '/' . $activity->id . '/';

		// Activity is something else
		} else {

			// Set redirect to group activity stream
			if ( $group = groups_get_group( array( 'group_id' => $activity->item_id ) ) ) {
				$redirect = bp_get_group_permalink( $group ) . bp_get_activity_slug() . '/' . $activity->id . '/';
			}
		}

	// Set redirect to users' activity stream
ale's avatar
ale committed
93
	} elseif ( ! empty( $activity->user_id ) ) {
lechuck's avatar
lechuck committed
94
95
96
97
		$redirect = bp_core_get_user_domain( $activity->user_id, $activity->user_nicename, $activity->user_login ) . bp_get_activity_slug() . '/' . $activity->id . '/';
	}

	// If set, add the original query string back onto the redirect URL
lechuck's avatar
lechuck committed
98
	if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
lechuck's avatar
lechuck committed
99
100
101
		$query_frags = array();
		wp_parse_str( $_SERVER['QUERY_STRING'], $query_frags );
		$redirect = add_query_arg( urlencode_deep( $query_frags ), $redirect );
root's avatar
root committed
102
103
	}

ale's avatar
ale committed
104
105
106
107
108
109
110
	/**
	 * Filter the intended redirect url before the redirect occurs for the single activity item.
	 *
	 * @since BuddyPress (1.2.2)
	 *
	 * @param array Array with url to redirect to and activity related to the redirect.
	 */
lechuck's avatar
lechuck committed
111
	if ( ! $redirect = apply_filters_ref_array( 'bp_activity_permalink_redirect_url', array( $redirect, &$activity ) ) ) {
root's avatar
root committed
112
		bp_core_redirect( bp_get_root_domain() );
lechuck's avatar
lechuck committed
113
	}
root's avatar
root committed
114
115
116
117
118
119
120
121
122

	// Redirect to the actual activity permalink page
	bp_core_redirect( $redirect );
}
add_action( 'bp_actions', 'bp_activity_action_permalink_router' );

/**
 * Delete specific activity item and redirect to previous page.
 *
ale's avatar
ale committed
123
 * @since BuddyPress (1.1.0)
root's avatar
root committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 *
 * @param int $activity_id Activity id to be deleted. Defaults to 0.
 *
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses bp_action_variable()
 * @uses check_admin_referer()
 * @uses bp_activity_user_can_delete()
 * @uses do_action() Calls 'bp_activity_before_action_delete_activity' hook to allow actions to be taken before the activity is deleted.
 * @uses bp_activity_delete()
 * @uses bp_core_add_message()
 * @uses do_action() Calls 'bp_activity_action_delete_activity' hook to allow actions to be taken after the activity is deleted.
 * @uses bp_core_redirect()
 *
lechuck's avatar
lechuck committed
138
 * @return bool False on failure.
root's avatar
root committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 */
function bp_activity_action_delete_activity( $activity_id = 0 ) {

	// Not viewing activity or action is not delete
	if ( !bp_is_activity_component() || !bp_is_current_action( 'delete' ) )
		return false;

	if ( empty( $activity_id ) && bp_action_variable( 0 ) )
		$activity_id = (int) bp_action_variable( 0 );

	// Not viewing a specific activity item
	if ( empty( $activity_id ) )
		return false;

	// Check the nonce
	check_admin_referer( 'bp_activity_delete_link' );

	// Load up the activity item
	$activity = new BP_Activity_Activity( $activity_id );

	// Check access
lechuck's avatar
lechuck committed
160
	if ( ! bp_activity_user_can_delete( $activity ) )
root's avatar
root committed
161
162
		return false;

ale's avatar
ale committed
163
164
165
166
167
168
169
170
	/**
	 * Fires before the deletion so plugins can still fetch information about it.
	 *
	 * @since BuddyPress (1.5.0)
	 *
	 * @param int $activity_id The activity ID.
	 * @param int $user_id     The user associated with the activity.
	 */
root's avatar
root committed
171
172
173
174
175
176
177
178
	do_action( 'bp_activity_before_action_delete_activity', $activity_id, $activity->user_id );

	// Delete the activity item and provide user feedback
	if ( bp_activity_delete( array( 'id' => $activity_id, 'user_id' => $activity->user_id ) ) )
		bp_core_add_message( __( 'Activity deleted successfully', 'buddypress' ) );
	else
		bp_core_add_message( __( 'There was an error when deleting that activity', 'buddypress' ), 'error' );

ale's avatar
ale committed
179
180
181
182
183
184
185
186
	/**
	 * Fires after the deletion so plugins can act afterwards based on the activity.
	 *
	 * @since BuddyPress (1.1.0)
	 *
	 * @param int $activity_id The activity ID.
	 * @param int $user_id     The user associated with the activity.
	 */
root's avatar
root committed
187
188
189
190
191
192
193
194
195
196
	do_action( 'bp_activity_action_delete_activity', $activity_id, $activity->user_id );

	// Check for the redirect query arg, otherwise let WP handle things
 	if ( !empty( $_GET['redirect_to'] ) )
		bp_core_redirect( esc_url( $_GET['redirect_to'] ) );
	else
		bp_core_redirect( wp_get_referer() );
}
add_action( 'bp_actions', 'bp_activity_action_delete_activity' );

lechuck's avatar
lechuck committed
197
/**
lechuck's avatar
lechuck committed
198
199
 * Mark specific activity item as spam and redirect to previous page.
 *
ale's avatar
ale committed
200
 * @since BuddyPress (1.6.0)
lechuck's avatar
lechuck committed
201
202
 *
 * @param int $activity_id Activity id to be deleted. Defaults to 0.
ale's avatar
ale committed
203
 *
lechuck's avatar
lechuck committed
204
 * @return bool False on failure.
lechuck's avatar
lechuck committed
205
206
 */
function bp_activity_action_spam_activity( $activity_id = 0 ) {
ale's avatar
ale committed
207
	$bp = buddypress();
lechuck's avatar
lechuck committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

	// Not viewing activity, or action is not spam, or Akismet isn't present
	if ( !bp_is_activity_component() || !bp_is_current_action( 'spam' ) || empty( $bp->activity->akismet ) )
		return false;

	if ( empty( $activity_id ) && bp_action_variable( 0 ) )
		$activity_id = (int) bp_action_variable( 0 );

	// Not viewing a specific activity item
	if ( empty( $activity_id ) )
		return false;

	// Is the current user allowed to spam items?
	if ( !bp_activity_user_can_mark_spam() )
		return false;

	// Load up the activity item
	$activity = new BP_Activity_Activity( $activity_id );
	if ( empty( $activity->id ) )
		return false;

	// Check nonce
	check_admin_referer( 'bp_activity_akismet_spam_' . $activity->id );

ale's avatar
ale committed
232
233
234
235
236
237
238
239
	/**
	 * Fires before the marking activity as spam so plugins can modify things if they want to.
	 *
	 * @since BuddyPress (1.6.0)
	 *
	 * @param int    $activity_id Activity ID to be marked as spam.
	 * @param object $activity    Activity object for the ID to be marked as spam.
	 */
lechuck's avatar
lechuck committed
240
241
242
243
244
245
	do_action( 'bp_activity_before_action_spam_activity', $activity->id, $activity );

	// Mark as spam
	bp_activity_mark_as_spam( $activity );
	$activity->save();

ale's avatar
ale committed
246
	// Tell the user the spamming has been successful
lechuck's avatar
lechuck committed
247
248
	bp_core_add_message( __( 'The activity item has been marked as spam and is no longer visible.', 'buddypress' ) );

ale's avatar
ale committed
249
250
251
252
253
254
255
256
	/**
	 * Fires after the marking activity as spam so plugins can act afterwards based on the activity.
	 *
	 * @since BuddyPress (1.6.0)
	 *
	 * @param int $activity_id Activity ID that was marked as spam.
	 * @param int $user_id     User ID associated with activity.
	 */
lechuck's avatar
lechuck committed
257
258
259
260
261
262
263
264
265
266
	do_action( 'bp_activity_action_spam_activity', $activity_id, $activity->user_id );

	// Check for the redirect query arg, otherwise let WP handle things
 	if ( !empty( $_GET['redirect_to'] ) )
		bp_core_redirect( esc_url( $_GET['redirect_to'] ) );
	else
		bp_core_redirect( wp_get_referer() );
}
add_action( 'bp_actions', 'bp_activity_action_spam_activity' );

root's avatar
root committed
267
268
269
/**
 * Post user/group activity update.
 *
ale's avatar
ale committed
270
 * @since BuddyPress (1.2.0)
root's avatar
root committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 *
 * @uses is_user_logged_in()
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses check_admin_referer()
 * @uses apply_filters() To call 'bp_activity_post_update_content' hook.
 * @uses apply_filters() To call 'bp_activity_post_update_object' hook.
 * @uses apply_filters() To call 'bp_activity_post_update_item_id' hook.
 * @uses bp_core_add_message()
 * @uses bp_core_redirect()
 * @uses bp_activity_post_update()
 * @uses groups_post_update()
 * @uses bp_core_redirect()
 * @uses apply_filters() To call 'bp_activity_custom_update' hook.
 *
lechuck's avatar
lechuck committed
286
 * @return bool False on failure.
root's avatar
root committed
287
288
289
290
291
292
293
294
295
296
 */
function bp_activity_action_post_update() {

	// Do not proceed if user is not logged in, not viewing activity, or not posting
	if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'post' ) )
		return false;

	// Check the nonce
	check_admin_referer( 'post_update', '_wpnonce_post_update' );

ale's avatar
ale committed
297
298
299
300
301
302
303
	/**
	 * Filters the content provided in the activity input field.
	 *
	 * @since BuddyPress (1.2.0)
	 *
	 * @param string $value Activity message being posted.
	 */
lechuck's avatar
lechuck committed
304
305
306
	$content = apply_filters( 'bp_activity_post_update_content', $_POST['whats-new'] );

	if ( ! empty( $_POST['whats-new-post-object'] ) ) {
ale's avatar
ale committed
307
308
309
310
311
312
313
314

		/**
		 * Filters the item type that the activity update should be associated with.
		 *
		 * @since BuddyPress (1.2.0)
		 *
		 * @param string $value Item type to associate with.
		 */
lechuck's avatar
lechuck committed
315
316
317
318
		$object = apply_filters( 'bp_activity_post_update_object', $_POST['whats-new-post-object'] );
	}

	if ( ! empty( $_POST['whats-new-post-in'] ) ) {
ale's avatar
ale committed
319
320
321
322
323
324
325
326

		/**
		 * Filters what component the activity is being to.
		 *
		 * @since BuddyPress (1.2.0)
		 *
		 * @param string $value Chosen component to post activity to.
		 */
lechuck's avatar
lechuck committed
327
328
		$item_id = apply_filters( 'bp_activity_post_update_item_id', $_POST['whats-new-post-in'] );
	}
root's avatar
root committed
329
330
331
332
333
334
335
336
337
338
339
340

	// No activity content so provide feedback and redirect
	if ( empty( $content ) ) {
		bp_core_add_message( __( 'Please enter some content to post.', 'buddypress' ), 'error' );
		bp_core_redirect( wp_get_referer() );
	}

	// No existing item_id
	if ( empty( $item_id ) ) {
		$activity_id = bp_activity_post_update( array( 'content' => $content ) );

	// Post to groups object
ale's avatar
ale committed
341
	} elseif ( 'groups' == $object && bp_is_active( 'groups' ) ) {
lechuck's avatar
lechuck committed
342
		if ( (int) $item_id ) {
root's avatar
root committed
343
344
345
346
			$activity_id = groups_post_update( array( 'content' => $content, 'group_id' => $item_id ) );
		}

	} else {
ale's avatar
ale committed
347
348
349
350
351
352
353
354
355
356

		/**
		 * Filters activity object for BuddyPress core and plugin authors before posting activity update.
		 *
		 * @since BuddyPress (1.2.0)
		 *
		 * @param string $object  Activity item being associated to.
		 * @param string $item_id Component ID being posted to.
		 * @param string $content Activity content being posted.
		 */
root's avatar
root committed
357
358
359
360
361
362
363
		$activity_id = apply_filters( 'bp_activity_custom_update', $object, $item_id, $content );
	}

	// Provide user feedback
	if ( !empty( $activity_id ) )
		bp_core_add_message( __( 'Update Posted!', 'buddypress' ) );
	else
ale's avatar
ale committed
364
		bp_core_add_message( __( 'There was an error when posting your update. Please try again.', 'buddypress' ), 'error' );
root's avatar
root committed
365
366
367
368
369
370
371
372
373

	// Redirect
	bp_core_redirect( wp_get_referer() );
}
add_action( 'bp_actions', 'bp_activity_action_post_update' );

/**
 * Post new activity comment.
 *
ale's avatar
ale committed
374
 * @since BuddyPress (1.2.0)
root's avatar
root committed
375
376
377
378
379
380
381
382
383
384
385
386
 *
 * @uses is_user_logged_in()
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses check_admin_referer()
 * @uses apply_filters() To call 'bp_activity_post_comment_activity_id' hook.
 * @uses apply_filters() To call 'bp_activity_post_comment_content' hook.
 * @uses bp_core_add_message()
 * @uses bp_core_redirect()
 * @uses bp_activity_new_comment()
 * @uses wp_get_referer()
 *
lechuck's avatar
lechuck committed
387
 * @return bool False on failure.
root's avatar
root committed
388
389
390
 */
function bp_activity_action_post_comment() {

lechuck's avatar
lechuck committed
391
	if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'reply' ) )
root's avatar
root committed
392
393
394
395
396
		return false;

	// Check the nonce
	check_admin_referer( 'new_activity_comment', '_wpnonce_new_activity_comment' );

ale's avatar
ale committed
397
398
399
400
401
402
403
	/**
	 * Filters the activity ID a comment will be in reply to.
	 *
	 * @since BuddyPress (1.2.0)
	 *
	 * @param string $value ID of the activity being replied to.
	 */
root's avatar
root committed
404
	$activity_id = apply_filters( 'bp_activity_post_comment_activity_id', $_POST['comment_form_id'] );
ale's avatar
ale committed
405
406
407
408
409
410
411
412

	/**
	 * Filters the comment content for a comment reply.
	 *
	 * @since BuddyPress (1.2.0)
	 *
	 * @param string $value Comment content being posted.
	 */
root's avatar
root committed
413
414
415
416
417
418
419
420
	$content = apply_filters( 'bp_activity_post_comment_content', $_POST['ac_input_' . $activity_id] );

	if ( empty( $content ) ) {
		bp_core_add_message( __( 'Please do not leave the comment area blank.', 'buddypress' ), 'error' );
		bp_core_redirect( wp_get_referer() . '#ac-form-' . $activity_id );
	}

	$comment_id = bp_activity_new_comment( array(
lechuck's avatar
lechuck committed
421
		'content'     => $content,
root's avatar
root committed
422
		'activity_id' => $activity_id,
lechuck's avatar
lechuck committed
423
		'parent_id'   => false
root's avatar
root committed
424
425
426
427
428
	));

	if ( !empty( $comment_id ) )
		bp_core_add_message( __( 'Reply Posted!', 'buddypress' ) );
	else
ale's avatar
ale committed
429
		bp_core_add_message( __( 'There was an error posting that reply. Please try again.', 'buddypress' ), 'error' );
root's avatar
root committed
430
431
432
433
434
435
436
437

	bp_core_redirect( wp_get_referer() . '#ac-form-' . $activity_id );
}
add_action( 'bp_actions', 'bp_activity_action_post_comment' );

/**
 * Mark activity as favorite.
 *
ale's avatar
ale committed
438
 * @since BuddyPress (1.2.0)
root's avatar
root committed
439
440
441
442
443
444
445
446
447
448
449
 *
 * @uses is_user_logged_in()
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses check_admin_referer()
 * @uses bp_activity_add_user_favorite()
 * @uses bp_action_variable()
 * @uses bp_core_add_message()
 * @uses bp_core_redirect()
 * @uses wp_get_referer()
 *
lechuck's avatar
lechuck committed
450
 * @return bool False on failure.
root's avatar
root committed
451
452
453
 */
function bp_activity_action_mark_favorite() {

lechuck's avatar
lechuck committed
454
	if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'favorite' ) )
root's avatar
root committed
455
456
457
458
459
460
461
462
		return false;

	// Check the nonce
	check_admin_referer( 'mark_favorite' );

	if ( bp_activity_add_user_favorite( bp_action_variable( 0 ) ) )
		bp_core_add_message( __( 'Activity marked as favorite.', 'buddypress' ) );
	else
ale's avatar
ale committed
463
		bp_core_add_message( __( 'There was an error marking that activity as a favorite. Please try again.', 'buddypress' ), 'error' );
root's avatar
root committed
464
465
466
467
468
469
470
471

	bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) );
}
add_action( 'bp_actions', 'bp_activity_action_mark_favorite' );

/**
 * Remove activity from favorites.
 *
ale's avatar
ale committed
472
 * @since BuddyPress (1.2.0)
root's avatar
root committed
473
474
475
476
477
478
479
480
481
482
483
 *
 * @uses is_user_logged_in()
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses check_admin_referer()
 * @uses bp_activity_remove_user_favorite()
 * @uses bp_action_variable()
 * @uses bp_core_add_message()
 * @uses bp_core_redirect()
 * @uses wp_get_referer()
 *
lechuck's avatar
lechuck committed
484
 * @return bool False on failure.
root's avatar
root committed
485
486
487
 */
function bp_activity_action_remove_favorite() {

lechuck's avatar
lechuck committed
488
	if ( ! is_user_logged_in() || ! bp_is_activity_component() || ! bp_is_current_action( 'unfavorite' ) )
root's avatar
root committed
489
490
491
492
493
494
495
496
		return false;

	// Check the nonce
	check_admin_referer( 'unmark_favorite' );

	if ( bp_activity_remove_user_favorite( bp_action_variable( 0 ) ) )
		bp_core_add_message( __( 'Activity removed as favorite.', 'buddypress' ) );
	else
ale's avatar
ale committed
497
		bp_core_add_message( __( 'There was an error removing that activity as a favorite. Please try again.', 'buddypress' ), 'error' );
root's avatar
root committed
498
499
500
501
502
503

	bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) );
}
add_action( 'bp_actions', 'bp_activity_action_remove_favorite' );

/**
lechuck's avatar
lechuck committed
504
 * Load the sitewide activity feed.
root's avatar
root committed
505
 *
ale's avatar
ale committed
506
 * @since BuddyPress (1.0.0)
root's avatar
root committed
507
508
509
510
511
512
 *
 * @uses bp_is_activity_component()
 * @uses bp_is_current_action()
 * @uses bp_is_user()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
513
 * @return bool False on failure.
root's avatar
root committed
514
515
 */
function bp_activity_action_sitewide_feed() {
ale's avatar
ale committed
516
	$bp = buddypress();
root's avatar
root committed
517

lechuck's avatar
lechuck committed
518
	if ( ! bp_is_activity_component() || ! bp_is_current_action( 'feed' ) || bp_is_user() || ! empty( $bp->groups->current_group ) )
root's avatar
root committed
519
520
		return false;

lechuck's avatar
lechuck committed
521
522
523
524
525
	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'sitewide',

		/* translators: Sitewide activity RSS title - "[Site Name] | Site Wide Activity" */
lucha's avatar
lucha committed
526
		'title'         => sprintf( __( '%s | Site-Wide Activity', 'buddypress' ), bp_get_site_name() ),
root's avatar
root committed
527

lechuck's avatar
lechuck committed
528
529
530
531
		'link'          => bp_get_activity_directory_permalink(),
		'description'   => __( 'Activity feed for the entire site.', 'buddypress' ),
		'activity_args' => 'display_comments=threaded'
	) );
root's avatar
root committed
532
533
534
535
}
add_action( 'bp_actions', 'bp_activity_action_sitewide_feed' );

/**
lechuck's avatar
lechuck committed
536
 * Load a user's personal activity feed.
root's avatar
root committed
537
 *
ale's avatar
ale committed
538
 * @since BuddyPress (1.0.0)
root's avatar
root committed
539
540
541
542
543
 *
 * @uses bp_is_user_activity()
 * @uses bp_is_current_action()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
544
 * @return bool False on failure.
root's avatar
root committed
545
546
 */
function bp_activity_action_personal_feed() {
lechuck's avatar
lechuck committed
547
	if ( ! bp_is_user_activity() || ! bp_is_current_action( 'feed' ) ) {
root's avatar
root committed
548
		return false;
lechuck's avatar
lechuck committed
549
550
551
552
553
	}

	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'personal',
root's avatar
root committed
554

lechuck's avatar
lechuck committed
555
556
		/* translators: Personal activity RSS title - "[Site Name] | [User Display Name] | Activity" */
		'title'         => sprintf( __( '%1$s | %2$s | Activity', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),
root's avatar
root committed
557

lechuck's avatar
lechuck committed
558
559
560
561
		'link'          => trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() ),
		'description'   => sprintf( __( 'Activity feed for %s.', 'buddypress' ), bp_get_displayed_user_fullname() ),
		'activity_args' => 'user_id=' . bp_displayed_user_id()
	) );
root's avatar
root committed
562
563
564
565
}
add_action( 'bp_actions', 'bp_activity_action_personal_feed' );

/**
lechuck's avatar
lechuck committed
566
 * Load a user's friends' activity feed.
root's avatar
root committed
567
 *
ale's avatar
ale committed
568
 * @since BuddyPress (1.0.0)
root's avatar
root committed
569
570
571
572
573
574
575
576
 *
 * @uses bp_is_active()
 * @uses bp_is_user_activity()
 * @uses bp_is_current_action()
 * @uses bp_get_friends_slug()
 * @uses bp_is_action_variable()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
577
 * @return bool False on failure.
root's avatar
root committed
578
579
 */
function bp_activity_action_friends_feed() {
lechuck's avatar
lechuck committed
580
	if ( ! bp_is_active( 'friends' ) || ! bp_is_user_activity() || ! bp_is_current_action( bp_get_friends_slug() ) || ! bp_is_action_variable( 'feed', 0 ) ) {
root's avatar
root committed
581
		return false;
lechuck's avatar
lechuck committed
582
583
584
585
586
	}

	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'friends',
root's avatar
root committed
587

lechuck's avatar
lechuck committed
588
589
		/* translators: Friends activity RSS title - "[Site Name] | [User Display Name] | Friends Activity" */
		'title'         => sprintf( __( '%1$s | %2$s | Friends Activity', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),
root's avatar
root committed
590

lechuck's avatar
lechuck committed
591
592
593
594
		'link'          => trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_get_friends_slug() ),
		'description'   => sprintf( __( "Activity feed for %s's friends.", 'buddypress' ), bp_get_displayed_user_fullname() ),
		'activity_args' => 'scope=friends'
	) );
root's avatar
root committed
595
596
597
598
}
add_action( 'bp_actions', 'bp_activity_action_friends_feed' );

/**
lechuck's avatar
lechuck committed
599
 * Load the activity feed for a user's groups.
root's avatar
root committed
600
 *
ale's avatar
ale committed
601
 * @since BuddyPress (1.2.0)
root's avatar
root committed
602
603
604
605
606
607
608
609
 *
 * @uses bp_is_active()
 * @uses bp_is_user_activity()
 * @uses bp_is_current_action()
 * @uses bp_get_groups_slug()
 * @uses bp_is_action_variable()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
610
 * @return bool False on failure.
root's avatar
root committed
611
612
 */
function bp_activity_action_my_groups_feed() {
lechuck's avatar
lechuck committed
613
	if ( ! bp_is_active( 'groups' ) || ! bp_is_user_activity() || ! bp_is_current_action( bp_get_groups_slug() ) || ! bp_is_action_variable( 'feed', 0 ) ) {
root's avatar
root committed
614
		return false;
lechuck's avatar
lechuck committed
615
	}
root's avatar
root committed
616

lechuck's avatar
lechuck committed
617
618
619
620
621
622
623
624
625
626
627
628
	// get displayed user's group IDs
	$groups    = groups_get_user_groups();
	$group_ids = implode( ',', $groups['groups'] );

	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'mygroups',

		/* translators: Member groups activity RSS title - "[Site Name] | [User Display Name] | Groups Activity" */
		'title'         => sprintf( __( '%1$s | %2$s | Group Activity', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),

		'link'          => trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_get_groups_slug() ),
lucha's avatar
lucha committed
629
		'description'   => sprintf( __( "Public group activity feed of which %s is a member.", 'buddypress' ), bp_get_displayed_user_fullname() ),
lechuck's avatar
lechuck committed
630
631
632
633
634
635
		'activity_args' => array(
			'object'           => buddypress()->groups->id,
			'primary_id'       => $group_ids,
			'display_comments' => 'threaded'
		)
	) );
root's avatar
root committed
636
637
638
639
640
641
}
add_action( 'bp_actions', 'bp_activity_action_my_groups_feed' );

/**
 * Load a user's @mentions feed.
 *
ale's avatar
ale committed
642
 * @since BuddyPress (1.2.0)
root's avatar
root committed
643
644
645
646
647
648
 *
 * @uses bp_is_user_activity()
 * @uses bp_is_current_action()
 * @uses bp_is_action_variable()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
649
 * @return bool False on failure.
root's avatar
root committed
650
651
 */
function bp_activity_action_mentions_feed() {
lechuck's avatar
lechuck committed
652
653
654
	if ( ! bp_activity_do_mentions() ) {
		return false;
	}
root's avatar
root committed
655

lechuck's avatar
lechuck committed
656
	if ( !bp_is_user_activity() || ! bp_is_current_action( 'mentions' ) || ! bp_is_action_variable( 'feed', 0 ) ) {
root's avatar
root committed
657
		return false;
lechuck's avatar
lechuck committed
658
	}
root's avatar
root committed
659

lechuck's avatar
lechuck committed
660
661
662
	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'mentions',
root's avatar
root committed
663

lechuck's avatar
lechuck committed
664
665
666
667
668
669
670
671
672
		/* translators: User mentions activity RSS title - "[Site Name] | [User Display Name] | Mentions" */
		'title'         => sprintf( __( '%1$s | %2$s | Mentions', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),

		'link'          => bp_displayed_user_domain() . bp_get_activity_slug() . '/mentions/',
		'description'   => sprintf( __( "Activity feed mentioning %s.", 'buddypress' ), bp_get_displayed_user_fullname() ),
		'activity_args' => array(
			'search_terms' => '@' . bp_core_get_username( bp_displayed_user_id() )
		)
	) );
root's avatar
root committed
673
674
675
676
677
678
}
add_action( 'bp_actions', 'bp_activity_action_mentions_feed' );

/**
 * Load a user's favorites feed.
 *
ale's avatar
ale committed
679
 * @since BuddyPress (1.2.0)
root's avatar
root committed
680
681
682
683
684
685
 *
 * @uses bp_is_user_activity()
 * @uses bp_is_current_action()
 * @uses bp_is_action_variable()
 * @uses status_header()
 *
lechuck's avatar
lechuck committed
686
 * @return bool False on failure.
root's avatar
root committed
687
688
 */
function bp_activity_action_favorites_feed() {
lechuck's avatar
lechuck committed
689
	if ( ! bp_is_user_activity() || ! bp_is_current_action( 'favorites' ) || ! bp_is_action_variable( 'feed', 0 ) ) {
root's avatar
root committed
690
		return false;
lechuck's avatar
lechuck committed
691
692
693
694
695
696
697
698
699
	}

	// get displayed user's favorite activity IDs
	$favs = bp_activity_get_user_favorites( bp_displayed_user_id() );
	$fav_ids = implode( ',', (array) $favs );

	// setup the feed
	buddypress()->activity->feed = new BP_Activity_Feed( array(
		'id'            => 'favorites',
root's avatar
root committed
700

lechuck's avatar
lechuck committed
701
702
		/* translators: User activity favorites RSS title - "[Site Name] | [User Display Name] | Favorites" */
		'title'         => sprintf( __( '%1$s | %2$s | Favorites', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),
root's avatar
root committed
703

lechuck's avatar
lechuck committed
704
705
706
707
		'link'          => bp_displayed_user_domain() . bp_get_activity_slug() . '/favorites/',
		'description'   => sprintf( __( "Activity feed of %s's favorites.", 'buddypress' ), bp_get_displayed_user_fullname() ),
		'activity_args' => 'include=' . $fav_ids
	) );
root's avatar
root committed
708
709
710
}
add_action( 'bp_actions', 'bp_activity_action_favorites_feed' );

lechuck's avatar
lechuck committed
711
/**
lechuck's avatar
lechuck committed
712
 * Loads Akismet filtering for activity.
lechuck's avatar
lechuck committed
713
 *
ale's avatar
ale committed
714
715
 * @since BuddyPress (1.6.0)
 * @since BuddyPress (2.3.0) We only support Akismet 3+.
lechuck's avatar
lechuck committed
716
717
 */
function bp_activity_setup_akismet() {
ale's avatar
ale committed
718
	$bp = buddypress();
lechuck's avatar
lechuck committed
719
720

	// Bail if Akismet is not active
ale's avatar
ale committed
721
722
723
724
725
726
	if ( ! defined( 'AKISMET_VERSION' ) ) {
		return;
	}

	// Bail if older version of Akismet
	if ( ! class_exists( 'Akismet' ) ) {
lechuck's avatar
lechuck committed
727
		return;
ale's avatar
ale committed
728
	}
lechuck's avatar
lechuck committed
729
730

	// Bail if no Akismet key is set
ale's avatar
ale committed
731
	if ( ! bp_get_option( 'wordpress_api_key' ) && ! defined( 'WPCOM_API_KEY' ) ) {
lechuck's avatar
lechuck committed
732
		return;
ale's avatar
ale committed
733
	}
lechuck's avatar
lechuck committed
734

ale's avatar
ale committed
735
736
737
738
739
740
741
742
	/**
	 * Filters if BuddyPress Activity Akismet support has been disabled by another plugin.
	 *
	 * @since BuddyPress (1.6.0)
	 *
	 * @param bool $value Return value of bp_is_akismet_active boolean function.
	 */
	if ( ! apply_filters( 'bp_activity_use_akismet', bp_is_akismet_active() ) ) {
lechuck's avatar
lechuck committed
743
		return;
ale's avatar
ale committed
744
	}
lechuck's avatar
lechuck committed
745
746
747
748

	// Instantiate Akismet for BuddyPress
	$bp->activity->akismet = new BP_Akismet();
}
lucha's avatar
lucha committed
749
750
751
752
753
754
755
756
757
758
759
760

/**
 * AJAX endpoint for Suggestions API lookups.
 *
 * @since BuddyPress (2.1.0)
 */
function bp_ajax_get_suggestions() {
	if ( ! bp_is_user_active() || empty( $_GET['term'] ) || empty( $_GET['type'] ) ) {
		wp_send_json_error( 'missing_parameter' );
		exit;
	}

ale's avatar
ale committed
761
	$args = array(
lucha's avatar
lucha committed
762
763
		'term' => sanitize_text_field( $_GET['term'] ),
		'type' => sanitize_text_field( $_GET['type'] ),
ale's avatar
ale committed
764
765
766
767
768
769
770
771
	);

	// Support per-Group suggestions.
	if ( ! empty( $_GET['group-id'] ) ) {
		$args['group_id'] = absint( $_GET['group-id'] );
	}

	$results = bp_core_get_suggestions( $args );
lucha's avatar
lucha committed
772
773
774
775
776
777
778
779
780

	if ( is_wp_error( $results ) ) {
		wp_send_json_error( $results->get_error_message() );
		exit;
	}

	wp_send_json_success( $results );
}
add_action( 'wp_ajax_bp_get_suggestions', 'bp_ajax_get_suggestions' );
ale's avatar
ale committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

/**
 * Detect a change in post type status, and initiate an activity update if necessary.
 *
 * @since BuddyPress (2.2.0)
 *
 * @todo Support untrashing better.
 *
 * @param string $new_status New status for the post.
 * @param string $old_status Old status for the post.
 * @param object $post       Post data.
 */
function bp_activity_catch_transition_post_type_status( $new_status, $old_status, $post ) {
	if ( ! post_type_supports( $post->post_type, 'buddypress-activity' ) ) {
		return;
	}

	// This is an edit.
	if ( $new_status === $old_status ) {
		// An edit of an existing post should update the existing activity item.
		if ( $new_status == 'publish' ) {
			bp_activity_post_type_update( $post );
		}

		return;
	}

	// Publishing a previously unpublished post.
	if ( 'publish' === $new_status ) {
		// Untrashing the post type - nothing here yet.
		if ( 'trash' == $old_status ) {

			/**
			 * Fires if untrashing post in a post type.
			 *
			 * This is a variable filter that is dependent on the post type
			 * being untrashed.
			 *
			 * @since BuddyPress (2.2.0)
			 *
			 * @param WP_Post $post Post data.
			 */
			do_action( 'bp_activity_post_type_untrash_' . $post->post_type, $post );
		} else {
			// Record the post.
			bp_activity_post_type_publish( $post->ID, $post );
		}

	// Unpublishing a previously published post.
	} elseif ( 'publish' === $old_status ) {
		// Some form of pending status - only remove the activity entry
		bp_activity_post_type_unpublish( $post->ID, $post );
	}
}
add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );