diff --git a/composer.json b/composer.json index c582a216..39103a3d 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/includes/action-scheduler/class-init.php b/includes/action-scheduler/class-init.php index 489df57f..b6020419 100644 --- a/includes/action-scheduler/class-init.php +++ b/includes/action-scheduler/class-init.php @@ -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 action’s 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 action’s 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 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->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 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->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(). + * It’s 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; + } } } } diff --git a/includes/action-scheduler/class-proxy.php b/includes/action-scheduler/class-proxy.php deleted file mode 100644 index 55050c67..00000000 --- a/includes/action-scheduler/class-proxy.php +++ /dev/null @@ -1,201 +0,0 @@ -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 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->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 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->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 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->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(). - * It’s 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; - } - } - } -} diff --git a/includes/admin/core/class-admin-settings.php b/includes/admin/core/class-admin-settings.php index fd256b9c..af46bd2e 100644 --- a/includes/admin/core/class-admin-settings.php +++ b/includes/admin/core/class-admin-settings.php @@ -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', diff --git a/includes/class-config.php b/includes/class-config.php index 5391f9c1..547e3772 100644 --- a/includes/class-config.php +++ b/includes/class-config.php @@ -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' ); diff --git a/includes/class-init.php b/includes/class-init.php index 04983ba5..dbf13815 100644 --- a/includes/class-init.php +++ b/includes/class-init.php @@ -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(); } diff --git a/includes/action-scheduler/class-email.php b/includes/common/actions/class-emails.php similarity index 54% rename from includes/action-scheduler/class-email.php rename to includes/common/actions/class-emails.php index 9a014fe4..b82ec101 100644 --- a/includes/action-scheduler/class-email.php +++ b/includes/common/actions/class-emails.php @@ -1,21 +1,21 @@ mail()->send( $user_email, $template ); + UM()->mail()->send( $user_email, $template, $args ); } } } diff --git a/includes/common/class-init.php b/includes/common/class-init.php index 1e9e74cd..d9c7f5f7 100644 --- a/includes/common/class-init.php +++ b/includes/common/class-init.php @@ -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 * diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 9569e402..a95b3f94 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -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 ) ) {