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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / PushNotifications / Services / NotificationPreferencesService.php
woocommerce / src / Internal / PushNotifications / Services Last commit date
NotificationPreferencesService.php 4 weeks ago NotificationProcessor.php 4 weeks ago NotificationRetryHandler.php 4 weeks ago PendingNotificationStore.php 4 weeks ago
NotificationPreferencesService.php
206 lines
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace Automattic\WooCommerce\Internal\PushNotifications\Services;
6
7 defined( 'ABSPATH' ) || exit;
8
9 use Automattic\WooCommerce\Internal\PushNotifications\DataStores\NotificationPreferencesDataStore;
10 use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification;
11
12 /**
13 * Manages per-user push notification preferences.
14 *
15 * Owns the domain logic — the default preference values and how arbitrary
16 * input is sanitized — and delegates persistence to
17 * `NotificationPreferencesDataStore`.
18 *
19 * @since 10.8.0
20 */
21 class NotificationPreferencesService {
22 /**
23 * The data store used for persistence.
24 *
25 * @var NotificationPreferencesDataStore
26 */
27 private NotificationPreferencesDataStore $data_store;
28
29 /**
30 * Initialize injected dependencies.
31 *
32 * @internal
33 *
34 * @param NotificationPreferencesDataStore $data_store The data store.
35 *
36 * @since 10.8.0
37 */
38 final public function init( NotificationPreferencesDataStore $data_store ): void {
39 $this->data_store = $data_store;
40 }
41
42 /**
43 * Retrieve a user's notification preferences.
44 *
45 * Falls back to defaults for users with no stored preferences. Stored
46 * preferences are overlaid on top of the defaults so that any newer keys
47 * not yet on disk are filled in.
48 *
49 * @param int $user_id The user ID.
50 *
51 * @return array<string, array<string, mixed>> Map of preference key => sub-options.
52 *
53 * @since 10.8.0
54 */
55 public function get_preferences( int $user_id ): array {
56 $envelope = $this->data_store->read( $user_id );
57
58 if ( null === $envelope ) {
59 return $this->get_defaults();
60 }
61
62 $stored = isset( $envelope['preferences'] ) && is_array( $envelope['preferences'] )
63 ? $envelope['preferences']
64 : array();
65
66 return $this->sanitize( array_replace_recursive( $this->get_defaults(), $stored ) );
67 }
68
69 /**
70 * Persist a partial update to a user's notification preferences.
71 *
72 * Unknown top-level keys and unknown sub-fields per key are dropped.
73 * The merged result is wrapped in the current versioned envelope and
74 * handed to the data store.
75 *
76 * @param int $user_id The user ID.
77 * @param array<string, array<string, mixed>> $preferences Partial preferences to merge over existing values.
78 *
79 * @return array<string, array<string, mixed>> The merged, sanitized preferences map after the save.
80 *
81 * @throws \WC_Data_Exception Propagated from the data store on real persistence failure.
82 *
83 * @since 10.8.0
84 */
85 public function save_preferences( int $user_id, array $preferences ): array {
86 $current = $this->get_preferences( $user_id );
87 $merged = $this->sanitize( array_replace_recursive( $current, $preferences ) );
88
89 // Data store throws WC_Data_Exception on real failure; let it propagate.
90 $this->data_store->write(
91 $user_id,
92 array(
93 'schema_version' => NotificationPreferencesDataStore::CURRENT_SCHEMA_VERSION,
94 'preferences' => $merged,
95 )
96 );
97
98 return $merged;
99 }
100
101 /**
102 * Return the default preferences for a new user.
103 *
104 * Each preference is a small object so future fields (thresholds, sub-toggles)
105 * can be added without bumping the schema version. The keyset is derived from
106 * `Notification::NOTIFICATION_CLASSES` so adding a new notification type
107 * automatically opts it into preferences — no parallel list to keep in sync.
108 *
109 * @return array<string, array<string, mixed>> Map of preference key => default sub-options.
110 *
111 * @since 10.8.0
112 */
113 public function get_defaults(): array {
114 $defaults = array();
115 foreach ( array_keys( Notification::NOTIFICATION_CLASSES ) as $type ) {
116 $defaults[ $type ] = array( 'enabled' => true );
117 }
118
119 $defaults['store_order']['min_amount'] = null;
120 $defaults['store_review']['max_rating'] = null;
121 $defaults['store_stock']['low_stock'] = true;
122 $defaults['store_stock']['out_of_stock'] = true;
123 $defaults['store_stock']['on_backorder'] = true;
124
125 return $defaults;
126 }
127
128 /**
129 * Drop unknown top-level keys and unknown sub-fields per key, coercing
130 * known sub-fields to their expected types.
131 *
132 * @param array $preferences Arbitrary preferences map.
133 *
134 * @return array<string, array<string, mixed>> Sanitized preferences.
135 */
136 private function sanitize( array $preferences ): array {
137 $allowed = $this->get_defaults();
138 $sanitized = array();
139
140 foreach ( $allowed as $key => $default_shape ) {
141 $value = $preferences[ $key ] ?? array();
142 $value = is_array( $value ) ? $value : array();
143 $sanitized[ $key ] = $this->sanitize_value( $key, $value, $default_shape );
144 }
145
146 return $sanitized;
147 }
148
149 /**
150 * Apply per-key sanitization to a single preference's sub-options.
151 *
152 * Unknown sub-keys are dropped; missing sub-keys fall back to their default.
153 * Today only `enabled` is recognized; future preference types extend this method
154 * (or its dispatch) to validate their additional sub-fields.
155 *
156 * @param string $key Preference key (e.g. `store_order`).
157 * @param array $value Submitted sub-options for the key.
158 * @param array<string, mixed> $default_shape Default sub-options for the key.
159 *
160 * @return array<string, mixed>
161 */
162 protected function sanitize_value( string $key, array $value, array $default_shape ): array {
163 unset( $key );
164
165 $sanitized = array();
166
167 foreach ( $default_shape as $sub_key => $sub_default ) {
168 if ( 'enabled' === $sub_key ) {
169 $sanitized[ $sub_key ] = array_key_exists( $sub_key, $value )
170 ? (bool) $value[ $sub_key ]
171 : (bool) $sub_default;
172 continue;
173 }
174
175 if ( 'min_amount' === $sub_key ) {
176 if ( ! array_key_exists( $sub_key, $value ) || null === $value[ $sub_key ] ) {
177 $sanitized[ $sub_key ] = null;
178 continue;
179 }
180 $amount = (float) $value[ $sub_key ];
181 $sanitized[ $sub_key ] = $amount > 0 ? $amount : null;
182 continue;
183 }
184
185 if ( 'max_rating' === $sub_key ) {
186 if ( ! array_key_exists( $sub_key, $value ) || null === $value[ $sub_key ] ) {
187 $sanitized[ $sub_key ] = null;
188 continue;
189 }
190 $rating = (int) $value[ $sub_key ];
191 $sanitized[ $sub_key ] = ( $rating >= 1 && $rating <= 5 ) ? $rating : null;
192 continue;
193 }
194
195 if ( in_array( $sub_key, array( 'low_stock', 'out_of_stock', 'on_backorder' ), true ) ) {
196 $sanitized[ $sub_key ] = array_key_exists( $sub_key, $value )
197 ? (bool) $value[ $sub_key ]
198 : (bool) $sub_default;
199 continue;
200 }
201 }//end foreach
202
203 return $sanitized;
204 }
205 }
206