ActionScheduler_ActionFactory::create
Creates a scheduled action.
This general purpose method can be used in place of specific methods such as async(), async_unique(), single() or single_unique(), etc.
Метод класса: ActionScheduler_ActionFactory{}
Хуков нет.
Возвращает
int. The action ID. Zero if there was an error scheduling the action.
Использование
$ActionScheduler_ActionFactory = new ActionScheduler_ActionFactory(); $ActionScheduler_ActionFactory->create( $options );
- $options(массив)
Describes the action we wish to schedule.
По умолчанию: array()
-
type(строка)
Must be one of 'async', 'cron', 'recurring', or 'single'. -
hook(строка)
The hook to be executed. -
arguments(массив)
Arguments to be passed to the callback. -
group(строка)
The action group. -
unique(true|false)
If the action should be unique. -
when(int)
Timestamp. Indicates when the action, or first instance of the action in the case of recurring or cron actions, becomes due. -
pattern(int|строка)
Recurrence pattern. This is either an interval in seconds for recurring actions or a cron expression for cron actions. - priority(int)
Lower values means higher priority. Should be in the range 0-255.
-
Код ActionScheduler_ActionFactory::create() ActionScheduler ActionFactory::create WC 10.4.3
public function create( array $options = array() ) {
$defaults = array(
'type' => 'single',
'hook' => '',
'arguments' => array(),
'group' => '',
'unique' => false,
'when' => time(),
'pattern' => null,
'priority' => 10,
);
$options = array_merge( $defaults, $options );
// Cron/recurring actions without a pattern are treated as single actions (this gives calling code the ability
// to use functions like as_schedule_recurring_action() to schedule recurring as well as single actions).
if ( ( 'cron' === $options['type'] || 'recurring' === $options['type'] ) && empty( $options['pattern'] ) ) {
$options['type'] = 'single';
}
switch ( $options['type'] ) {
case 'async':
$schedule = new ActionScheduler_NullSchedule();
break;
case 'cron':
$date = as_get_datetime_object( $options['when'] );
$cron = CronExpression::factory( $options['pattern'] );
$schedule = new ActionScheduler_CronSchedule( $date, $cron );
break;
case 'recurring':
$date = as_get_datetime_object( $options['when'] );
$schedule = new ActionScheduler_IntervalSchedule( $date, $options['pattern'] );
break;
case 'single':
$date = as_get_datetime_object( $options['when'] );
$schedule = new ActionScheduler_SimpleSchedule( $date );
break;
default:
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." );
return 0;
}
$action = new ActionScheduler_Action( $options['hook'], $options['arguments'], $schedule, $options['group'] );
$action->set_priority( $options['priority'] );
$action_id = 0;
try {
$action_id = $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action );
} catch ( Exception $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log(
sprintf(
/* translators: %1$s is the name of the hook to be enqueued, %2$s is the exception message. */
__( 'Caught exception while enqueuing action "%1$s": %2$s', 'woocommerce' ),
$options['hook'],
$e->getMessage()
)
);
}
return $action_id;
}