bp-activity-admin.php 38.8 KB
Newer Older
lechuck's avatar
lechuck committed
1
2
<?php
/**
lechuck's avatar
lechuck committed
3
 * BuddyPress Activity component admin screen.
lechuck's avatar
lechuck committed
4
 *
lechuck's avatar
lechuck committed
5
6
 * Props to WordPress core for the Comments admin screen, and its contextual
 * help text, on which this implementation is heavily based.
lechuck's avatar
lechuck committed
7
8
 *
 * @package BuddyPress
lechuck's avatar
lechuck committed
9
10
 * @subpackage ActivityAdmin
 * @since 1.6.0
lechuck's avatar
lechuck committed
11
12
 */

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

lechuck's avatar
lechuck committed
16
// Include WP's list table class.
lechuck's avatar
lechuck committed
17
18
if ( !class_exists( 'WP_List_Table' ) ) require( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

lechuck's avatar
lechuck committed
19
20
21
require dirname( __FILE__ ) . '/classes/class-bp-activity-list-table.php';

// Per_page screen option. Has to be hooked in extremely early.
lechuck's avatar
lechuck committed
22
23
24
25
if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-activity' == $_REQUEST['page'] )
	add_filter( 'set-screen-option', 'bp_activity_admin_screen_options', 10, 3 );

/**
lechuck's avatar
lechuck committed
26
 * Register the Activity component admin screen.
lechuck's avatar
lechuck committed
27
 *
lechuck's avatar
lechuck committed
28
 * @since 1.6.0
lechuck's avatar
lechuck committed
29
30
31
 */
function bp_activity_add_admin_menu() {

lechuck's avatar
lechuck committed
32
	// Add our screen.
lechuck's avatar
lechuck committed
33
	$hook = add_menu_page(
lucha's avatar
lucha committed
34
35
		_x( 'Activity', 'Admin Dashbord SWA page title', 'buddypress' ),
		_x( 'Activity', 'Admin Dashbord SWA menu', 'buddypress' ),
lechuck's avatar
lechuck committed
36
		'bp_moderate',
lechuck's avatar
lechuck committed
37
38
39
40
		'bp-activity',
		'bp_activity_admin',
		'div'
	);
lechuck's avatar
lechuck committed
41
42
43
44
45
46

	// Hook into early actions to load custom CSS and our init handler.
	add_action( "load-$hook", 'bp_activity_admin_load' );
}
add_action( bp_core_admin_hook(), 'bp_activity_add_admin_menu' );

lechuck's avatar
lechuck committed
47
/**
lechuck's avatar
lechuck committed
48
 * Add activity component to custom menus array.
lechuck's avatar
lechuck committed
49
 *
lechuck's avatar
lechuck committed
50
51
52
 * Several BuddyPress components have top-level menu items in the Dashboard,
 * which all appear together in the middle of the Dashboard menu. This function
 * adds the Activity page to the array of these menu items.
lechuck's avatar
lechuck committed
53
 *
lechuck's avatar
lechuck committed
54
 * @since 1.7.0
lechuck's avatar
lechuck committed
55
56
 *
 * @param array $custom_menus The list of top-level BP menu items.
ale's avatar
ale committed
57
 * @return array $custom_menus List of top-level BP menu items, with Activity added.
lechuck's avatar
lechuck committed
58
59
60
61
62
63
64
 */
function bp_activity_admin_menu_order( $custom_menus = array() ) {
	array_push( $custom_menus, 'bp-activity' );
	return $custom_menus;
}
add_filter( 'bp_admin_menu_order', 'bp_activity_admin_menu_order' );

lechuck's avatar
lechuck committed
65
/**
lechuck's avatar
lechuck committed
66
 * AJAX receiver for Activity replies via the admin screen.
lechuck's avatar
lechuck committed
67
 *
lechuck's avatar
lechuck committed
68
69
70
 * Processes requests to add new activity comments, and echoes HTML for a new
 * table row.
 *
lechuck's avatar
lechuck committed
71
 * @since 1.6.0
lechuck's avatar
lechuck committed
72
73
 */
function bp_activity_admin_reply() {
lechuck's avatar
lechuck committed
74
	// Check nonce.
lechuck's avatar
lechuck committed
75
76
77
78
79
80
81
82
83
	check_ajax_referer( 'bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply' );

	$parent_id = ! empty( $_REQUEST['parent_id'] ) ? (int) $_REQUEST['parent_id'] : 0;
	$root_id   = ! empty( $_REQUEST['root_id'] )   ? (int) $_REQUEST['root_id']   : 0;

	// $parent_id is required
	if ( empty( $parent_id ) )
		die( '-1' );

lechuck's avatar
lechuck committed
84
	// If $root_id not set (e.g. for root items), use $parent_id.
lechuck's avatar
lechuck committed
85
86
87
	if ( empty( $root_id ) )
		$root_id = $parent_id;

lechuck's avatar
lechuck committed
88
	// Check that a reply has been entered.
lechuck's avatar
lechuck committed
89
90
91
	if ( empty( $_REQUEST['content'] ) )
		die( __( 'ERROR: Please type a reply.', 'buddypress' ) );

lechuck's avatar
lechuck committed
92
	// Check parent activity exists.
lechuck's avatar
lechuck committed
93
94
95
96
97
98
	$parent_activity = new BP_Activity_Activity( $parent_id );
	if ( empty( $parent_activity->component ) )
		die( __( 'ERROR: The item you are trying to reply to cannot be found, or it has been deleted.', 'buddypress' ) );

	// @todo: Check if user is allowed to create new activity items
	// if ( ! current_user_can( 'bp_new_activity' ) )
lechuck's avatar
lechuck committed
99
	if ( ! current_user_can( 'bp_moderate' ) )
lechuck's avatar
lechuck committed
100
101
		die( '-1' );

lechuck's avatar
lechuck committed
102
	// Add new activity comment.
lechuck's avatar
lechuck committed
103
	$new_activity_id = bp_activity_new_comment( array(
lechuck's avatar
lechuck committed
104
		'activity_id' => $root_id,              // ID of the root activity item.
lechuck's avatar
lechuck committed
105
		'content'     => $_REQUEST['content'],
lechuck's avatar
lechuck committed
106
		'parent_id'   => $parent_id,            // ID of a parent comment.
lechuck's avatar
lechuck committed
107
108
	) );

lechuck's avatar
lechuck committed
109
	// Fetch the new activity item, as we need it to create table markup to return.
lechuck's avatar
lechuck committed
110
111
	$new_activity = new BP_Activity_Activity( $new_activity_id );

lechuck's avatar
lechuck committed
112
	// This needs to be set for the BP_Activity_List_Table constructor to work.
lechuck's avatar
lechuck committed
113
114
	set_current_screen( 'toplevel_page_bp-activity' );

lechuck's avatar
lechuck committed
115
	// Set up an output buffer.
lechuck's avatar
lechuck committed
116
117
118
119
	ob_start();
	$list_table = new BP_Activity_List_Table();
	$list_table->single_row( (array) $new_activity );

lechuck's avatar
lechuck committed
120
	// Get table markup.
lechuck's avatar
lechuck committed
121
122
123
124
125
126
127
128
	$response =  array(
		'data'     => ob_get_contents(),
		'id'       => $new_activity_id,
		'position' => -1,
		'what'     => 'bp_activity',
	);
	ob_end_clean();

lechuck's avatar
lechuck committed
129
	// Send response.
lechuck's avatar
lechuck committed
130
131
132
133
134
135
136
137
138
	$r = new WP_Ajax_Response();
	$r->add( $response );
	$r->send();

	exit();
}
add_action( 'wp_ajax_bp-activity-admin-reply', 'bp_activity_admin_reply' );

/**
lechuck's avatar
lechuck committed
139
140
 * Handle save/update of screen options for the Activity component admin screen.
 *
lechuck's avatar
lechuck committed
141
 * @since 1.6.0
lechuck's avatar
lechuck committed
142
 *
ale's avatar
ale committed
143
144
 * @param string $value     Will always be false unless another plugin filters it first.
 * @param string $option    Screen option name.
lechuck's avatar
lechuck committed
145
 * @param string $new_value Screen option form value.
lechuck's avatar
lechuck committed
146
147
148
149
150
151
 * @return string Option value. False to abandon update.
 */
function bp_activity_admin_screen_options( $value, $option, $new_value ) {
	if ( 'toplevel_page_bp_activity_per_page' != $option && 'toplevel_page_bp_activity_network_per_page' != $option )
		return $value;

lechuck's avatar
lechuck committed
152
	// Per page.
lechuck's avatar
lechuck committed
153
154
155
156
157
158
159
160
	$new_value = (int) $new_value;
	if ( $new_value < 1 || $new_value > 999 )
		return $value;

	return $new_value;
}

/**
lechuck's avatar
lechuck committed
161
162
 * Hide the advanced edit meta boxes by default, so we don't clutter the screen.
 *
lechuck's avatar
lechuck committed
163
 * @since 1.6.0
lechuck's avatar
lechuck committed
164
 *
ale's avatar
ale committed
165
 * @param array     $hidden Array of items to hide.
lechuck's avatar
lechuck committed
166
167
 * @param WP_Screen $screen Screen identifier.
 * @return array Hidden Meta Boxes.
lechuck's avatar
lechuck committed
168
169
170
171
172
 */
function bp_activity_admin_edit_hidden_metaboxes( $hidden, $screen ) {
	if ( empty( $screen->id ) || 'toplevel_page_bp-activity' != $screen->id && 'toplevel_page_bp-activity_network' != $screen->id )
		return $hidden;

lechuck's avatar
lechuck committed
173
	// Hide the primary link meta box by default.
lechuck's avatar
lechuck committed
174
175
	$hidden  = array_merge( (array) $hidden, array( 'bp_activity_itemids', 'bp_activity_link', 'bp_activity_type', 'bp_activity_userid', ) );

ale's avatar
ale committed
176
177
178
	/**
	 * Filters default hidden metaboxes so plugins can alter list.
	 *
lechuck's avatar
lechuck committed
179
	 * @since 1.6.0
ale's avatar
ale committed
180
181
182
183
	 *
	 * @param array     $hidden Default metaboxes to hide.
	 * @param WP_Screen $screen Screen identifier.
	 */
lechuck's avatar
lechuck committed
184
185
186
187
188
	return apply_filters( 'bp_hide_meta_boxes', array_unique( $hidden ), $screen );
}
add_filter( 'default_hidden_meta_boxes', 'bp_activity_admin_edit_hidden_metaboxes', 10, 2 );

/**
lechuck's avatar
lechuck committed
189
 * Set up the Activity admin page.
lechuck's avatar
lechuck committed
190
 *
lechuck's avatar
lechuck committed
191
192
 * Does the following:
 *   - Register contextual help and screen options for this admin page.
ale's avatar
ale committed
193
194
 *   - Enqueues scripts and styles.
 *   - Catches POST and GET requests related to Activity.
lechuck's avatar
lechuck committed
195
 *
lechuck's avatar
lechuck committed
196
 * @since 1.6.0
lechuck's avatar
lechuck committed
197
 *
ale's avatar
ale committed
198
 * @global object                 $bp                     BuddyPress global settings.
lechuck's avatar
lechuck committed
199
 * @global BP_Activity_List_Table $bp_activity_list_table Activity screen list table.
lechuck's avatar
lechuck committed
200
201
 */
function bp_activity_admin_load() {
lechuck's avatar
lechuck committed
202
203
	global $bp_activity_list_table;

lechuck's avatar
lechuck committed
204
	$bp       = buddypress();
lechuck's avatar
lechuck committed
205
	$doaction = bp_admin_list_table_current_bulk_action();
lechuck's avatar
lechuck committed
206
	$min      = bp_core_get_minified_asset_suffix();
lechuck's avatar
lechuck committed
207

ale's avatar
ale committed
208
209
210
	/**
	 * Fires at top of Activity admin page.
	 *
lechuck's avatar
lechuck committed
211
	 * @since 1.6.0
ale's avatar
ale committed
212
213
214
	 *
	 * @param string $doaction Current $_GET action being performed in admin screen.
	 */
lechuck's avatar
lechuck committed
215
216
	do_action( 'bp_activity_admin_load', $doaction );

lechuck's avatar
lechuck committed
217
	// Edit screen.
lechuck's avatar
lechuck committed
218
	if ( 'edit' == $doaction && ! empty( $_GET['aid'] ) ) {
lechuck's avatar
lechuck committed
219
		// Columns screen option.
lechuck's avatar
lechuck committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
		add_screen_option( 'layout_columns', array( 'default' => 2, 'max' => 2, ) );

		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-activity-edit-overview',
			'title'   => __( 'Overview', 'buddypress' ),
			'content' =>
				'<p>' . __( 'You edit activities made on your site similar to the way you edit a comment. This is useful if you need to change which page the activity links to, or when you notice that the author has made a typographical error.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'The two big editing areas for the activity title and content are fixed in place, but you can reposition all the other boxes using drag and drop, and can minimize or expand them by clicking the title bar of each box. Use the Screen Options tab to unhide more boxes (Primary Item/Secondary Item, Link, Type, Author ID) or to choose a 1- or 2-column layout for this screen.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'You can also moderate the activity from this screen using the Status box, where you can also change the timestamp of the activity.', 'buddypress' ) . '</p>'
		) );

		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-activity-edit-advanced',
			'title'   => __( 'Item, Link, Type', 'buddypress' ),
			'content' =>
				'<p>' . __( '<strong>Primary Item/Secondary Item</strong> - These identify the object that created the activity. For example, the fields could reference a comment left on a specific site. Some types of activity may only use one, or none, of these fields.', 'buddypress' ) . '</p>' .
lucha's avatar
lucha committed
236
				'<p>' . __( '<strong>Link</strong> - Used by some types of activity (e.g blog posts and comments, and forum topics and replies) to store a link back to the original content.', 'buddypress' ) . '</p>' .
lechuck's avatar
lechuck committed
237
238
239
240
				'<p>' . __( '<strong>Type</strong> - Each distinct kind of activity has its own type. For example, <code>created_group</code> is used when a group is created and <code>joined_group</code> is used when a user joins a group.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'For information about when and how BuddyPress uses all of these settings, see the Managing Activity link in the panel to the side.', 'buddypress' ) . '</p>'
		) );

