ActionScheduler_DBStore::save_action_to_db
Save an action.
Method of the class: ActionScheduler_DBStore{}
Hooks from the method
Returns
Int. Action ID.
Usage
// private - for code of main (parent) class only $result = $this->save_action_to_db( $action, ?DateTime $date, $unique );
- $action(ActionScheduler_Action) (required)
- Action object.
- ?DateTime $date
- .
Default:null - $unique(true|false)
- Whether the action should be unique.
Default:false
ActionScheduler_DBStore::save_action_to_db() ActionScheduler DBStore::save action to db code WC 10.7.0
private function save_action_to_db( ActionScheduler_Action $action, ?DateTime $date = null, $unique = false ) {
global $wpdb;
try {
$this->validate_action( $action );
$data = array(
'hook' => $action->get_hook(),
'status' => ( $action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING ),
'scheduled_date_gmt' => $this->get_scheduled_date_string( $action, $date ),
'scheduled_date_local' => $this->get_scheduled_date_string_local( $action, $date ),
'schedule' => serialize( $action->get_schedule() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
'group_id' => current( $this->get_group_ids( $action->get_group() ) ),
'priority' => $action->get_priority(),
);
$args = wp_json_encode( $action->get_args() );
if ( strlen( $args ) <= static::$max_index_length ) {
$data['args'] = $args;
} else {
$data['args'] = $this->hash_args( $args );
$data['extended_args'] = $args;
}
$insert_sql = $this->build_insert_sql( $data, $unique );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_sql should be already prepared.
$wpdb->query( $insert_sql );
$action_id = $wpdb->insert_id;
if ( is_wp_error( $action_id ) ) {
throw new \RuntimeException( $action_id->get_error_message() );
} elseif ( empty( $action_id ) ) {
if ( $unique ) {
return 0;
}
throw new \RuntimeException( $wpdb->last_error ? $wpdb->last_error : __( 'Database error.', 'woocommerce' ) );
}
do_action( 'action_scheduler_stored_action', $action_id );
return $action_id;
} catch ( \Exception $e ) {
/* translators: %s: error message */
throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'woocommerce' ), $e->getMessage() ), 0 );
}
}