- changed wp-admin > Users page;

* updated filters by status, avoid slow queries for getting users count;
* updated bulk-actions for changing statuses (moved to WP native dropdown)
* separate handlers for changing user statuses on wp-admin and frontend (partially implemented);
* created class UM()->common()->users() to handle user statuses in more clear format;
* deprecated old hooks and old functions
This commit is contained in:
Mykyta Synelnikov
2024-09-20 18:41:08 +03:00
parent 2c0478757f
commit 512dc53a18
22 changed files with 1250 additions and 1020 deletions
+292 -255
View File
@@ -2,6 +2,7 @@
namespace um\admin;
use WP_User;
use WP_User_Query;
if ( ! defined( 'ABSPATH' ) ) {
exit;
@@ -20,17 +21,157 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
* Users_Columns constructor.
*/
public function __construct() {
add_filter( 'manage_users_columns', array( &$this, 'manage_users_columns' ) );
add_filter( 'manage_users_custom_column', array( &$this, 'manage_users_custom_column' ), 10, 3 );
add_action( 'pre_user_query', array( &$this, 'sort_by_newest' ) );
add_filter( 'users_list_table_query_args', array( &$this, 'hide_by_caps' ), 1 );
add_filter( 'views_users', array( &$this, 'restrict_role_links' ) );
add_filter( 'user_row_actions', array( &$this, 'user_row_actions' ), 10, 2 );
add_filter( 'bulk_actions-users', array( &$this, 'add_bulk_actions' ) );
// add_filter( 'handle_bulk_actions-users', array( &$this, 'handle_bulk_actions' ), 10, 3 );
// add_action( 'manage_users_extra_tablenav', array( &$this, 'filter_by_status_action' ) );
//
// add_filter( 'user_row_actions', array( &$this, 'user_row_actions' ), 10, 2 );
//
// add_filter( 'users_list_table_query_args', array( &$this, 'hide_by_caps' ), 1 );
// add_action( 'pre_user_query', array( &$this, 'sort_by_newest' ) );
// add_action( 'pre_user_query', array( &$this, 'filter_users_by_status' ) );
//
// add_filter( 'removable_query_args', array( &$this, 'add_removable_query_args' ) );
add_filter( 'handle_bulk_actions-users', array( &$this, 'handle_bulk_actions' ), 10, 3 );
add_action( 'manage_users_extra_tablenav', array( &$this, 'add_status_filter' ) );
add_action( 'pre_user_query', array( &$this, 'filter_users_by_status' ) );
add_filter( 'removable_query_args', array( &$this, 'add_removable_query_args' ) );
}
/**
* Filter: Add column 'Status'
*
* @param array $columns
*
* @return array
*/
public function manage_users_columns( $columns ) {
$columns['account_status'] = __( 'Status', 'ultimate-member' );
return $columns;
}
/**
* Filter: Show column 'Status'
*
* @param string $value
* @param string $column_name
* @param int $user_id
*
* @return string
*/
public function manage_users_custom_column( $value, $column_name, $user_id ) {
if ( 'account_status' === $column_name ) {
um_fetch_user( $user_id );
$value = um_user( 'account_status_name' );
um_reset_user();
}
return $value;
}
/**
* Change default sorting at WP Users list table
*
* @param WP_User_Query $query Current instance of WP_User_Query (passed by reference).
*/
public function sort_by_newest( $query ) {
global $pagenow;
// phpcs:ignore WordPress.Security.NonceVerification -- situated in WP native query and just checking sorting
if ( 'users.php' === $pagenow && ! isset( $_REQUEST['orderby'] ) && is_admin() ) {
$query->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'] = '<a href="' . esc_url( um_user_profile_url( $user_id ) ) . '">' . esc_html__( 'View profile', 'ultimate-member' ) . '</a>';
// 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'] = '<a href="#" data-modal="UM_preview_registration" data-modal-size="smaller"
data-dynamic-content="um_admin_review_registration" data-arg1="' . esc_attr( $user_id ) . '" data-arg2="edit_registration">' . esc_html__( 'Info', 'ultimate-member' ) . '</a>';
// For new modal below.
// $actions['view_info'] = '<a href="#" class="um-preview-registration" data-user_id="' . esc_attr( $user_id ) . '">' . esc_html__( 'Info', 'ultimate-member' ) . '</a>';
}
// 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 );
}
/**
@@ -46,7 +187,7 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
'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_reactivate' => __( 'Reactivate', 'ultimate-member' ), // um_reenable
);
/**
* Filters wp-admin > Users List Table bulk actions.
@@ -92,41 +233,41 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
}
/**
* Add query args to list of query variable names to remove.
* Adds HTML with the filter by the Ultimate Member status.
*
* @param array $removable_query_args An array of query variable names to remove from a URL
*
* @return array
* @param string $which Where the callback's hook fired.
*/
public function add_removable_query_args( $removable_query_args ) {
$removable_query_args[] = '_um_wpnonce';
$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;
}
public function add_status_filter( $which ) {
if ( 'top' !== $which ) {
return;
}
/**
* Get the user statuses list.
*
* @return array
*/
public function get_user_statuses() {
$statuses = apply_filters(
'um_admin_get_user_statuses',
array(
'approved' => __( 'Approved', 'ultimate-member' ),
'awaiting_admin_review' => __( 'Pending review', 'ultimate-member' ),
'awaiting_email_confirmation' => __( 'Waiting e-mail confirmation', 'ultimate-member' ),
'inactive' => __( 'Inactive', 'ultimate-member' ),
'rejected' => __( 'Rejected', 'ultimate-member' ),
)
);
// Set default statuses if not already done.
UM()->setup()->set_default_user_status();
return $statuses;
$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();
?>
<div class="alignleft actions um-filter-by-status">
<label class="screen-reader-text" for="<?php echo esc_attr( $id ); ?>"><?php esc_html_e( 'All Statuses', 'ultimate-member' ); ?></label>
<select name="<?php echo esc_attr( $id ); ?>" id="<?php echo esc_attr( $id ); ?>">
<option value=""><?php esc_html_e( 'All Statuses', 'ultimate-member' ); ?></option>
<?php
foreach ( $statuses as $k => $v ) {
$selected = isset( $_GET[ $id ] ) && sanitize_key( $_GET[ $id ] ) === $k; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- native WordPress nonce is used
?>
<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $selected ); ?>><?php echo esc_html( $v ); ?></option>
<?php
}
?>
</select>
<?php submit_button( __( 'Filter', 'ultimate-member' ), '', 'um_filter_users', false ); ?>
</div>
<?php
}
/**
@@ -146,25 +287,30 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
}
// 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( 'bulk-users', '_um_wpnonce' );
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()->user()->approve( $user_id );
$res = UM()->common()->users()->approve( $user_id );
if ( $res ) {
$approved_count++;
++$approved_count;
}
}
@@ -176,12 +322,13 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
case 'um_reactivate':
$reactivated_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->user()->reactivate( $user_id );
$res = UM()->common()->users()->reactivate( $user_id );
if ( $res ) {
$reactivated_count++;
++$reactivated_count;
}
}
@@ -193,12 +340,13 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
case 'um_reject_membership':
$rejected_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->user()->reject( $user_id );
$res = UM()->common()->users()->reject( $user_id );
if ( $res ) {
$rejected_count++;
++$rejected_count;
}
}
@@ -210,12 +358,13 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
case 'um_deactivate':
$deactivated_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->user()->deactivate( $user_id );
$res = UM()->common()->users()->deactivate( $user_id );
if ( $res ) {
$deactivated_count++;
++$deactivated_count;
}
}
@@ -227,12 +376,13 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
case 'um_put_as_pending':
$pending_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->user()->set_as_pending( $user_id );
$res = UM()->common()->users()->set_as_pending( $user_id );
if ( $res ) {
$pending_count++;
++$pending_count;
}
}
@@ -244,12 +394,13 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
case 'um_resend_activation':
$email_pending_count = 0;
foreach ( $users as $user_id ) {
$res = UM()->common()->user()->resend_activation( $user_id );
$res = UM()->common()->users()->send_activation( $user_id );
if ( $res ) {
$email_pending_count++;
++$email_pending_count;
}
}
@@ -261,221 +412,89 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$this->set_redirect_uri( $sendback )
);
break;
default:
// hook for the handling custom UM actions added via 'um_admin_bulk_user_actions_hook' hook
$sendback = apply_filters( "um_handle_bulk_actions-users-{$current_action}", $sendback, $userids );
/**
* 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 <caption>Handle custom-action and set redirect after it.</caption>
* 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;
}
/**
* Adds HTML with the filter by the Ultimate Member status.
*
* @param string $which Where the callback's hook fired.
*/
public function filter_by_status_action( $which ) {
$id = 'bottom' === $which ? 'um_status2' : 'um_status';
$button_id = 'bottom' === $which ? 'um_filter_action2' : 'um_filter_action';
if ( 'top' === $which ) {
// 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('bulk-users', '_um_wpnonce', false );
}
// Set default statuses if not already done.
UM()->install()->set_default_user_status();
$statuses = $this->get_user_statuses();
?>
<div class="alignleft actions">
<label class="screen-reader-text" for="<?php echo esc_attr( $id ); ?>"><?php _e( 'All Statuses', 'ultimate-member' ); ?></label>
<select name="<?php echo esc_attr( $id ); ?>" id="<?php echo esc_attr( $id ); ?>">
<option value=""><?php esc_html_e( 'All Statuses', 'ultimate-member' ); ?></option>
<?php
foreach ( $statuses as $k => $v ) {
$selected = isset( $_GET['um_status'] ) && $k === sanitize_key( $_GET['um_status'] );
?>
<option value="<?php echo esc_attr( $k ) ?>" <?php selected( $selected ) ?>><?php echo esc_html( $v ); ?></option>
<?php
}
?>
</select>
<?php submit_button( __( 'Filter', 'ultimate-member' ), '', $button_id, false ); ?>
</div>
<?php
}
/**
* 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'] = '<a href="' . esc_url( um_user_profile_url( $user_id ) ) . '">' . esc_html__( 'View profile', 'ultimate-member' ) . '</a>';
// 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'] = '<a href="#" class="um-preview-registration" data-user_id="' . esc_attr( $user_id ) . '">' . esc_html__( 'Info', 'ultimate-member' ) . '</a>';
}
// Remove row actions for now Administrator role and who cannot view profiles of row's user.
// @todo make the um_can_view_profile() function review. Maybe rewrite it.
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 2.8.7
* @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 );
}
/**
* Hide users who are hidden by role access for not Administrator user
*
* @param array $args
* @return array
*/
public function hide_by_caps( $args ) {
if ( current_user_can( 'administrator' ) ) {
return $args;
}
// @todo avoid um_user() function using
$can_view_roles = um_user( 'can_view_roles' );
if ( um_user( 'can_view_all' ) && ! empty( $can_view_roles ) ) {
$args['role__in'] = $can_view_roles;
}
return $args;
}
/**
* Change default sorting at WP Users list table
*
* @param \WP_User_Query $query
*/
public function sort_by_newest( $query ) {
global $pagenow;
if ( is_admin() && 'users.php' === $pagenow ) {
if ( ! isset( $_REQUEST['orderby'] ) ) {
$query->query_vars['order'] = 'desc';
$query->query_orderby = ' ORDER BY user_registered ' . ( 'desc' === $query->query_vars['order'] ? 'desc ' : 'asc ' ); //set sort order
}
}
}
/**
* Filter WP users by UM Status
*
* @param \WP_User_Query $query
* WP_User_Query $query Current instance of WP_User_Query (passed by reference).
*/
public function filter_users_by_status( $query ) {
global $wpdb, $pagenow;
if ( is_admin() && 'users.php' === $pagenow && ! empty( $_REQUEST['um_status'] ) ) {
$status = sanitize_key( $_REQUEST['um_status'] );
$skip_status_filter = apply_filters( 'um_skip_filter_users_by_status', false, $status );
if ( ! $skip_status_filter ) {
$query->query_where = str_replace(
'WHERE 1=1',
"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 = '{$status}')",
$query->query_where
);
}
if ( 'users.php' !== $pagenow || ! is_admin() ) {
return;
}
}
/**
* Does an action to user asap
*
* @param string $action
*/
public function user_action_hook( $action ) {
switch ( $action ) {
default:
/**
* UM hook
*
* @type action
* @title um_admin_custom_hook_{$action}
* @description Integration hook on user action
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_admin_custom_hook_{$action}', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_admin_custom_hook_{$action}', 'my_admin_custom_hook', 10, 1 );
* function my_admin_after_main_notices( $user_id ) {
* // your code here
* }
* ?>
*/
do_action( "um_admin_custom_hook_{$action}", UM()->user()->id );
break;
// case 'um_put_as_pending':
// UM()->user()->pending();
// break;
// case 'um_approve_membership':
// case 'um_reenable':
//
// add_filter( 'um_template_tags_patterns_hook', array( UM()->password(), 'add_placeholder' ), 10, 1 );
// add_filter( 'um_template_tags_replaces_hook', array( UM()->password(), 'add_replace_placeholder' ), 10, 1 );
//
// UM()->user()->approve();
// break;
// case 'um_reject_membership':
// UM()->user()->reject();
// break;
// case 'um_resend_activation':
//
// add_filter( 'um_template_tags_patterns_hook', array( UM()->user(), 'add_activation_placeholder' ), 10, 1 );
// add_filter( 'um_template_tags_replaces_hook', array( UM()->user(), 'add_activation_replace_placeholder' ), 10, 1 );
//
// UM()->user()->email_pending();
// break;
// case 'um_deactivate':
// UM()->user()->deactivate();
// break;
case 'um_delete':
if ( is_admin() ) {
wp_die( __( 'This action is not allowed in backend.', 'ultimate-member' ) );
}
UM()->user()->delete();
break;
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
);
}
/**
@@ -489,11 +508,29 @@ if ( ! class_exists( 'um\admin\Users_Columns' ) ) {
$uri = add_query_arg( 's', sanitize_text_field( $_REQUEST['s'] ), $uri );
}
if ( ! empty( $_REQUEST['um_status'] ) ) {
$uri = add_query_arg( 'um_status', sanitize_key( $_REQUEST['um_status'] ), $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;
}
}
}