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
+236 -71
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 );
}
}
}
}
@@ -226,7 +428,7 @@ if ( ! class_exists( 'um\core\User' ) ) {
$hide_in_members = UM()->member_directory()->get_hide_in_members_default();
if ( ! empty( $_meta_value ) ) {
if ( $_meta_value == 'Yes' || $_meta_value == __( 'Yes', 'ultimate-member' ) ||
array_intersect( array( 'Yes', __( 'Yes', 'ultimate-member' ) ), $_meta_value ) ) {
array_intersect( array( 'Yes', __( 'Yes', 'ultimate-member' ) ), $_meta_value ) ) {
$hide_in_members = true;
} else {
$hide_in_members = false;
@@ -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
*
@@ -1053,11 +1196,11 @@ if ( ! class_exists( 'um\core\User' ) ) {
$role_meta = apply_filters( 'um_user_permissions_filter', $role_meta, $this->id );
/*$role_meta = array_map( function( $key, $item ) {
if ( strpos( $key, '_um_' ) === 0 )
$key = str_replace( '_um_', '', $key );
if ( strpos( $key, '_um_' ) === 0 )
$key = str_replace( '_um_', '', $key );
return array( $key => $item );
}, array_keys( $role_meta ), $role_meta );*/
return array( $key => $item );
}, array_keys( $role_meta ), $role_meta );*/
$this->profile = array_merge( $this->profile, (array)$role_meta );
@@ -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' );
}
}
}