lechuck's avatar
lechuck committed
241
		// Help panel - sidebar links.
lechuck's avatar
lechuck committed
242
243
		get_current_screen()->set_help_sidebar(
			'<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
ale's avatar
ale committed
244
245
			'<p>' . __( '<a href="https://codex.buddypress.org/administrator-guide/activity-stream-management-panels/">Managing Activity</a>', 'buddypress' ) . '</p>' .
			'<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
lechuck's avatar
lechuck committed
246
247
248
249
250
251
252
253
254
		);

		// Register metaboxes for the edit screen.
		add_meta_box( 'submitdiv',           _x( 'Status', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_status', get_current_screen()->id, 'side', 'core' );
		add_meta_box( 'bp_activity_itemids', _x( 'Primary Item/Secondary Item', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_itemids', get_current_screen()->id, 'normal', 'core' );
		add_meta_box( 'bp_activity_link',    _x( 'Link', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_link', get_current_screen()->id, 'normal', 'core' );
		add_meta_box( 'bp_activity_type',    _x( 'Type', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_type', get_current_screen()->id, 'normal', 'core' );
		add_meta_box( 'bp_activity_userid',  _x( 'Author ID', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_userid', get_current_screen()->id, 'normal', 'core' );

lechuck's avatar
lechuck committed
255
256
257
258
259
260
261
262
		/**
		 * Fires after the registration of all of the default activity meta boxes.
		 *
		 * @since 2.4.0
		 */
		do_action( 'bp_activity_admin_meta_boxes' );

		// Enqueue JavaScript files.
lechuck's avatar
lechuck committed
263
264
265
266
		wp_enqueue_script( 'postbox' );
		wp_enqueue_script( 'dashboard' );
		wp_enqueue_script( 'comment' );

lechuck's avatar
lechuck committed
267
	// Index screen.
lechuck's avatar
lechuck committed
268
	} else {
lechuck's avatar
lechuck committed
269
		// Create the Activity screen list table.
lechuck's avatar
lechuck committed
270
271
		$bp_activity_list_table = new BP_Activity_List_Table();

lechuck's avatar
lechuck committed
272
		// The per_page screen option.
lechuck's avatar
lechuck committed
273
274
		add_screen_option( 'per_page', array( 'label' => _x( 'Activity', 'Activity items per page (screen options)', 'buddypress' )) );

lechuck's avatar
lechuck committed
275
		// Help panel - overview text.
lechuck's avatar
lechuck committed
276
277
278
279
280
281
282
283
		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-activity-overview',
			'title'   => __( 'Overview', 'buddypress' ),
			'content' =>
				'<p>' . __( 'You can manage activities made on your site similar to the way you manage comments and other content. This screen is customizable in the same ways as other management screens, and you can act on activities using the on-hover action links or the Bulk Actions.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'There are many different types of activities. Some are generated automatically by BuddyPress and other plugins, and some are entered directly by a user in the form of status update. To help manage the different activity types, use the filter dropdown box to switch between them.', 'buddypress' ) . '</p>'
		) );

lechuck's avatar
lechuck committed
284
		// Help panel - moderation text.
lechuck's avatar
lechuck committed
285
286
287
288
289
290
291
292
		get_current_screen()->add_help_tab( array(
			'id'		=> 'bp-activity-moderating',
			'title'		=> __( 'Moderating Activity', 'buddypress' ),
			'content'	=>
				'<p>' . __( 'In the <strong>Activity</strong> column, above each activity it says &#8220;Submitted on,&#8221; followed by the date and time the activity item was generated on your site. Clicking on the date/time link will take you to that activity on your live site. Hovering over any activity gives you options to reply, edit, spam mark, or delete that activity.', 'buddypress' ) . '</p>' .
				'<p>' . __( "In the <strong>In Response To</strong> column, if the activity was in reply to another activity, it shows that activity's author's picture and name, and a link to that activity on your live site. If there is a small bubble, the number in it shows how many other activities are related to this one; these are usually comments. Clicking the bubble will filter the activity screen to show only related activity items.", 'buddypress' ) . '</p>'
		) );

lechuck's avatar
lechuck committed
293
		// Help panel - sidebar links.
lechuck's avatar
lechuck committed
294
295
		get_current_screen()->set_help_sidebar(
			'<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
ale's avatar
ale committed
296
			'<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
lechuck's avatar
lechuck committed
297
		);
lechuck's avatar
lechuck committed
298
299
300
301
302
303
304

		// Add accessible hidden heading and text for Activity screen pagination.
		if ( bp_get_major_wp_version() >= 4.4 ) {
			get_current_screen()->set_screen_reader_content( array(
				'heading_pagination' => __( 'Activity list navigation', 'buddypress' ),
			) );
		}
lechuck's avatar
lechuck committed
305
306
	}

lechuck's avatar
lechuck committed
307
308
	// Enqueue CSS and JavaScript.
	wp_enqueue_script( 'bp_activity_admin_js', $bp->plugin_url . "bp-activity/admin/js/admin{$min}.js",   array( 'jquery', 'wp-ajax-response' ), bp_get_version(), true );
lechuck's avatar
lechuck committed
309
	wp_localize_script( 'bp_activity_admin_js', 'bp_activity_admin_vars', array(
lechuck's avatar
lechuck committed
310
311
312
		'page' => get_current_screen()->id
	) );
	wp_enqueue_style( 'bp_activity_admin_css', $bp->plugin_url . "bp-activity/admin/css/admin{$min}.css", array(),                               bp_get_version()       );
lechuck's avatar
lechuck committed
313

lucha's avatar
lucha committed
314
315
316
317
318
	wp_style_add_data( 'bp_activity_admin_css', 'rtl', true );
	if ( $min ) {
		wp_style_add_data( 'bp_activity_admin_css', 'suffix', $min );
	}

lechuck's avatar
lechuck committed
319
320
321
322
323
324
	/**
	 * Fires after the activity js and style has been enqueued.
	 *
	 * @since 2.4.0
	 */
	do_action( 'bp_activity_admin_enqueue_scripts' );
lucha's avatar
lucha committed
325

lechuck's avatar
lechuck committed
326
	// Handle spam/un-spam/delete of activities.
lechuck's avatar
lechuck committed
327
328
	if ( !empty( $doaction ) && ! in_array( $doaction, array( '-1', 'edit', 'save', ) ) ) {

lechuck's avatar
lechuck committed
329
		// Build redirection URL.
lechuck's avatar
lechuck committed
330
331
332
		$redirect_to = remove_query_arg( array( 'aid', 'deleted', 'error', 'spammed', 'unspammed', ), wp_get_referer() );
		$redirect_to = add_query_arg( 'paged', $bp_activity_list_table->get_pagenum(), $redirect_to );

lechuck's avatar
lechuck committed
333
		// Get activity IDs.
lechuck's avatar
lechuck committed
334
335
		$activity_ids = array_map( 'absint', (array) $_REQUEST['aid'] );

ale's avatar
ale committed
336
337
338
		/**
		 * Filters list of IDs being spammed/un-spammed/deleted.
		 *
lechuck's avatar
lechuck committed
339
		 * @since 1.6.0
ale's avatar
ale committed
340
341
342
		 *
		 * @param array $activity_ids Activity IDs to spam/un-spam/delete.
		 */
lechuck's avatar
lechuck committed
343
344
345
346
		$activity_ids = apply_filters( 'bp_activity_admin_action_activity_ids', $activity_ids );

		// Is this a bulk request?
		if ( 'bulk_' == substr( $doaction, 0, 5 ) && ! empty( $_REQUEST['aid'] ) ) {
lechuck's avatar
lechuck committed
347
			// Check this is a valid form submission.
lechuck's avatar
lechuck committed
348
349
			check_admin_referer( 'bulk-activities' );

lechuck's avatar
lechuck committed
350
			// Trim 'bulk_' off the action name to avoid duplicating a ton of code.
lechuck's avatar
lechuck committed
351
352
353
354
355
			$doaction = substr( $doaction, 5 );

		// This is a request to delete, spam, or un-spam, a single item.
		} elseif ( !empty( $_REQUEST['aid'] ) ) {

lechuck's avatar
lechuck committed
356
			// Check this is a valid form submission.
lechuck's avatar
lechuck committed
357
358
359
			check_admin_referer( 'spam-activity_' . $activity_ids[0] );
		}

lechuck's avatar
lechuck committed
360
		// Initialise counters for how many of each type of item we perform an action on.
lechuck's avatar
lechuck committed
361
362
		$deleted = $spammed = $unspammed = 0;

lechuck's avatar
lechuck committed
363
		// Store any errors that occurs when updating the database items.
lechuck's avatar
lechuck committed
364
365
366
367
368
		$errors = array();

		// "We'd like to shoot the monster, could you move, please?"
		foreach ( $activity_ids as $activity_id ) {
			// @todo: Check the permissions on each
lechuck's avatar
lechuck committed
369
370
371
			// if ( ! current_user_can( 'bp_edit_activity', $activity_id ) )
			// continue;
			// Get the activity from the database.
lechuck's avatar
lechuck committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
			$activity = new BP_Activity_Activity( $activity_id );
			if ( empty( $activity->component ) ) {
				$errors[] = $activity_id;
				continue;
			}

			switch ( $doaction ) {
				case 'delete' :
					if ( 'activity_comment' == $activity->type )
						bp_activity_delete_comment( $activity->item_id, $activity->id );
					else
						bp_activity_delete( array( 'id' => $activity->id ) );

					$deleted++;
					break;

				case 'ham' :
					/**
					 * Remove moderation and blacklist checks in case we want to ham an activity
					 * which contains one of these listed keys.
					 */
					remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 );
					remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2, 1 );

					bp_activity_mark_as_ham( $activity );
					$result = $activity->save();

lechuck's avatar
lechuck committed
399
					// Check for any error during activity save.
lechuck's avatar
lechuck committed
400
401
402
403
404
405
406
407
408
409
					if ( ! $result )
						$errors[] = $activity->id;
					else
						$unspammed++;
					break;

				case 'spam' :
					bp_activity_mark_as_spam( $activity );
					$result = $activity->save();

lechuck's avatar
lechuck committed
410
					// Check for any error during activity save.
lechuck's avatar
lechuck committed
411
412
413
414
415
416
417
418
419
420
					if ( ! $result )
						$errors[] = $activity->id;
					else
						$spammed++;
					break;

				default:
					break;
			}

lechuck's avatar
lechuck committed
421
			// Release memory.
lechuck's avatar
lechuck committed
422
423
424
			unset( $activity );
		}

ale's avatar
ale committed
425
426
427
428
429
		/**
		 * Fires before redirect for plugins to do something with activity.
		 *
		 * Passes an activity array counts how many were spam, not spam, deleted, and IDs that were errors.
		 *
lechuck's avatar
lechuck committed
430
		 * @since 1.6.0
ale's avatar
ale committed
431
432
433
434
435
		 *
		 * @param array  $value        Array holding spam, not spam, deleted counts, error IDs.
		 * @param string $redirect_to  URL to redirect to.
		 * @param array  $activity_ids Original array of activity IDs.
		 */
lechuck's avatar
lechuck committed
436
437
438
439
440
441
442
443
444
445
446
447
		do_action( 'bp_activity_admin_action_after', array( $spammed, $unspammed, $deleted, $errors ), $redirect_to, $activity_ids );

		// Add arguments to the redirect URL so that on page reload, we can easily display what we've just done.
		if ( $spammed )
			$redirect_to = add_query_arg( 'spammed', $spammed, $redirect_to );

		if ( $unspammed )
			$redirect_to = add_query_arg( 'unspammed', $unspammed, $redirect_to );

		if ( $deleted )
			$redirect_to = add_query_arg( 'deleted', $deleted, $redirect_to );

lechuck's avatar
lechuck committed
448
		// If an error occurred, pass back the activity ID that failed.
lechuck's avatar
lechuck committed
449
450
451
		if ( ! empty( $errors ) )
			$redirect_to = add_query_arg( 'error', implode ( ',', array_map( 'absint', $errors ) ), $redirect_to );

ale's avatar
ale committed
452
453
454
		/**
		 * Filters redirect URL after activity spamming/un-spamming/deletion occurs.
		 *
lechuck's avatar
lechuck committed
455
		 * @since 1.6.0
ale's avatar
ale committed
456
457
458
		 *
		 * @param string $redirect_to URL to redirect to.
		 */
lechuck's avatar
lechuck committed
459
460
461
462
		wp_redirect( apply_filters( 'bp_activity_admin_action_redirect', $redirect_to ) );
		exit;


lechuck's avatar
lechuck committed
463
	// Save the edit.
lechuck's avatar
lechuck committed
464
	} elseif ( $doaction && 'save' == $doaction ) {
lechuck's avatar
lechuck committed
465
		// Build redirection URL.
lechuck's avatar
lechuck committed
466
467
		$redirect_to = remove_query_arg( array( 'action', 'aid', 'deleted', 'error', 'spammed', 'unspammed', ), $_SERVER['REQUEST_URI'] );

lechuck's avatar
lechuck committed
468
		// Get activity ID.
lechuck's avatar
lechuck committed
469
470
		$activity_id = (int) $_REQUEST['aid'];

lechuck's avatar
lechuck committed
471
		// Check this is a valid form submission.
lechuck's avatar
lechuck committed
472
473
		check_admin_referer( 'edit-activity_' . $activity_id );

lechuck's avatar
lechuck committed
474
		// Get the activity from the database.
lechuck's avatar
lechuck committed
475
476
		$activity = new BP_Activity_Activity( $activity_id );

lechuck's avatar
lechuck committed
477
		// If the activity doesn't exist, just redirect back to the index.
lechuck's avatar
lechuck committed
478
479
480
481
482
		if ( empty( $activity->component ) ) {
			wp_redirect( $redirect_to );
			exit;
		}

lechuck's avatar
lechuck committed
483
484
		// Check the form for the updated properties.
		// Store any error that occurs when updating the database item.
lechuck's avatar
lechuck committed
485
486
		$error = 0;

lechuck's avatar
lechuck committed
487
		// Activity spam status.
lechuck's avatar
lechuck committed
488
489
490
491
492
493
		$prev_spam_status = $new_spam_status = false;
		if ( ! empty( $_POST['activity_status'] ) ) {
			$prev_spam_status = $activity->is_spam;
			$new_spam_status  = ( 'spam' == $_POST['activity_status'] ) ? true : false;
		}

lechuck's avatar
lechuck committed
494
		// Activity action.
lechuck's avatar
lechuck committed
495
496
497
		if ( isset( $_POST['bp-activities-action'] ) )
			$activity->action = $_POST['bp-activities-action'];

lechuck's avatar
lechuck committed
498
		// Activity content.
lechuck's avatar
lechuck committed
499
500
501
		if ( isset( $_POST['bp-activities-content'] ) )
			$activity->content = $_POST['bp-activities-content'];

lechuck's avatar
lechuck committed
502
		// Activity primary link.
lechuck's avatar
lechuck committed
503
504
505
		if ( ! empty( $_POST['bp-activities-link'] ) )
			$activity->primary_link = $_POST['bp-activities-link'];

lechuck's avatar
lechuck committed
506
		// Activity user ID.
lechuck's avatar
lechuck committed
507
508
509
		if ( ! empty( $_POST['bp-activities-userid'] ) )
			$activity->user_id = (int) $_POST['bp-activities-userid'];

lechuck's avatar
lechuck committed
510
		// Activity item primary ID.
lechuck's avatar
lechuck committed
511
512
513
		if ( isset( $_POST['bp-activities-primaryid'] ) )
			$activity->item_id = (int) $_POST['bp-activities-primaryid'];

lechuck's avatar
lechuck committed
514
		// Activity item secondary ID.
lechuck's avatar
lechuck committed
515
516
517
		if ( isset( $_POST['bp-activities-secondaryid'] ) )
			$activity->secondary_item_id = (int) $_POST['bp-activities-secondaryid'];

lechuck's avatar
lechuck committed
518
		// Activity type.
lechuck's avatar
lechuck committed
519
		if ( ! empty( $_POST['bp-activities-type'] ) ) {
lechuck's avatar
lechuck committed
520
			$actions = bp_activity_admin_get_activity_actions();
lechuck's avatar
lechuck committed
521

lechuck's avatar
lechuck committed
522
			// Check that the new type is a registered activity type.
lechuck's avatar
lechuck committed
523
			if ( in_array( $_POST['bp-activities-type'], $actions ) ) {
lechuck's avatar
lechuck committed
524
				$activity->type = $_POST['bp-activities-type'];
lechuck's avatar
lechuck committed
525
			}
lechuck's avatar
lechuck committed
526
527
		}

lechuck's avatar
lechuck committed
528
		// Activity timestamp.
lechuck's avatar
lechuck committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
		if ( ! empty( $_POST['aa'] ) && ! empty( $_POST['mm'] ) && ! empty( $_POST['jj'] ) && ! empty( $_POST['hh'] ) && ! empty( $_POST['mn'] ) && ! empty( $_POST['ss'] ) ) {
			$aa = $_POST['aa'];
			$mm = $_POST['mm'];
			$jj = $_POST['jj'];
			$hh = $_POST['hh'];
			$mn = $_POST['mn'];
			$ss = $_POST['ss'];
			$aa = ( $aa <= 0 ) ? date( 'Y' ) : $aa;
			$mm = ( $mm <= 0 ) ? date( 'n' ) : $mm;
			$jj = ( $jj > 31 ) ? 31 : $jj;
			$jj = ( $jj <= 0 ) ? date( 'j' ) : $jj;
			$hh = ( $hh > 23 ) ? $hh -24 : $hh;
			$mn = ( $mn > 59 ) ? $mn -60 : $mn;
			$ss = ( $ss > 59 ) ? $ss -60 : $ss;

lechuck's avatar
lechuck committed
544
			// Reconstruct the date into a timestamp.
lechuck's avatar
lechuck committed
545
546
547
548
549
550
551
552
553
554
555
556
557
			$gmt_date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );

			$activity->date_recorded = $gmt_date;
		}

		// Has the spam status has changed?
		if ( $new_spam_status != $prev_spam_status ) {
			if ( $new_spam_status )
				bp_activity_mark_as_spam( $activity );
			else
				bp_activity_mark_as_ham( $activity );
		}

lechuck's avatar
lechuck committed
558
		// Save.
lechuck's avatar
lechuck committed
559
560
		$result = $activity->save();

lechuck's avatar
lechuck committed
561
		// Clear the activity stream first page cache, in case this activity's timestamp was changed.
lechuck's avatar
lechuck committed
562
563
		wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );

lechuck's avatar
lechuck committed
564
		// Check for any error during activity save.
lechuck's avatar
lechuck committed
565
566
567
		if ( false === $result )
			$error = $activity->id;

ale's avatar
ale committed
568
569
570
		/**
		 * Fires before redirect so plugins can do something first on save action.
		 *
lechuck's avatar
lechuck committed
571
		 * @since 1.6.0
ale's avatar
ale committed
572
		 *
lechuck's avatar
lechuck committed
573
		 * @param array $value Array holding activity object and ID that holds error.
ale's avatar
ale committed
574
		 */
lechuck's avatar
lechuck committed
575
576
		do_action_ref_array( 'bp_activity_admin_edit_after', array( &$activity, $error ) );

lechuck's avatar
lechuck committed
577
		// If an error occurred, pass back the activity ID that failed.
lechuck's avatar
lechuck committed
578
579
580
581
582
		if ( $error )
			$redirect_to = add_query_arg( 'error', (int) $error, $redirect_to );
		else
			$redirect_to = add_query_arg( 'updated', (int) $activity->id, $redirect_to );

ale's avatar
ale committed
583
584
585
		/**
		 * Filters URL to redirect to after saving.
		 *
lechuck's avatar
lechuck committed
586
		 * @since 1.6.0
ale's avatar
ale committed
587
588
589
		 *
		 * @param string $redirect_to URL to redirect to.
		 */
lechuck's avatar
lechuck committed
590
591
592
593
594
595
596
597
598
599
600
601
		wp_redirect( apply_filters( 'bp_activity_admin_edit_redirect', $redirect_to ) );
		exit;


	// If a referrer and a nonce is supplied, but no action, redirect back.
	} elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
		wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) );
		exit;
	}
}

