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.

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

Хуков нет.

Возвращает

int. The number of tokens deleted.

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

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->delete_for_user( $user_id ): int;
$user_id(int) (обязательный)
The user whose tokens should be deleted.

Список изменений

С версии 11.2.0 Введена.

Код PushTokensDataStore::delete_for_user() WC 11.2.2

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();

	return $deleted;
}