query_vars['order'] = 'desc';
$query->query_orderby = ' ORDER BY user_registered DESC';
}
}
/**
* Hide users who are hidden by role access for not Administrator user
*
* @param array $args Arguments passed to WP_User_Query to retrieve items for the current
* users list table
*
* @return array
*/
public function hide_by_caps( $args ) {
if ( current_user_can( 'manage_options' ) ) {
return $args;
}
// @todo avoid um_user() function using
// @todo check another restrictions not only the role settings. We need to exclude users per user ID.
$can_view_roles = um_user( 'can_view_roles' );
if ( ! empty( $can_view_roles ) && um_user( 'can_view_all' ) ) {
$args['role__in'] = $can_view_roles;
}
return $args;
}
/**
* Hide role filters with not accessible roles
*
* @param array $views
* @return array
*/
public function restrict_role_links( $views ) {
if ( current_user_can( 'manage_options' ) ) {
return $views;
}
$can_view_roles = um_user( 'can_view_roles' );
if ( ! empty( $can_view_roles ) && um_user( 'can_view_all' ) ) {
$wp_roles = wp_roles();
foreach ( $wp_roles->get_names() as $this_role => $name ) {
if ( ! in_array( $this_role, $can_view_roles, true ) ) {
unset( $views[ $this_role ] );
}
}
}
return $views;
}
/**
* Custom row actions for users page
*
* @param array $actions
* @param WP_User $user_object
*
* @return array
*/
public function user_row_actions( $actions, $user_object ) {
$user_id = $user_object->ID;
// Link to Ultimate Member Profile.
$actions['frontend_profile'] = '' . esc_html__( 'View profile', 'ultimate-member' ) . '';
// The link for open popup with the registration data submitted through Ultimate Member Registration form.
$submitted = get_user_meta( $user_id, 'submitted', true );
if ( ! empty( $submitted ) ) {
$actions['view_info'] = '' . esc_html__( 'Info', 'ultimate-member' ) . '';
// For new modal below.
// $actions['view_info'] = '' . esc_html__( 'Info', 'ultimate-member' ) . '';
}
// Remove row actions for now Administrator role and who cannot view profiles of row's user.
if ( ! current_user_can( 'manage_options' ) && ! um_can_view_profile( $user_id ) ) {
unset( $actions['frontend_profile'], $actions['view_info'], $actions['view'] );
}
/**
* Filters the rows actions for the user in wp-admin > Users List Table screen.
*
* Note: Row actions format is 'key' => 'action_link_html'
*
* @since 1.3.x
* @hook um_admin_user_row_actions
*
* @param {array} $actions User's row actions.
* @param {int} $user_id Row's user ID.
*
* @return {array} User's row actions.
*/
return apply_filters( 'um_admin_user_row_actions', $actions, $user_id );
}
/**
* Get the list with the bulk actions.
*
* @return array
*/
private function get_user_bulk_actions() {
// @todo check verified users module for the proper integration. remove old integration way
$um_actions = array(
'um_approve_membership' => __( 'Approve Membership', 'ultimate-member' ),
'um_reject_membership' => __( 'Reject Membership', 'ultimate-member' ),
'um_put_as_pending' => __( 'Put as Pending Review', 'ultimate-member' ),
'um_resend_activation' => __( 'Resend Activation E-mail', 'ultimate-member' ),
'um_deactivate' => __( 'Deactivate', 'ultimate-member' ),
'um_reactivate' => __( 'Reactivate', 'ultimate-member' ), // um_reenable
);
/**
* Filters wp-admin > Users List Table bulk actions.
*
* @since 1.3.x
* @since 2.8.7 changed format from `$action_slug => array( 'label' => $action_title )` to `$action_slug => $action_title`
* @hook um_admin_bulk_user_actions_hook
*
* @param {array} $um_actions Users admin actions.
*
* @return {array} Users admin actions.
*
* @example
Add `$action_title` to Users List Table bulk actions.
* function um_custom_admin_bulk_user_actions_hook( $um_actions ) {
* $um_actions[ $action_slug ] = $action_title;
* return $um_actions;
* }
* add_filter( 'um_admin_bulk_user_actions_hook', 'um_custom_admin_bulk_user_actions_hook' );
*/
return apply_filters( 'um_admin_bulk_user_actions_hook', $um_actions );
}
/**
* @param array $actions
*
* @return array
*/
public function add_bulk_actions( $actions ) {
$rolename = UM()->roles()->get_priority_user_role( get_current_user_id() );
$role = get_role( $rolename );
if ( null === $role ) {
return $actions;
}
// Add Ultimate Member bulk actions only when the current user has 'edit_users' capability.
if ( ! current_user_can( 'edit_users' ) && ! $role->has_cap( 'edit_users' ) ) {
return $actions;
}
$actions[ esc_html__( 'Ultimate Member', 'ultimate-member' ) ] = $this->get_user_bulk_actions();
return $actions;
}
/**
* Adds HTML with the filter by the Ultimate Member status.
*
* @param string $which Where the callback's hook fired.
*/
public function add_status_filter( $which ) {
if ( 'top' !== $which ) {
return;
}
// Set default statuses if not already done.
UM()->setup()->set_default_user_status();
$id = 'um_user_status';
// need to add there additional nonce field because WordPress native _wpnonce field isn't visible on the users.php screen then custom actions
wp_nonce_field( 'um-bulk-users', '_um_wpnonce', false );
$statuses = UM()->common()->users()->statuses_list();
?>
get_user_bulk_actions();
if ( ! array_key_exists( $current_action, $um_actions ) ) {
return $sendback;
}
// need to handle there additional nonce field because WordPress native _wpnonce field isn't visible on the users.php screen then custom actions
check_admin_referer( 'um-bulk-users', '_um_wpnonce' );
$rolename = UM()->roles()->get_priority_user_role( get_current_user_id() );
$role = get_role( $rolename );
if ( null === $role ) {
return $sendback;
}
// Make Ultimate Member bulk actions only when the current user has 'edit_users' capability.
if ( ! current_user_can( 'edit_users' ) && ! $role->has_cap( 'edit_users' ) ) {
wp_die( esc_html__( 'You do not have enough permissions to do that.', 'ultimate-member' ) );
}
$users = array_map( 'absint', $userids );
$users = array_diff( $users, array( get_current_user_id() ) ); // cannot make any action related to himself.
switch ( $current_action ) {
case 'um_approve_membership':
$approved_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->approve( $user_id );
if ( $res ) {
++$approved_count;
}
}
$sendback = add_query_arg(
array(
'approved_count' => $approved_count,
'update' => 'um_approved',
),
$this->set_redirect_uri( $sendback )
);
break;
case 'um_reactivate':
$reactivated_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->reactivate( $user_id );
if ( $res ) {
++$reactivated_count;
}
}
$sendback = add_query_arg(
array(
'reactivated_count' => $reactivated_count,
'update' => 'um_reactivated',
),
$this->set_redirect_uri( $sendback )
);
break;
case 'um_reject_membership':
$rejected_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->reject( $user_id );
if ( $res ) {
++$rejected_count;
}
}
$sendback = add_query_arg(
array(
'rejected_count' => $rejected_count,
'update' => 'um_rejected',
),
$this->set_redirect_uri( $sendback )
);
break;
case 'um_deactivate':
$deactivated_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->deactivate( $user_id );
if ( $res ) {
++$deactivated_count;
}
}
$sendback = add_query_arg(
array(
'deactivated_count' => $deactivated_count,
'update' => 'um_deactivate',
),
$this->set_redirect_uri( $sendback )
);
break;
case 'um_put_as_pending':
$pending_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->set_as_pending( $user_id );
if ( $res ) {
++$pending_count;
}
}
$sendback = add_query_arg(
array(
'pending_count' => $pending_count,
'update' => 'um_pending',
),
$this->set_redirect_uri( $sendback )
);
break;
case 'um_resend_activation':
$email_pending_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->users()->send_activation( $user_id );
if ( $res ) {
++$email_pending_count;
}
}
$sendback = add_query_arg(
array(
'resend_activation_count' => $email_pending_count,
'update' => 'um_resend_activation',
),
$this->set_redirect_uri( $sendback )
);
break;
default:
/**
* Fires when a custom Ultimate Member bulk action for wp-admin > Users list table should be handled.
*
* The redirect link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* The dynamic portion of the hook name, `$current_action`, refers to the current bulk action.
* Use together with custom actions added via `um_admin_bulk_user_actions_hook` hook.
*
* @param {string} $sendback The redirect URL.
* @param {array} $userids Selected users in bulk action.
*
* @return {string} The redirect URL.
*
* @since 2.8.7
* @hook um_handle_bulk_actions-users-{$current_action}
*
* @example Handle custom-action and set redirect after it.
* function um_custom_bulk_actions_users( $sendback, $userids ) {
* foreach ( $userids as $user_id ) {
* // make some action here
* }
* return add_query_arg( 'action_counter', 'completed action count', $sendback );
* }
* add_filter( 'um_handle_bulk_actions-users-custom-action', 'um_custom_bulk_actions_users' );
*/
$sendback = apply_filters( "um_handle_bulk_actions-users-{$current_action}", $sendback, $userids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
break;
}
return $sendback;
}
/**
* Filter WP users by UM Status
*
* WP_User_Query $query Current instance of WP_User_Query (passed by reference).
*/
public function filter_users_by_status( $query ) {
global $wpdb, $pagenow;
if ( 'users.php' !== $pagenow || ! is_admin() ) {
return;
}
if ( empty( $_REQUEST['um_user_status'] ) ) {
return;
}
$status = sanitize_key( $_REQUEST['um_user_status'] );
/**
* Filters the marker to disable Ultimate Member default filter by user status.
*
* @since 2.8.7
* @hook um_skip_filter_users_by_status
*
* @param {bool} $skip Marker to skip Ultimate Member core user filter handler.
* @param {string} $status User Status
*
* @return {array} User's row actions.
*/
$skip_status_filter = apply_filters( 'um_skip_filter_users_by_status', false, $status );
if ( false !== $skip_status_filter ) {
return;
}
$query->query_where = str_replace(
'WHERE 1=1',
$wpdb->prepare(
"WHERE 1=1 AND
{$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id
FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = 'account_status' AND
{$wpdb->usermeta}.meta_value = %s
)",
$status
),
$query->query_where
);
}
/**
* Sets redirect URI after bulk action
*
* @param string $uri
* @return string
*/
private function set_redirect_uri( $uri ) {
if ( ! empty( $_REQUEST['s'] ) ) {
$uri = add_query_arg( 's', sanitize_text_field( $_REQUEST['s'] ), $uri );
}
if ( ! empty( $_REQUEST['um_user_status'] ) ) {
$uri = add_query_arg( 'um_user_status', sanitize_key( $_REQUEST['um_user_status'] ), $uri );
}
return $uri;
}
/**
* Add query args to list of query variable names to remove.
*
* @param array $removable_query_args An array of query variable names to remove from a URL
*
* @return array
*/
public function add_removable_query_args( $removable_query_args ) {
$removable_query_args[] = '_um_wpnonce'; // need to add there additional nonce field because WordPress native _wpnonce field isn't visible on the users.php screen then custom actions
$removable_query_args[] = 'approved_count';
$removable_query_args[] = 'rejected_count';
$removable_query_args[] = 'reactivated_count';
$removable_query_args[] = 'deactivated_count';
$removable_query_args[] = 'pending_count';
$removable_query_args[] = 'resend_activation_count';
return $removable_query_args;
}
}
}