ActionScheduler_DBStore::save_action_to_db()privateWC 1.0

Save an action.

Метод класса: ActionScheduler_DBStore{}

Хуки из метода

Возвращает

int. Action ID.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->save_action_to_db( $action, $date, $unique );
$action(ActionScheduler_Action) (обязательный)
Action object.
$date(?DateTime)
Optional schedule date.
По умолчанию: null
$unique(true|false)
Whether the action should be unique.
По умолчанию: false

Код ActionScheduler_DBStore::save_action_to_db() WC 8.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 );
	}
}