/**
lechuck's avatar
lechuck committed
602
 * Output the Activity component admin screens.
lechuck's avatar
lechuck committed
603
 *
lechuck's avatar
lechuck committed
604
 * @since 1.6.0
lechuck's avatar
lechuck committed
605
606
 */
function bp_activity_admin() {
lechuck's avatar
lechuck committed
607
	// Decide whether to load the index or edit screen.
lechuck's avatar
lechuck committed
608
609
	$doaction = ! empty( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';

lechuck's avatar
lechuck committed
610
	// Display the single activity edit screen.
lechuck's avatar
lechuck committed
611
612
613
	if ( 'edit' == $doaction && ! empty( $_GET['aid'] ) )
		bp_activity_admin_edit();

lechuck's avatar
lechuck committed
614
	// Otherwise, display the Activity index screen.
lechuck's avatar
lechuck committed
615
616
617
618
619
	else
		bp_activity_admin_index();
}

/**
lechuck's avatar
lechuck committed
620
 * Display the single activity edit screen.
lechuck's avatar
lechuck committed
621
 *
lechuck's avatar
lechuck committed
622
 * @since 1.6.0
lechuck's avatar
lechuck committed
623
624
625
626
627
628
629
630
 */
function bp_activity_admin_edit() {

	// @todo: Check if user is allowed to edit activity items
	// if ( ! current_user_can( 'bp_edit_activity' ) )
	if ( ! is_super_admin() )
		die( '-1' );

lechuck's avatar
lechuck committed
631
	// Get the activity from the database.
lechuck's avatar
lechuck committed
632
633
634
635
636
637
638
639
640
641
642
	$activity = bp_activity_get( array(
		'in'               => ! empty( $_REQUEST['aid'] ) ? (int) $_REQUEST['aid'] : 0,
		'max'              => 1,
		'show_hidden'      => true,
		'spam'             => 'all',
		'display_comments' => 0
	) );

	if ( ! empty( $activity['activities'][0] ) ) {
		$activity = $activity['activities'][0];

lechuck's avatar
lechuck committed
643
		// Workaround to use WP's touch_time() without duplicating that function.
lechuck's avatar
lechuck committed
644
645
646
647
648
649
		$GLOBALS['comment'] = new stdClass;
		$GLOBALS['comment']->comment_date = $activity->date_recorded;
	} else {
		$activity = '';
	}

lechuck's avatar
lechuck committed
650
	// Construct URL for form.
lechuck's avatar
lechuck committed
651
652
653
	$form_url = remove_query_arg( array( 'action', 'deleted', 'error', 'spammed', 'unspammed', ), $_SERVER['REQUEST_URI'] );
	$form_url = add_query_arg( 'action', 'save', $form_url );

ale's avatar
ale committed
654
655
656
	/**
	 * Fires before activity edit form is displays so plugins can modify the activity.
	 *
lechuck's avatar
lechuck committed
657
	 * @since 1.6.0
ale's avatar
ale committed
658
659
660
	 *
	 * @param array $value Array holding single activity object that was passed by reference.
	 */
lechuck's avatar
lechuck committed
661
	do_action_ref_array( 'bp_activity_admin_edit', array( &$activity ) ); ?>
lechuck's avatar
lechuck committed
662
663

	<div class="wrap">
lechuck's avatar
lechuck committed
664
		<h1><?php printf( __( 'Editing Activity (ID #%s)', 'buddypress' ), number_format_i18n( (int) $_REQUEST['aid'] ) ); ?></h1>
lechuck's avatar
lechuck committed
665
666
667

		<?php if ( ! empty( $activity ) ) : ?>

ale's avatar
ale committed
668
			<form action="<?php echo esc_url( $form_url ); ?>" id="bp-activities-edit-form" method="post">
lechuck's avatar
lechuck committed
669
670
				<div id="poststuff">

lechuck's avatar
lechuck committed
671
					<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
lechuck's avatar
lechuck committed
672
						<div id="post-body-content">
lechuck's avatar
lechuck committed
673
							<div id="postdiv">
lechuck's avatar
lechuck committed
674
								<div id="bp_activity_action" class="postbox">
lechuck's avatar
lechuck committed
675
									<h2><?php _e( 'Action', 'buddypress' ); ?></h2>
lechuck's avatar
lechuck committed
676
677
678
679
680
681
									<div class="inside">
										<?php wp_editor( stripslashes( $activity->action ), 'bp-activities-action', array( 'media_buttons' => false, 'textarea_rows' => 7, 'teeny' => true, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
									</div>
								</div>

								<div id="bp_activity_content" class="postbox">
lechuck's avatar
lechuck committed
682
									<h2><?php _e( 'Content', 'buddypress' ); ?></h2>
lechuck's avatar
lechuck committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
									<div class="inside">
										<?php wp_editor( stripslashes( $activity->content ), 'bp-activities-content', array( 'media_buttons' => false, 'teeny' => true, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
									</div>
								</div>
							</div>
						</div><!-- #post-body-content -->

						<div id="postbox-container-1" class="postbox-container">
							<?php do_meta_boxes( get_current_screen()->id, 'side', $activity ); ?>
						</div>

						<div id="postbox-container-2" class="postbox-container">
							<?php do_meta_boxes( get_current_screen()->id, 'normal', $activity ); ?>
							<?php do_meta_boxes( get_current_screen()->id, 'advanced', $activity ); ?>
						</div>
					</div><!-- #post-body -->

				</div><!-- #poststuff -->
				<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
				<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
				<?php wp_nonce_field( 'edit-activity_' . $activity->id ); ?>
			</form>

		<?php else : ?>
lechuck's avatar
lechuck committed
707
708
709
710
			<p>
				<?php _e( 'No activity found with this ID.', 'buddypress' ); ?>
				<a href="<?php echo esc_url( bp_get_admin_url( 'admin.php?page=bp-activity' ) ); ?>"><?php _e( 'Go back and try again.', 'buddypress' ); ?></a>
			</p>
lechuck's avatar
lechuck committed
711
712
713
714
715
716
717
718
		<?php endif; ?>

	</div><!-- .wrap -->

<?php
}

/**
lechuck's avatar
lechuck committed
719
 * Status metabox for the Activity admin edit screen.
lechuck's avatar
lechuck committed
720
 *
lechuck's avatar
lechuck committed
721
 * @since 1.6.0
lechuck's avatar
lechuck committed
722
723
 *
 * @param object $item Activity item.
lechuck's avatar
lechuck committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
 */
function bp_activity_admin_edit_metabox_status( $item ) {
?>

	<div class="submitbox" id="submitcomment">

		<div id="minor-publishing">
			<div id="minor-publishing-actions">
				<div id="preview-action">
					<a class="button preview" href="<?php echo esc_attr( bp_activity_get_permalink( $item->id, $item ) ); ?>" target="_blank"><?php _e( 'View Activity', 'buddypress' ); ?></a>
				</div>

				<div class="clear"></div>
			</div><!-- #minor-publishing-actions -->

			<div id="misc-publishing-actions">
				<div class="misc-pub-section" id="comment-status-radio">
lechuck's avatar
lechuck committed
741
742
					<label class="approved" for="activity-status-approved"><input type="radio" name="activity_status" id="activity-status-approved" value="ham" <?php checked( $item->is_spam, 0 ); ?>><?php _e( 'Approved', 'buddypress' ); ?></label><br />
					<label class="spam" for="activity-status-spam"><input type="radio" name="activity_status" id="activity-status-spam" value="spam" <?php checked( $item->is_spam, 1 ); ?>><?php _e( 'Spam', 'buddypress' ); ?></label>
lechuck's avatar
lechuck committed
743
744
745
746
				</div>

				<div class="misc-pub-section curtime misc-pub-section-last">
					<?php
lechuck's avatar
lechuck committed
747
					// Translators: Publish box date format, see http://php.net/date.
lechuck's avatar
lechuck committed
748
749
750
					$datef = __( 'M j, Y @ G:i', 'buddypress' );
					$date  = date_i18n( $datef, strtotime( $item->date_recorded ) );
					?>
lechuck's avatar
lechuck committed
751
					<span id="timestamp"><?php printf( __( 'Submitted on: %s', 'buddypress' ), '<strong>' . $date . '</strong>' ); ?></span>&nbsp;<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" tabindex='4'><?php _e( 'Edit', 'buddypress' ); ?></a>
lechuck's avatar
lechuck committed
752
753
754
755
756
757
758
759
760
761
762
763

					<div id='timestampdiv' class='hide-if-js'>
						<?php touch_time( 1, 0, 5 ); ?>
					</div><!-- #timestampdiv -->
				</div>
			</div> <!-- #misc-publishing-actions -->

			<div class="clear"></div>
		</div><!-- #minor-publishing -->

		<div id="major-publishing-actions">
			<div id="publishing-action">
lechuck's avatar
lechuck committed
764
				<?php submit_button( __( 'Update', 'buddypress' ), 'primary', 'save', false ); ?>
lechuck's avatar
lechuck committed
765
766
767
768
769
770
771
772
773
774
			</div>
			<div class="clear"></div>
		</div><!-- #major-publishing-actions -->

	</div><!-- #submitcomment -->

<?php
}

/**
lechuck's avatar
lechuck committed
775
 * Primary link metabox for the Activity admin edit screen.
lechuck's avatar
lechuck committed
776
 *
lechuck's avatar
lechuck committed
777
 * @since 1.6.0
lechuck's avatar
lechuck committed
778
779
 *
 * @param object $item Activity item.
lechuck's avatar
lechuck committed
780
781
782
783
784
 */
function bp_activity_admin_edit_metabox_link( $item ) {
?>

	<label class="screen-reader-text" for="bp-activities-link"><?php _e( 'Link', 'buddypress' ); ?></label>
lechuck's avatar
lechuck committed
785
786
	<input type="url" name="bp-activities-link" id="bp-activities-link" value="<?php echo esc_url( $item->primary_link ); ?>" aria-describedby="bp-activities-link-description" />
	<p id="bp-activities-link-description"><?php _e( 'Activity generated by posts and comments, forum topics and replies, and some plugins, uses the link field for a permalink back to the content item.', 'buddypress' ); ?></p>
lechuck's avatar
lechuck committed
787
788
789
790
791

<?php
}

/**
lechuck's avatar
lechuck committed
792
 * User ID metabox for the Activity admin edit screen.
lechuck's avatar
lechuck committed
793
 *
lechuck's avatar
lechuck committed
794
 * @since 1.6.0
lechuck's avatar
lechuck committed
795
796
 *
 * @param object $item Activity item.
lechuck's avatar
lechuck committed
797
798
799
800
801
 */
function bp_activity_admin_edit_metabox_userid( $item ) {
?>

	<label class="screen-reader-text" for="bp-activities-userid"><?php _e( 'Author ID', 'buddypress' ); ?></label>
lechuck's avatar
lechuck committed
802
	<input type="number" name="bp-activities-userid" id="bp-activities-userid" value="<?php echo esc_attr( $item->user_id ); ?>" min="1" />
lechuck's avatar
lechuck committed
803
804
805
806

<?php
}

lechuck's avatar
lechuck committed
807
808
809
810
811
/**
 * Get flattened array of all registered activity actions.
 *
 * Format is [activity_type] => Pretty name for activity type.
 *
lechuck's avatar
lechuck committed
812
 * @since 2.0.0
lechuck's avatar
lechuck committed
813
 *
lechuck's avatar
lechuck committed
814
 * @return array $actions
lechuck's avatar
lechuck committed
815
816
817
818
819
 */
function bp_activity_admin_get_activity_actions() {
	$actions  = array();

	// Walk through the registered actions, and build an array of actions/values.
ale's avatar
ale committed
820
	foreach ( bp_activity_get_actions() as $action ) {
lechuck's avatar
lechuck committed
821
822
823
824
825
826
827
		$action = array_values( (array) $action );

		for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
			$actions[ $action[$i]['key'] ] = $action[$i]['value'];
		}
	}

lechuck's avatar
lechuck committed
828
	// This was a mis-named activity type from before BP 1.6.
lechuck's avatar
lechuck committed
829
830
	unset( $actions['friends_register_activity_action'] );

lechuck's avatar
lechuck committed
831
	// Sort array by the human-readable value.
lechuck's avatar
lechuck committed
832
833
834
835
836
	natsort( $actions );

	return $actions;
}

lechuck's avatar
lechuck committed
837
/**
ale's avatar
ale committed
838
 * Activity type metabox for the Activity admin edit screen.
lechuck's avatar
lechuck committed
839
 *
lechuck's avatar
lechuck committed
840
 * @since 1.6.0
lechuck's avatar
lechuck committed
841
842
 *
 * @param object $item Activity item.
lechuck's avatar
lechuck committed
843
844
 */
function bp_activity_admin_edit_metabox_type( $item ) {
ale's avatar
ale committed
845
	$bp = buddypress();
lechuck's avatar
lechuck committed
846
847
848
849
850

	$actions  = array();
	$selected = $item->type;

	// Walk through the registered actions, and build an array of actions/values.
ale's avatar
ale committed
851
	foreach ( bp_activity_get_actions() as $action ) {
lechuck's avatar
lechuck committed
852
853
854
855
856
857
		$action = array_values( (array) $action );

		for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ )
			$actions[ $action[$i]['key'] ] = $action[$i]['value'];
	}

lechuck's avatar
lechuck committed
858
	// This was a mis-named activity type from before BP 1.6.
lechuck's avatar
lechuck committed
859
860
	unset( $actions['friends_register_activity_action'] );

lechuck's avatar
lechuck committed
861
	// Sort array by the human-readable value.
lechuck's avatar
lechuck committed
862
863
	natsort( $actions );

lechuck's avatar
lechuck committed
864
865
866
867
868
	/*
	 * If the activity type is not registered properly (eg, a plugin has
	 * not called bp_activity_set_action()), add the raw type to the end
	 * of the list.
	 */
lechuck's avatar
lechuck committed
869
870
871
872
873
874
	if ( ! isset( $actions[ $selected ] ) ) {
		_doing_it_wrong( __FUNCTION__, sprintf( __( 'This activity item has a type (%s) that is not registered using bp_activity_set_action(), so no label is available.', 'buddypress' ), $selected ), '2.0.0' );
		$actions[ $selected ] = $selected;
	}

	?>
lechuck's avatar
lechuck committed
875

lechuck's avatar
lechuck committed
876
877
	<label for="bp-activities-type" class="screen-reader-text"><?php esc_html_e( 'Select activity type', 'buddypress' ); ?></label>
	<select name="bp-activities-type" id="bp-activities-type">
lechuck's avatar
lechuck committed
878
879
880
881
882
883
884
885
886
		<?php foreach ( $actions as $k => $v ) : ?>
			<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $k,  $selected ); ?>><?php echo esc_html( $v ); ?></option>
		<?php endforeach; ?>
	</select>

<?php
}

/**
lechuck's avatar
lechuck committed
887
 * Primary item ID/Secondary item ID metabox for the Activity admin edit screen.
lechuck's avatar
lechuck committed
888
 *
lechuck's avatar
lechuck committed
889
 * @since 1.6.0
lechuck's avatar
lechuck committed
890
891
 *
 * @param object $item Activity item.
lechuck's avatar
lechuck committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
 */
function bp_activity_admin_edit_metabox_itemids( $item ) {
?>

	<label for="bp-activities-primaryid"><?php _e( 'Primary Item ID', 'buddypress' ); ?></label>
	<input type="number" name="bp-activities-primaryid" id="bp-activities-primaryid" value="<?php echo esc_attr( $item->item_id ); ?>" min="0" />
	<br />

	<label for="bp-activities-secondaryid"><?php _e( 'Secondary Item ID', 'buddypress' ); ?></label>
	<input type="number" name="bp-activities-secondaryid" id="bp-activities-secondaryid" value="<?php echo esc_attr( $item->secondary_item_id ); ?>" min="0" />

	<p><?php _e( 'These identify the object that created this activity. For example, the fields could reference a pair of site and comment IDs.', 'buddypress' ); ?></p>

<?php
}

/**
 * Display the Activity admin index screen, which contains a list of all the activities.
 *
lechuck's avatar
lechuck committed
911
 * @since 1.6.0
lechuck's avatar
lechuck committed
912
 *
ale's avatar
ale committed
913
914
 * @global BP_Activity_List_Table $bp_activity_list_table Activity screen list table.
 * @global string                 $plugin_page            The current plugin page.
lechuck's avatar
lechuck committed
915
916
917
918
919
920
 */
function bp_activity_admin_index() {
	global $bp_activity_list_table, $plugin_page;

	$messages = array();

lechuck's avatar
lechuck committed
921
	// If the user has just made a change to an activity item, build status messages.
lechuck's avatar
lechuck committed
922
923
924
925
926
927
928
929
930
	if ( ! empty( $_REQUEST['deleted'] ) || ! empty( $_REQUEST['spammed'] ) || ! empty( $_REQUEST['unspammed'] ) || ! empty( $_REQUEST['error'] ) || ! empty( $_REQUEST['updated'] ) ) {
		$deleted   = ! empty( $_REQUEST['deleted']   ) ? (int) $_REQUEST['deleted']   : 0;
		$errors    = ! empty( $_REQUEST['error']     ) ? $_REQUEST['error']           : '';
		$spammed   = ! empty( $_REQUEST['spammed']   ) ? (int) $_REQUEST['spammed']   : 0;
		$unspammed = ! empty( $_REQUEST['unspammed'] ) ? (int) $_REQUEST['unspammed'] : 0;
		$updated   = ! empty( $_REQUEST['updated']   ) ? (int) $_REQUEST['updated']   : 0;

		$errors = array_map( 'absint', explode( ',', $errors ) );

lechuck's avatar
lechuck committed
931
		// Make sure we don't get any empty values in $errors.
lechuck's avatar
lechuck committed
932
		for ( $i = 0, $errors_count = count( $errors ); $i < $errors_count; $i++ ) {
lechuck's avatar
lechuck committed
933
			if ( 0 === $errors[$i] ) {
lechuck's avatar
lechuck committed
934
				unset( $errors[$i] );
lechuck's avatar
lechuck committed
935
			}
lechuck's avatar
lechuck committed
936
937
		}

lechuck's avatar
lechuck committed
938
		// Reindex array.
lechuck's avatar
lechuck committed
939
940
941
		$errors = array_values( $errors );

		if ( $deleted > 0 )
lechuck's avatar
lechuck committed
942
			$messages[] = sprintf( _n( '%s activity item has been permanently deleted.', '%s activity items have been permanently deleted.', $deleted, 'buddypress' ), number_format_i18n( $deleted ) );
lechuck's avatar
lechuck committed
943
944
945
946
947
948
949
950
951

		if ( ! empty( $errors ) ) {
			if ( 1 == count( $errors ) ) {
				$messages[] = sprintf( __( 'An error occurred when trying to update activity ID #%s.', 'buddypress' ), number_format_i18n( $errors[0] ) );

			} else {
				$error_msg  = __( 'Errors occurred when trying to update these activity items:', 'buddypress' );
				$error_msg .= '<ul class="activity-errors">';

lechuck's avatar
lechuck committed
952
				// Display each error as a list item.
lechuck's avatar
lechuck committed
953
				foreach ( $errors as $error ) {
lechuck's avatar
lechuck committed
954
					// Translators: This is a bulleted list of item IDs.
lechuck's avatar
lechuck committed
955
956
957
958
959
960
961
962
963
					$error_msg .= '<li>' . sprintf( __( '#%s', 'buddypress' ), number_format_i18n( $error ) ) . '</li>';
				}

				$error_msg  .= '</ul>';
				$messages[] = $error_msg;
			}
		}

		if ( $spammed > 0 )
lechuck's avatar
lechuck committed
964
			$messages[] = sprintf( _n( '%s activity item has been successfully spammed.', '%s activity items have been successfully spammed.', $spammed, 'buddypress' ), number_format_i18n( $spammed ) );
lechuck's avatar
lechuck committed
965
966

		if ( $unspammed > 0 )
lechuck's avatar
lechuck committed
967
			$messages[] = sprintf( _n( '%s activity item has been successfully unspammed.', '%s activity items have been successfully unspammed.', $unspammed, 'buddypress' ), number_format_i18n( $unspammed ) );
lechuck's avatar
lechuck committed
968
969

		if ( $updated > 0 )
ale's avatar
ale committed
970
			$messages[] = __( 'The activity item has been updated successfully.', 'buddypress' );
lechuck's avatar
lechuck committed
971
972
	}

lechuck's avatar
lechuck committed
973
	// Prepare the activity items for display.
lechuck's avatar
lechuck committed
974
975
	$bp_activity_list_table->prepare_items();

ale's avatar
ale committed
976
977
978
	/**
	 * Fires before edit form is displayed so plugins can modify the activity messages.
	 *
lechuck's avatar
lechuck committed
979
	 * @since 1.6.0
ale's avatar
ale committed
980
981
982
	 *
	 * @param array $messages Array of messages to display at top of page.
	 */
lechuck's avatar
lechuck committed
983
	do_action( 'bp_activity_admin_index', $messages ); ?>
lechuck's avatar
lechuck committed
984
985

	<div class="wrap">
lechuck's avatar
lechuck committed
986
		<h1>
lechuck's avatar
lechuck committed
987
988
989
			<?php if ( !empty( $_REQUEST['aid'] ) ) : ?>
				<?php printf( __( 'Activity related to ID #%s', 'buddypress' ), number_format_i18n( (int) $_REQUEST['aid'] ) ); ?>
			<?php else : ?>
lucha's avatar
lucha committed
990
				<?php _ex( 'Activity', 'Admin SWA page', 'buddypress' ); ?>
lechuck's avatar
lechuck committed
991
992
993
994
995
			<?php endif; ?>

			<?php if ( !empty( $_REQUEST['s'] ) ) : ?>
				<span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
			<?php endif; ?>
lechuck's avatar
lechuck committed
996
		</h1>
lechuck's avatar
lechuck committed
997

lechuck's avatar
lechuck committed
998
		<?php // If the user has just made a change to an activity item, display the status messages. ?>
lechuck's avatar
lechuck committed
999
1000
		<?php if ( !empty( $messages ) ) : ?>
			<div id="moderated" class="<?php echo ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
For faster browsing, not all history is shown. View entire blame