BP_XProfile_User_Admin

Load xProfile Profile admin area.

Defined (1)

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

/bp-xprofile/classes/class-bp-xprofile-user-admin.php  
  1. class BP_XProfile_User_Admin { 
  2.  
  3. /** 
  4. * Setup xProfile User Admin. 
  5. * @since 2.0.0 
  6. */ 
  7. public static function register_xprofile_user_admin() { 
  8.  
  9. // Bail if not in admin. 
  10. if ( ! is_admin() ) { 
  11. return; 
  12.  
  13. $bp = buddypress(); 
  14.  
  15. if ( empty( $bp->profile->admin ) ) { 
  16. $bp->profile->admin = new self; 
  17.  
  18. return $bp->profile->admin; 
  19.  
  20. /** 
  21. * Constructor method. 
  22. * @since 2.0.0 
  23. */ 
  24. public function __construct() { 
  25. $this->setup_actions(); 
  26.  
  27. /** 
  28. * Set admin-related actions and filters. 
  29. * @since 2.0.0 
  30. */ 
  31. private function setup_actions() { 
  32. // Enqueue scripts. 
  33. add_action( 'bp_members_admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 1 ); 
  34.  
  35. // Register the metabox in Member's community admin profile. 
  36. add_action( 'bp_members_admin_xprofile_metabox', array( $this, 'register_metaboxes' ), 10, 3 ); 
  37.  
  38. // Saves the profile actions for user ( avatar, profile fields ). 
  39. add_action( 'bp_members_admin_update_user', array( $this, 'user_admin_load' ), 10, 4 ); 
  40.  
  41. /** 
  42. * Enqueue needed scripts. 
  43. * @since 2.3.0 
  44. * @param int $screen_id Screen ID being displayed. 
  45. */ 
  46. public function enqueue_scripts( $screen_id ) { 
  47. if ( ( false === strpos( $screen_id, 'users_page_bp-profile-edit' ) 
  48. && false === strpos( $screen_id, 'profile_page_bp-profile-edit' ) ) 
  49. || bp_core_get_root_option( 'bp-disable-avatar-uploads' ) 
  50. || ! buddypress()->avatar->show_avatars 
  51. || ! bp_attachments_is_wp_version_supported() ) { 
  52. return; 
  53.  
  54. /** 
  55. * Get Thickbox. 
  56. * We cannot simply use add_thickbox() here as WordPress is not playing 
  57. * nice with Thickbox width/height see https://core.trac.wordpress.org/ticket/17249 
  58. * Using media-upload might be interesting in the future for the send to editor stuff 
  59. * and we make sure the tb_window is wide enougth 
  60. */ 
  61. wp_enqueue_style ( 'thickbox' ); 
  62. wp_enqueue_script( 'media-upload' ); 
  63.  
  64. // Get Avatar Uploader. 
  65. bp_attachments_enqueue_scripts( 'BP_Attachment_Avatar' ); 
  66.  
  67. /** 
  68. * Register the xProfile metabox on Community Profile admin page. 
  69. * @since 2.0.0 
  70. * @param int $user_id ID of the user being edited. 
  71. * @param string $screen_id Screen ID to load the metabox in. 
  72. * @param object|null $stats_metabox Context and priority for the stats metabox. 
  73. */ 
  74. public function register_metaboxes( $user_id = 0, $screen_id = '', $stats_metabox = null ) { 
  75.  
  76. // Set the screen ID if none was passed. 
  77. if ( empty( $screen_id ) ) { 
  78. $screen_id = buddypress()->members->admin->user_page; 
  79.  
  80. // Setup a new metabox class if none was passed. 
  81. if ( empty( $stats_metabox ) ) { 
  82. $stats_metabox = new StdClass(); 
  83.  
  84. // Moving the Stats Metabox. 
  85. $stats_metabox->context = 'side'; 
  86. $stats_metabox->priority = 'low'; 
  87.  
  88. // Each Group of fields will have his own metabox. 
  89. $profile_args = array( 
  90. 'fetch_fields' => false,  
  91. 'user_id' => $user_id,  
  92. ); 
  93.  
  94. if ( ! bp_is_user_spammer( $user_id ) && bp_has_profile( $profile_args ) ) { 
  95.  
  96. // Loop through field groups and add a metabox for each one. 
  97. while ( bp_profile_groups() ) : bp_the_profile_group(); 
  98. add_meta_box( 
  99. 'bp_xprofile_user_admin_fields_' . sanitize_key( bp_get_the_profile_group_slug() ),  
  100. esc_html( bp_get_the_profile_group_name() ),  
  101. array( $this, 'user_admin_profile_metaboxes' ),  
  102. $screen_id,  
  103. 'normal',  
  104. 'core',  
  105. array( 'profile_group_id' => bp_get_the_profile_group_id() ) 
  106. ); 
  107. endwhile; 
  108.  
  109.  
  110. } else { 
  111. // If member is already a spammer, show a generic metabox. 
  112. add_meta_box( 
  113. 'bp_xprofile_user_admin_empty_profile',  
  114. _x( 'User marked as a spammer', 'xprofile user-admin edit screen', 'buddypress' ),  
  115. array( $this, 'user_admin_spammer_metabox' ),  
  116. $screen_id,  
  117. 'normal',  
  118. 'core' 
  119. ); 
  120.  
  121. if ( buddypress()->avatar->show_avatars ) { 
  122. // Avatar Metabox. 
  123. add_meta_box( 
  124. 'bp_xprofile_user_admin_avatar',  
  125. _x( 'Profile Photo', 'xprofile user-admin edit screen', 'buddypress' ),  
  126. array( $this, 'user_admin_avatar_metabox' ),  
  127. $screen_id,  
  128. 'side',  
  129. 'low' 
  130. ); 
  131.  
  132. /** 
  133. * Save the profile fields in Members community profile page. 
  134. * Loaded before the page is rendered, this function is processing form 
  135. * requests. 
  136. * @since 2.0.0 
  137. * @param string $doaction Action being run. 
  138. * @param int $user_id ID for the user whose profile is being saved. 
  139. * @param array $request Request being made. 
  140. * @param string $redirect_to Where to redirect user to. 
  141. */ 
  142. public function user_admin_load( $doaction = '', $user_id = 0, $request = array(), $redirect_to = '' ) { 
  143.  
  144. // Eventually delete avatar. 
  145. if ( 'delete_avatar' === $doaction ) { 
  146.  
  147. check_admin_referer( 'delete_avatar' ); 
  148.  
  149. $redirect_to = remove_query_arg( '_wpnonce', $redirect_to ); 
  150.  
  151. if ( bp_core_delete_existing_avatar( array( 'item_id' => $user_id ) ) ) { 
  152. $redirect_to = add_query_arg( 'updated', 'avatar', $redirect_to ); 
  153. } else { 
  154. $redirect_to = add_query_arg( 'error', 'avatar', $redirect_to ); 
  155.  
  156. bp_core_redirect( $redirect_to ); 
  157.  
  158. } elseif ( isset( $_POST['field_ids'] ) ) { 
  159. // Update profile fields. 
  160. // Check the nonce. 
  161. check_admin_referer( 'edit-bp-profile_' . $user_id ); 
  162.  
  163. // Check we have field ID's. 
  164. if ( empty( $_POST['field_ids'] ) ) { 
  165. $redirect_to = add_query_arg( 'error', '1', $redirect_to ); 
  166. bp_core_redirect( $redirect_to ); 
  167.  
  168. /** 
  169. * Unlike front-end edit-fields screens, the wp-admin/profile 
  170. * displays all groups of fields on a single page, so the list of 
  171. * field ids is an array gathering for each group of fields a 
  172. * distinct comma separated list of ids. 
  173. * As a result, before using the wp_parse_id_list() function, we 
  174. * must ensure that these ids are "merged" into a single comma 
  175. * separated list. 
  176. */ 
  177. $merge_ids = join( ', ', $_POST['field_ids'] ); 
  178.  
  179. // Explode the posted field IDs into an array so we know which fields have been submitted. 
  180. $posted_field_ids = wp_parse_id_list( $merge_ids ); 
  181. $is_required = array(); 
  182.  
  183. // Loop through the posted fields formatting any datebox values then validate the field. 
  184. foreach ( (array) $posted_field_ids as $field_id ) { 
  185. bp_xprofile_maybe_format_datebox_post_data( $field_id ); 
  186.  
  187. $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' ); 
  188. if ( $is_required[ $field_id ] && empty( $_POST['field_' . $field_id ] ) ) { 
  189. $redirect_to = add_query_arg( 'error', '2', $redirect_to ); 
  190. bp_core_redirect( $redirect_to ); 
  191.  
  192. // Set the errors var. 
  193. $errors = false; 
  194.  
  195. // Now we've checked for required fields, let's save the values. 
  196. $old_values = $new_values = array(); 
  197. foreach ( (array) $posted_field_ids as $field_id ) { 
  198.  
  199. /** 
  200. * Certain types of fields (checkboxes, multiselects) may come 
  201. * through empty. Save them as an empty array so that they don't 
  202. * get overwritten by the default on the next edit. 
  203. */ 
  204. $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : ''; 
  205.  
  206. $visibility_level = ! empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public'; 
  207. /** 
  208. * Save the old and new values. They will be 
  209. * passed to the filter and used to determine 
  210. * whether an activity item should be posted. 
  211. */ 
  212. $old_values[ $field_id ] = array( 
  213. 'value' => xprofile_get_field_data( $field_id, $user_id ),  
  214. 'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),  
  215. ); 
  216.  
  217. // Update the field data and visibility level. 
  218. xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 
  219. $field_updated = xprofile_set_field_data( $field_id, $user_id, $value, $is_required[ $field_id ] ); 
  220. $value = xprofile_get_field_data( $field_id, $user_id ); 
  221.  
  222. $new_values[ $field_id ] = array( 
  223. 'value' => $value,  
  224. 'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),  
  225. ); 
  226.  
  227. if ( ! $field_updated ) { 
  228. $errors = true; 
  229. } else { 
  230.  
  231. /** 
  232. * Fires after the saving of each profile field, if successful. 
  233. * @since 1.1.0 
  234. * @param int $field_id ID of the field being updated. 
  235. * @param string $value Value that was saved to the field. 
  236. */ 
  237. do_action( 'xprofile_profile_field_data_updated', $field_id, $value ); 
  238.  
  239. /** 
  240. * Fires after all XProfile fields have been saved for the current profile. 
  241. * @since 1.0.0 
  242. * @since 2.6.0 Added $old_values and $new_values parameters. 
  243. * @param int $user_id ID for the user whose profile is being saved. 
  244. * @param array $posted_field_ids Array of field IDs that were edited. 
  245. * @param bool $errors Whether or not any errors occurred. 
  246. * @param array $old_values Array of original values before update. 
  247. * @param array $new_values Array of newly saved values after update. 
  248. */ 
  249. do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, $errors, $old_values, $new_values ); 
  250.  
  251. // Set the feedback messages. 
  252. if ( ! empty( $errors ) ) { 
  253. $redirect_to = add_query_arg( 'error', '3', $redirect_to ); 
  254. } else { 
  255. $redirect_to = add_query_arg( 'updated', '1', $redirect_to ); 
  256.  
  257. bp_core_redirect( $redirect_to ); 
  258.  
  259. /** 
  260. * Render the xprofile metabox for Community Profile screen. 
  261. * @since 2.0.0 
  262. * @param WP_User|null $user The WP_User object for the user being edited. 
  263. * @param array $args Aray of arguments for metaboxes. 
  264. */ 
  265. public function user_admin_profile_metaboxes( $user = null, $args = array() ) { 
  266.  
  267. // Bail if no user ID. 
  268. if ( empty( $user->ID ) ) { 
  269. return; 
  270.  
  271. $r = bp_parse_args( $args['args'], array( 
  272. 'profile_group_id' => 0,  
  273. 'user_id' => $user->ID 
  274. ), 'bp_xprofile_user_admin_profile_loop_args' ); 
  275.  
  276. // We really need these args. 
  277. if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) { 
  278. return; 
  279.  
  280. // Bail if no profile fields are available. 
  281. if ( ! bp_has_profile( $r ) ) { 
  282. return; 
  283.  
  284. // Loop through profile groups & fields. 
  285. while ( bp_profile_groups() ) : bp_the_profile_group(); ?> 
  286.  
  287. <input type="hidden" name="field_ids[]" id="<?php echo esc_attr( 'field_ids_' . bp_get_the_profile_group_slug() ); ?>" value="<?php echo esc_attr( bp_get_the_profile_group_field_ids() ); ?>" /> 
  288.  
  289. <?php if ( bp_get_the_profile_group_description() ) : ?> 
  290.  
  291. <p class="description"><?php bp_the_profile_group_description(); ?></p> 
  292.  
  293. <?php endif; ?> 
  294.  
  295. <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?> 
  296.  
  297. <div<?php bp_field_css_class( 'bp-profile-field' ); ?>> 
  298.  
  299. <?php 
  300.  
  301. $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() ); 
  302. $field_type->edit_field_html( array( 'user_id' => $r['user_id'] ) ); 
  303.  
  304. if ( bp_get_the_profile_field_description() ) : ?> 
  305.  
  306. <p class="description"><?php bp_the_profile_field_description(); ?></p> 
  307.  
  308. <?php endif; 
  309.  
  310. /** 
  311. * Fires before display of visibility form elements for profile metaboxes. 
  312. * @since 1.7.0 
  313. */ 
  314. do_action( 'bp_custom_profile_edit_fields_pre_visibility' ); 
  315.  
  316. $can_change_visibility = bp_current_user_can( 'bp_xprofile_change_field_visibility' ); ?> 
  317.  
  318. <p class="field-visibility-settings-<?php echo $can_change_visibility ? 'toggle' : 'notoggle'; ?>" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id(); ?>"> 
  319.  
  320. <?php 
  321. printf( 
  322. __( 'This field can be seen by: %s', 'buddypress' ),  
  323. '<span class="current-visibility-level">' . bp_get_the_profile_field_visibility_level_label() . '</span>' 
  324. ); 
  325. ?> 
  326.  
  327. <?php if ( $can_change_visibility ) : ?> 
  328.  
  329. <button type="button" class="button visibility-toggle-link"><?php esc_html_e( 'Change', 'buddypress' ); ?></button> 
  330.  
  331. <?php endif; ?> 
  332. </p> 
  333.  
  334. <?php if ( $can_change_visibility ) : ?> 
  335.  
  336. <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>"> 
  337. <fieldset> 
  338. <legend><?php _e( 'Who can see this field?', 'buddypress' ); ?></legend> 
  339.  
  340. <?php bp_profile_visibility_radio_buttons(); ?> 
  341.  
  342. </fieldset> 
  343. <button type="button" class="button field-visibility-settings-close"><?php esc_html_e( 'Close', 'buddypress' ); ?></button> 
  344. </div> 
  345.  
  346. <?php endif; ?> 
  347.  
  348. <?php 
  349.  
  350. /** 
  351. * Fires at end of custom profile field items on your xprofile screen tab. 
  352. * @since 1.1.0 
  353. */ 
  354. do_action( 'bp_custom_profile_edit_fields' ); ?> 
  355.  
  356. </div> 
  357.  
  358. <?php endwhile; // End bp_profile_fields(). ?> 
  359.  
  360. <?php endwhile; // End bp_profile_groups. 
  361.  
  362. /** 
  363. * Render the fallback metabox in case a user has been marked as a spammer. 
  364. * @since 2.0.0 
  365. * @param WP_User|null $user The WP_User object for the user being edited. 
  366. */ 
  367. public function user_admin_spammer_metabox( $user = null ) { 
  368. ?> 
  369. <p><?php printf( __( '%s has been marked as a spammer. All BuddyPress data associated with the user has been removed', 'buddypress' ), esc_html( bp_core_get_user_displayname( $user->ID ) ) ) ;?></p> 
  370. <?php 
  371.  
  372. /** 
  373. * Render the Avatar metabox to moderate inappropriate images. 
  374. * @since 2.0.0 
  375. * @param WP_User|null $user The WP_User object for the user being edited. 
  376. */ 
  377. public function user_admin_avatar_metabox( $user = null ) { 
  378.  
  379. if ( empty( $user->ID ) ) { 
  380. return; 
  381. } ?> 
  382.  
  383. <div class="avatar"> 
  384.  
  385. <?php echo bp_core_fetch_avatar( array( 
  386. 'item_id' => $user->ID,  
  387. 'object' => 'user',  
  388. 'type' => 'full',  
  389. 'title' => $user->display_name 
  390. ) ); ?> 
  391.  
  392. <?php if ( bp_get_user_has_avatar( $user->ID ) ) : 
  393.  
  394. $query_args = array( 
  395. 'user_id' => $user->ID,  
  396. 'action' => 'delete_avatar' 
  397. ); 
  398.  
  399. if ( ! empty( $_REQUEST['wp_http_referer'] ) ) { 
  400. $query_args['wp_http_referer'] = urlencode( wp_unslash( $_REQUEST['wp_http_referer'] ) ); 
  401.  
  402. $community_url = add_query_arg( $query_args, buddypress()->members->admin->edit_profile_url ); 
  403. $delete_link = wp_nonce_url( $community_url, 'delete_avatar' ); ?> 
  404.  
  405. <a href="<?php echo esc_url( $delete_link ); ?>" class="bp-xprofile-avatar-user-admin"><?php esc_html_e( 'Delete Profile Photo', 'buddypress' ); ?></a> 
  406.  
  407. <?php endif; 
  408.  
  409. // Load the Avatar UI templates if user avatar uploads are enabled and current WordPress version is supported. 
  410. if ( ! bp_core_get_root_option( 'bp-disable-avatar-uploads' ) && bp_attachments_is_wp_version_supported() ) : ?> 
  411. <a href="#TB_inline?width=800px&height=400px&inlineId=bp-xprofile-avatar-editor" class="thickbox bp-xprofile-avatar-user-edit"><?php esc_html_e( 'Edit Profile Photo', 'buddypress' ); ?></a> 
  412. <div id="bp-xprofile-avatar-editor" style="display:none;"> 
  413. <?php bp_attachments_get_template_part( 'avatars/index' ); ?> 
  414. </div> 
  415. <?php endif; ?> 
  416.  
  417. </div> 
  418. <?php 
  419.