Automattic\WooCommerce\Internal\PushNotifications\DataStores

PushTokensDataStore::record_last_sent_at │ public │ WC 11.2.0

Records that the given tokens were successfully sent to WPCOM.

Buffers the stamps and writes them once on shutdown rather than per call. A single request often processes several notifications — the loopback receives every notification a store event produced, and a bulk order update can produce dozens — and each one would otherwise repeat the same write against the same handful of tokens. Buffering makes the cost a function of the request rather than of the notification count, while each token still keeps the exact time of its own most recent send.

Method of the class: PushTokensDataStore{}

No Hooks.

Returns

void. Nothing (null).

Usage

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->record_last_sent_at( $push_tokens ): void;
$push_tokens(PushToken[]) (required)
The tokens WPCOM accepted.

Changelog

Since 11.2.0 Introduced.

PushTokensDataStore::record_last_sent_at() code WC 11.3.0

public function record_last_sent_at( array $push_tokens ): void {
	$timestamp = gmdate( 'Y-m-d H:i:s' );

	foreach ( $push_tokens as $push_token ) {
		$id = $push_token->get_id();

		if ( $id ) {
			$this->pending_last_sent_at[ $id ] = $timestamp;
		}
	}

	if ( empty( $this->pending_last_sent_at ) || $this->last_sent_at_flush_registered ) {
		return;
	}

	add_action( 'shutdown', array( $this, 'flush_last_sent_at' ) );

	// The safety net and retry jobs run under an Action Scheduler queue
	// runner, which is routinely killed on a time limit. Shutdown functions
	// do not run on a kill, so flush after each action as well.
	add_action( 'action_scheduler_after_execute', array( $this, 'flush_last_sent_at' ) );

	$this->last_sent_at_flush_registered = true;
}