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 / Classes / Core.php

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

321 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FileBird\Classes;
3
4 use FileBird\Admin\Settings;
5 use FileBird\Utils\Singleton;
6 use FileBird\Model\Folder as FolderModel;
7 use FileBird\Utils\Vite;
8 use FileBird\Classes\Helpers as Helpers;
9 use FileBird\Controller\Import\ImportController;
10 use FileBird\I18n as I18n;
11 use FileBird\Model\UserSettingModel;
12 use FileBird\Model\SettingModel;
13 use FileBird\Controller\Convert as ConvertController;
14 use FileBird\Classes\FolderStateManager;
15
16 defined( 'ABSPATH' ) || exit;
17
18 class Core {
19 private $userSettingModel = null;
20 private $settingModel = null;
21
22 use Singleton;
23
24 public function __construct() {
25 $this->userSettingModel = UserSettingModel::getInstance();
26 $this->settingModel = SettingModel::getInstance();
27 $this->loadModules();
28 $this->checkUpdateDatabase();
29 }
30
31 private function doHooks() {
32 add_filter( 'media_library_infinite_scrolling', '__return_true' );
33 add_filter( 'ajax_query_attachments_args', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 );
34 add_filter( 'mla_media_modal_query_final_terms', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 );
35 add_filter( 'restrict_manage_posts', array( $this, 'restrictManagePosts' ) );
36 add_filter( 'posts_clauses', array( $this, 'postsClauses' ), 10, 2 );
37 add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 10, 2 );
38
39 add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminScripts' ), PHP_INT_MAX );
40 add_action( 'add_attachment', array( $this, 'addAttachment' ) );
41 add_action( 'delete_attachment', array( $this, 'deleteAttachment' ) );
42 add_action( 'pre-upload-ui', array( $this, 'actionPluploadUi' ) );
43 add_action( 'wp_ajax_fbv_first_folder_notice', array( $this, 'ajax_first_folder_notice' ) );
44 add_action( 'admin_notices', array( $this, 'adminNotices' ) );
45 add_action( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
46 add_filter( 'wp_edited_image_metadata', array( $this, 'edited_image_metadata' ), 10, 3 );
47
48 add_filter( 'users_have_additional_content', array( $this, 'users_have_additional_content' ), 10, 2 );
49 add_action( 'deleted_user', array( $this, 'deleted_user' ), 10, 3 );
50 // Fix WordPress VIP doesn't load CSS file correctly
51 add_filter( 'css_do_concat', array( $this, 'wordpress_vip_css_concat_filter' ), 10, 2 );
52 // -----
53 }
54
55 public function checkUpdateDatabase() {
56 if ( is_admin() ) {
57 $is_converted = get_option( 'fbv_old_data_updated_to_v4', '0' );
58 if ( $is_converted !== '1' ) {
59 if ( ConvertController::countOldFolders() > 0 && ! isset( $_GET['autorun'] ) ) {
60 add_filter( 'fbv_update_database_notice', '__return_true' );
61 }
62 }
63 }
64 }
65
66 public function wordpress_vip_css_concat_filter( $do_concat, $handle ) {
67 if ( 'filebird-ui' === $handle ) {
68 return false;
69 }
70 return $do_concat;
71 }
72
73 private function loadModules() {
74 new Attachment\AttachmentSize();
75 new Modules\ModuleExclude();
76 new Modules\ModuleUser();
77 new Modules\ModuleCompatibility();
78 new Modules\ModuleSvg();
79 }
80
81 public function adminNotices() {
82 global $pagenow;
83
84 $optionFirstFolder = get_option( 'fbv_first_folder_notice' );
85 if ( FolderModel::countFolder() === 0 && $pagenow !== 'upload.php' &&
86 ( $optionFirstFolder === false || time() >= intval( $optionFirstFolder ) ) ) {
87 include NJFB_PLUGIN_PATH . '/views/notices/html-notice-first-folder.php';
88 }
89
90 if ( $pagenow !== 'upload.php' && apply_filters( 'fbv_update_database_notice', false ) ) {
91 include NJFB_PLUGIN_PATH . '/views/notices/html-notice-update-database.php';
92 }
93 }
94
95 public function ajax_first_folder_notice() {
96 check_ajax_referer( 'fbv_nonce', 'nonce', true );
97 update_option( 'fbv_first_folder_notice', time() + 30 * 60 * 60 * 24 ); //After 3 months show
98 wp_send_json_success();
99 }
100
101 public function enqueueAdminScripts( $screenId ) {
102 $script_handle = Vite::enqueue_vite( 'main.tsx' );
103
104 wp_enqueue_script( 'jquery-ui-draggable' );
105 wp_enqueue_script( 'jquery-ui-droppable' );
106 wp_enqueue_script( 'wp-dom-ready' );
107 wp_enqueue_style( 'filebird-ui', NJFB_PLUGIN_URL . 'assets/dist/style.css', array(), NJFB_VERSION );
108
109 if ( wp_is_mobile() ) {
110 wp_enqueue_script( 'jquery-touch-punch-fixed', NJFB_PLUGIN_URL . 'assets/js/jquery.ui.touch-punch.js', array( 'jquery-ui-widget', 'jquery-ui-mouse' ), NJFB_VERSION, false );
111 }
112
113 wp_localize_script(
114 $script_handle,
115 'fbv_data',
116 apply_filters(
117 'fbv_data',
118 array(
119 'nonce' => wp_create_nonce( 'fbv_nonce' ),
120 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
121 'is_upload_screen' => 'upload.php' === $screenId ? '1' : '0',
122 'i18n' => I18n::getTranslation(),
123 'media_mode' => get_user_option( 'media_library_mode', get_current_user_id() ),
124 'json_url' => apply_filters( 'filebird_json_url', get_rest_url( null, NJFB_REST_URL ) ),
125 'media_url' => admin_url( 'upload.php' ),
126 'asset_url' => NJFB_PLUGIN_URL . 'assets/',
127 'data_import' => ImportController::get_notice_import( $screenId ),
128 'data_import_url' => esc_url(
129 add_query_arg(
130 array(
131 'page' => Settings::SETTING_PAGE_SLUG . '#/import-export',
132 ),
133 admin_url( 'admin.php' )
134 )
135 ),
136 'is_new_user' => get_option( 'fbv_is_new_user', false ),
137 'update_database_notice' => apply_filters( 'fbv_update_database_notice', false ),
138 'is_rtl' => is_rtl(),
139 'tree_mode' => 'attachment',
140 'folder_search_api' => apply_filters( 'fbv_folder_api_search', $this->settingModel->get( 'IS_SEARCH_USING_API' ) ),
141 )
142 )
143 );
144 }
145
146 public function restrictManagePosts() {
147 $screen = get_current_screen();
148 if ( $screen->id == 'upload' ) {
149 $fbv = ( ( isset( $_GET['fbv'] ) ) ? (int) sanitize_text_field( $_GET['fbv'] ) : FolderModel::ALL_CATEGORIES );
150 $folders = FolderModel::allFolders();
151
152 $all = new \stdClass();
153 $all->id = -1;
154 $all->name = __( 'All Folders', 'filebird' );
155
156 $uncategorized = new \stdClass();
157 $uncategorized->id = 0;
158 $uncategorized->name = __( 'Uncategorized', 'filebird' );
159
160 array_unshift( $folders, $all, $uncategorized );
161 echo '<select name="fbv" id="filter-by-fbv" class="fbv-filter attachment-filters fbv">';
162 foreach ( $folders as $k => $folder ) {
163 echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', esc_html( $folder->id ), esc_html( $folder->name ), selected( intval( $folder->id ), $fbv, false ) );
164 }
165 echo '</select>';
166 }
167 }
168
169 public function postsClauses( $clauses, $query ) {
170 global $wpdb;
171
172 if ( $query->get( 'post_type' ) !== 'attachment' ) {
173 return $clauses;
174 }
175
176 $fb_folder = ( new FolderStateManager( $query, $this->userSettingModel ) )->getFbFolder();
177
178 if ( ! \is_null( $fb_folder ) ) {
179 if ( FolderModel::ALL_CATEGORIES === $fb_folder ) {
180 return $clauses;
181 } elseif ( FolderModel::UN_CATEGORIZED === $fb_folder ) {
182 $clauses = FolderModel::getRelationsWithFolderUser( $clauses );
183 } else {
184 $clauses['join'] .= $wpdb->prepare( " LEFT JOIN {$wpdb->prefix}fbv_attachment_folder AS fbva ON fbva.attachment_id = {$wpdb->posts}.ID AND fbva.folder_id = %d ", $fb_folder );
185 $clauses['where'] .= ' AND fbva.folder_id IS NOT NULL';
186 }
187 }
188
189 return $clauses;
190 }
191
192 public function addAttachment( $post_id ) {
193 $fbv = ( ( isset( $_REQUEST['fbv'] ) ) ? sanitize_text_field( $_REQUEST['fbv'] ) : '' );
194 if ( $fbv != '' ) {
195 if ( is_numeric( $fbv ) ) {
196 $parent = $fbv;
197 } else {
198 $fbv = explode( '/', ltrim( rtrim( $fbv, '/' ), '/' ) );
199 $parent = (int) $fbv[0];
200 if ( $parent < 0 ) {
201 $parent = 0; //important
202 }
203 if ( apply_filters( 'fbv_auto_create_folders', true ) ) {
204 unset( $fbv[0] );
205 foreach ( $fbv as $k => $v ) {
206 $folder = FolderModel::newOrGet( $v, $parent );
207 $parent = $folder['id'];
208 }
209 }
210 }
211
212 $ids = array_map( 'intval', apply_filters( 'fbv_ids_assigned_to_folder', array( $post_id ) ) );
213
214 FolderModel::setFoldersForPosts( $ids, $parent );
215 }
216 }
217
218 public function deleteAttachment( $post_id ) {
219 FolderModel::deleteFoldersOfPost( $post_id );
220 }
221
222 public function ajaxQueryAttachmentsArgs( $query ) {
223 // phpcs::ignore WordPress.Security.NonceVerification.Recommended
224 if ( isset( $_REQUEST['query']['fbv'] ) ) {
225 $fbv = Helpers::sanitize_intval_array( $_REQUEST['query']['fbv'] );
226 $query['fbv'] = ( new FolderStateManager( $query, $this->userSettingModel ) )->getState( $fbv );
227 }
228 return $query;
229 }
230
231 public function attachment_fields_to_edit( $form_fields, $post ) {
232 $screen = null;
233 if ( function_exists( 'get_current_screen' ) ) {
234 $screen = get_current_screen();
235
236 if ( ! is_null( $screen ) && 'attachment' === $screen->id ) {
237 return $form_fields;
238 }
239 }
240
241 $fbv_folder = FolderModel::getFolderFromPostId( $post->ID );
242 $fbv_folder = count( $fbv_folder ) > 0 ? $fbv_folder[0] : (object) array(
243 'folder_id' => 0,
244 'name' => __( 'Uncategorized', 'filebird' ),
245 );
246 $folder_name = esc_attr( $fbv_folder->name );
247 $folder_id = (int) $fbv_folder->folder_id;
248 $post_id = (int) $post->ID;
249
250 $form_fields['fbv'] = array(
251 'html' => "<div class='fbv-attachment-edit-wrapper' data-folder-id='{$folder_id}' data-attachment-id='{$post_id}'><input readonly type='text' value='{$folder_name}'/></div>",
252 'label' => esc_html__( 'FileBird folder:', 'filebird' ),
253 'helps' => esc_html__( 'Click on the folder name to move this file to another folder', 'filebird' ),
254 'input' => 'html',
255 );
256
257 return $form_fields;
258 }
259
260 public function edited_image_metadata( $new_image_meta, $new_attachment_id, $attachment_id ) {
261 $folder = FolderModel::getFolderFromPostId( $attachment_id );
262 if ( is_array( $folder ) && count( $folder ) > 0 ) {
263 if ( (int) $folder[0]->folder_id > 0 ) {
264 FolderModel::setFoldersForPosts( $new_attachment_id, (int) $folder[0]->folder_id );
265 }
266 }
267 return $new_image_meta;
268 }
269
270 public function attachment_fields_to_save( $post, $attachment ) {
271 if ( isset( $attachment['fbv'] ) ) {
272 FolderModel::setFoldersForPosts( $post['ID'], $attachment['fbv'] );
273 }
274 return $post;
275 }
276
277 public function actionPluploadUi() {
278 ?>
279 <div class="fbv-upload-inline">
280 <label for="fbv"><?php esc_html_e( 'Choose folder: ', 'filebird' ); ?></label>
281 <span id="fbv-folder-selector" class="fbv-folder-selector" name="fbv"></span>
282 </div>
283 <?php
284 }
285
286 public function getFlatTree( $data = array(), $parent = 0, $default = null, $level = 0 ) {
287 $tree = is_null( $default ) ? array() : $default;
288 foreach ( $data as $k => $v ) {
289 if ( $v->parent == $parent ) {
290 $node = array(
291 'title' => str_repeat( '-', $level ) . $v->name,
292 'value' => $v->id,
293 );
294 $tree[] = $node;
295 $children = $this->getFlatTree( $data, $v->id, null, $level + 1 );
296 foreach ( $children as $k2 => $child ) {
297 $tree[] = $child;
298 }
299 }
300 }
301 return $tree;
302 }
303
304 public function deleted_user( $id, $reassign, $user ) {
305 if ( $reassign === null ) {
306 FolderModel::deleteByAuthor( $id );
307 } else {
308 FolderModel::updateAuthor( $id, (int) $reassign );
309 }
310 }
311
312 public function users_have_additional_content( $users_have_content, $userids ) {
313 global $wpdb;
314 if ( $userids && ! $users_have_content ) {
315 if ( $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fbv WHERE created_by IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
316 $users_have_content = true;
317 }
318 }
319 return $users_have_content;
320 }
321 }