Automattic\WooCommerce\Internal\PushNotifications\DataStores

NotificationPreferencesDataStore::write │ public │ WC 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.

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

Хуков нет.

Возвращает

void. Ничего (null).

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

$NotificationPreferencesDataStore = new NotificationPreferencesDataStore();
$NotificationPreferencesDataStore->write( $user_id, $envelope ): void;
$user_id(int) (обязательный)
The user ID.
$envelope(массив) (обязательный)
The envelope to persist (must have schema_version and preferences keys).

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

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

Код NotificationPreferencesDataStore::write() WC 11.2.2

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
	}
}