PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3.2
FileBird – WordPress Media Library Folders & File Manager v6.3.2
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 / SettingModel.php

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

235 lines 6.2 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 SettingModel {
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 'USER_MODE' => array(
65 'get' => 'getUserMode',
66 'set' => 'setUserMode',
67 ),
68 'SVG_SUPPORT' => array(
69 'get' => 'getSvgSupport',
70 'set' => 'setSvgSupport',
71 ),
72 'IS_SEARCH_USING_API' => array(
73 'get' => 'getFolderSearchMethod',
74 'set' => 'setFolderSearchMethod',
75 ),
76 );
77 }
78
79 public function addUserSettingsData( $data ) {
80 $data['user_settings'] = $this->settings;
81 return $data;
82 }
83
84 public function loadSettings() {
85 foreach ( $this->config as $key => $value ) {
86 $this->settings[ $key ] = $this->{$value['get']}();
87 }
88
89 return $this->settings;
90 }
91
92 public function get( $key ) {
93 if ( in_array( $key, array_keys( $this->config ) ) ) {
94 return $this->settings[ $key ];
95 }
96 }
97
98 public function setSettings( $params ) {
99 foreach ( $params as $key => $value ) {
100 if ( isset( $this->config[ $key ] ) ) {
101 $this->{$this->config[ $key ]['set']}( $value );
102 }
103 }
104 }
105
106 public function getSortFiles() {
107 $meta = get_user_meta( $this->userId, self::DEFAULT_SORT_FILES, true );
108
109 if ( ! empty( $meta ) ) {
110 $parse = explode( '-', $meta );
111
112 return array(
113 'orderby' => $parse[0],
114 'order' => $parse[1] ?? '',
115 );
116 }
117
118 return null;
119 }
120
121 public function getFolderStartup() {
122 $defaultFolder = $this->settings['DEFAULT_FOLDER'];
123 $startupFolder = get_user_meta( $this->userId, self::FOLDER_STARTUP, true );
124 $startupFolder = is_numeric( $startupFolder ) ? intval( $startupFolder ) : FolderModel::ALL_CATEGORIES;
125
126 if ( $defaultFolder == FolderModel::PREVIOUS_FOLDER ) {
127 if ( $startupFolder > 0 ) {
128 if ( FolderModel::isFolderExist( $startupFolder ) ) {
129 return intval( $startupFolder );
130 } else {
131 $this->settings['FOLDER_STARTUP'] = FolderModel::ALL_CATEGORIES;
132 $this->setFolderStartup( FolderModel::ALL_CATEGORIES );
133 return FolderModel::ALL_CATEGORIES;
134 }
135 } else {
136 if ( in_array( $startupFolder, array( FolderModel::ALL_CATEGORIES, FolderModel::UN_CATEGORIZED ), true ) ) {
137 return $startupFolder;
138 }
139 }
140 }
141
142 return FolderModel::ALL_CATEGORIES;
143 }
144
145 public function setFolderStartup( $value ) {
146 update_user_meta( $this->userId, self::FOLDER_STARTUP, $value );
147 }
148
149 public function getSortFolders() {
150 return get_user_meta( $this->userId, self::DEFAULT_SORT_FOLDERS, true );
151 }
152
153 public function setSortFolders( $value ) {
154 update_user_meta( $this->userId, self::DEFAULT_SORT_FOLDERS, $value );
155 }
156
157 public function getCurrentTheme() {
158 $theme = get_user_meta( get_current_user_id(), self::THEME, true ) ?: 'default';
159
160 return array(
161 'name' => $theme,
162 'colors' => ThemeModel::COLORS,
163 );
164 }
165
166 public function getDefaultFolder() {
167 $folder_id = get_user_meta( $this->userId, self::DEFAULT_FOLDER, true );
168 $folder_id = intval( $folder_id );
169
170 if ( $folder_id > 0 ) {
171 if ( is_null( FolderModel::findById( $folder_id ) ) ) {
172 $folder_id = FolderModel::ALL_CATEGORIES;
173 }
174 }
175 return $folder_id;
176 }
177
178 public function getFolderCounterType() {
179 $type = get_user_meta( $this->userId, self::FOLDER_COUNTER_TYPE, true );
180 return empty( $type ) ? 'counter_file_in_folder' : $type;
181 }
182
183 public function getBreadCrumb() {
184 return get_user_meta( $this->userId, self::SHOW_BREAD_CRUMB, true );
185 }
186
187 public function setDefaultFolder( $value ) {
188 $value = (int) $value;
189 update_user_meta( $this->userId, self::DEFAULT_FOLDER, $value );
190 }
191
192 public function setSortFiles( $value ) {
193 $value = $value ? "{$value['orderby']}-{$value['order']}" : '';
194
195 update_user_meta( $this->userId, self::DEFAULT_SORT_FILES, $value );
196 }
197
198 public function setCurrentTheme( $theme ) {
199 // Fixed old FileBird version below 6
200 update_user_meta( $this->userId, self::THEME, $theme['name'] === 'default' ? '' : $theme['name'] );
201 }
202
203 public function setFolderCounterType( $type ) {
204 update_user_meta( $this->userId, self::FOLDER_COUNTER_TYPE, $type );
205 $this->settings['FOLDER_COUNTER_TYPE'] = $type;
206 }
207
208 public function setBreadCrumb( $value ) {
209 update_user_meta( $this->userId, self::SHOW_BREAD_CRUMB, $value );
210 }
211
212 public function getUserMode() {
213 return get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
214 }
215
216 public function setUserMode( $value ) {
217 update_option( 'njt_fbv_folder_per_user', $value );
218 }
219
220 public function getSvgSupport() {
221 return get_option( 'njt_fbv_allow_svg_upload', '0' ) === '1';
222 }
223
224 public function setSvgSupport( $value ) {
225 update_option( 'njt_fbv_allow_svg_upload', $value );
226 }
227
228 public function getFolderSearchMethod() {
229 return get_option( 'njt_fbv_is_search_using_api', '0' ) === '1';
230 }
231
232 public function setFolderSearchMethod( $value ) {
233 update_option( 'njt_fbv_is_search_using_api', $value );
234 }
235 }