PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3.4
FileBird – WordPress Media Library Folders & File Manager v6.3.4
6.5.8 6.5.7 6.5.5 6.5.4 trunk 4.3.1 5.1.6 5.3 5.3.2 5.4.3 5.4.4 5.4.5 5.5 5.5.3 5.5.5 5.5.8 5.5.8.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 6.2.3 6.2.5 6.3 All 36 releases
filebird / includes / Model / UserSettingModel.php

UserSettingModel.php in FileBird – WordPress Media Library Folders & File Manager 6.3.4, at includes/Model/UserSettingModel.php

199 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FileBird\Model;
3
4 use FileBird\Utils\Singleton;
5 use FileBird\Model\Folder as FolderModel;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class UserSettingModel {
10 use Singleton;
11
12 const DEFAULT_FOLDER = '_njt_fbv_default_folder';
13 const DEFAULT_SORT_FILES = '_njt_fbv_default_sort_files';
14 const DEFAULT_SORT_FOLDERS = 'fbv_folder_sort';
15 const FOLDER_COUNTER_TYPE = 'fbv_counter_type';
16 const FOLDER_STARTUP = 'fbv_folder_startup';
17 const THEME = 'fbv_theme';
18 const SHOW_BREAD_CRUMB = 'fbv_show_breadcrumb';
19
20 private $userId = '';
21
22 private $settings = array();
23 private $config = array();
24
25 public function __construct() {
26 $this->initialize();
27
28 $this->userId = get_current_user_id();
29 $this->settings = $this->loadSettings();
30
31 add_filter( 'fbv_data', array( $this, 'addUserSettingsData' ), 10, 1 );
32 }
33
34 public function initialize() {
35 $this->config = array(
36 'DEFAULT_FOLDER' => array(
37 'get' => 'getDefaultFolder',
38 'set' => 'setDefaultFolder',
39 ),
40 'DEFAULT_SORT_FILES' => array(
41 'get' => 'getSortFiles',
42 'set' => 'setSortFiles',
43 ),
44 'DEFAULT_SORT_FOLDERS' => array(
45 'get' => 'getSortFolders',
46 'set' => 'setSortFolders',
47 ),
48 'FOLDER_STARTUP' => array(
49 'get' => 'getFolderStartup',
50 'set' => 'setFolderStartup',
51 ),
52 'FOLDER_COUNTER_TYPE' => array(
53 'get' => 'getFolderCounterType',
54 'set' => 'setFolderCounterType',
55 ),
56 'THEME' => array(
57 'get' => 'getCurrentTheme',
58 'set' => 'setCurrentTheme',
59 ),
60 'SHOW_BREAD_CRUMB' => array(
61 'get' => 'getBreadCrumb',
62 'set' => 'setBreadCrumb',
63 ),
64 );
65 }
66
67 public function addUserSettingsData( $data ) {
68 $data['user_settings'] = $this->settings;
69 return $data;
70 }
71
72 public function loadSettings() {
73 foreach ( $this->config as $key => $value ) {
74 $this->settings[ $key ] = $this->{$value['get']}();
75 }
76
77 return $this->settings;
78 }
79
80 public function get( $key ) {
81 if ( in_array( $key, array_keys( $this->config ) ) ) {
82 return $this->settings[ $key ];
83 }
84 }
85
86 public function setSettings( $params ) {
87 foreach ( $params as $key => $value ) {
88 if ( isset( $this->config[ $key ] ) ) {
89 $this->{$this->config[ $key ]['set']}( $value );
90 }
91 }
92 }
93
94 public function getSortFiles() {
95 $meta = get_user_meta( $this->userId, self::DEFAULT_SORT_FILES, true );
96
97 if ( ! empty( $meta ) ) {
98 $parse = explode( '-', $meta );
99
100 return array(
101 'orderby' => $parse[0],
102 'order' => $parse[1] ?? '',
103 );
104 }
105
106 return null;
107 }
108
109 public function getFolderStartup() {
110 $defaultFolder = $this->settings['DEFAULT_FOLDER'];
111 $startupFolder = get_user_meta( $this->userId, self::FOLDER_STARTUP, true );
112 $startupFolder = is_numeric( $startupFolder ) ? intval( $startupFolder ) : FolderModel::ALL_CATEGORIES;
113
114 if ( $defaultFolder == FolderModel::PREVIOUS_FOLDER ) {
115 if ( $startupFolder > 0 ) {
116 if ( FolderModel::isFolderExist( $startupFolder ) ) {
117 return intval( $startupFolder );
118 } else {
119 $this->settings['FOLDER_STARTUP'] = FolderModel::ALL_CATEGORIES;
120 $this->setFolderStartup( FolderModel::ALL_CATEGORIES );
121 return FolderModel::ALL_CATEGORIES;
122 }
123 } else {
124 if ( in_array( $startupFolder, array( FolderModel::ALL_CATEGORIES, FolderModel::UN_CATEGORIZED ), true ) ) {
125 return $startupFolder;
126 }
127 }
128 }
129
130 return FolderModel::ALL_CATEGORIES;
131 }
132
133 public function setFolderStartup( $value ) {
134 update_user_meta( $this->userId, self::FOLDER_STARTUP, $value );
135 }
136
137 public function getSortFolders() {
138 return get_user_meta( $this->userId, self::DEFAULT_SORT_FOLDERS, true );
139 }
140
141 public function setSortFolders( $value ) {
142 update_user_meta( $this->userId, self::DEFAULT_SORT_FOLDERS, $value );
143 }
144
145 public function getCurrentTheme() {
146 $theme = get_user_meta( get_current_user_id(), self::THEME, true ) ?: 'default';
147
148 return array(
149 'name' => $theme,
150 'colors' => ThemeModel::COLORS,
151 );
152 }
153
154 public function getDefaultFolder() {
155 $folder_id = get_user_meta( $this->userId, self::DEFAULT_FOLDER, true );
156 $folder_id = intval( $folder_id );
157
158 if ( $folder_id > 0 ) {
159 if ( is_null( FolderModel::findById( $folder_id ) ) ) {
160 $folder_id = FolderModel::ALL_CATEGORIES;
161 }
162 }
163 return $folder_id;
164 }
165
166 public function getFolderCounterType() {
167 $type = get_user_meta( $this->userId, self::FOLDER_COUNTER_TYPE, true );
168 return empty( $type ) ? 'counter_file_in_folder' : $type;
169 }
170
171 public function getBreadCrumb() {
172 return get_user_meta( $this->userId, self::SHOW_BREAD_CRUMB, true );
173 }
174
175 public function setDefaultFolder( $value ) {
176 $value = (int) $value;
177 update_user_meta( $this->userId, self::DEFAULT_FOLDER, $value );
178 }
179
180 public function setSortFiles( $value ) {
181 $value = $value ? "{$value['orderby']}-{$value['order']}" : '';
182
183 update_user_meta( $this->userId, self::DEFAULT_SORT_FILES, $value );
184 }
185
186 public function setCurrentTheme( $theme ) {
187 // Fixed old FileBird version below 6
188 update_user_meta( $this->userId, self::THEME, $theme['name'] === 'default' ? '' : $theme['name'] );
189 }
190
191 public function setFolderCounterType( $type ) {
192 update_user_meta( $this->userId, self::FOLDER_COUNTER_TYPE, $type );
193 $this->settings['FOLDER_COUNTER_TYPE'] = $type;
194 }
195
196 public function setBreadCrumb( $value ) {
197 update_user_meta( $this->userId, self::SHOW_BREAD_CRUMB, $value );
198 }
199 }