Automattic\WooCommerce\Internal\PushNotifications\DataStores

PushTokensDataStore::delete_for_user │ public │ WC 11.2.0

Deletes every push token belonging to a user.

A non-positive ID is refused rather than queried, so a caller that loses the user ID cannot match the rows of every author-less token at once.

Method of the class: PushTokensDataStore{}

No Hooks.

Returns

int. The number of tokens deleted.

Usage

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->delete_for_user( $user_id ): int;
$user_id(int) (required)
The user whose tokens should be deleted.

Changelog

Since 11.2.0 Introduced.

PushTokensDataStore::delete_for_user() code WC 11.3.0

public function delete_for_user( int $user_id ): int {
	if ( $user_id < 1 ) {
		return 0;
	}

	global $wpdb;

	// Direct query so pre_get_posts filters cannot hide a token, and any status is deleted.
	$post_ids = $wpdb->get_col(
		$wpdb->prepare(
			"SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_author = %d",
			PushToken::POST_TYPE,
			$user_id
		)
	);

	if ( empty( $post_ids ) ) {
		return 0;
	}

	$deleted = 0;

	foreach ( $post_ids as $post_id ) {
		if ( wp_delete_post( (int) $post_id, true ) ) {
			++$deleted;
		}
	}

	// Anything read earlier in this request now includes deleted tokens.
	$this->tokens_by_roles_cache = array();
	$this->token_count           = null;

	return $deleted;
}