Automattic\WooCommerce\Internal\PushNotifications\DataStores

PushTokensDataStore::createpublicWC 10.5.0

Creates a post representing the push token.

Method of the class: PushTokensDataStore{}

No Hooks.

Returns

PushToken. The created push token with ID set.

Usage

$PushTokensDataStore = new PushTokensDataStore();
$PushTokensDataStore->create( $data ): PushToken;
$data(array) (required)
Token data with keys: user_id, token, platform, device_uuid (optional), origin.

Changelog

Since 10.5.0 Introduced.

PushTokensDataStore::create() code WC 10.9.4

public function create( array $data ): PushToken {
	$push_token = new PushToken( $data );

	if ( ! $push_token->can_be_created() ) {
		throw new PushTokenInvalidDataException(
			'Can\'t create push token because the push token data provided is invalid.'
		);
	}

	$id = wp_insert_post(
		array(
			'post_author' => (int) $push_token->get_user_id(),
			'post_type'   => PushToken::POST_TYPE,
			'post_status' => 'private',
			'meta_input'  => $this->build_meta_array_from_token( $push_token ),
		),
		true
	);

	if ( is_wp_error( $id ) ) {
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
		throw new WC_Data_Exception(
			(string) $id->get_error_code(),
			$id->get_error_message(),
			WP_Http::INTERNAL_SERVER_ERROR
		);
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	$push_token->set_id( $id );

	return $push_token;
}