members->admin ) ) { $bp->members->admin = new self; } return $bp->members->admin; } /** * Constructor method. * * @since 2.0.0 */ public function __construct() { $this->setup_globals(); $this->setup_actions(); } /** * Set admin-related globals. * * @since 2.0.0 */ private function setup_globals() { $bp = buddypress(); // Paths and URLs $this->admin_dir = trailingslashit( $bp->plugin_dir . 'bp-members/admin' ); // Admin path. $this->admin_url = trailingslashit( $bp->plugin_url . 'bp-members/admin' ); // Admin URL. $this->css_url = trailingslashit( $this->admin_url . 'css' ); // Admin CSS URL. $this->js_url = trailingslashit( $this->admin_url . 'js' ); // Admin CSS URL. // Capability depends on config. $this->capability = bp_core_do_network_admin() ? 'manage_network_users' : 'edit_users'; // The Edit Profile Screen id. $this->user_page = ''; // The Show Profile Screen id. $this->user_profile = is_network_admin() ? 'users' : 'profile'; // The current user id. $this->current_user_id = get_current_user_id(); // The user id being edited. $this->user_id = 0; // Is a member editing their own profile. $this->is_self_profile = false; // The screen ids to load specific css for. $this->screen_id = array(); // The stats metabox default position. $this->stats_metabox = new StdClass(); // BuddyPress edit user's profile args. $this->edit_profile_args = array( 'page' => 'bp-profile-edit' ); $this->edit_profile_url = ''; $this->edit_url = ''; // Data specific to signups. $this->users_page = ''; $this->signups_page = ''; $this->users_url = bp_get_admin_url( 'users.php' ); $this->users_screen = bp_core_do_network_admin() ? 'users-network' : 'users'; // Specific config: BuddyPress is not network activated. $this->subsite_activated = (bool) is_multisite() && ! bp_is_network_activated(); // When BuddyPress is not network activated, only Super Admin can moderate signups. if ( ! empty( $this->subsite_activated ) ) { $this->capability = 'manage_network_users'; } } /** * Set admin-related actions and filters. * * @since 2.0.0 */ private function setup_actions() { /** Extended Profile ************************************************* */ // Enqueue all admin JS and CSS. add_action( 'bp_admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Add some page specific output to the
. add_action( 'bp_admin_head', array( $this, 'admin_head' ), 999 ); // Add menu item to all users menu. add_action( 'admin_menu', array( $this, 'admin_menus' ), 5 ); add_action( 'network_admin_menu', array( $this, 'admin_menus' ), 5 ); add_action( 'user_admin_menu', array( $this, 'user_profile_menu' ), 5 ); // Create the Profile Navigation (Profile/Extended Profile). add_action( 'edit_user_profile', array( $this, 'profile_nav' ), 99, 1 ); add_action( 'show_user_profile', array( $this, 'profile_nav' ), 99, 1 ); // Editing users of a specific site. add_action( "admin_head-site-users.php", array( $this, 'profile_admin_head' ) ); // Add a row action to users listing. if ( bp_core_do_network_admin() ) { add_filter( 'ms_user_row_actions', array( $this, 'row_actions' ), 10, 2 ); add_action( 'admin_init', array( $this, 'add_edit_profile_url_filter' ) ); add_action( 'wp_after_admin_bar_render', array( $this, 'remove_edit_profile_url_filter' ) ); } // Add user row actions for single site. add_filter( 'user_row_actions', array( $this, 'row_actions' ), 10, 2 ); // Process changes to member type. add_action( 'bp_members_admin_load', array( $this, 'process_member_type_update' ) ); /** Signups ********************************************************** */ if ( is_admin() ) { // Filter non multisite user query to remove sign-up users. if ( ! is_multisite() ) { add_action( 'pre_user_query', array( $this, 'remove_signups_from_user_query' ), 10, 1 ); } // Reorganise the views navigation in users.php and signups page. if ( current_user_can( $this->capability ) ) { add_filter( "views_{$this->users_screen}", array( $this, 'signup_filter_view' ), 10, 1 ); add_filter( 'set-screen-option', array( $this, 'signup_screen_options' ), 10, 3 ); } } } /** * Get the user ID. * * Look for $_GET['user_id']. If anything else, force the user ID to the * current user's ID so they aren't left without a user to edit. * * @since 2.1.0 * * @return int */ private function get_user_id() { if ( ! empty( $this->user_id ) ) { return $this->user_id; } $this->user_id = (int) get_current_user_id(); // We'll need a user ID when not on self profile. if ( ! empty( $_GET['user_id'] ) ) { $this->user_id = (int) $_GET['user_id']; } return $this->user_id; } /** * Can the current user edit the one displayed. * * Self profile editing / or bp_moderate check. * This might be replaced by more granular capabilities * in the future. * * @since 2.1.0 * * @param int $user_id ID of the user being checked for edit ability. * * @return bool */ private function member_can_edit( $user_id = 0 ) { $retval = false; // Bail if no user ID was passed. if ( empty( $user_id ) ) { return $retval; } // Member can edit if they are viewing their own profile. if ( $this->current_user_id === $user_id ) { $retval = true; // Trust the 'bp_moderate' capability. } else { $retval = bp_current_user_can( 'bp_moderate' ); } return $retval; } /** * Get admin notice when saving a user or member profile. * * @since 2.1.0 * * @return array */ private function get_user_notice() { // Setup empty notice for return value. $notice = array(); // Updates. if ( ! empty( $_REQUEST['updated'] ) ) { switch ( $_REQUEST['updated'] ) { case 'avatar': $notice = array( 'class' => 'updated', 'message' => __( 'Profile photo was deleted.', 'buddypress' ) ); break; case 'ham' : $notice = array( 'class' => 'updated', 'message' => __( 'User removed as spammer.', 'buddypress' ) ); break; case 'spam' : $notice = array( 'class' => 'updated', 'message' => __( 'User marked as spammer. Spam users are visible only to site admins.', 'buddypress' ) ); break; case 1 : $notice = array( 'class' => 'updated', 'message' => __( 'Profile updated.', 'buddypress' ) ); break; } } // Errors. if ( ! empty( $_REQUEST['error'] ) ) { switch ( $_REQUEST['error'] ) { case 'avatar': $notice = array( 'class' => 'error', 'message' => __( 'There was a problem deleting that profile photo. Please try again.', 'buddypress' ) ); break; case 'ham' : $notice = array( 'class' => 'error', 'message' => __( 'User could not be removed as spammer.', 'buddypress' ) ); break; case 'spam' : $notice = array( 'class' => 'error', 'message' => __( 'User could not be marked as spammer.', 'buddypress' ) ); break; case 1 : $notice = array( 'class' => 'error', 'message' => __( 'An error occurred while trying to update the profile.', 'buddypress' ) ); break; case 2: $notice = array( 'class' => 'error', 'message' => __( 'Please make sure you fill in all required fields in this profile field group before saving.', 'buddypress' ) ); break; case 3: $notice = array( 'class' => 'error', 'message' => __( 'There was a problem updating some of your profile information. Please try again.', 'buddypress' ) ); break; } } return $notice; } /** * Create the /user/ admin Profile submenus for all members. * * @since 2.1.0 * * @uses add_submenu_page() To add the Edit Profile page in Profile section. */ public function user_profile_menu() { // Setup the hooks array. $hooks = array(); // Add the faux "Edit Profile" submenu page. $hooks['user'] = $this->user_page = add_submenu_page( 'profile.php', __( 'Edit Profile', 'buddypress' ), __( 'Edit Profile', 'buddypress' ), 'exist', 'bp-profile-edit', array( $this, 'user_admin' ) ); // Setup the screen ID's. $this->screen_id = array( $this->user_page . '-user', $this->user_profile . '-user' ); // Loop through new hooks and add method actions. foreach ( $hooks as $key => $hook ) { add_action( "load-{$hook}", array( $this, $key . '_admin_load' ) ); } // Add the profile_admin_head method to proper admin_head actions. add_action( "admin_head-{$this->user_page}", array( $this, 'profile_admin_head' ) ); add_action( "admin_head-profile.php", array( $this, 'profile_admin_head' ) ); } /** * Create the All Users / Profile > Edit Profile and All Users Signups submenus. * * @since 2.0.0 * * @uses add_submenu_page() To add the Edit Profile page in Users/Profile section. */ public function admin_menus() { // Setup the hooks array. $hooks = array(); // Manage user's profile. $hooks['user'] = $this->user_page = add_submenu_page( $this->user_profile . '.php', __( 'Edit Profile', 'buddypress' ), __( 'Edit Profile', 'buddypress' ), 'read', 'bp-profile-edit', array( $this, 'user_admin' ) ); // Only show sign-ups where they belong. if ( ! is_multisite() || is_network_admin() ) { // Manage signups. $hooks['signups'] = $this->signups_page = add_users_page( __( 'Manage Signups', 'buddypress' ), __( 'Manage Signups', 'buddypress' ), $this->capability, 'bp-signups', array( $this, 'signups_admin' ) ); } $edit_page = 'user-edit'; $profile_page = 'profile'; $this->users_page = 'users'; // Self profile check is needed for this pages. $page_head = array( $edit_page . '.php', $profile_page . '.php', $this->user_page, $this->users_page . '.php', ); // Append '-network' to each array item if in network admin. if ( is_network_admin() ) { $edit_page .= '-network'; $profile_page .= '-network'; $this->user_page .= '-network'; $this->users_page .= '-network'; $this->signups_page .= '-network'; } // Setup the screen ID's. $this->screen_id = array( $edit_page, $this->user_page, $profile_page ); // Loop through new hooks and add method actions. foreach ( $hooks as $key => $hook ) { add_action( "load-{$hook}", array( $this, $key . '_admin_load' ) ); } // Add the profile_admin_head method to proper admin_head actions. foreach ( $page_head as $head ) { add_action( "admin_head-{$head}", array( $this, 'profile_admin_head' ) ); } } /** * Highlight the Users menu if on Edit Profile and check if on the user's admin profile. * * @since 2.1.0 */ public function profile_admin_head() { global $submenu_file, $parent_file; // Is the user editing their own profile? if ( is_user_admin() || ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) ) { $this->is_self_profile = true; // Is the user attempting to edit their own profile. } elseif ( isset( $_GET['user_id' ] ) || ( isset( $_GET['page'] ) && ( 'bp-profile-edit' === $_GET['page'] ) ) ) { $this->is_self_profile = (bool) ( $this->get_user_id() === $this->current_user_id ); } // Force the parent file to users.php to open the correct top level menu // but only if not editing a site via the network site editing page. if ( 'sites.php' !== $parent_file ) { $parent_file = 'users.php'; $submenu_file = 'users.php'; } // Editing your own profile, so recheck some vars. if ( true === $this->is_self_profile ) { // Use profile.php as the edit page. $edit_page = 'profile.php'; // Set profile.php as the parent & sub files to correct the menu nav. if ( is_blog_admin() || is_user_admin() ) { $parent_file = 'profile.php'; $submenu_file = 'profile.php'; } // Not editing yourself, so use user-edit.php. } else { $edit_page = 'user-edit.php'; } if ( is_user_admin() ) { $this->edit_profile_url = add_query_arg( $this->edit_profile_args, user_admin_url( 'profile.php' ) ); $this->edit_url = user_admin_url( 'profile.php' ); } elseif ( is_blog_admin() ) { $this->edit_profile_url = add_query_arg( $this->edit_profile_args, admin_url( 'users.php' ) ); $this->edit_url = admin_url( $edit_page ); } elseif ( is_network_admin() ) { $this->edit_profile_url = add_query_arg( $this->edit_profile_args, network_admin_url( 'users.php' ) ); $this->edit_url = network_admin_url( $edit_page ); } } /** * Remove the Edit Profile page. * * We add these pages in order to integrate with WP's Users panel, but * we want them to show up as a row action of the WP panel, not as separate * subnav items under the Users menu. * * @since 2.0.0 */ public function admin_head() { remove_submenu_page( 'users.php', 'bp-profile-edit' ); remove_submenu_page( 'profile.php', 'bp-profile-edit' ); } /** Community Profile *****************************************************/ /** * Add some specific styling to the Edit User and Edit User's Profile page. * * @since 2.0.0 */ public function enqueue_scripts() { if ( ! in_array( get_current_screen()->id, $this->screen_id ) ) { return; } $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $css = $this->css_url . "admin{$min}.css"; /** * Filters the CSS URL to enqueue in the Members admin area. * * @since 2.0.0 * * @param string $css URL to the CSS admin file to load. */ $css = apply_filters( 'bp_members_admin_css', $css ); wp_enqueue_style( 'bp-members-css', $css, array(), bp_get_version() ); wp_style_add_data( 'bp-members-css', 'rtl', true ); if ( $min ) { wp_style_add_data( 'bp-members-css', 'suffix', $min ); } // Only load JavaScript for BuddyPress profile. if ( get_current_screen()->id == $this->user_page ) { $js = $this->js_url . "admin{$min}.js"; /** * Filters the JS URL to enqueue in the Members admin area. * * @since 2.0.0 * * @param string $js URL to the JavaScript admin file to load. */ $js = apply_filters( 'bp_members_admin_js', $js ); wp_enqueue_script( 'bp-members-js', $js, array( 'jquery' ), bp_get_version(), true ); } /** * Fires after all of the members JavaScript and CSS are enqueued. * * @since 2.0.0 * * @param string $id ID of the current screen. * @param array $screen_id Array of allowed screens to add scripts and styles to. */ do_action( 'bp_members_admin_enqueue_scripts', get_current_screen()->id, $this->screen_id ); } /** * Create the Profile navigation in Edit User & Edit Profile pages. * * @since 2.0.0 * * @param object|null $user User to create profile navigation for. * @param string $active Which profile to highlight. * @return string */ public function profile_nav( $user = null, $active = 'WordPress' ) { // Bail if no user ID exists here. if ( empty( $user->ID ) ) { return; } // Add the user ID to query arguments when not editing yourself. if ( false === $this->is_self_profile ) { $query_args = array( 'user_id' => $user->ID ); } else { $query_args = array(); } // Conditionally add a referer if it exists in the existing request. if ( ! empty( $_REQUEST['wp_http_referer'] ) ) { $query_args['wp_http_referer'] = urlencode( stripslashes_deep( $_REQUEST['wp_http_referer'] ) ); } // Setup the two distinct "edit" URL's. $community_url = add_query_arg( $query_args, $this->edit_profile_url ); $wordpress_url = add_query_arg( $query_args, $this->edit_url ); $bp_active = false; $wp_active = ' nav-tab-active'; if ( 'BuddyPress' === $active ) { $bp_active = ' nav-tab-active'; $wp_active = false; } ?>' . __( 'This is the admin view of a user's profile.', 'buddypress' ) . '
' . '' . __( 'In the main column, you can edit the fields of the user's extended profile.', 'buddypress' ) . '
' . '' . __( 'In the right-hand column, you can update the user's status, delete the user's avatar, and view recent statistics.', 'buddypress' ) . '
' ) ); // Help panel - sidebar links. get_current_screen()->set_help_sidebar( '' . __( 'For more information:', 'buddypress' ) . '
' . '' . __( 'Managing Profiles', 'buddypress' ) . '
' . '' . __( 'Support Forums', 'buddypress' ) . '
' ); // Register metaboxes for the edit screen. add_meta_box( 'submitdiv', _x( 'Status', 'members user-admin edit screen', 'buddypress' ), array( $this, 'user_admin_status_metabox' ), get_current_screen()->id, 'side', 'core' ); // In case xprofile is not active. $this->stats_metabox->context = 'normal'; $this->stats_metabox->priority = 'core'; /** * Fires before loading the profile fields if component is active. * * Plugins should not use this hook, please use 'bp_members_admin_user_metaboxes' instead. * * @since 2.0.0 * * @param int $user_id Current user ID for the screen. * @param string $id Current screen ID. * @param object $stats_metabox Object holding position data for use with the stats metabox. */ do_action_ref_array( 'bp_members_admin_xprofile_metabox', array( $user_id, get_current_screen()->id, $this->stats_metabox ) ); // If xProfile is inactive, difficult to know what's profile we're on. if ( 'normal' === $this->stats_metabox->context ) { $display_name = bp_core_get_user_displayname( $user_id ); } else { $display_name = __( 'Member', 'buddypress' ); } // User Stat metabox. add_meta_box( 'bp_members_admin_user_stats', sprintf( _x( "%s's Stats", 'members user-admin edit screen', 'buddypress' ), $display_name ), array( $this, 'user_admin_stats_metabox' ), get_current_screen()->id, sanitize_key( $this->stats_metabox->context ), sanitize_key( $this->stats_metabox->priority ) ); // Member Type metabox. Only added if member types have been registered. $member_types = bp_get_member_types(); if ( ! empty( $member_types ) ) { add_meta_box( 'bp_members_admin_member_type', _x( 'Member Type', 'members user-admin edit screen', 'buddypress' ), array( $this, 'user_admin_member_type_metabox' ), get_current_screen()->id, 'side', 'core' ); } /** * Fires at the end of the Community Profile screen. * * Plugins can restrict metabox to "bp_moderate" admins by checking if * the first argument ($this->is_self_profile) is false in their callback. * They can also restrict their metabox to self profile editing * by setting it to true. * * @since 2.0.0 * * @param bool $is_self_profile Whether or not it is the current user's profile. * @param int $user_id Current user ID. */ do_action( 'bp_members_admin_user_metaboxes', $this->is_self_profile, $user_id ); // Enqueue JavaScript files. wp_enqueue_script( 'postbox' ); wp_enqueue_script( 'dashboard' ); // Spam or Ham user. } elseif ( in_array( $doaction, array( 'spam', 'ham' ) ) && empty( $this->is_self_profile ) ) { check_admin_referer( 'edit-bp-profile_' . $user_id ); if ( bp_core_process_spammer_status( $user_id, $doaction ) ) { $redirect_to = add_query_arg( 'updated', $doaction, $redirect_to ); } else { $redirect_to = add_query_arg( 'error', $doaction, $redirect_to ); } bp_core_redirect( $redirect_to ); // Update other stuff once above ones are done. } else { $this->redirect = $redirect_to; /** * Fires at end of user profile admin load if doaction does not match any available actions. * * @since 2.0.0 * * @param string $doaction Current bulk action being processed. * @param int $user_id Current user ID. * @param array $_REQUEST Current $_REQUEST global. * @param string $redirect Determined redirect url to send user to. */ do_action_ref_array( 'bp_members_admin_update_user', array( $doaction, $user_id, $_REQUEST, $this->redirect ) ); bp_core_redirect( $this->redirect ); } } /** * Display the user's profile. * * @since 2.0.0 */ public function user_admin() { if ( ! bp_current_user_can( 'bp_moderate' ) && empty( $this->is_self_profile ) ) { die( '-1' ); } // Get the user ID. $user_id = $this->get_user_id(); $user = get_user_to_edit( $user_id ); // Construct title. if ( true === $this->is_self_profile ) { $title = __( 'Profile', 'buddypress' ); } else { $title = __( 'Edit User', 'buddypress' ); } // Construct URL for form. $request_url = remove_query_arg( array( 'action', 'error', 'updated', 'spam', 'ham' ), $_SERVER['REQUEST_URI'] ); $form_action_url = add_query_arg( 'action', 'update', $request_url ); $wp_http_referer = false; if ( ! empty( $_REQUEST['wp_http_referer'] ) ) { $wp_http_referer = remove_query_arg( array( 'action', 'updated' ), $_REQUEST['wp_http_referer'] ); } // Prepare notice for admin. $notice = $this->get_user_notice(); if ( ! empty( $notice ) ) : ?>Go back and try again.', 'buddypress' ), esc_url( bp_get_admin_url( 'users.php' ) ) ); ?>
ID ) ) ) ;?>
ID ) ) { return; } // If account is not activated last activity is the time user registered. if ( isset( $user->user_status ) && 2 == $user->user_status ) { $last_active = $user->user_registered; // Account is activated, getting user's last activity. } else { $last_active = bp_get_user_last_activity( $user->ID ); } $datef = __( 'M j, Y @ G:i', 'buddypress' ); $date = date_i18n( $datef, strtotime( $last_active ) ); ?>' . __( 'This is the administration screen for pending accounts on your site.', 'buddypress' ) . '
' . '' . __( 'From the screen options, you can customize the displayed columns and the pagination of this screen.', 'buddypress' ) . '
' . '' . __( 'You can reorder the list of your pending accounts by clicking on the Username, Email or Registered column headers.', 'buddypress' ) . '
' . '' . __( 'Using the search form, you can find pending accounts more easily. The Username and Email fields will be included in the search.', 'buddypress' ) . '
' ) ); get_current_screen()->add_help_tab( array( 'id' => 'bp-signups-actions', 'title' => __( 'Actions', 'buddypress' ), 'content' => '' . __( 'Hovering over a row in the pending accounts list will display action links that allow you to manage pending accounts. You can perform the following actions:', 'buddypress' ) . '
' . '' . __( 'By clicking on a Username you will be able to activate a pending account from the confirmation screen.', 'buddypress' ) . '
' . '' . __( 'Bulk actions allow you to perform these 3 actions for the selected rows.', 'buddypress' ) . '
' ) ); // Help panel - sidebar links. get_current_screen()->set_help_sidebar( '' . __( 'For more information:', 'buddypress' ) . '
' . '' . __( 'Support Forums', 'buddypress' ) . '
' ); } else { if ( ! empty( $_REQUEST['signup_ids' ] ) ) { $signups = wp_parse_id_list( $_REQUEST['signup_ids' ] ); } // Handle resent activation links. if ( 'do_resend' == $doaction ) { // Nonce check. check_admin_referer( 'signups_resend' ); $resent = BP_Signup::resend( $signups ); if ( empty( $resent ) ) { $redirect_to = add_query_arg( 'error', $doaction, $redirect_to ); } else { $query_arg = array( 'updated' => 'resent' ); if ( ! empty( $resent['resent'] ) ) { $query_arg['resent'] = count( $resent['resent'] ); } if ( ! empty( $resent['errors'] ) ) { $query_arg['notsent'] = count( $resent['errors'] ); set_transient( '_bp_admin_signups_errors', $resent['errors'], 30 ); } $redirect_to = add_query_arg( $query_arg, $redirect_to ); } bp_core_redirect( $redirect_to ); // Handle activated accounts. } elseif ( 'do_activate' == $doaction ) { // Nonce check. check_admin_referer( 'signups_activate' ); $activated = BP_Signup::activate( $signups ); if ( empty( $activated ) ) { $redirect_to = add_query_arg( 'error', $doaction, $redirect_to ); } else { $query_arg = array( 'updated' => 'activated' ); if ( ! empty( $activated['activated'] ) ) { $query_arg['activated'] = count( $activated['activated'] ); } if ( ! empty( $activated['errors'] ) ) { $query_arg['notactivated'] = count( $activated['errors'] ); set_transient( '_bp_admin_signups_errors', $activated['errors'], 30 ); } $redirect_to = add_query_arg( $query_arg, $redirect_to ); } bp_core_redirect( $redirect_to ); // Handle sign-ups delete. } elseif ( 'do_delete' == $doaction ) { // Nonce check. check_admin_referer( 'signups_delete' ); $deleted = BP_Signup::delete( $signups ); if ( empty( $deleted ) ) { $redirect_to = add_query_arg( 'error', $doaction, $redirect_to ); } else { $query_arg = array( 'updated' => 'deleted' ); if ( ! empty( $deleted['deleted'] ) ) { $query_arg['deleted'] = count( $deleted['deleted'] ); } if ( ! empty( $deleted['errors'] ) ) { $query_arg['notdeleted'] = count( $deleted['errors'] ); set_transient( '_bp_admin_signups_errors', $deleted['errors'], 30 ); } $redirect_to = add_query_arg( $query_arg, $redirect_to ); } bp_core_redirect( $redirect_to ); // Plugins can update other stuff from here. } else { $this->redirect = $redirect_to; /** * Fires at end of signups admin load if doaction does not match any actions. * * @since 2.0.0 * * @param string $doaction Current bulk action being processed. * @param array $_REQUEST Current $_REQUEST global. * @param string $redirect Determined redirect url to send user to. */ do_action( 'bp_members_admin_update_signups', $doaction, $_REQUEST, $this->redirect ); bp_core_redirect( $this->redirect ); } } } /** * Display any activation errors. * * @since 2.0.0 */ public function signups_display_errors() { // Look for sign-up errors. $errors = get_transient( '_bp_admin_signups_errors' ); // Bail if no activation errors. if ( empty( $errors ) ) { return; } // Loop through errors and display them. foreach ( $errors as $error ) : ?>