From ec18f52dd9598080d58cdfb784fc833e97a1e2d3 Mon Sep 17 00:00:00 2001 From: Yurii Nalivaiko Date: Wed, 4 Sep 2024 18:03:22 +0200 Subject: [PATCH] Added Action Scheduler --- includes/action-scheduler/class-email.php | 30 ++++++++++++ includes/action-scheduler/class-init.php | 48 +++++++++++++++++++ .../class-proxy.php} | 36 ++++++++------ includes/class-init.php | 19 ++++++++ includes/common/class-init.php | 13 ----- includes/core/class-user.php | 10 +++- 6 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 includes/action-scheduler/class-email.php create mode 100644 includes/action-scheduler/class-init.php rename includes/{common/class-action-scheduler.php => action-scheduler/class-proxy.php} (92%) diff --git a/includes/action-scheduler/class-email.php b/includes/action-scheduler/class-email.php new file mode 100644 index 00000000..05299759 --- /dev/null +++ b/includes/action-scheduler/class-email.php @@ -0,0 +1,30 @@ +mail()->send( $user_email, $template ); + } + } +} diff --git a/includes/action-scheduler/class-init.php b/includes/action-scheduler/class-init.php new file mode 100644 index 00000000..b05930a2 --- /dev/null +++ b/includes/action-scheduler/class-init.php @@ -0,0 +1,48 @@ +email(); + } + + /** + * @return Email + * @since 2.6.x + * + */ + public function email() { + if ( empty( UM()->classes['um\action_scheduler\email'] ) ) { + UM()->classes['um\action_scheduler\email'] = new Email(); + } + + return UM()->classes['um\action_scheduler\email']; + } + + /** + * @return Proxy + * @since 2.6.x + * + */ + public function proxy() { + if ( empty( UM()->classes['um\action_scheduler\proxy'] ) ) { + UM()->classes['um\action_scheduler\proxy'] = new Proxy(); + } + + return UM()->classes['um\action_scheduler\proxy']; + } + } +} diff --git a/includes/common/class-action-scheduler.php b/includes/action-scheduler/class-proxy.php similarity index 92% rename from includes/common/class-action-scheduler.php rename to includes/action-scheduler/class-proxy.php index 421a8adf..07069e70 100644 --- a/includes/common/class-action-scheduler.php +++ b/includes/action-scheduler/class-proxy.php @@ -1,23 +1,23 @@ prefix . $group; + $group = $this->set_group( $group ); return as_enqueue_async_action( $hook, $args, $group, $unique, $priority ); } @@ -56,7 +56,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. */ public function schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_schedule_single_action( $timestamp, $hook, $args, $group, $unique, $priority ); } @@ -75,7 +75,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. */ public function schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group, $unique, $priority ); } @@ -96,7 +96,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. */ public function schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group, $unique, $priority ); } @@ -111,7 +111,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return int|null */ public function unschedule_action( $hook, $args = array(), $group = '' ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_unschedule_action( $hook, $args, $group ); } @@ -126,7 +126,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return string|null The scheduled action ID if a scheduled action was found, or null if no matching action found. */ public function unschedule_all_actions( $hook, $args = array(), $group = '' ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_unschedule_all_actions( $hook, $args, $group ); } @@ -141,7 +141,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return int|bool The timestamp for the next occurrence of a pending scheduled action, true for an async or in-progress action or false if there is no matching action. */ public function next_scheduled_action( $hook, $args = array(), $group = '' ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_next_scheduled_action( $hook, $args, $group ); } @@ -157,7 +157,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { * @return bool True if a matching action is pending or in-progress, false otherwise. */ public function has_scheduled_action( $hook, $args = array(), $group = '' ) { - $group = $this->prefix . $group; + $group = $this->set_group( $group ); return as_has_scheduled_action( $hook, $args, $group ); } @@ -183,9 +183,17 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) { */ public function get_scheduled_actions( $args, $return_format = 'OBJECT' ) { if ( ! empty( $args['group'] ) ) { - $args['group'] = $this->prefix . $args['group']; + $args['group'] = $this->set_group( $args['group'] ); } return as_get_scheduled_actions( $args, $return_format ); } + + public function set_group( $group ) { + if ( empty( $group ) ) { + return $this->default_group; + } else { + return $this->default_group . '_' . $group; + } + } } } diff --git a/includes/class-init.php b/includes/class-init.php index efa6cf07..04983ba5 100644 --- a/includes/class-init.php +++ b/includes/class-init.php @@ -1,4 +1,5 @@ action_scheduler(); + if ( $this->options()->get( 'enable_action_scheduler' ) ) { + $this->action_scheduler()->proxy(); + } + $this->common()->includes(); + $this->access(); if ( $this->is_request( 'ajax' ) ) { @@ -1453,6 +1460,18 @@ if ( ! class_exists( 'UM' ) ) { return $this->classes['multisite']; } + /** + * @since 2.6.8 + * + * @return um\action_scheduler\Init + */ + public function action_scheduler() { + if ( empty( $this->classes['action_scheduler'] ) ) { + $this->classes['action_scheduler'] = new um\action_scheduler\Init(); + } + return $this->classes['action_scheduler']; + } + /** * Include files with hooked filters/actions diff --git a/includes/common/class-init.php b/includes/common/class-init.php index a4a61111..1e9e74cd 100644 --- a/includes/common/class-init.php +++ b/includes/common/class-init.php @@ -25,7 +25,6 @@ if ( ! class_exists( 'um\common\Init' ) ) { $this->secure()->hooks(); $this->site_health(); $this->theme()->hooks(); - $this->action_scheduler(); } /** @@ -87,17 +86,5 @@ if ( ! class_exists( 'um\common\Init' ) ) { } return UM()->classes['um\common\theme']; } - - /** - * @since 2.6.8 - * - * @return Action_Scheduler - */ - public function action_scheduler() { - if ( empty( UM()->classes['um\common\action_scheduler'] ) ) { - UM()->classes['um\common\action_scheduler'] = new Action_Scheduler(); - } - return UM()->classes['um\common\action_scheduler']; - } } } diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 914a82b2..f098ff3d 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -625,7 +625,15 @@ if ( ! class_exists( 'um\core\User' ) ) { // send email notifications if ( $this->send_mail_on_delete ) { - UM()->mail()->send( um_user( 'user_email' ), 'deletion_email' ); + $user_email = um_user( 'user_email' ); + $template = 'deletion_email'; + + if ( UM()->options()->get( 'enable_action_scheduler' ) ) { + UM()->action_scheduler()->proxy()->enqueue_async_action( 'um_send_deleted_user_email', array( $user_email, $template ) ); + } else { + do_action( 'um_send_deleted_user_email', $user_email, $template ); +// UM()->mail()->send( $user_email, $template ); + } $emails = um_multi_admin_email(); if ( ! empty( $emails ) ) {