| 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 |
private static $frame_defaults = [ |
| 19 |
'minWidth' => 460, |
| 20 |
'maxWidth' => 900, |
| 21 |
'defaultWidth' => 520, |
| 22 |
'defaultOrigin' => [ 1, 0 ], |
| 23 |
'breakpoint' => 650, |
| 24 |
]; |
| 25 |
|
| 26 |
/** |
| 27 |
* Default state for the current user. |
| 28 |
*/ |
| 29 |
public static $default_state = [ |
| 30 |
'appOrder' => [], |
| 31 |
'window' => [ |
| 32 |
'origin' => [ 1, 0 ], |
| 33 |
'width' => 520, |
| 34 |
'isHidden' => false, |
| 35 |
'hiddenAppearance' => '', |
| 36 |
], |
| 37 |
'appearance' => [ |
| 38 |
'brightness' => 'light', |
| 39 |
], |
| 40 |
'history' => [ |
| 41 |
'index' => 0, |
| 42 |
'entries' => [], |
| 43 |
], |
| 44 |
'searchHistory' => [], |
| 45 |
'shouldShowInAdmin' => true, |
| 46 |
'isAppHidden' => false, |
| 47 |
'shortcuts' => [], |
| 48 |
]; |
| 49 |
|
| 50 |
public static function get_default_state() { |
| 51 |
return static::$default_state; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get the saved state for the current user. |
| 56 |
*/ |
| 57 |
public static function get() { |
| 58 |
|
| 59 |
$saved = get_user_meta( |
| 60 |
wp_get_current_user()->ID, |
| 61 |
static::FL_ASSISTANT_STATE, |
| 62 |
true |
| 63 |
); |
| 64 |
|
| 65 |
// Filter Window |
| 66 |
if ( isset( $saved['window'] ) ) { |
| 67 |
$window = $saved['window']; |
| 68 |
|
| 69 |
// Remove deprecated |
| 70 |
if ( isset( $window['size'] ) ) { |
| 71 |
unset( $window['size'] ); |
| 72 |
} |
| 73 |
if ( isset( $window['overlayToolbar'] ) ) { |
| 74 |
unset( $window['overlayToolbar'] ); |
| 75 |
} |
| 76 |
$saved['window'] = $window; |
| 77 |
} |
| 78 |
|
| 79 |
return array_replace_recursive( |
| 80 |
static::get_default_state(), |
| 81 |
$saved ? (array) $saved : [] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Update the saved state for the current user |
| 87 |
*/ |
| 88 |
public static function update( array $state = [] ) { |
| 89 |
|
| 90 |
$saved = static::get(); |
| 91 |
|
| 92 |
update_user_meta( |
| 93 |
wp_get_current_user()->ID, |
| 94 |
static::FL_ASSISTANT_STATE, |
| 95 |
array_merge( $saved, $state ) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
public static function getFrameDefaults() { |
| 100 |
return static::$frame_defaults; |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|