| @@ -265,8 +265,47 @@ | ||
| 265 | 265 | return $preferences; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
| 269 | + * Store one explicit card choice for a user. | |
| 270 | + * | |
| 271 | + * The write half of the preference map: the Station Home app's | |
| 272 | + * Customize switches land here, and so may any plugin that wants to | |
| 273 | + * flip a card on a user's behalf. Refuses ids that are not registered | |
| 274 | + * for the current user, so a stale switch cannot mint a preference | |
| 275 | + * for a card that no longer exists. | |
| 276 | + * | |
| 277 | + * @param int $user_id User id. | |
| 278 | + * @param string $id Card id. | |
| 279 | + * @param bool $enabled New explicit state. | |
| 280 | + * @return bool Whether the choice was stored. | |
| 281 | + */ | |
| 282 | +function openstation_station_home_set_card_preference( $user_id, $id, $enabled ) { | |
| 283 | + $user_id = (int) $user_id; | |
| 284 | + $id = sanitize_key( (string) $id ); | |
| 285 | + $cards = openstation_station_home_get_registered_cards(); | |
| 286 | + if ( $user_id <= 0 || '' === $id || ! isset( $cards[ $id ] ) ) { | |
| 287 | + return false; | |
| 288 | + } | |
| 289 | + | |
| 290 | + $enabled = (bool) $enabled; | |
| 291 | + $preferences = openstation_station_home_get_card_preferences( $user_id ); | |
| 292 | + $preferences[ $id ] = $enabled; | |
| 293 | + update_user_meta( $user_id, OPENSTATION_STATION_HOME_CARD_PREFERENCES_META, $preferences ); | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Fires after a user opts in to or out of a Station Home card. | |
| 297 | + * | |
| 298 | + * @param int $user_id User id. | |
| 299 | + * @param string $id Card id. | |
| 300 | + * @param bool $enabled New explicit state. | |
| 301 | + */ | |
| 302 | + do_action( 'openstation_station_home_card_preference_updated', $user_id, $id, $enabled ); | |
| 303 | + | |
| 304 | + return true; | |
| 305 | +} | |
| 306 | + | |
| 307 | +/** | |
| 269 | 308 | * Resolve a card's effective per-user enabled state. |
| 270 | 309 | * |
| 271 | 310 | * @param string $id Card id. |
| 272 | 311 | * @param array $entry Registry entry. |