ajax.php ➔ bp_nouveau_ajax_post_update()   F
last analyzed

Complexity

Conditions 18
Paths 576

Size

Total Lines 100

Duplication

Lines 9
Ratio 9 %

Importance

Changes 0
Metric Value
cc 18
nc 576
nop 0
dl 9
loc 100
rs 1.031
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Activity Ajax functions
4
 *
5
 * @since 3.0.0
6
 * @version 3.1.0
7
 */
8
9
// Exit if accessed directly.
10
defined( 'ABSPATH' ) || exit;
11
12
add_action( 'admin_init', function() {
13
	$ajax_actions = array(
14
		array(
15
			'activity_filter' => array(
16
				'function' => 'bp_nouveau_ajax_object_template_loader',
17
				'nopriv'   => true,
18
			),
19
		),
20
		array(
21
			'get_single_activity_content' => array(
22
				'function' => 'bp_nouveau_ajax_get_single_activity_content',
23
				'nopriv'   => true,
24
			),
25
		),
26
		array(
27
			'activity_mark_fav' => array(
28
				'function' => 'bp_nouveau_ajax_mark_activity_favorite',
29
				'nopriv'   => false,
30
			),
31
		),
32
		array(
33
			'activity_mark_unfav' => array(
34
				'function' => 'bp_nouveau_ajax_unmark_activity_favorite',
35
				'nopriv'   => false,
36
			),
37
		),
38
		array(
39
			'activity_clear_new_mentions' => array(
40
				'function' => 'bp_nouveau_ajax_clear_new_mentions',
41
				'nopriv'   => false,
42
			),
43
		),
44
		array(
45
			'delete_activity' => array(
46
				'function' => 'bp_nouveau_ajax_delete_activity',
47
				'nopriv'   => false,
48
			),
49
		),
50
		array(
51
			'new_activity_comment' => array(
52
				'function' => 'bp_nouveau_ajax_new_activity_comment',
53
				'nopriv'   => false,
54
			),
55
		),
56
		array(
57
			'bp_nouveau_get_activity_objects' => array(
58
				'function' => 'bp_nouveau_ajax_get_activity_objects',
59
				'nopriv'   => false,
60
			),
61
		),
62
		array(
63
			'post_update' => array(
64
				'function' => 'bp_nouveau_ajax_post_update',
65
				'nopriv'   => false,
66
			),
67
		),
68
		array(
69
			'bp_spam_activity' => array(
70
				'function' => 'bp_nouveau_ajax_spam_activity',
71
				'nopriv'   => false,
72
			),
73
		),
74
	);
75
76 View Code Duplication
	foreach ( $ajax_actions as $ajax_action ) {
77
		$action = key( $ajax_action );
78
79
		add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
80
81
		if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
82
			add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
83
		}
84
	}
85
}, 12 );
86
87
/**
88
 * Mark an activity as a favourite via a POST request.
89
 *
90
 * @since 3.0.0
91
 *
92
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
 */
94
function bp_nouveau_ajax_mark_activity_favorite() {
95
	if ( ! bp_is_post_request() ) {
96
		wp_send_json_error();
97
	}
98
99
	// Nonce check!
100
	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
101
		wp_send_json_error();
102
	}
103
104
	if ( bp_activity_add_user_favorite( $_POST['id'] ) ) {
105
		$response = array( 'content' => __( 'Remove Favorite', 'buddypress' ) );
106
107
		if ( ! bp_is_user() ) {
108
			$fav_count = (int) bp_get_total_favorite_count_for_user( bp_loggedin_user_id() );
109
110
			if ( 1 === $fav_count ) {
111
				$response['directory_tab'] = '<li id="activity-favorites" data-bp-scope="favorites" data-bp-object="activity">
112
					<a href="' . bp_loggedin_user_domain() . bp_get_activity_slug() . '/favorites/">
113
						' . esc_html__( 'My Favorites', 'buddypress' ) . '
114
					</a>
115
				</li>';
116
			} else {
117
				$response['fav_count'] = $fav_count;
118
			}
119
		}
120
121
		wp_send_json_success( $response );
122
	} else {
123
		wp_send_json_error();
124
	}
125
}
126
127
/**
128
 * Un-favourite an activity via a POST request.
129
 *
130
 * @since 3.0.0
131
 *
132
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
133
 */
134
function bp_nouveau_ajax_unmark_activity_favorite() {
135
	if ( ! bp_is_post_request() ) {
136
		wp_send_json_error();
137
	}
138
139
	// Nonce check!
140
	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
141
		wp_send_json_error();
142
	}
