Encoder
1 month ago
EncoderFactory
1 month ago
Repository
1 month ago
Table
1 month ago
Encoder.php
1 month ago
EncoderFactory.php
1 month ago
KeyValue.php
1 month ago
Option.php
1 month ago
OptionData.php
1 month ago
OptionDataFactory.php
1 month ago
OptionFactory.php
1 month ago
SiteOption.php
1 month ago
SiteOptionFactory.php
1 month ago
Table.php
1 month ago
Timestamp.php
1 month ago
Transaction.php
1 month ago
UserData.php
1 month ago
UserMeta.php
1 month ago
UserOption.php
1 month ago
UserOption.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Storage; |
| 6 | |
| 7 | /** |
| 8 | * Similar to storing metadata, but this prefixes the key with the site ID when running a multisite installation. |
| 9 | */ |
| 10 | class UserOption implements UserData |
| 11 | { |
| 12 | |
| 13 | private string $key; |
| 14 | |
| 15 | private int $user_id; |
| 16 | |
| 17 | public function __construct(string $key, ?int $user_id = null) |
| 18 | { |
| 19 | if (null === $user_id) { |
| 20 | $user_id = get_current_user_id(); |
| 21 | } |
| 22 | |
| 23 | $this->key = $key; |
| 24 | $this->user_id = $user_id; |
| 25 | } |
| 26 | |
| 27 | public function get() |
| 28 | { |
| 29 | return get_user_option($this->key, $this->user_id); |
| 30 | } |
| 31 | |
| 32 | public function save($value): void |
| 33 | { |
| 34 | // Prevent duplicate rows. update_user_option() calls update_user_meta() internally, |
| 35 | // which can insert duplicates when concurrent requests race. |
| 36 | delete_user_option($this->user_id, $this->key); |
| 37 | update_user_option($this->user_id, $this->key, $value); |
| 38 | } |
| 39 | |
| 40 | public function delete(): void |
| 41 | { |
| 42 | delete_user_option($this->user_id, $this->key); |
| 43 | } |
| 44 | |
| 45 | } |