Automattic\WooCommerce\Internal\PushNotifications\DataStores

NotificationPreferencesDataStore::writepublicWC 10.8.0

Persist an envelope for a user.

No-ops when the stored value already matches the supplied envelope. Throws WC_Data_Exception when the underlying user meta write fails for a reason other than the value being unchanged.

Method of the class: NotificationPreferencesDataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

$NotificationPreferencesDataStore = new NotificationPreferencesDataStore();
$NotificationPreferencesDataStore->write( $user_id, $envelope ): void;
$user_id(int) (required)
The user ID.
$envelope(array) (required)
The envelope to persist (must have schema_version and preferences keys).

Changelog

Since 10.8.0 Introduced.

NotificationPreferencesDataStore::write() code WC 10.9.1

public function write( int $user_id, array $envelope ): void {
	// Skip the write when the stored envelope already matches. This avoids
	// the ambiguous `false` return from update_user_meta() that means
	// either "value unchanged" or "DB write failed" — by short-circuiting
	// the no-op case, a `false` from the call below unambiguously means
	// the write itself failed and we can surface it.
	$stored = Users::get_site_user_meta( $user_id, self::META_KEY );
	if ( $stored === $envelope ) {
		return;
	}

	$result = Users::update_site_user_meta( $user_id, self::META_KEY, $envelope );

	if ( false === $result ) {
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
		throw new WC_Data_Exception(
			'woocommerce_push_notification_preferences_save_failed',
			'Failed to save push notification preferences.',
			WP_Http::INTERNAL_SERVER_ERROR
		);
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}
}