diff --git a/includes/admin/core/class-admin-menu.php b/includes/admin/core/class-admin-menu.php index 26c07eb3..7ec8b88c 100644 --- a/includes/admin/core/class-admin-menu.php +++ b/includes/admin/core/class-admin-menu.php @@ -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' ) ) ) { diff --git a/includes/admin/core/class-admin-users.php b/includes/admin/core/class-admin-users.php index b7a0d2a1..ee9eff56 100644 --- a/includes/admin/core/class-admin-users.php +++ b/includes/admin/core/class-admin-users.php @@ -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 ) { diff --git a/includes/core/class-query.php b/includes/core/class-query.php index 7061be61..2320ecd0 100644 --- a/includes/core/class-query.php +++ b/includes/core/class-query.php @@ -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 = new \WP_User_Query( $args ); - return count( $users->results ); + + $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 ); + 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' ); + function get_pending_users_count() { + $users_count = get_transient( 'um_count_users_pending' ); + if ( false === $users_count ) { + $args = array( + 'fields' => 'ids', + 'number' => 1, + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'account_status', + 'value' => 'awaiting_email_confirmation', + 'compare' => '=', + ), + array( + 'key' => 'account_status', + 'value' => 'awaiting_admin_review', + 'compare' => '=', + ), + ), + 'um_custom_user_query' => true, + ); - $args['meta_query'][] = array( - array( - 'key' => 'account_status', - 'value' => $status, - '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 + * + * @example + * + */ + $args = apply_filters( 'um_admin_pending_queue_filter', $args ); - $users = new \WP_User_Query( $args ); - return $users->results; + $users = new \WP_User_Query( $args ); + if ( empty( $users ) || is_wp_error( $users ) ) { + $users_count = 0; + } else { + $users_count = $users->get_total(); + } + + set_transient( 'um_count_users_pending', $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; + } } } diff --git a/includes/core/class-setup.php b/includes/core/class-setup.php index d788f7fe..d99f26a8 100644 --- a/includes/core/class-setup.php +++ b/includes/core/class-setup.php @@ -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 ); + } + + if ( empty( $result ) ) { + return; + } + + foreach ( $result as $user_id ) { + update_user_meta( $user_id, 'account_status', 'approved' ); + } + } } -} \ No newline at end of file +} diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 069be147..33fbaaa4 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -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,74 @@ 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; + } + + $old = get_user_meta( $object_id, $meta_key, true ); + + $values = array( $old, $_meta_value ); + foreach ( $values as $value ) { + delete_transient( "um_count_users_{$value}" ); + } + + $maybe_flush_pending = array_intersect( $values, array( 'awaiting_email_confirmation', 'awaiting_admin_review' ) ); + if ( ! empty( $maybe_flush_pending ) ) { + delete_transient( 'um_count_users_pending' ); + } + } + + + /** + * @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; + } + + delete_transient( "um_count_users_{$_meta_value}" ); + if ( in_array( $_meta_value, array( 'awaiting_email_confirmation', 'awaiting_admin_review' ), true ) ) { + delete_transient( 'um_count_users_pending' ); + } + } + + + /** + * @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 ); + delete_transient( "um_count_users_{$value}" ); + + if ( in_array( $value, array( 'awaiting_email_confirmation', 'awaiting_admin_review' ), true ) ) { + delete_transient( 'um_count_users_pending' ); + } } @@ -226,7 +291,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 +389,8 @@ 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' ); } @@ -391,60 +458,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 - * - * @example - * - */ - $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 * @@ -634,6 +647,7 @@ if ( ! class_exists( 'um\core\User' ) ) { do_action( 'um_user_register', $user_id, $_POST ); } + delete_transient( 'um_count_users_unassigned' ); } @@ -807,14 +821,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 +1059,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 ); @@ -2187,5 +2193,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' ); + } } } diff --git a/includes/core/rest/class-api.php b/includes/core/rest/class-api.php index ef8c3fb3..ba12fb9c 100644 --- a/includes/core/rest/class-api.php +++ b/includes/core/rest/class-api.php @@ -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 ); /** @@ -612,4 +612,4 @@ if ( ! class_exists( 'um\core\rest\API' ) ) { } } -} \ No newline at end of file +} diff --git a/includes/core/um-actions-register.php b/includes/core/um-actions-register.php index b170a89e..604f3177 100644 --- a/includes/core/um-actions-register.php +++ b/includes/core/um-actions-register.php @@ -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 ); diff --git a/uninstall.php b/uninstall.php index c1d6626a..70ad4733 100644 --- a/uninstall.php +++ b/uninstall.php @@ -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' ); + delete_transient( 'um_count_users_unassigned' ); + //remove all users cache UM()->user()->remove_cache_all_users();