Automattic\WooCommerce\Internal\PushNotifications\DataStores

PushTokensDataStore::get_by_token_or_device_idpublicWC 10.5.0

Find tokens for this user and platform that match either the token or device UUID. We check the token value to avoid creating a duplicate. We check the device UUID value because only one token should be issued per device, therefore if we already have one then we can update it to avoid creating a duplicate.

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

Хуков нет.

Возвращает

null|PushToken.

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

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->get_by_token_or_device_id( $push_token ): ?PushToken;
$push_token(PushToken) (обязательный)
An instance of PushToken.

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

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

Код PushTokensDataStore::get_by_token_or_device_id() WC 10.5.2

public function get_by_token_or_device_id( PushToken &$push_token ): ?PushToken {
	if (
		! $push_token->get_user_id()
		|| ! $push_token->get_platform()
		|| ! $push_token->get_origin()
		|| (
			/**
			 * Platforms iOS and Android require token OR device UUID.
			 */
			$push_token->get_platform() !== PushToken::PLATFORM_BROWSER
			&& ! $push_token->get_token()
			&& ! $push_token->get_device_uuid()
		)
		|| (
			/**
			 * Browsers don't have device UUIDs, so require token.
			 */
			$push_token->get_platform() === PushToken::PLATFORM_BROWSER
			&& ! $push_token->get_token()
		)
	) {
		throw new InvalidArgumentException(
			'Can\'t retrieve push token because the push token data provided is invalid.'
		);
	}

	$query = new WP_Query(
		array(
			'post_type'      => PushToken::POST_TYPE,
			'post_status'    => 'private',
			'author'         => $push_token->get_user_id(),
			'posts_per_page' => -1,
			'orderby'        => 'ID',
			'order'          => 'DESC',
			'fields'         => 'ids',
		)
	);

	$post_ids = $query->posts;

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

	update_meta_cache( 'post', $post_ids );

	foreach ( $post_ids as $post_id ) {
		$candidate = new PushToken();
		$candidate->set_id( $post_id );

		try {
			$meta = $this->build_meta_array_from_database( $candidate );
		} catch ( Exception $e ) {
			wc_get_logger()->warning(
				'Failed to load meta for push token.',
				array(
					'token_id' => $post_id,
					'error'    => $e->getMessage(),
				)
			);

			continue;
		}

		if (
			$meta['platform'] === $push_token->get_platform()
			&& $meta['origin'] === $push_token->get_origin()
			&& (
				( $push_token->get_token() && $push_token->get_token() === $meta['token'] )
				|| ( $push_token->get_device_uuid() && $push_token->get_device_uuid() === $meta['device_uuid'] )
			)
		) {
			$push_token->set_id( $post_id );
			$push_token->set_token( $meta['token'] );
			$push_token->set_device_uuid( $meta['device_uuid'] );
			return $push_token;
		}
	}

	return null;
}