Added Action Scheduler

This commit is contained in:
Yurii Nalivaiko
2024-09-04 18:03:22 +02:00
parent f2d4c6135a
commit ec18f52dd9
6 changed files with 128 additions and 28 deletions
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace um\action_scheduler;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\action_scheduler\Email' ) ) {
class Email {
public function __construct() {
add_action( 'um_send_deleted_user_email', array( $this, 'send_deleted_user_email' ), 10, 2 );
}
/**
* Send an email after user account was deleted.
*
* @param string $user_email User email.
* @param string $template Template name.
*/
public function send_deleted_user_email( $user_email, $template ) {
if ( empty( $user_email ) && empty( $template ) ) {
return;
}
UM()->mail()->send( $user_email, $template );
}
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace um\action_scheduler;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\action_scheduler\Init' ) ) {
/**
* Class Init
*
* @package um\action_scheduler
*/
class Init {
public function __construct() {
$this->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'];
}
}
}
@@ -1,23 +1,23 @@
<?php
namespace um\common;
namespace um\action_scheduler;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\common\Action_Scheduler' ) ) {
if ( ! class_exists( 'um\Action_Scheduler\Proxy' ) ) {
/**
* Class Action_Scheduler
*
* Wrapper for action-scheduler
*
* @package um\common
* @package um\action_scheduler
*/
class Action_Scheduler {
class Proxy {
protected $prefix = 'um_';
protected $default_group = 'ultimate-member';
// TODO: Do we need prefix for hook?.
/**
@@ -39,7 +39,7 @@ if ( ! class_exists( 'um\common\Action_Scheduler' ) ) {
* @return int Еhe actions ID. Zero if there was an error scheduling the action. The error will be sent to error_log
*/
public function enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
$group = $this->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 actions 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 actions 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 actions 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;
}
}
}
}
+19
View File
@@ -1,4 +1,5 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@@ -502,7 +503,13 @@ if ( ! class_exists( 'UM' ) ) {
*/
public function includes() {
$this->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
-13
View File
@@ -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'];
}
}
}
+9 -1
View File
@@ -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 ) ) {