- updated structure for action scheduler;

This commit is contained in:
Mykyta Synelnikov
2024-09-06 15:03:50 +03:00
parent 5385e07a5f
commit 6df83f4f09
9 changed files with 201 additions and 236 deletions
+2 -2
View File
@@ -22,8 +22,8 @@
"ext-fileinfo": "*",
"ext-curl": "*",
"ext-iconv": "*",
"woocommerce/action-scheduler": "3.2.1",
"composer/installers": "^2.3"
"composer/installers": "^2.3",
"woocommerce/action-scheduler": "3.2.1"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
+173 -14
View File
@@ -14,34 +14,193 @@ if ( ! class_exists( 'um\action_scheduler\Init' ) ) {
*/
class Init {
protected $enabled = false;
protected $default_group = 'ultimate-member';
// TODO: Do we need prefix for hook?.
public function __construct() {
$this->email();
if ( UM()->options()->get( 'enable_action_scheduler' ) ) {
$lib_path = UM_PATH . 'includes/lib/action-scheduler/action-scheduler.php';
if ( file_exists( $lib_path ) ) {
$this->enabled = true;
require_once $lib_path;
}
}
}
/**
* @return Email
* @since 2.6.x
* Enqueue an action to run one time, as soon as possible.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @return int Еhe actions ID. Zero if there was an error scheduling the action. The error will be sent to error_log
*/
public function email() {
if ( empty( UM()->classes['um\action_scheduler\email'] ) ) {
UM()->classes['um\action_scheduler\email'] = new Email();
public function enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
if ( $this->enabled ) {
$group = $this->set_group( $group );
return as_enqueue_async_action( $hook, $args, $group, $unique, $priority );
}
return UM()->classes['um\action_scheduler\email'];
do_action_ref_array( 'um_send_deleted_user_email', $args );
return 0;
}
/**
* @return Proxy
* @since 2.6.x
* Schedule an action to run one time at some defined point in the future.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array()
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.)
*
* @return int The actions ID. Zero if there was an error scheduling the action. The error will be sent to error_log.
*/
public function proxy() {
if ( empty( UM()->classes['um\action_scheduler\proxy'] ) ) {
UM()->classes['um\action_scheduler\proxy'] = new Proxy();
}
public function schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
$group = $this->set_group( $group );
return UM()->classes['um\action_scheduler\proxy'];
return as_schedule_single_action( $timestamp, $hook, $args, $group, $unique, $priority );
}
/**
* Schedule an action to run repeatedly with a specified interval in seconds.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param integer $interval_in_seconds Required. How long to wait between runs.
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @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->set_group( $group );
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group, $unique, $priority );
}
/**
* Schedule an action that recurs on a cron-like schedule.
*
* If execution of a cron-like action is delayed, the next attempt will still be scheduled according to the provided cron expression.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param string $schedule Required. A cron-like schedule string, see http://en.wikipedia.org/wiki/Cron.
* @param string $hook Required Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @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->set_group( $group );
return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group, $unique, $priority );
}
/**
* Cancel the next occurrence of a scheduled action.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @return int|null
*/
public function unschedule_action( $hook, $args = array(), $group = '' ) {
$group = $this->set_group( $group );
return as_unschedule_action( $hook, $args, $group );
}
/**
* Cancel all occurrences of a scheduled action.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_unschedule_all_actions( $hook, $args, $group );
}
/**
* Returns the next timestamp for a scheduled action.
*
* @param string $hook Required. Name of the action hook. Default: none.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_next_scheduled_action( $hook, $args, $group );
}
/**
* Check if there is a scheduled action in the queue, but more efficiently than as_next_scheduled_action().
* Its recommended to use this function when you need to know whether a specific action is currently scheduled.
*
* @param string $hook Required. Name of the action hook. Default: none.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_has_scheduled_action( $hook, $args, $group );
}
/**
* @param array $args $args (array) Arguments to search and filter results by. Possible arguments, with their default values:
* 'hook' => '' - the name of the action that will be triggered
* 'args' => NULL - the args array that will be passed with the action
* 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime().
* 'date_compare' => '<= - operator for testing “date”. accepted values are !=, >, >=, <, <=, =
* 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime().
* 'modified_compare' => '<=' - operator for testing “modified”. accepted values are !=, >, >=, <, <=, =
* 'group' => '' - the group the action belongs to
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
* 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
* 'per_page' => 5 - Number of results to return
* 'offset' => 0
* 'orderby' => 'date' - accepted values are hook, group, modified, or date
* 'order' => 'ASC'
* @param string $return_format The format in which to return the scheduled actions: 'OBJECT', 'ARRAY_A', or 'ids'. Default: 'OBJECT'.
*
* @return array Array of action rows matching the criteria specified with $args.
*/
public function get_scheduled_actions( $args, $return_format = 'OBJECT' ) {
if ( ! empty( $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;
}
}
}
}
-201
View File
@@ -1,201 +0,0 @@
<?php
namespace um\action_scheduler;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\action_scheduler\Proxy' ) ) {
/**
* Class Action_Scheduler
*
* Wrapper for action-scheduler
*
* @package um\action_scheduler
*/
class Proxy {
protected $default_group = 'ultimate-member';
// TODO: Do we need prefix for hook?.
/**
* Action_Scheduler constructor.
*/
public function __construct() {
$lib_path = UM_PATH . 'includes/lib/action-scheduler/action-scheduler.php';
if ( file_exists( $lib_path ) ) {
require_once UM_PATH . 'includes/lib/action-scheduler/action-scheduler.php';
}
}
/**
* Enqueue an action to run one time, as soon as possible.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @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->set_group( $group );
return as_enqueue_async_action( $hook, $args, $group, $unique, $priority );
}
/**
* Schedule an action to run one time at some defined point in the future.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array()
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.)
*
* @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->set_group( $group );
return as_schedule_single_action( $timestamp, $hook, $args, $group, $unique, $priority );
}
/**
* Schedule an action to run repeatedly with a specified interval in seconds.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param integer $interval_in_seconds Required. How long to wait between runs.
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @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->set_group( $group );
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group, $unique, $priority );
}
/**
* Schedule an action that recurs on a cron-like schedule.
*
* If execution of a cron-like action is delayed, the next attempt will still be scheduled according to the provided cron expression.
*
* @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run.
* @param string $schedule Required. A cron-like schedule string, see http://en.wikipedia.org/wiki/Cron.
* @param string $hook Required Name of the action hook.
* @param array $args Arguments to pass to callbacks when the hook triggers. Default: array().
* @param string $group The group to assign this job to. Default: ''.
* @param boolean $unique Whether the action should be unique. Default: false.
* @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
*
* @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->set_group( $group );
return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group, $unique, $priority );
}
/**
* Cancel the next occurrence of a scheduled action.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @return int|null
*/
public function unschedule_action( $hook, $args = array(), $group = '' ) {
$group = $this->set_group( $group );
return as_unschedule_action( $hook, $args, $group );
}
/**
* Cancel all occurrences of a scheduled action.
*
* @param string $hook Required. Name of the action hook.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_unschedule_all_actions( $hook, $args, $group );
}
/**
* Returns the next timestamp for a scheduled action.
*
* @param string $hook Required. Name of the action hook. Default: none.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_next_scheduled_action( $hook, $args, $group );
}
/**
* Check if there is a scheduled action in the queue, but more efficiently than as_next_scheduled_action().
* Its recommended to use this function when you need to know whether a specific action is currently scheduled.
*
* @param string $hook Required. Name of the action hook. Default: none.
* @param array $args Arguments passed to callbacks when the hook triggers. Default: array().
* @param string $group The group the job is assigned to. Default: ''.
*
* @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->set_group( $group );
return as_has_scheduled_action( $hook, $args, $group );
}
/**
* @param array $args $args (array) Arguments to search and filter results by. Possible arguments, with their default values:
* 'hook' => '' - the name of the action that will be triggered
* 'args' => NULL - the args array that will be passed with the action
* 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime().
* 'date_compare' => '<= - operator for testing “date”. accepted values are !=, >, >=, <, <=, =
* 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime().
* 'modified_compare' => '<=' - operator for testing “modified”. accepted values are !=, >, >=, <, <=, =
* 'group' => '' - the group the action belongs to
* 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
* 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
* 'per_page' => 5 - Number of results to return
* 'offset' => 0
* 'orderby' => 'date' - accepted values are hook, group, modified, or date
* 'order' => 'ASC'
* @param string $return_format The format in which to return the scheduled actions: 'OBJECT', 'ARRAY_A', or 'ids'. Default: 'OBJECT'.
*
* @return array Array of action rows matching the criteria specified with $args.
*/
public function get_scheduled_actions( $args, $return_format = 'OBJECT' ) {
if ( ! empty( $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;
}
}
}
}
+1 -1
View File
@@ -1006,7 +1006,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
'sanitize' => 'bool',
),
'enable_action_scheduler' => array(
'sanitize' => 'bool',
'sanitize' => 'bool',
),
'rest_api_version' => array(
'sanitize' => 'text',
+1
View File
@@ -732,6 +732,7 @@ if ( ! class_exists( 'um\Config' ) ) {
'secure_notify_admins_banned_accounts__interval' => 'instant',
'secure_allowed_redirect_hosts' => '',
'delete_comments' => false,
'enable_action_scheduler' => false,
);
add_filter( 'um_get_tabs_from_config', '__return_true' );
+2 -5
View File
@@ -503,10 +503,7 @@ if ( ! class_exists( 'UM' ) ) {
*/
public function includes() {
$this->action_scheduler();
if ( $this->options()->get( 'enable_action_scheduler' ) ) {
$this->action_scheduler()->proxy();
}
$this->maybe_action_scheduler();
$this->common()->includes();
@@ -1465,7 +1462,7 @@ if ( ! class_exists( 'UM' ) ) {
*
* @return um\action_scheduler\Init
*/
public function action_scheduler() {
public function maybe_action_scheduler() {
if ( empty( $this->classes['action_scheduler'] ) ) {
$this->classes['action_scheduler'] = new um\action_scheduler\Init();
}
@@ -1,21 +1,21 @@
<?php
namespace um\action_scheduler;
namespace um\common\actions;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\action_scheduler\Email' ) ) {
if ( ! class_exists( 'um\common\actions\Emails' ) ) {
/**
* Class Email
* Class Emails
*
* @package um\action_scheduler
* @package um\common\actions
*/
class Email {
class Emails {
public function __construct() {
add_action( 'um_send_deleted_user_email', array( $this, 'send_deleted_user_email' ), 10, 2 );
add_action( 'um_send_deleted_user_email', array( $this, 'send' ), 10, 3 );
}
/**
@@ -23,13 +23,14 @@ if ( ! class_exists( 'um\action_scheduler\Email' ) ) {
*
* @param string $user_email User email.
* @param string $template Template name.
* @param array $args Email additional arguments.
*/
public function send_deleted_user_email( $user_email, $template ) {
public function send( $user_email, $template, $args = array() ) {
if ( empty( $user_email ) && empty( $template ) ) {
return;
}
UM()->mail()->send( $user_email, $template );
UM()->mail()->send( $user_email, $template, $args );
}
}
}
+12
View File
@@ -20,6 +20,8 @@ if ( ! class_exists( 'um\common\Init' ) ) {
* @used-by \UM::includes()
*/
public function includes() {
$this->actions();
$this->cpt()->hooks();
$this->screen();
$this->secure()->hooks();
@@ -27,6 +29,16 @@ if ( ! class_exists( 'um\common\Init' ) ) {
$this->theme()->hooks();
}
/**
* @since 2.6.8
*
*/
private function actions() {
if ( empty( UM()->classes['um\common\actions\emails'] ) ) {
UM()->classes['um\common\actions\emails'] = new actions\Emails();
}
}
/**
* @since 2.6.8
*
+1 -5
View File
@@ -628,11 +628,7 @@ if ( ! class_exists( 'um\core\User' ) ) {
$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()->maybe_action_scheduler()->enqueue_async_action( 'um_send_deleted_user_email', array( $user_email, $template ) );
$emails = um_multi_admin_email();
if ( ! empty( $emails ) ) {