143
144
	if ( bp_activity_remove_user_favorite( $_POST['id'] ) ) {
145
		$response = array( 'content' => __( 'Favorite', 'buddypress' ) );
146
147
		$fav_count = (int) bp_get_total_favorite_count_for_user( bp_loggedin_user_id() );
148
149
		if ( 0 === $fav_count && ! bp_is_single_activity() ) {
150
			$response['no_favorite'] = '<li><div class="bp-feedback bp-messages info">
151
				' . __( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ) . '
152
			</div></li>';
153
		} else {
154
			$response['fav_count'] = $fav_count;
155
		}
156
157
		wp_send_json_success( $response );
158
	} else {
159
		wp_send_json_error();
160
	}
161
}
162
163
/**
164
 * Clear mentions if the directory tab is clicked
165
 *
166
 * @since 3.0.0
167
 *
168
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
169
 */
170
function bp_nouveau_ajax_clear_new_mentions() {
171
	if ( ! bp_is_post_request() ) {
172
		wp_send_json_error();
173
	}
174
175
	// Nonce check!
176
	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
177
		wp_send_json_error();
178
	}
179
180
	bp_activity_clear_new_mentions( bp_loggedin_user_id() );
181
	wp_send_json_success();
182
}
183
184
/**
185
 * Deletes an Activity item/Activity comment item received via a POST request.
186
 *
187
 * @since 3.0.0
188
 *
189
 * @return string JSON reply.
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
190
 */
191
function bp_nouveau_ajax_delete_activity() {
192
	$response = array(
193
		'feedback' => sprintf(
194
			'<div class="bp-feedback bp-messages error">%s</div>',
195
			esc_html__( 'There was a problem when deleting. Please try again.', 'buddypress' )
196
		),
197
	);
198
199
	// Bail if not a POST action.
200
	if ( ! bp_is_post_request() ) {
201
		wp_send_json_error( $response );
202
	}
203
204
	// Nonce check!
205 View Code Duplication
	if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bp_activity_delete_link' ) ) {
206
		wp_send_json_error( $response );
207
	}
208
209
	if ( ! is_user_logged_in() ) {
210
		wp_send_json_error( $response );
211
	}
212
213
	if ( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
214
		wp_send_json_error( $response );
215
	}
216
217
	$activity = new BP_Activity_Activity( (int) $_POST['id'] );
218
219
	// Check access.
220
	if ( ! bp_activity_user_can_delete( $activity ) ) {
221
		wp_send_json_error( $response );
222
	}
223
224
	/** This action is documented in bp-activity/bp-activity-actions.php */
225
	do_action( 'bp_activity_before_action_delete_activity', $activity->id, $activity->user_id );
226
227
	// Deleting an activity comment.
228
	if ( ! empty( $_POST['is_comment'] ) ) {
229
		if ( ! bp_activity_delete_comment( $activity->item_id, $activity->id ) ) {
230
			wp_send_json_error( $response );
231
		}
232
233
	// Deleting an activity.
234
	} else {
235
		if ( ! bp_activity_delete( array( 'id' => $activity->id, 'user_id' => $activity->user_id ) ) ) {
236
			wp_send_json_error( $response );
237
		}
238
	}
239
240
	/** This action is documented in bp-activity/bp-activity-actions.php */
241
	do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
242
243
	// The activity has been deleted successfully
244
	$response = array( 'deleted' => $activity->id );
245
246
	// If on a single activity redirect to user's home.
247 View Code Duplication
	if ( ! empty( $_POST['is_single'] ) ) {
248
		$response['redirect'] = bp_core_get_user_domain( $activity->user_id );
249
		bp_core_add_message( __( 'Activity deleted successfully', 'buddypress' ) );
250
	}
251
252
	wp_send_json_success( $response );
253
}
254
255
/**
256
 * Fetches an activity's full, non-excerpted content via a POST request.
257
 * Used for the 'Read More' link on long activity items.
258
 *
259
 * @since 3.0.0
260
 *
261
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
262
 */
263
function bp_nouveau_ajax_get_single_activity_content() {
264
	$response = array(
265
		'feedback' => sprintf(
266
			'<div class="bp-feedback bp-messages error">%s</div>',
267
			esc_html__( 'There was a problem displaying the content. Please try again.', 'buddypress' )
268
		),
269
	);
270
271
	// Bail if not a POST action.
272
	if ( ! bp_is_post_request() ) {
273
		wp_send_json_error( $response );
274
	}
275
276
	// Nonce check!
277 View Code Duplication
	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
278
		wp_send_json_error( $response );
279
	}
