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 |