Automattic\WooCommerce\Internal\PushNotifications\DataStores

PushTokensDataStore::readpublicWC 10.5.0

Gets post representing a push token.

Method of the class: PushTokensDataStore{}

No Hooks.

Returns

PushToken. The populated push token.

Usage

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->read( $id ): PushToken;
$id(int) (required)
The push token ID.

Changelog

Since 10.5.0 Introduced.

PushTokensDataStore::read() code WC 10.8.1

public function read( int $id ): PushToken {
	$push_token = new PushToken( array( 'id' => $id ) );
	$post       = get_post( $push_token->get_id() );

	if ( ! $post || PushToken::POST_TYPE !== $post->post_type ) {
		throw new PushTokenNotFoundException();
	}

	$meta = $this->build_meta_array_from_database( (int) $push_token->get_id() );

	if (
		empty( $meta['token'] )
		|| empty( $meta['platform'] )
		|| empty( $meta['origin'] )
		|| (
			empty( $meta['device_uuid'] )
			&& PushToken::PLATFORM_BROWSER !== $meta['platform']
		)
	) {
		throw new PushTokenInvalidDataException(
			'Can\'t read push token because the push token record is malformed.'
		);
	}

	$push_token->set_user_id( (int) $post->post_author );
	$push_token->set_token( $meta['token'] );
	$push_token->set_device_uuid( $meta['device_uuid'] ?? null );
	$push_token->set_platform( $meta['platform'] );
	$push_token->set_origin( $meta['origin'] );

	/**
	 * These meta items were added after the ability to store tokens, so may
	 * not be available for older tokens. Use sensible defaults.
	 */
	$push_token->set_device_locale( $meta['device_locale'] ?? PushToken::DEFAULT_DEVICE_LOCALE );
	$push_token->set_metadata( $meta['metadata'] ?? array() );

	return $push_token;
}