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 9.0.2

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' ),
	);

	// Set claim filters.
	if ( ! empty( $hooks ) ) {
		$this->set_claim_filter( 'hooks', $hooks );
	} else {
		$hooks = $this->get_claim_filter( 'hooks' );
	}
	if ( ! empty( $group ) ) {
		$this->set_claim_filter( 'group', $group );
	} else {
		$group = $this->get_claim_filter( 'group' );
	}

	$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 ) );
	}

	$group_operator = 'IN';
	if ( empty( $group ) ) {
		$group = $this->get_claim_filter( 'exclude-groups' );
		$group_operator = 'NOT IN';
	}

	if ( ! empty( $group ) ) {
		$group_ids = $this->get_group_ids( $group, false );

		// throw exception if no matching group(s) found, this matches ActionScheduler_wpPostStore's behaviour.
		if ( empty( $group_ids ) ) {
			throw new InvalidArgumentException(
				sprintf(
					/* translators: %s: group name(s) */
					_n(
						'The group "%s" does not exist.',
						'The groups "%s" do not exist.',
						is_array( $group ) ? count( $group ) : 1,
						'woocommerce'
					),
					$group
				)
			);
		}

		$id_list = implode( ',', array_map( 'intval', $group_ids ) );
		$where   .= " AND group_id {$group_operator} ( $id_list )";
	}

	/**
	 * 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 priority ASC, 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 ) {
		$error = empty( $wpdb->last_error )
			? _x( 'unknown', 'database error', 'woocommerce' )
			: $wpdb->last_error;

		throw new \RuntimeException(
			sprintf(
				/* translators: %s database error. */
				__( 'Unable to claim actions. Database error: %s.', 'woocommerce' ),
				$error
			)
		);
	}

	return (int) $rows_affected;
}