280
281
	$activity_array = bp_activity_get_specific(
282
		array(
283
			'activity_ids'     => $_POST['id'],
284
			'display_comments' => 'stream',
285
		)
286
	);
287
288
	if ( empty( $activity_array['activities'][0] ) ) {
289
		wp_send_json_error( $response );
290
	}
291
292
	$activity = $activity_array['activities'][0];
293
294
	/**
295
	 * Fires before the return of an activity's full, non-excerpted content via a POST request.
296
	 *
297
	 * @since 3.0.0
298
	 *
299
	 * @param string $activity Activity content. Passed by reference.
300
	 */
301
	do_action_ref_array( 'bp_nouveau_get_single_activity_content', array( &$activity ) );
302
303
	// Activity content retrieved through AJAX should run through normal filters, but not be truncated.
304
	remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
305
306
	/** This filter is documented in bp-activity/bp-activity-template.php */
307
	$content = apply_filters( 'bp_get_activity_content_body', $activity->content );
308
309
	wp_send_json_success( array( 'contents' => $content ) );
310
}
311
312
/**
313
 * Posts new Activity comments received via a POST request.
314
 *
315
 * @since 3.0.0
316
 *
317
 * @global BP_Activity_Template $activities_template
318
 *
319
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by John James Jacoby
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
320
 */
321
function bp_nouveau_ajax_new_activity_comment() {
322
	global $activities_template;
323
	$bp = buddypress();
324
325
	$response = array(
326
		'feedback' => sprintf(
327
			'<div class="bp-feedback bp-messages error">%s</div>',
328
			esc_html__( 'There was an error posting your reply. Please try again.', 'buddypress' )
329
		),
330
	);
331
332
	// Bail if not a POST action.
333
	if ( ! bp_is_post_request() ) {
334
		wp_send_json_error( $response );
335
	}
336
337
	// Nonce check!
338
	if ( empty( $_POST['_wpnonce_new_activity_comment'] ) || ! wp_verify_nonce( $_POST['_wpnonce_new_activity_comment'], 'new_activity_comment' ) ) {
339
		wp_send_json_error( $response );
340
	}
341
342
	if ( ! is_user_logged_in() ) {
343
		wp_send_json_error( $response );
344
	}
345
346
	if ( empty( $_POST['content'] ) ) {
347
		wp_send_json_error( array( 'feedback' => sprintf(
348
			'<div class="bp-feedback bp-messages error">%s</div>',
349
			esc_html__( 'Please do not leave the comment area blank.', 'buddypress' )
350
		) ) );
351
	}
352
353 View Code Duplication
	if ( empty( $_POST['form_id'] ) || empty( $_POST['comment_id'] ) || ! is_numeric( $_POST['form_id'] ) || ! is_numeric( $_POST['comment_id'] ) ) {
354
		wp_send_json_error( $response );
355
	}
356
357
	$comment_id = bp_activity_new_comment( array(
358
		'activity_id' => $_POST['form_id'],
359
		'content'     => $_POST['content'],
360
		'parent_id'   => $_POST['comment_id'],
361
	) );
362
363
	if ( ! $comment_id ) {
364
		if ( ! empty( $bp->activity->errors['new_comment'] ) && is_wp_error( $bp->activity->errors['new_comment'] ) ) {
365
			$response = array( 'feedback' => sprintf(
366
				'<div class="bp-feedback bp-messages error">%s</div>',
367
				esc_html( $bp->activity->errors['new_comment']->get_error_message() )
368
			) );
369
			unset( $bp->activity->errors['new_comment'] );
370
		}
371
372
		wp_send_json_error( $response );
373
	}
374
375
	// Load the new activity item into the $activities_template global.
376
	bp_has_activities(
377
		array(
378
			'display_comments' => 'stream',
379
			'hide_spam'        => false,
380
			'show_hidden'      => true,
381
			'include'          => $comment_id,
382
		)
383
	);
384
385
	// Swap the current comment with the activity item we just loaded.
386 View Code Duplication
	if ( isset( $activities_template->activities[0] ) ) {
387
		$activities_template->activity                  = new stdClass();
388
		$activities_template->activity->id              = $activities_template->activities[0]->item_id;
389
		$activities_template->activity->current_comment = $activities_template->activities[0];
390
391
		// Because the whole tree has not been loaded, we manually
392
		// determine depth.
393
		$depth     = 1;
394
		$parent_id = (int) $activities_template->activities[0]->secondary_item_id;
395
		while ( $parent_id !== (int) $activities_template->activities[0]->item_id ) {
396
			$depth++;
397
			$p_obj     = new BP_Activity_Activity( $parent_id );
398
			$parent_id = (int) $p_obj->secondary_item_id;
399
		}
400
		$activities_template->activity->current_comment->depth = $depth;
401
	}
402
403
	ob_start();
404
	// Get activity comment template part.
405
	bp_get_template_part( 'activity/comment' );
406
	$response = array( 'contents' => ob_get_contents() );
407
	ob_end_clean();
408
409
	unset( $activities_template );
410
411
	wp_send_json_success( $response );
412
}
413
414
/**
415
 * Get items to attach the activity to.
416
 *
417
 * This is used within the activity post form autocomplete field.
418
 *
419
 * @since 3.0.0
420
 *
421
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
422
 */
