ActionScheduler_ActionFactory::create()publicWC 1.0

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.

Method of the class: ActionScheduler_ActionFactory{}

No Hooks.

Return

Int. The action ID. Zero if there was an error scheduling the action.

Usage

$ActionScheduler_ActionFactory = new ActionScheduler_ActionFactory();
$ActionScheduler_ActionFactory->create( $options );
$options(array)

Describes the action we wish to schedule.

Default: array()

  • type(string)
    Must be one of 'async', 'cron', 'recurring', or 'single'.

  • hook(string)
    The hook to be executed.

  • arguments(array)
    Arguments to be passed to the callback.

  • group(string)
    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|string)
    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() code WC 9.3.1

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:
			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 ) {
		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;
}