ActionScheduler_wpPostStore::claim_actions()protectedWC 1.0

Claim actions.

Method of the class: ActionScheduler_wpPostStore{}

No Hooks.

Return

Int. The number of actions that were claimed.

Usage

// protected - for code of main (parent) or child class
$result = $this->claim_actions( $claim_id, $limit, $before_date, $hooks, $group );
$claim_id(string) (required)
Claim ID.
$limit(int) (required)
Limit.
$before_date(DateTime)
Should use UTC timezone.
Default: null
$hooks(array)
Claim only actions with a hook or hooks.
Default: array()
$group(string)
Claim only actions in the given group.
Default: ''

ActionScheduler_wpPostStore::claim_actions() code WC 8.6.1

protected function claim_actions( $claim_id, $limit, DateTime $before_date = null, $hooks = array(), $group = '' ) {
	// Set up initial variables.
	$date      = null === $before_date ? as_get_datetime_object() : clone $before_date;
	$limit_ids = ! empty( $group );
	$ids       = $limit_ids ? $this->get_actions_by_group( $group, $limit, $date ) : array();

	// If limiting by IDs and no posts found, then return early since we have nothing to update.
	if ( $limit_ids && 0 === count( $ids ) ) {
		return 0;
	}

	/**
	 * Global wpdb object.
	 *
	 * @var wpdb $wpdb
	 */
	global $wpdb;

	/*
	 * Build up custom query to update the affected posts. Parameters are built as a separate array
	 * to make it easier to identify where they are in the query.
	 *
	 * We can't use $wpdb->update() here because of the "ID IN ..." clause.
	 */
	$update = "UPDATE {$wpdb->posts} SET post_password = %s, post_modified_gmt = %s, post_modified = %s";
	$params = array(
		$claim_id,
		current_time( 'mysql', true ),
		current_time( 'mysql' ),
	);

	// Build initial WHERE clause.
	$where    = "WHERE post_type = %s AND post_status = %s AND post_password = ''";
	$params[] = self::POST_TYPE;
	$params[] = ActionScheduler_Store::STATUS_PENDING;

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

	/*
	 * Add the IDs to the WHERE clause. IDs not escaped because they came directly from a prior DB query.
	 *
	 * If we're not limiting by IDs, then include the post_date_gmt clause.
	 */
	if ( $limit_ids ) {
		$where .= ' AND ID IN (' . join( ',', $ids ) . ')';
	} else {
		$where   .= ' AND post_date_gmt <= %s';
		$params[] = $date->format( 'Y-m-d H:i:s' );
	}

	// Add the ORDER BY clause and,ms limit.
	$order    = 'ORDER BY menu_order ASC, post_date_gmt ASC, ID ASC LIMIT %d';
	$params[] = $limit;

	// Run the query and gather results.
	$rows_affected = $wpdb->query( $wpdb->prepare( "{$update} {$where} {$order}", $params ) ); // phpcs:ignore // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare

	if ( false === $rows_affected ) {
		throw new RuntimeException( __( 'Unable to claim actions. Database error.', 'woocommerce' ) );
	}

	return (int) $rows_affected;
}