423
function bp_nouveau_ajax_get_activity_objects() {
424
	$response = array();
425
426 View Code Duplication
	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
427
		wp_send_json_error( $response );
428
	}
429
430
	if ( 'group' === $_POST['type'] ) {
431
		$groups = groups_get_groups(
432
			array(
433
				'user_id'      => bp_loggedin_user_id(),
434
				'search_terms' => $_POST['search'],
435
				'show_hidden'  => true,
436
				'per_page'     => 2,
437
			)
438
		);
439
440
		wp_send_json_success( array_map( 'bp_nouveau_prepare_group_for_js', $groups['groups'] ) );
441
	} else {
442
443
		/**
444
		 * Filters the response for custom activity objects.
445
		 *
446
		 * @since 3.0.0
447
		 *
448
		 * @param array $response Array of custom response objects to send to AJAX return.
449
		 * @param array $value    Activity object type from $_POST global.
450
		 */
451
		$response = apply_filters( 'bp_nouveau_get_activity_custom_objects', $response, $_POST['type'] );
452
	}
453
454
	if ( empty( $response ) ) {
455
		wp_send_json_error( array( 'error' => __( 'No activities were found.', 'buddypress' ) ) );
456
	} else {
457
		wp_send_json_success( $response );
458
	}
459
}
460
461
/**
462
 * Processes Activity updates received via a POST request.
463
 *
464
 * @since 3.0.0
465
 *
466
 * @return string JSON reply
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
467
 */
