From 54deffd24498dbce6b681246582bb0c6cc1790f8 Mon Sep 17 00:00:00 2001 From: Mykyta Synelnikov Date: Tue, 22 Apr 2025 17:16:55 +0300 Subject: [PATCH] Enhance password reset logic and add Action Scheduler filter Ensure proper handling of user data in password reset functions by adding checks and updating parameter handling. Introduce a new filter to extend site health information and include a setting for enabling email sending via Action Scheduler. Improve code clarity with updated comments and function annotations. --- includes/action-scheduler/class-init.php | 13 +++++++++++++ includes/admin/class-site-health.php | 14 +++++++++++++- includes/core/class-password.php | 6 +++++- includes/core/class-user.php | 15 ++++++++++----- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/includes/action-scheduler/class-init.php b/includes/action-scheduler/class-init.php index 0c2356fe..27974be6 100644 --- a/includes/action-scheduler/class-init.php +++ b/includes/action-scheduler/class-init.php @@ -43,6 +43,7 @@ if ( ! class_exists( 'um\action_scheduler\Init' ) ) { public function __construct() { if ( ! $this->can_be_active() ) { add_action( 'init', array( $this, 'add_notice' ) ); + add_filter( 'um_site_health_extend', array( $this, 'change_site_health' ) ); } else { add_filter( 'um_settings_structure', array( $this, 'add_setting' ) ); // Since 2.10.3 we load library as soon as there are required library files. @@ -67,6 +68,18 @@ if ( ! class_exists( 'um\action_scheduler\Init' ) ) { ); } + /** + * Adds the Action Scheduler setting to Ultimate Member feature settings + * + * @param array $settings + * + * @return array + */ + public function change_site_health( $site_health ) { + $site_health['ultimate-member']['fields']['enable_as_email_sending']['value'] = __( 'No', 'ultimate-member' ); + return $site_health; + } + /** * Adds the Action Scheduler setting to Ultimate Member feature settings * diff --git a/includes/admin/class-site-health.php b/includes/admin/class-site-health.php index bdcd8b9f..7a8b7f1b 100644 --- a/includes/admin/class-site-health.php +++ b/includes/admin/class-site-health.php @@ -1238,6 +1238,10 @@ class Site_Health { 'label' => __( 'Remove Data on Uninstall?', 'ultimate-member' ), 'value' => UM()->options()->get( 'uninstall_on_delete' ) ? $labels['yes'] : $labels['no'], ), + 'enable_as_email_sending' => array( + 'label' => __( 'Email sending by Action Scheduler', 'ultimate-member' ), + 'value' => UM()->options()->get( 'enable_as_email_sending' ) ? $labels['yes'] : $labels['no'], + ), ); // Secure settings @@ -2549,6 +2553,14 @@ class Site_Health { } } - return $info; + /** + * Filters the site health information. + * @hook um_site_health_extend + * @since 2.10.3 + * @param {array} info - The site health info to be filtered. + * + * @return {array} The filtered site health info. + */ + return apply_filters( 'um_site_health_extend', $info ); } } diff --git a/includes/core/class-password.php b/includes/core/class-password.php index 5690e046..fcecc441 100644 --- a/includes/core/class-password.php +++ b/includes/core/class-password.php @@ -48,6 +48,10 @@ if ( ! class_exists( 'um\core\Password' ) ) { // New reset password key via WordPress native field. It maybe already exists here but generated twice to make sure that emailed with a proper and fresh hash. // But doing that only once in 1 request using static variable. Different email placeholders can use reset_url() and we have to use 1 time generated to avoid invalid keys. $user_data = get_userdata( $user_id ); + if ( empty( $user_data ) ) { + return ''; + } + if ( empty( $reset_key ) ) { $reset_key = UM()->user()->maybe_generate_password_reset_key( $user_data ); } @@ -466,7 +470,7 @@ if ( ! class_exists( 'um\core\Password' ) ) { um_fetch_user( $data->ID ); if ( false === UM()->options()->get( 'only_approved_user_reset_password' ) || UM()->common()->users()->has_status( $data->ID, 'approved' ) ) { - UM()->user()->password_reset(); + UM()->user()->password_reset( $data->ID ); } } diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 66baf197..6053eefd 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -1484,22 +1484,27 @@ if ( ! class_exists( 'um\core\User' ) ) { return get_password_reset_key( $userdata ); } - /** * Password reset email + * + * @param int|null $user_id + * + * @return void */ - function password_reset() { - $userdata = get_userdata( um_user( 'ID' ) ); + public function password_reset( $user_id = null ) { + if ( is_null( $user_id ) ) { + $user_id = um_user( 'ID' ); + } + $userdata = get_userdata( $user_id ); $this->maybe_generate_password_reset_key( $userdata ); add_filter( 'um_template_tags_patterns_hook', array( UM()->password(), 'add_placeholder' ) ); add_filter( 'um_template_tags_replaces_hook', array( UM()->password(), 'add_replace_placeholder' ) ); - UM()->maybe_action_scheduler()->enqueue_async_action( 'um_dispatch_email', array( $userdata->user_email, 'resetpw_email', array( 'fetch_user_id' => um_user( 'ID' ) ) ) ); + UM()->maybe_action_scheduler()->enqueue_async_action( 'um_dispatch_email', array( $userdata->user_email, 'resetpw_email', array( 'fetch_user_id' => $user_id ) ) ); } - /** * Password changed email *