Merge pull request #1026 from ultimatemember/fixed/wp-admin-performance

Fixed/wp admin performance
This commit is contained in:
Nikita Sinelnikov
2022-08-13 02:28:52 +03:00
committed by GitHub
12 changed files with 483 additions and 120 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
name: JobBoardWP GitHub Actions
name: Ultimate Member - Checking Code quality through PHPCS + WPCS
on:
push:
+48 -2
View File
@@ -72,6 +72,7 @@ if ( ! class_exists( 'um\admin\Admin' ) ) {
add_filter( "{$prefix}plugin_action_links_" . um_plugin, array( &$this, 'plugin_links' ) );
add_action( 'um_admin_do_action__user_cache', array( &$this, 'user_cache' ) );
add_action( 'um_admin_do_action__user_status_cache', array( &$this, 'user_status_cache' ) );
add_action( 'um_admin_do_action__purge_temp', array( &$this, 'purge_temp' ) );
add_action( 'um_admin_do_action__manual_upgrades_request', array( &$this, 'manual_upgrades_request' ) );
add_action( 'um_admin_do_action__duplicate_form', array( &$this, 'duplicate_form' ) );
@@ -1673,8 +1674,53 @@ if ( ! class_exists( 'um\admin\Admin' ) ) {
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'um_cache_userdata_%'" );
$url = add_query_arg( array( 'page' => 'ultimatemember', 'update' => 'cleared_cache' ), admin_url( 'admin.php' ) );
exit( wp_redirect( $url ) );
$url = add_query_arg(
array(
'page' => 'ultimatemember',
'update' => 'cleared_cache',
),
admin_url( 'admin.php' )
);
wp_redirect( $url );
exit;
}
/**
* Clear all users statuses count cache
*
* @param $action
*/
function user_status_cache( $action ) {
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
die();
}
$statuses = array(
'approved',
'awaiting_admin_review',
'awaiting_email_confirmation',
'inactive',
'rejected',
'pending_dot', // not real status key, just for the transient
'unassigned', // not real status key, just for the transient
);
foreach ( $statuses as $status ) {
delete_transient( "um_count_users_{$status}" );
}
do_action( 'um_flush_user_status_cache' );
$url = add_query_arg(
array(
'page' => 'ultimatemember',
'update' => 'cleared_status_cache',
),
admin_url( 'admin.php' )
);
wp_redirect( $url );
exit;
}
+1 -1
View File
@@ -121,7 +121,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Menu' ) ) {
return;
}
$count = UM()->user()->get_pending_users_count();
$count = UM()->query()->get_pending_users_count();
if ( is_array( $menu ) ) {
foreach ( $menu as $key => $menu_item ) {
if ( 0 === strpos( $menu_item[0], _x( 'Users', 'Admin menu name' ) ) ) {
@@ -440,6 +440,10 @@ if ( ! class_exists( 'um\admin\core\Admin_Notices' ) ) {
$messages[0]['content'] = __( 'Your user cache is now removed.', 'ultimate-member' );
break;
case 'cleared_status_cache':
$messages[0]['content'] = __( 'Your user statuses cache is now removed.', 'ultimate-member' );
break;
case 'got_updates':
$messages[0]['content'] = __( 'You have the latest updates.', 'ultimate-member' );
break;
+2 -1
View File
@@ -411,7 +411,8 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
'rejected' => __( 'Rejected', 'ultimate-member' ),
);
UM()->query()->count_users_by_status( 'unassigned' );
// set default statuses if not already done
UM()->setup()->set_default_user_status();
foreach ( $status as $k => $v ) {
if ( isset( $_REQUEST['um_status'] ) && sanitize_key( $_REQUEST['um_status'] ) === $k ) {
+10 -4
View File
@@ -1,5 +1,6 @@
<?php if ( ! defined( 'ABSPATH' ) ) exit;
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $wpdb;
@@ -7,11 +8,16 @@ $count = $wpdb->get_var(
"SELECT COUNT( option_id )
FROM {$wpdb->options}
WHERE option_name LIKE 'um_cache_userdata_%'"
); ?>
);
?>
<p><?php _e( 'Run this task from time to time to keep your DB clean.', 'ultimate-member' ) ?></p>
<p>
<a href="<?php echo esc_url( add_query_arg( 'um_adm_action', 'user_cache' ) ); ?>" class="button">
<?php printf( __( 'Clear cache of %s users', 'ultimate-member' ), $count ) ?>
<?php echo esc_html( sprintf( __( 'Clear cache of %s users', 'ultimate-member' ), $count ) ); ?>
</a>
<a href="<?php echo esc_url( add_query_arg( 'um_adm_action', 'user_status_cache' ) ); ?>" class="button">
<?php esc_html_e( 'Clear user statuses cache', 'ultimate-member' ); ?>
</a>
</p>
+116 -23
View File
@@ -217,48 +217,114 @@ if ( ! class_exists( 'um\core\Query' ) ) {
/**
* Count users by status
*
* @since 2.4.2 $status = 'unassigned' is unused. Please use `UM()->setup()->set_default_user_status()` instead. Will be deprecated since 3.0
*
* @param $status
*
* @return int
*/
function count_users_by_status( $status ) {
$args = array( 'fields' => 'ID', 'number' => 0, 'um_custom_user_query' => true );
if ( $status == 'unassigned' ) {
$args['meta_query'][] = array(array('key' => 'account_status','compare' => 'NOT EXISTS'));
$users = new \WP_User_Query( $args );
foreach ( $users->results as $user ) {
update_user_meta( $user, 'account_status', 'approved' );
}
if ( 'unassigned' === $status ) {
_deprecated_argument(
__FUNCTION__,
'2.4.2',
__( 'The "unassigned" $status has been removed. Use `UM()->setup()->set_default_user_status()` for setting up default user account status.', 'ultimate-member' )
);
UM()->setup()->set_default_user_status();
return 0;
} else {
$args['meta_query'][] = array(array('key' => 'account_status','value' => $status,'compare' => '='));
}
$users_count = get_transient( "um_count_users_{$status}" );
if ( false === $users_count ) {
$args = array(
'fields' => 'ids',
'number' => 1,
'meta_query' => array(
array(
'key' => 'account_status',
'value' => $status,
'compare' => '=',
),
),
'um_custom_user_query' => true,
);
$users = new \WP_User_Query( $args );
return count( $users->results );
if ( empty( $users ) || is_wp_error( $users ) ) {
$users_count = 0;
} else {
$users_count = $users->get_total();
}
set_transient( "um_count_users_{$status}", $users_count );
}
return $users_count;
}
/**
* Get users by status
* Get pending users (in queue)
*
* @param $status
* @param int $number
*
* @return array
* @return int
*/
function get_users_by_status($status, $number = 5){
$args = array( 'fields' => 'ID', 'number' => $number, 'orderby' => 'user_registered', 'order' => 'desc' );
$args['meta_query'][] = array(
function get_pending_users_count() {
$users_count = get_transient( 'um_count_users_pending_dot' );
if ( false === $users_count ) {
$args = array(
'fields' => 'ids',
'number' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'account_status',
'value' => $status,
'compare' => '='
)
'value' => 'awaiting_email_confirmation',
'compare' => '=',
),
array(
'key' => 'account_status',
'value' => 'awaiting_admin_review',
'compare' => '=',
),
),
'um_custom_user_query' => true,
);
/**
* UM hook
*
* @type filter
* @title um_admin_pending_queue_filter
* @description Change user query arguments when get pending users
* @input_vars
* [{"var":"$args","type":"array","desc":"WP_Users query arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_admin_pending_queue_filter', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_admin_pending_queue_filter', 'my_admin_pending_queue', 10, 1 );
* function my_admin_pending_queue( $args ) {
* // your code here
* return $args;
* }
* ?>
*/
$args = apply_filters( 'um_admin_pending_queue_filter', $args );
$users = new \WP_User_Query( $args );
return $users->results;
if ( empty( $users ) || is_wp_error( $users ) ) {
$users_count = 0;
} else {
$users_count = $users->get_total();
}
set_transient( 'um_count_users_pending_dot', $users_count );
}
return $users_count;
}
@@ -456,5 +522,32 @@ if ( ! class_exists( 'um\core\Query' ) ) {
}
}
/**
* Get users by status
*
* @param $status
* @param int $number
*
* @deprecated 2.4.2
*
* @return array
*/
function get_users_by_status( $status, $number = 5 ) {
_deprecated_function( __METHOD__, '2.4.2' );
$args = array( 'fields' => 'ID', 'number' => $number, 'orderby' => 'user_registered', 'order' => 'desc' );
$args['meta_query'][] = array(
array(
'key' => 'account_status',
'value' => $status,
'compare' => '='
)
);
$users = new \WP_User_Query( $args );
return $users->results;
}
}
}
+41 -3
View File
@@ -32,6 +32,7 @@ if ( ! class_exists( 'um\core\Setup' ) ) {
$this->install_default_forms();
$this->set_default_settings();
$this->set_default_role_meta();
$this->set_default_user_status();
}
@@ -262,12 +263,49 @@ KEY meta_value_indx (um_value(191))
* Set UM roles meta to Default WP roles
*/
function set_default_role_meta() {
//for set accounts without account status approved status
UM()->query()->count_users_by_status( 'unassigned' );
foreach ( UM()->config()->default_roles_metadata as $role => $meta ) {
add_option( "um_role_{$role}_meta", $meta );
}
}
/**
* Set accounts without account_status meta to 'approved' status
*
* @since 2.4.2
*/
function set_default_user_status() {
$result = get_transient( 'um_count_users_unassigned' );
if ( false === $result ) {
$args = array(
'fields' => 'ids',
'number' => 0,
'meta_query' => array(
array(
'key' => 'account_status',
'compare' => 'NOT EXISTS',
),
),
'um_custom_user_query' => true,
);
$users = new \WP_User_Query( $args );
if ( empty( $users ) || is_wp_error( $users ) ) {
$result = array();
} else {
$result = $users->get_results();
}
set_transient( 'um_count_users_unassigned', $result, DAY_IN_SECONDS );
}
if ( empty( $result ) ) {
return;
}
foreach ( $result as $user_id ) {
update_user_meta( $user_id, 'account_status', 'approved' );
}
}
}
}
+231 -66
View File
@@ -65,11 +65,8 @@ if ( ! class_exists( 'um\core\User' ) ) {
$this->target_id = null;
// When the cache should be cleared
add_action( 'um_delete_user_hook', array( &$this, 'remove_cached_queue' ) );
add_action( 'um_delete_user', array( &$this, 'remove_cache' ), 10, 1 );
add_action( 'um_after_user_status_is_changed_hook', array( &$this, 'remove_cached_queue' ) );
// When user cache should be cleared
add_action( 'um_after_user_updated', array( &$this, 'remove_cache' ) );
add_action( 'um_after_user_account_updated', array( &$this, 'remove_cache' ) );
@@ -111,6 +108,211 @@ if ( ! class_exists( 'um\core\User' ) ) {
add_action( 'added_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
add_action( 'deleted_user_meta', array( &$this, 'on_delete_usermeta' ), 10, 4 );
add_action( 'update_user_meta', array( &$this, 'flush_um_count_users_transient_update' ), 10, 4 );
add_action( 'added_user_meta', array( &$this, 'flush_um_count_users_transient_add' ), 10, 4 );
add_action( 'delete_user_meta', array( &$this, 'flush_um_count_users_transient_delete' ), 10, 4 );
}
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_update( $meta_ids, $object_id, $meta_key, $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return;
}
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array( $_meta_value, array( 'checkmail', 'pending' ), true ) ) {
return;
}
$pending_statuses = array(
'awaiting_email_confirmation',
'awaiting_admin_review',
);
$old = get_user_meta( $object_id, $meta_key, true );
if ( $old === $_meta_value ) {
return;
}
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( ! in_array( $old, array( 'checkmail', 'pending' ), true ) ) {
// deduct old transient count
$count = get_transient( "um_count_users_{$old}" );
if ( false !== $count ) {
if ( ! is_numeric( $count ) ) {
delete_transient( "um_count_users_{$old}" );
} else {
if ( 0 < $count ) {
$count--;
} else {
$count = 0;
}
set_transient( "um_count_users_{$old}", $count );
}
}
if ( in_array( $old, $pending_statuses, true ) && ! in_array( $_meta_value, $pending_statuses, true ) ) {
// deduct old transient count
$count = get_transient( 'um_count_users_pending_dot' );
if ( false !== $count ) {
if ( ! is_numeric( $count ) ) {
delete_transient( 'um_count_users_pending_dot' );
} else {
if ( 0 < $count ) {
$count--;
} else {
$count = 0;
}
set_transient( 'um_count_users_pending_dot', $count );
}
}
}
}
// add new transient count
$count = get_transient( "um_count_users_{$_meta_value}" );
if ( false !== $count ) {
if ( is_numeric( $count ) ) {
$count++;
} else {
$count = 1;
}
} else {
$count = 1;
}
set_transient( "um_count_users_{$_meta_value}", $count );
if ( in_array( $_meta_value, $pending_statuses, true ) && ! in_array( $old, $pending_statuses, true ) ) {
// add new transient count
$count = get_transient( 'um_count_users_pending_dot' );
if ( false !== $count ) {
if ( is_numeric( $count ) ) {
$count++;
} else {
$count = 1;
}
} else {
$count = 1;
}
set_transient( 'um_count_users_pending_dot', $count );
}
}
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_add( $meta_ids, $object_id, $meta_key, $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return;
}
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array( $_meta_value, array( 'checkmail', 'pending' ), true ) ) {
return;
}
$pending_statuses = array(
'awaiting_email_confirmation',
'awaiting_admin_review',
);
// add new transient count
$count = get_transient( "um_count_users_{$_meta_value}" );
if ( false !== $count ) {
if ( is_numeric( $count ) ) {
$count++;
} else {
$count = 1;
}
} else {
$count = 1;
}
set_transient( "um_count_users_{$_meta_value}", $count );
if ( in_array( $_meta_value, $pending_statuses, true ) ) {
// add new transient count
$pending_count = get_transient( 'um_count_users_pending_dot' );
if ( false !== $pending_count ) {
if ( is_numeric( $pending_count ) ) {
$pending_count++;
} else {
$pending_count = 1;
}
} else {
$pending_count = 1;
}
set_transient( 'um_count_users_pending_dot', $pending_count );
}
}
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_delete( $meta_ids, $object_id, $meta_key, $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return;
}
$value = ( '' !== $_meta_value ) ? $_meta_value : get_user_meta( $object_id, $meta_key, true );
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array( $value, array( 'checkmail', 'pending' ), true ) ) {
return;
}
$pending_statuses = array(
'awaiting_email_confirmation',
'awaiting_admin_review',
);
// deduct old transient count
$count = get_transient( "um_count_users_{$value}" );
if ( false !== $count ) {
if ( ! is_numeric( $count ) ) {
delete_transient( "um_count_users_{$value}" );
} else {
if ( 0 < $count ) {
$count--;
} else {
$count = 0;
}
set_transient( "um_count_users_{$value}", $count );
}
}
if ( in_array( $value, $pending_statuses, true ) ) {
// deduct old transient count
$count = get_transient( 'um_count_users_pending_dot' );
if ( false !== $count ) {
if ( ! is_numeric( $count ) ) {
delete_transient( 'um_count_users_pending_dot' );
} else {
if ( 0 < $count ) {
$count--;
} else {
$count = 0;
}
set_transient( 'um_count_users_pending_dot', $count );
}
}
}
}
@@ -324,6 +526,9 @@ if ( ! class_exists( 'um\core\User' ) ) {
// remove uploads
UM()->files()->remove_dir( UM()->files()->upload_temp );
UM()->files()->remove_dir( UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR );
delete_transient( 'um_count_users_unassigned' );
delete_transient( 'um_count_users_pending_dot' );
}
@@ -391,60 +596,6 @@ if ( ! class_exists( 'um\core\User' ) ) {
}
/**
* Get pending users (in queue)
*/
function get_pending_users_count() {
$cached_users_queue = get_option( 'um_cached_users_queue' );
if ( $cached_users_queue > 0 && ! isset( $_REQUEST['delete_count'] ) ) {
return $cached_users_queue;
}
$args = array( 'fields' => 'ID', 'number' => 1 );
$args['meta_query']['relation'] = 'OR';
$args['meta_query'][] = array(
'key' => 'account_status',
'value' => 'awaiting_email_confirmation',
'compare' => '='
);
$args['meta_query'][] = array(
'key' => 'account_status',
'value' => 'awaiting_admin_review',
'compare' => '='
);
/**
* UM hook
*
* @type filter
* @title um_admin_pending_queue_filter
* @description Change user query arguments when get pending users
* @input_vars
* [{"var":"$args","type":"array","desc":"WP_Users query arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_admin_pending_queue_filter', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_admin_pending_queue_filter', 'my_admin_pending_queue', 10, 1 );
* function my_admin_pending_queue( $args ) {
* // your code here
* return $args;
* }
* ?>
*/
$args = apply_filters( 'um_admin_pending_queue_filter', $args );
$users = new \WP_User_Query( $args );
delete_option( 'um_cached_users_queue' );
add_option( 'um_cached_users_queue', $users->get_total(), '', 'no' );
return $users->get_total();
}
/**
* @param $user_id
*
@@ -597,7 +748,6 @@ if ( ! class_exists( 'um\core\User' ) ) {
* @param $user_id
*/
function user_register_via_admin( $user_id ) {
if ( empty( $user_id ) ) {
return;
}
@@ -634,6 +784,7 @@ if ( ! class_exists( 'um\core\User' ) ) {
do_action( 'um_user_register', $user_id, $_POST );
}
delete_transient( 'um_count_users_unassigned' );
}
@@ -807,14 +958,6 @@ if ( ! class_exists( 'um\core\User' ) ) {
}
/**
* Remove cached queue from Users backend
*/
function remove_cached_queue() {
delete_option( 'um_cached_users_queue' );
}
/**
* Converts object to array
*
@@ -2197,5 +2340,27 @@ if ( ! class_exists( 'um\core\User' ) ) {
$replace_placeholders[] = um_user( 'account_activation_link' );
return $replace_placeholders;
}
/**
* Get pending users (in queue)
*
* @deprecated 2.4.2
*/
function get_pending_users_count() {
_deprecated_function( __METHOD__, '2.4.2', 'UM()->query()->get_pending_users_count()' );
return UM()->query()->get_pending_users_count();
}
/**
* Remove cached queue from Users backend
*
* @deprecated 2.4.2
*/
function remove_cached_queue() {
_deprecated_function( __METHOD__, '2.4.2', '' );
delete_option( 'um_cached_users_queue' );
}
}
}
+1 -1
View File
@@ -296,7 +296,7 @@ if ( ! class_exists( 'um\core\rest\API' ) ) {
$count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}users" ) );
$response['stats']['total_users'] = $count;
$pending = UM()->user()->get_pending_users_count();
$pending = UM()->query()->get_pending_users_count();
$response['stats']['pending_users'] = absint( $pending );
/**
-4
View File
@@ -51,14 +51,10 @@ add_action('um_post_registration_pending_hook', 'um_post_registration_pending_ho
* @param $args
*/
function um_after_insert_user( $user_id, $args ) {
if ( empty( $user_id ) || ( is_object( $user_id ) && is_a( $user_id, 'WP_Error' ) ) ) {
return;
}
//clear Users cached queue
UM()->user()->remove_cached_queue();
um_fetch_user( $user_id );
if ( ! empty( $args['submitted'] ) ) {
UM()->user()->set_registration_details( $args['submitted'], $args );
+14
View File
@@ -115,6 +115,20 @@ if ( ! empty( $delete_options ) ) {
delete_option( '__ultimatemember_sitekey' );
delete_option( 'um_flush_rewrite_rules' );
$statuses = array(
'approved',
'awaiting_admin_review',
'awaiting_email_confirmation',
'inactive',
'rejected',
);
foreach ( $statuses as $status ) {
delete_transient( "um_count_users_{$status}" );
}
delete_transient( 'um_count_users_pending_dot' );
delete_transient( 'um_count_users_unassigned' );
//remove all users cache
UM()->user()->remove_cache_all_users();