| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Data; |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* Class UserState |
| 9 |
* @package FL\Assistant\Data |
| 10 |
*/ |
| 11 |
class UserState { |
| 12 |
|
| 13 |
/** |
| 14 |
* Storage key for user meta |
| 15 |
*/ |
| 16 |
const FL_ASSISTANT_STATE = 'fl_assistant_state'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Default state for the current user. |
| 20 |
*/ |
| 21 |
public static $default_state = [ |
| 22 |
'appOrder' => [], |
| 23 |
'window' => [ |
| 24 |
'origin' => [ 1, 0 ], |
| 25 |
'size' => 'mini', |
| 26 |
'isHidden' => false, |
| 27 |
'hiddenAppearance' => '', |
| 28 |
'overlayToolbar' => false, |
| 29 |
], |
| 30 |
'appearance' => [ |
| 31 |
'brightness' => 'light', |
| 32 |
], |
| 33 |
'history' => [ |
| 34 |
'index' => 0, |
| 35 |
'entries' => [], |
| 36 |
], |
| 37 |
'searchHistory' => [], |
| 38 |
'shouldReduceMotion' => false, |
| 39 |
'shouldShowLabels' => true, |
| 40 |
'shouldShowInAdmin' => true, |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the saved state for the current user. |
| 45 |
*/ |
| 46 |
public static function get() { |
| 47 |
|
| 48 |
$saved = get_user_meta( |
| 49 |
wp_get_current_user()->ID, |
| 50 |
static::FL_ASSISTANT_STATE, |
| 51 |
true |
| 52 |
); |
| 53 |
|
| 54 |
return array_replace_recursive( |
| 55 |
static::$default_state, |
| 56 |
$saved ? (array) $saved : [] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update the saved state for the current user |
| 62 |
*/ |
| 63 |
public static function update( array $state = [] ) { |
| 64 |
|
| 65 |
$saved = static::get(); |
| 66 |
|
| 67 |
update_user_meta( |
| 68 |
wp_get_current_user()->ID, |
| 69 |
static::FL_ASSISTANT_STATE, |
| 70 |
array_merge( $saved, $state ) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|