ActionScheduler_DBStore::claim_actions()protectedWC 1.0

Mark actions claimed.

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

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

Возвращает

int. The number of actions that were claimed.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->claim_actions( $claim_id, $limit, $before_date, $hooks, $group );
$claim_id(строка) (обязательный)
Claim Id.
$limit(int) (обязательный)
Number of action to include in claim.
$before_date(\DateTime)
Should use UTC timezone.
По умолчанию: null
$hooks(массив)
Hooks to filter for.
По умолчанию: array()
$group(строка)
Group to filter for.
По умолчанию: ''

Код ActionScheduler_DBStore::claim_actions() WC 7.7.0

protected function claim_actions( $claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '' ) {
	/** @var \wpdb $wpdb */
	global $wpdb;

	$now  = as_get_datetime_object();
	$date = is_null( $before_date ) ? $now : clone $before_date;

	// can't use $wpdb->update() because of the <= condition.
	$update = "UPDATE {$wpdb->actionscheduler_actions} SET claim_id=%d, last_attempt_gmt=%s, last_attempt_local=%s";
	$params = array(
		$claim_id,
		$now->format( 'Y-m-d H:i:s' ),
		current_time( 'mysql' ),
	);

	$where    = 'WHERE claim_id = 0 AND scheduled_date_gmt <= %s AND status=%s';
	$params[] = $date->format( 'Y-m-d H:i:s' );
	$params[] = self::STATUS_PENDING;

	if ( ! empty( $hooks ) ) {
		$placeholders = array_fill( 0, count( $hooks ), '%s' );
		$where        .= ' AND hook IN (' . join( ', ', $placeholders ) . ')';
		$params       = array_merge( $params, array_values( $hooks ) );
	}

	if ( ! empty( $group ) ) {

		$group_id = $this->get_group_id( $group, false );

		// throw exception if no matching group found, this matches ActionScheduler_wpPostStore's behaviour.
		if ( empty( $group_id ) ) {
			/* translators: %s: group name */
			throw new InvalidArgumentException( sprintf( __( 'The group "%s" does not exist.', 'woocommerce' ), $group ) );
		}

		$where    .= ' AND group_id = %d';
		$params[] = $group_id;
	}

	/**
	 * Sets the order-by clause used in the action claim query.
	 *
	 * @since 3.4.0
	 *
	 * @param string $order_by_sql
	 */
	$order    = apply_filters( 'action_scheduler_claim_actions_order_by', 'ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC' );
	$params[] = $limit;

	$sql           = $wpdb->prepare( "{$update} {$where} {$order} LIMIT %d", $params ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders
	$rows_affected = $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
	if ( false === $rows_affected ) {
		throw new \RuntimeException( __( 'Unable to claim actions. Database error.', 'woocommerce' ) );
	}

	return (int) $rows_affected;
}