bp_activity_admin_load

Set up the Activity admin page.

Description

bp_activity_admin_load(); 

Does the following: - Register contextual help and screen options for this admin page. - Enqueues scripts and styles. - Catches POST and GET requests related to Activity.


Usage

  1. if ( !function_exists( 'bp_activity_admin_load' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-admin.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_activity_admin_load(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-admin.php  
  1. function bp_activity_admin_load() { 
  2. global $bp_activity_list_table; 
  3.  
  4. $bp =buddypress); 
  5.  
  6. /** 
  7. * Fires at top of Activity admin page. 
  8. * @since 1.6.0 
  9. * @param string $doaction Current $_GET action being performed in admin screen. 
  10. */ 
  11.  
  12. // Edit screen. 
  13. if ( 'edit' == $doaction && ! empty( $_GET['aid'] ) ) { 
  14. // Columns screen option. 
  15. add_screen_option( 'layout_columns', array( 'default' => 2, 'max' => 2, ) ); 
  16.  
  17. get_current_screen()->add_help_tab( array( 
  18. 'id' => 'bp-activity-edit-overview',  
  19. 'title' => __( 'Overview', buddypress ),  
  20. 'content' => 
  21. '<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>' . 
  22. '<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>' . 
  23. '<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>' 
  24. ) ); 
  25.  
  26. get_current_screen()->add_help_tab( array( 
  27. 'id' => 'bp-activity-edit-advanced',  
  28. 'title' => __( 'Item, Link, Type', buddypress ),  
  29. 'content' => 
  30. '<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>' . 
  31. '<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>' . 
  32. '<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>' . 
  33. '<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>' 
  34. ) ); 
  35.  
  36. // Help panel - sidebar links. 
  37. get_current_screen()->set_help_sidebar( 
  38. '<p><strong>' . __( 'For more information:', buddypress ) . '</strong></p>' . 
  39. '<p>' . __( '<a href="https://codex.buddypress.org/administrator-guide/activity-stream-management-panels/">Managing Activity</a>', buddypress ) . '</p>' . 
  40. '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', buddypress ) . '</p>' 
  41. ); 
  42.  
  43. // Register metaboxes for the edit screen. 
  44. add_meta_box( 'submitdiv', _x( 'Status', 'activity admin edit screen', buddypress ), 'bp_activity_admin_edit_metabox_status', get_current_screen()->id, 'side', 'core' ); 
  45. 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' ); 
  46. 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' ); 
  47. 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' ); 
  48. 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' ); 
  49.  
  50. /** 
  51. * Fires after the registration of all of the default activity meta boxes. 
  52. * @since 2.4.0 
  53. */ 
  54.  
  55. // Enqueue JavaScript files. 
  56. wp_enqueue_script( 'postbox' ); 
  57. wp_enqueue_script( 'dashboard' ); 
  58. wp_enqueue_script( 'comment' ); 
  59.  
  60. // Index screen. 
  61. } else { 
  62. // Create the Activity screen list table. 
  63. $bp_activity_list_table = new BP_Activity_List_Table(); 
  64.  
  65. // The per_page screen option. 
  66. add_screen_option( 'per_page', array( 'label' => _x( 'Activity', 'Activity items per page (screen options)', buddypress )) ); 
  67.  
  68. // Help panel - overview text. 
  69. get_current_screen()->add_help_tab( array( 
  70. 'id' => 'bp-activity-overview',  
  71. 'title' => __( 'Overview', buddypress ),  
  72. 'content' => 
  73. '<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>' . 
  74. '<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>' 
  75. ) ); 
  76.  
  77. // Help panel - moderation text. 
  78. get_current_screen()->add_help_tab( array( 
  79. 'id' => 'bp-activity-moderating',  
  80. 'title' => __( 'Moderating Activity', buddypress ),  
  81. 'content' => 
  82. '<p>' . __( 'In the <strong>Activity</strong> column, above each activity it says “Submitted on, ” 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>' . 
  83. '<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>' 
  84. ) ); 
  85.  
  86. // Help panel - sidebar links. 
  87. get_current_screen()->set_help_sidebar( 
  88. '<p><strong>' . __( 'For more information:', buddypress ) . '</strong></p>' . 
  89. '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', buddypress ) . '</p>' 
  90. ); 
  91.  
  92. // Add accessible hidden heading and text for Activity screen pagination. 
  93. if ( bp_get_major_wp_version() >= 4.4 ) { 
  94. get_current_screen()->set_screen_reader_content( array( 
  95. /** translators: accessibility text */ 
  96. 'heading_pagination' => __( 'Activity list navigation', buddypress ),  
  97. ) ); 
  98.  
  99. // Enqueue CSS and JavaScript. 
  100. 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 ); 
  101. wp_localize_script( 'bp_activity_admin_js', 'bp_activity_admin_vars', array( 
  102. 'page' => get_current_screen()->id 
  103. ) ); 
  104. wp_enqueue_style( 'bp_activity_admin_css', $bp->plugin_url . "bp-activity/admin/css/admin{$min}.css", array(), bp_get_version() ); 
  105.  
  106. wp_style_add_data( 'bp_activity_admin_css', 'rtl', true ); 
  107. if ( $min ) { 
  108. wp_style_add_data( 'bp_activity_admin_css', 'suffix', $min ); 
  109.  
  110. /** 
  111. * Fires after the activity js and style has been enqueued. 
  112. * @since 2.4.0 
  113. */ 
  114.  
  115. // Handle spam/un-spam/delete of activities. 
  116. if ( !empty( $doaction ) && ! in_array( $doaction, array( '-1', 'edit', 'save', ) ) ) { 
  117.  
  118. // Build redirection URL. 
  119. $redirect_to = remove_query_arg( array( 'aid', 'deleted', error, 'spammed', 'unspammed', ), wp_get_referer() ); 
  120. $redirect_to = add_query_arg( 'paged', $bp_activity_list_table->get_pagenum(), $redirect_to ); 
  121.  
  122. // Get activity IDs. 
  123. $activity_ids = array_map( 'absint', (array) $_REQUEST['aid'] ); 
  124.  
  125. /** 
  126. * Filters list of IDs being spammed/un-spammed/deleted. 
  127. * @since 1.6.0 
  128. * @param array $activity_ids Activity IDs to spam/un-spam/delete. 
  129. */ 
  130. $activity_ids = apply_filters( 'bp_activity_admin_action_activity_ids', $activity_ids ); 
  131.  
  132. // Is this a bulk request? 
  133. if ( 'bulk_' == substr( $doaction, 0, 5 ) && ! empty( $_REQUEST['aid'] ) ) { 
  134. // Check this is a valid form submission. 
  135. check_admin_referer( 'bulk-activities' ); 
  136.  
  137. // Trim 'bulk_' off the action name to avoid duplicating a ton of code. 
  138. $doaction = substr( $doaction, 5 ); 
  139.  
  140. // This is a request to delete, spam, or un-spam, a single item. 
  141. } elseif ( !empty( $_REQUEST['aid'] ) ) { 
  142.  
  143. // Check this is a valid form submission. 
  144. check_admin_referer( 'spam-activity_' . $activity_ids[0] ); 
  145.  
  146. // Initialise counters for how many of each type of item we perform an action on. 
  147. $deleted = $spammed = $unspammed = 0; 
  148.  
  149. // Store any errors that occurs when updating the database items. 
  150. $errors = array(); 
  151.  
  152. // "We'd like to shoot the monster, could you move, please?" 
  153. foreach ( $activity_ids as $activity_id ) { 
  154. // @todo: Check the permissions on each 
  155. // if ( ! current_user_can( 'bp_edit_activity', $activity_id ) ) 
  156. // continue; 
  157. // Get the activity from the database. 
  158. $activity = new BP_Activity_Activity( $activity_id ); 
  159. if ( empty( $activity->component ) ) { 
  160. $errors[] = $activity_id; 
  161. continue; 
  162.  
  163. switch ( $doaction ) { 
  164. case 'delete' : 
  165. if ( 'activity_comment' == $activity->type ) 
  166. bp_activity_delete_comment( $activity->item_id, $activity->id ); 
  167. else 
  168. bp_activity_delete( array( 'id' => $activity->id ) ); 
  169.  
  170. $deleted++; 
  171. break; 
  172.  
  173. case 'ham' : 
  174. /** 
  175. * Remove moderation and blacklist checks in case we want to ham an activity 
  176. * which contains one of these listed keys. 
  177. */ 
  178.  
  179. bp_activity_mark_as_ham( $activity ); 
  180. $result = $activity->save(); 
  181.  
  182. // Check for anyerrorduring activity save. 
  183. if ( ! $result ) 
  184. $errors[] = $activity->id; 
  185. else 
  186. $unspammed++; 
  187. break; 
  188.  
  189. case 'spam' : 
  190. bp_activity_mark_as_spam( $activity ); 
  191. $result = $activity->save(); 
  192.  
  193. // Check for anyerrorduring activity save. 
  194. if ( ! $result ) 
  195. $errors[] = $activity->id; 
  196. else 
  197. $spammed++; 
  198. break; 
  199.  
  200. default: 
  201. break; 
  202.  
  203. // Release memory. 
  204. unset( $activity ); 
  205.  
  206. /** 
  207. * Fires before redirect for plugins to do something with activity. 
  208. * Passes an activity array counts how many were spam, not spam, deleted, and IDs that were errors. 
  209. * @since 1.6.0 
  210. * @param array $value Array holding spam, not spam, deleted counts,errorIDs. 
  211. * @param string $redirect_to URL to redirect to. 
  212. * @param array $activity_ids Original array of activity IDs. 
  213. */ 
  214. do_action( 'bp_activity_admin_action_after', array( $spammed, $unspammed, $deleted, $errors ), $redirect_to, $activity_ids ); 
  215.  
  216. // Add arguments to the redirect URL so that on page reload, we can easily display what we've just done. 
  217. if ( $spammed ) 
  218. $redirect_to = add_query_arg( 'spammed', $spammed, $redirect_to ); 
  219.  
  220. if ( $unspammed ) 
  221. $redirect_to = add_query_arg( 'unspammed', $unspammed, $redirect_to ); 
  222.  
  223. if ( $deleted ) 
  224. $redirect_to = add_query_arg( 'deleted', $deleted, $redirect_to ); 
  225.  
  226. // If anerroroccurred, pass back the activity ID that failed. 
  227. if ( ! empty( $errors ) ) 
  228. $redirect_to = add_query_arg( error, implode ( ', ', array_map( 'absint', $errors ) ), $redirect_to ); 
  229.  
  230. /** 
  231. * Filters redirect URL after activity spamming/un-spamming/deletion occurs. 
  232. * @since 1.6.0 
  233. * @param string $redirect_to URL to redirect to. 
  234. */ 
  235. exit; 
  236.  
  237.  
  238. // Save the edit. 
  239. } elseif ( $doaction && 'save' == $doaction ) { 
  240. // Build redirection URL. 
  241. $redirect_to = remove_query_arg( array( 'action', 'aid', 'deleted', error, 'spammed', 'unspammed', ), $_SERVER['REQUEST_URI'] ); 
  242.  
  243. // Get activity ID. 
  244. $activity_id = (int) $_REQUEST['aid']; 
  245.  
  246. // Check this is a valid form submission. 
  247. check_admin_referer( 'edit-activity_' . $activity_id ); 
  248.  
  249. // Get the activity from the database. 
  250. $activity = new BP_Activity_Activity( $activity_id ); 
  251.  
  252. // If the activity doesn't exist, just redirect back to the index. 
  253. if ( empty( $activity->component ) ) { 
  254. wp_redirect( $redirect_to ); 
  255. exit; 
  256.  
  257. // Check the form for the updated properties. 
  258. // Store anyerrorthat occurs when updating the database item. 
  259. $error = 0; 
  260.  
  261. // Activity spam status. 
  262. $prev_spam_status = $new_spam_status = false; 
  263. if ( ! empty( $_POST['activity_status'] ) ) { 
  264. $prev_spam_status = $activity->is_spam; 
  265. $new_spam_status = ( 'spam' == $_POST['activity_status'] ) ? true : false; 
  266.  
  267. // Activity action. 
  268. if ( isset( $_POST['bp-activities-action'] ) ) 
  269. $activity->action = $_POST['bp-activities-action']; 
  270.  
  271. // Activity content. 
  272. if ( isset( $_POST['bp-activities-content'] ) ) 
  273. $activity->content = $_POST['bp-activities-content']; 
  274.  
  275. // Activity primary link. 
  276. if ( ! empty( $_POST['bp-activities-link'] ) ) 
  277. $activity->primary_link = $_POST['bp-activities-link']; 
  278.  
  279. // Activity user ID. 
  280. if ( ! empty( $_POST['bp-activities-userid'] ) ) 
  281. $activity->user_id = (int) $_POST['bp-activities-userid']; 
  282.  
  283. // Activity item primary ID. 
  284. if ( isset( $_POST['bp-activities-primaryid'] ) ) 
  285. $activity->item_id = (int) $_POST['bp-activities-primaryid']; 
  286.  
  287. // Activity item secondary ID. 
  288. if ( isset( $_POST['bp-activities-secondaryid'] ) ) 
  289. $activity->secondary_item_id = (int) $_POST['bp-activities-secondaryid']; 
  290.  
  291. // Activity type. 
  292. if ( ! empty( $_POST['bp-activities-type'] ) ) { 
  293.  
  294. // Check that the new type is a registered activity type. 
  295. if ( in_array( $_POST['bp-activities-type'], $actions ) ) { 
  296. $activity->type = $_POST['bp-activities-type']; 
  297.  
  298. // Activity timestamp. 
  299. if ( ! empty( $_POST['aa'] ) && ! empty( $_POST['mm'] ) && ! empty( $_POST['jj'] ) && ! empty( $_POST['hh'] ) && ! empty( $_POST['mn'] ) && ! empty( $_POST['ss'] ) ) { 
  300. $aa = $_POST['aa']; 
  301. $mm = $_POST['mm']; 
  302. $jj = $_POST['jj']; 
  303. $hh = $_POST['hh']; 
  304. $mn = $_POST['mn']; 
  305. $ss = $_POST['ss']; 
  306. $aa = ( $aa <= 0 ) ? date( 'Y' ) : $aa; 
  307. $mm = ( $mm <= 0 ) ? date( 'n' ) : $mm; 
  308. $jj = ( $jj > 31 ) ? 31 : $jj; 
  309. $jj = ( $jj <= 0 ) ? date( 'j' ) : $jj; 
  310. $hh = ( $hh > 23 ) ? $hh -24 : $hh; 
  311. $mn = ( $mn > 59 ) ? $mn -60 : $mn; 
  312. $ss = ( $ss > 59 ) ? $ss -60 : $ss; 
  313.  
  314. // Reconstruct the date into a timestamp. 
  315. $gmt_date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); 
  316.  
  317. $activity->date_recorded = $gmt_date; 
  318.  
  319. // Has the spam status has changed? 
  320. if ( $new_spam_status != $prev_spam_status ) { 
  321. if ( $new_spam_status ) 
  322. bp_activity_mark_as_spam( $activity ); 
  323. else 
  324. bp_activity_mark_as_ham( $activity ); 
  325.  
  326. // Save. 
  327. $result = $activity->save(); 
  328.  
  329. // Clear the activity stream first page cache, in case this activity's timestamp was changed. 
  330. wp_cache_delete( 'bp_activity_sitewide_front', 'bp' ); 
  331.  
  332. // Check for anyerrorduring activity save. 
  333. if ( false === $result ) 
  334. $error = $activity->id; 
  335.  
  336. /** 
  337. * Fires before redirect so plugins can do something first on save action. 
  338. * @since 1.6.0 
  339. * @param array $value Array holding activity object and ID that holds error. 
  340. */ 
  341. do_action_ref_array( 'bp_activity_admin_edit_after', array( &$activity, $error ) ); 
  342.  
  343. // If anerroroccurred, pass back the activity ID that failed. 
  344. if ( $error ) 
  345. $redirect_to = add_query_arg( error, $error, $redirect_to ); 
  346. else 
  347. $redirect_to = add_query_arg( 'updated', $activity->id, $redirect_to ); 
  348.  
  349. /** 
  350. * Filters URL to redirect to after saving. 
  351. * @since 1.6.0 
  352. * @param string $redirect_to URL to redirect to. 
  353. */ 
  354. exit; 
  355.  
  356.  
  357. // If a referrer and a nonce is supplied, but no action, redirect back. 
  358. } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) { 
  359. wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ); 
  360. exit;