468
function bp_nouveau_ajax_post_update() {
469
	$bp = buddypress();
470
471
	if ( ! is_user_logged_in() || empty( $_POST['_wpnonce_post_update'] ) || ! wp_verify_nonce( $_POST['_wpnonce_post_update'], 'post_update' ) ) {
472
		wp_send_json_error();
473
	}
474
475
	if ( empty( $_POST['content'] ) ) {
476
		wp_send_json_error(
477
			array(
478
				'message' => __( 'Please enter some content to post.', 'buddypress' ),
479
			)
480
		);
481
	}
482
483
	$activity_id = 0;
484
	$item_id     = 0;
485
	$object      = '';
486
	$is_private  = false;
487
488
	// Try to get the item id from posted variables.
489
	if ( ! empty( $_POST['item_id'] ) ) {
490
		$item_id = (int) $_POST['item_id'];
491
	}
492
493
	// Try to get the object from posted variables.
494 View Code Duplication
	if ( ! empty( $_POST['object'] ) ) {
495
		$object = sanitize_key( $_POST['object'] );
496
497
	// If the object is not set and we're in a group, set the item id and the object
498
	} elseif ( bp_is_group() ) {
499
		$item_id = bp_get_current_group_id();
500
		$object  = 'group';
501
		$status  = groups_get_current_group()->status;
502
	}
503
504
	if ( 'user' === $object && bp_is_active( 'activity' ) ) {
505
		$activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
506
507
	} elseif ( 'group' === $object ) {
508
		if ( $item_id && bp_is_active( 'groups' ) ) {
509
			// This function is setting the current group!
510
			$activity_id = groups_post_update(
511
				array(
512
					'content'  => $_POST['content'],
513
					'group_id' => $item_id,
514
				)
515
			);
516
517
			if ( empty( $status ) ) {
518
				if ( ! empty( $bp->groups->current_group->status ) ) {
519
					$status = $bp->groups->current_group->status;
520
				} else {
521
					$group  = groups_get_group( array( 'group_id' => $group_id ) );
0 ignored issues
show
Documentation introduced by Hugo Ashmore
array('group_id' => $group_id) is of type array<string,?>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
522
					$status = $group->status;
523
				}
524
525
				$is_private = 'public' !== $status;
526
			}
527
		}
528
529
	} else {
530
		/** This filter is documented in bp-activity/bp-activity-actions.php */
531
		$activity_id = apply_filters( 'bp_activity_custom_update', false, $object, $item_id, $_POST['content'] );
532
	}
533
534
	if ( empty( $activity_id ) ) {
535
		wp_send_json_error(
536
			array(
537
				'message' => __( 'There was a problem posting your update. Please try again.', 'buddypress' ),
538
			)
539
		);
540
	}
541
542
	ob_start();
543
	if ( bp_has_activities( array( 'include' => $activity_id, 'show_hidden' => $is_private ) ) ) {
544
		while ( bp_activities() ) {
545
			bp_the_activity();
546
			bp_get_template_part( 'activity/entry' );
547
		}
548
	}
549
	$acivity = ob_get_contents();
550
	ob_end_clean();
551
552
	wp_send_json_success( array(
553
		'id'           => $activity_id,
554
		'message'      => esc_html__( 'Update posted.', 'buddypress' ) . ' ' . sprintf( '<a href="%s" class="just-posted">%s</a>', esc_url( bp_activity_get_permalink( $activity_id ) ), esc_html__( 'View activity.', 'buddypress' ) ),
555
		'activity'     => $acivity,
556
557
		/**
558
		 * Filters whether or not an AJAX post update is private.
559
		 *
560
		 * @since 3.0.0
561
		 *
562
		 * @param string/bool $is_private Privacy status for the update.
0 ignored issues
show
Documentation introduced by Michael Beckwith
The doc-type string/bool could not be parsed: Unknown type name "string/bool" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
563
		 */
564
		'is_private'   => apply_filters( 'bp_nouveau_ajax_post_update_is_private', $is_private ),
565
		'is_directory' => bp_is_activity_directory(),
566
	) );
567
}
568
569
/**
570
 * AJAX spam an activity item or comment.
571
 *
572
 * @since 3.0.0
573
 *
574
 * @return string JSON reply.
0 ignored issues
show
Documentation introduced by Hugo Ashmore
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
575
 */
576
function bp_nouveau_ajax_spam_activity() {
577
	$bp = buddypress();
578
579
	$response = array(
580
		'feedback' => sprintf(
581
			'<div class="bp-feedback bp-messages error">%s</div>',
582
			esc_html__( 'There was a problem marking this activity as spam. Please try again.', 'buddypress' )
583
		),
584
	);
585
586
	// Bail if not a POST action.
587
	if ( ! bp_is_post_request() ) {
588
		wp_send_json_error( $response );
589
	}
590
591 View Code Duplication
	if ( ! is_user_logged_in() || ! bp_is_active( 'activity' ) || empty( $bp->activity->akismet ) ) {
592
		wp_send_json_error( $response );
593
	}
594
595
	if ( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
596
		wp_send_json_error( $response );
597
	}
598
599
	// Is the current user allowed to spam items?
600
	if ( ! bp_activity_user_can_mark_spam() ) {
601
		wp_send_json_error( $response );
602
	}
603
604
	$activity = new BP_Activity_Activity( (int) $_POST['id'] );
605
606
	if ( empty( $activity->component ) ) {
607
		wp_send_json_error( $response );
608
	}
609
610
	// Nonce check!
611 View Code Duplication
	if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bp_activity_akismet_spam_' . $activity->id ) ) {
612
		wp_send_json_error( $response );
613
	}
614
615
	/** This action is documented in bp-activity/bp-activity-actions.php */
616
	do_action( 'bp_activity_before_action_spam_activity', $activity->id, $activity );
617
618
	// Mark as spam.
619
	bp_activity_mark_as_spam( $activity );
620
	$activity->save();
621
622
	/** This action is documented in bp-activity/bp-activity-actions.php */
623
	do_action( 'bp_activity_action_spam_activity', $activity->id, $activity->user_id );
624
625
	// Prepare the successfull reply
626
	$response = array( 'spammed' => $activity->id );
627
628
	// If on a single activity redirect to user's home.
629 View Code Duplication
	if ( ! empty( $_POST['is_single'] ) ) {
630
		$response['redirect'] = bp_core_get_user_domain( $activity->user_id );
631
		bp_core_add_message( __( 'This activity has been marked as spam and is no longer visible.', 'buddypress' ) );
632
	}
633
634
	// Send the json reply
635
	wp_send_json_success( $response );
636
}
637