PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 5.3.2
FileBird – WordPress Media Library Folders & File Manager v5.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 / Controller / Folder.php

Folder.php in FileBird – WordPress Media Library Folders & File Manager 5.3.2, at includes/Controller/Folder.php

858 lines 26.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FileBird\Controller;
4
5 use FileBird\Controller\Convert as ConvertController;
6 use FileBird\Classes\Convert as ConvertModel;
7 use FileBird\Controller\Exclude;
8 use FileBird\Controller\UserSettings;
9 use FileBird\Controller\FolderUser;
10 use FileBird\Controller\Api;
11
12 use FileBird\Model\Folder as FolderModel;
13 use FileBird\Classes\Helpers as Helpers;
14 use FileBird\Classes\Tree;
15 use FileBird\Controller\Attachment\SizeMeta;
16
17 use FileBird\I18n as I18n;
18 use FileBird\Page\Settings;
19
20 defined( 'ABSPATH' ) || exit;
21
22 /**
23 * Folder Controller
24 */
25
26 class Folder extends Controller {
27 protected static $instance = null;
28 private $userSettings = null;
29
30 public static function getInstance() {
31 if ( null == self::$instance ) {
32 self::$instance = new self();
33 self::$instance->doHooks();
34 }
35 return self::$instance;
36 }
37
38 public function __construct() { }
39
40 private function doHooks() {
41 add_filter( 'media_library_infinite_scrolling', '__return_true' );
42 add_filter( 'ajax_query_attachments_args', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 );
43 add_filter( 'mla_media_modal_query_final_terms', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 );
44 add_filter( 'restrict_manage_posts', array( $this, 'restrictManagePosts' ) );
45 add_filter( 'posts_clauses', array( $this, 'postsClauses' ), 10, 2 );
46 add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 10, 2 );
47
48 add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminScripts' ), PHP_INT_MAX );
49 add_action( 'rest_api_init', array( $this, 'registerRestFields' ) );
50 add_action( 'add_attachment', array( $this, 'addAttachment' ) );
51 add_action( 'delete_attachment', array( $this, 'deleteAttachment' ) );
52 add_action( 'pre-upload-ui', array( $this, 'actionPluploadUi' ) );
53 add_action( 'wp_ajax_fbv_first_folder_notice', array( $this, 'ajax_first_folder_notice' ) );
54 // add_action( 'wp_ajax_fbv_close_buy_pro_dialog', array($this, 'ajaxCloseBuyProDialog'));
55 add_action( 'admin_notices', array( $this, 'adminNotices' ) );
56 add_action( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
57
58 $this->userSettings = UserSettings::getInstance();
59
60 // MailPoet plugin support
61 SizeMeta::getInstance()->doHooks();
62 new Exclude();
63 new Api();
64 new FolderUser();
65
66 $this->check_update_database();
67
68 add_filter( 'mailpoet_conflict_resolver_whitelist_script', array( $this, 'mailpoet_conflict_resolver_whitelist_script' ), 10, 1 );
69 add_filter( 'mailpoet_conflict_resolver_whitelist_style', array( $this, 'mailpoet_conflict_resolver_whitelist_style' ), 10, 1 );
70
71 add_filter( 'users_have_additional_content', array( $this, 'users_have_additional_content' ), 10, 2 );
72 add_action( 'deleted_user', array( $this, 'deleted_user' ), 10, 3 );
73 }
74
75 public function mailpoet_conflict_resolver_whitelist_script( $scripts ) {
76 $scripts[] = 'filebird';
77 $scripts[] = 'filebird-pro';
78 return $scripts;
79 }
80
81 public function mailpoet_conflict_resolver_whitelist_style( $styles ) {
82 $styles[] = 'filebird';
83 $styles[] = 'filebird-pro';
84 return $styles;
85 }
86
87 public function adminNotices() {
88 global $pagenow;
89
90 $optionFirstFolder = get_option( 'fbv_first_folder_notice' );
91 if ( FolderModel::countFolder() === 0 && $pagenow !== 'upload.php' &&
92 ( $optionFirstFolder === false || time() >= intval( $optionFirstFolder ) ) ) {
93 include NJFB_PLUGIN_PATH . '/views/notices/html-notice-first-folder.php';
94 }
95
96 if ( $pagenow !== 'upload.php' && apply_filters( 'fbv_update_database_notice', false ) ) {
97 include NJFB_PLUGIN_PATH . '/views/notices/html-notice-update-database.php';
98 }
99 }
100
101 public function ajax_first_folder_notice() {
102 check_ajax_referer( 'fbv_nonce', 'nonce', true );
103 update_option( 'fbv_first_folder_notice', time() + 30 * 60 * 60 * 24 ); //After 3 months show
104 wp_send_json_success();
105 }
106
107 public function check_update_database() {
108 $is_converted = get_option( 'fbv_old_data_updated_to_v4', '0' );
109 if ( $is_converted !== '1' ) {
110 if ( ConvertController::countOldFolders() > 0 && ! isset( $_GET['autorun'] ) ) {
111 add_filter( 'fbv_update_database_notice', '__return_true' );
112 }
113 }
114 }
115
116 public function registerRestFields() {
117 register_rest_route(
118 NJFB_REST_URL,
119 'get-folders',
120 array(
121 'methods' => 'GET',
122 'callback' => array( $this, 'ajaxGetFolder' ),
123 'permission_callback' => array( $this, 'resPermissionsCheck' ),
124 )
125 );
126 register_rest_route(
127 NJFB_REST_URL,
128 'gutenberg-get-folders',
129 array(
130 'methods' => 'GET',
131 'callback' => array( $this, 'ajaxGutenbergGetFolder' ),
132 'permission_callback' => array( $this, 'resPermissionsCheck' ),
133 )
134 );
135
136 register_rest_route(
137 NJFB_REST_URL,
138 'new-folder',
139 array(
140 'methods' => 'POST',
141 'callback' => array( $this, 'ajaxNewFolder' ),
142 'permission_callback' => array( $this, 'resPermissionsCheck' ),
143 )
144 );
145 register_rest_route(
146 NJFB_REST_URL,
147 'update-folder',
148 array(
149 'methods' => 'POST',
150 'callback' => array( $this, 'ajaxUpdateFolder' ),
151 'permission_callback' => array( $this, 'resPermissionsCheck' ),
152 )
153 );
154 register_rest_route(
155 NJFB_REST_URL,
156 'update-folder-ord',
157 array(
158 'methods' => 'POST',
159 'callback' => array( $this, 'ajaxUpdateFolderOrd' ),
160 'permission_callback' => array( $this, 'resPermissionsCheck' ),
161 )
162 );
163 register_rest_route(
164 NJFB_REST_URL,
165 'delete-folder',
166 array(
167 'methods' => 'POST',
168 'callback' => array( $this, 'ajaxDeleteFolder' ),
169 'permission_callback' => array( $this, 'resPermissionsCheck' ),
170 )
171 );
172 register_rest_route(
173 NJFB_REST_URL,
174 'set-folder-attachments',
175 array(
176 'methods' => 'POST',
177 'callback' => array( $this, 'ajaxSetFolder' ),
178 'permission_callback' => array( $this, 'resPermissionsCheck' ),
179 )
180 );
181 register_rest_route(
182 NJFB_REST_URL,
183 'generate-attachment-size',
184 array(
185 'methods' => \WP_REST_Server::CREATABLE,
186 'callback' => array( SizeMeta::getInstance(), 'apiCallback' ),
187 'permission_callback' => array( $this, 'resPermissionsCheck' ),
188 )
189 );
190 register_rest_route(
191 NJFB_REST_URL,
192 'update-tree',
193 array(
194 'methods' => 'POST',
195 'callback' => array( $this, 'ajaxUpdateTree' ),
196 'permission_callback' => array( $this, 'resPermissionsCheck' ),
197 )
198 );
199 register_rest_route(
200 NJFB_REST_URL,
201 'get-relations',
202 array(
203 'methods' => 'POST',
204 'callback' => array( $this, 'ajaxGetRelations' ),
205 'permission_callback' => array( $this, 'resPermissionsCheck' ),
206 )
207 );
208 register_rest_route(
209 NJFB_REST_URL,
210 'set-settings',
211 array(
212 'methods' => 'POST',
213 'callback' => array( $this, 'ajaxSetSettings' ),
214 'permission_callback' => array( $this, 'resPermissionsCheck' ),
215 )
216 );
217
218 register_rest_route(
219 NJFB_REST_URL,
220 'export-csv',
221 array(
222 'methods' => 'GET',
223 'callback' => array( $this, 'exportCSV' ),
224 'permission_callback' => array( $this, 'resPermissionsCheck' ),
225 )
226 );
227
228 register_rest_route(
229 NJFB_REST_URL,
230 'import-csv',
231 array(
232 'methods' => 'POST',
233 'callback' => array( $this, 'importCSV' ),
234 'permission_callback' => array( $this, 'resPermissionsCheck' ),
235 )
236 );
237
238 register_rest_route(
239 NJFB_REST_URL,
240 'import-csv-detail',
241 array(
242 'methods' => \WP_REST_Server::CREATABLE,
243 'callback' => array( $this, 'getImportCSVDetail' ),
244 'permission_callback' => array( $this, 'resPermissionsCheck' ),
245 )
246 );
247 }
248 public function resPermissionsCheck() {
249 return current_user_can( 'upload_files' );
250 }
251
252 public function enqueueAdminScripts( $screenId ) {
253 if ( function_exists( 'get_current_screen' ) ) {
254 if ( $screenId == 'upload.php' ) {
255 wp_register_script( 'jquery-resizable', NJFB_PLUGIN_URL . 'assets/js/jquery-resizable.min.js' );
256 wp_enqueue_script( 'jquery-resizable' );
257 }
258 }
259
260 if ( $screenId !== 'pagebuilders' ) {
261 wp_enqueue_script( 'fbv-setting', NJFB_PLUGIN_URL . 'assets/js/setting.js', array( 'jquery' ), NJFB_VERSION, false );
262 }
263
264 if ( $screenId === Settings::getInstance()->settingPageSuffix ) {
265 wp_enqueue_style( 'fbv-pro-tippy', NJFB_PLUGIN_URL . 'assets/css/tippy.min.css', array(), NJFB_VERSION );
266 }
267
268 wp_enqueue_script( 'jquery-ui-draggable' );
269 wp_enqueue_script( 'jquery-ui-droppable' );
270
271 if ( wp_is_mobile() ) {
272 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 );
273 }
274
275 wp_enqueue_script( 'fbv-folder', NJFB_PLUGIN_URL . 'assets/dist/js/app.js', array(), NJFB_VERSION, false );
276 wp_enqueue_script( 'fbv-lib', NJFB_PLUGIN_URL . 'assets/js/jstree/jstree.min.js', array(), NJFB_VERSION, false );
277
278 wp_enqueue_style( 'fbv-folder', NJFB_PLUGIN_URL . 'assets/dist/css/app.css', array(), NJFB_VERSION );
279 wp_style_add_data( 'fbv-folder', 'rtl', 'replace' );
280
281 wp_localize_script(
282 'fbv-folder',
283 'fbv_data',
284 apply_filters(
285 'fbv_data',
286 array(
287 'nonce' => wp_create_nonce( 'fbv_nonce' ),
288 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
289 'nonce_error' => __( 'Your request can\'t be processed.', 'filebird' ),
290 'current_folder' => ( ( isset( $_GET['fbv'] ) ) ? (int) sanitize_text_field( $_GET['fbv'] ) : -1 ), //-1: all files. 0: uncategorized
291 'folders' => FolderModel::allFolders( 'id as term_id, name as term_name', array( 'term_id', 'term_name' ) ),
292 'relations' => FolderModel::getRelations(),
293 'is_upload_screen' => 'upload.php' === $screenId ? '1' : '0',
294 'i18n' => I18n::getTranslation(),
295 'media_mode' => get_user_option( 'media_library_mode', get_current_user_id() ),
296 'json_url' => apply_filters( 'filebird_json_url', rtrim( rest_url( NJFB_REST_URL ), '/' ) ),
297 'media_url' => admin_url( 'upload.php' ),
298 'auto_import_url' => esc_url(
299 add_query_arg(
300 array(
301 'page' => 'filebird-settings',
302 'tab' => 'tools',
303 'autorun' => 'true',
304 ),
305 admin_url( 'admin.php' )
306 )
307 ),
308 'pll_lang' => apply_filters( 'fbv_pll_lang', '' ),
309 'is_new_user' => get_option( 'fbv_is_new_user', false ),
310 'import_other_plugins' => ConvertModel::getInstance()->get_plugin3rd_folders_to_import(),
311 'import_other_plugins_url' => esc_url(
312 add_query_arg(
313 array(
314 'page' => 'filebird-settings',
315 'tab' => 'import',
316 ),
317 admin_url( 'admin.php' )
318 )
319 ),
320 'update_database_notice' => apply_filters( 'fbv_update_database_notice', false ),
321 'utils' => new \stdClass(),
322 // 'close_buy_pro_dialog' => time() < get_option('fbv_close_buy_pro_dialog', time())
323 )
324 )
325 );
326 }
327
328 public function restrictManagePosts() {
329 $screen = get_current_screen();
330 if ( $screen->id == 'upload' ) {
331 $fbv = ( ( isset( $_GET['fbv'] ) ) ? (int) sanitize_text_field( $_GET['fbv'] ) : -1 );
332 $folders = FolderModel::allFolders();
333
334 $all = new \stdClass();
335 $all->id = -1;
336 $all->name = __( 'All Folders', 'filebird' );
337
338 $uncategorized = new \stdClass();
339 $uncategorized->id = 0;
340 $uncategorized->name = __( 'Uncategorized', 'filebird' );
341
342 array_unshift( $folders, $all, $uncategorized );
343 echo '<select name="fbv" id="filter-by-fbv" class="fbv-filter attachment-filters fbv">';
344 foreach ( $folders as $k => $folder ) {
345 echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $folder->id, $folder->name, selected( $folder->id, $fbv, false ) );
346 }
347 echo '</select>';
348 }
349 }
350
351 public function postsClauses( $clauses, $query ) {
352 global $wpdb;
353 if ( $query->get( 'post_type' ) !== 'attachment' ) {
354 return $clauses;
355 }
356
357 if ( Helpers::isListMode() && ! isset( $_GET['fbv'] ) ) {
358 return $clauses;
359 }
360
361 $fbvPropery = $query->get( 'fbv' );
362
363 if ( isset( $_GET['fbv'] ) || $fbvPropery !== '' ) {
364 $fbv = isset( $_GET['fbv'] ) ? (int) sanitize_text_field( $_GET['fbv'] ) : (int) $fbvPropery;
365
366 if ( $fbv === -1 ) {
367 return $clauses;
368 } elseif ( $fbv === 0 ) {
369 $clauses = FolderModel::getRelationsWithFolderUser( $clauses );
370 } else {
371 $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 ", $fbv );
372 $clauses['where'] .= ' AND fbva.folder_id IS NOT NULL';
373 }
374 }
375 return $clauses;
376 }
377 public function addAttachment( $post_id ) {
378 $fbv = ( ( isset( $_REQUEST['fbv'] ) ) ? sanitize_text_field( $_REQUEST['fbv'] ) : '' );
379 if ( $fbv != '' ) {
380 if ( is_numeric( $fbv ) ) {
381 $parent = $fbv;
382 } else {
383 $fbv = explode( '/', ltrim( rtrim( $fbv, '/' ), '/' ) );
384 $parent = (int) $fbv[0];
385 if ( $parent < 0 ) {
386 $parent = 0; //important
387 }
388 if ( apply_filters( 'fbv_auto_create_folders', true ) ) {
389 unset( $fbv[0] );
390 foreach ( $fbv as $k => $v ) {
391 $parent = FolderModel::newOrGet( $v, $parent );
392 }
393 }
394 }
395 FolderModel::setFoldersForPosts( $post_id, $parent );
396 }
397 }
398 public function deleteAttachment( $post_id ) {
399 FolderModel::deleteFoldersOfPost( $post_id );
400 }
401
402 public function ajaxQueryAttachmentsArgs( $query ) {
403 if ( isset( $_REQUEST['query']['fbv'] ) ) {
404 $fbv = $_REQUEST['query']['fbv'];
405 if ( is_array( $fbv ) ) {
406 $fbv = array_map( 'intval', $fbv );
407 } else {
408 $fbv = intval( $fbv );
409 }
410 $query['fbv'] = $fbv;
411 }
412 return $query;
413 }
414 public function ajaxGetFolder( $request ) {
415 $icl_lang = $request->get_param( 'icl_lang' );
416 $pll_lang = $request->get_param( 'pll_lang' );
417 $sort = sanitize_text_field( $request->get_param( 'sort' ) );
418
419 $order_by = null;
420 $sort_option = 'reset';
421
422 $lang = null;
423
424 if ( ! is_null( $icl_lang ) ) {
425 $lang = $icl_lang;
426 }
427 if ( ! is_null( $pll_lang ) ) {
428 $lang = $pll_lang;
429 }
430
431 if ( \in_array( $sort, array( 'name_asc', 'name_desc', 'reset' ) ) ) {
432 if ( 'name_asc' === $sort ) {
433 $order_by = 'LENGTH(name) ASC, name ASC';
434 $sort_option = $sort;
435 } elseif ( 'name_desc' === $sort ) {
436 $order_by = 'LENGTH(name) DESC, name DESC';
437 $sort_option = $sort;
438 }
439 update_option( 'njt_fb_sort_folder', $sort_option );
440 } else {
441 $njt_fb_sort_folder = get_option( 'njt_fb_sort_folder', 'reset' );
442 if ( $njt_fb_sort_folder === 'reset' ) {
443 $order_by = null;
444 } elseif ( $njt_fb_sort_folder === 'name_asc' ) {
445 $order_by = 'name asc';
446 } elseif ( $njt_fb_sort_folder === 'name_desc' ) {
447 $order_by = 'name desc';
448 }
449 }
450
451 $tree = Tree::getFolders( $order_by, false );
452
453 wp_send_json_success(
454 array(
455 'tree' => $tree,
456 'folder_count' => array(
457 'total' => Tree::getCount( -1, $lang ),
458 'folders' => Tree::getAllFoldersAndCount( $lang ),
459 ),
460 )
461 );
462 }
463 public function ajaxGutenbergGetFolder() {
464 $_folders = Tree::getFolders( null, true, 0, true );
465 $folders = array(
466 array(
467 'value' => 0,
468 'label' => __( 'Please choose folder', 'filebird' ),
469 'disabled' => true,
470 ),
471 );
472 foreach ( $_folders as $k => $v ) {
473 $folders[] = array(
474 'value' => $v['id'],
475 'label' => $v['text'],
476 );
477 }
478
479 wp_send_json_success( $folders );
480 }
481 public function ajaxNewFolder( $request ) {
482 $name = $request->get_param( 'name' );
483 $parent = $request->get_param( 'parent' );
484 $name = isset( $name ) ? sanitize_text_field( wp_unslash( $name ) ) : '';
485 $parent = isset( $parent ) ? sanitize_text_field( $parent ) : '';
486 if ( $name != '' && $parent != '' ) {
487 $insert = FolderModel::newOrGet( $name, $parent, false );
488 if ( $insert !== false ) {
489 wp_send_json_success( array( 'id' => $insert ) );
490 } else {
491 wp_send_json_error( array( 'mess' => __( 'A folder with this name already exists. Please choose another one.', 'filebird' ) ) );
492 }
493 } else {
494 wp_send_json_error(
495 array(
496 'mess' => __( 'Validation failed', 'filebird' ),
497 )
498 );
499 }
500 }
501 public function ajaxUpdateFolder( $request ) {
502 $id = $request->get_param( 'id' );
503 $parent = $request->get_param( 'parent' );
504 $name = $request->get_param( 'name' );
505
506 $id = isset( $id ) ? sanitize_text_field( $id ) : '';
507 $parent = isset( $parent ) ? intval( sanitize_text_field( $parent ) ) : '';
508 $name = isset( $name ) ? sanitize_text_field( wp_unslash( $name ) ) : '';
509 if ( is_numeric( $id ) && is_numeric( $parent ) && $name != '' ) {
510 $update = FolderModel::updateFolderName( $name, $parent, $id );
511 if ( $update === true ) {
512 wp_send_json_success();
513 } else {
514 wp_send_json_error( array( 'mess' => __( 'A folder with this name already exists. Please choose another one.', 'filebird' ) ) );
515 }
516 }
517 wp_send_json_error();
518 }
519 public function ajaxUpdateFolderOrd( $request ) {
520 $id = $request->get_param( 'id' );
521 $parent = $request->get_param( 'parent' );
522 $ord = $request->get_param( 'ord' );
523
524 $id = isset( $id ) ? sanitize_text_field( $id ) : '';
525 $parent = isset( $parent ) ? sanitize_text_field( wp_unslash( $parent ) ) : '';
526 $ord = isset( $ord ) ? sanitize_text_field( wp_unslash( $ord ) ) : '';
527 if ( is_numeric( $id ) && is_numeric( $parent ) && is_numeric( $ord ) ) {
528 FolderModel::updateOrdAndParent( $id, $ord, $parent );
529 wp_send_json_success();
530 }
531 wp_send_json_error();
532 }
533 public function ajaxDeleteFolder( $request ) {
534 $ids = $request->get_param( 'ids' );
535 $ids = isset( $ids ) ? Helpers::sanitize_array( $ids ) : '';
536 if ( $ids != '' ) {
537 if ( ! is_array( $ids ) ) {
538 $ids = array( $ids );
539 }
540 $ids = array_map( 'intval', $ids );
541
542 foreach ( $ids as $k => $v ) {
543 if ( $v > 0 ) {
544 FolderModel::deleteFolderAndItsChildren( $v );
545 }
546 }
547 wp_send_json_success();
548 }
549 wp_send_json_error(
550 array(
551 'mess' => __( 'Can\'t delete folder, please try again later', 'filebird' ),
552 )
553 );
554 }
555 public function ajaxSetFolder( $request ) {
556 $ids = $request->get_param( 'ids' );
557 $folder = $request->get_param( 'folder' );
558
559 $ids = isset( $ids ) ? Helpers::sanitize_array( $ids ) : '';
560 $folder = isset( $folder ) ? sanitize_text_field( $folder ) : '';
561 if ( $ids != '' && is_array( $ids ) && is_numeric( $folder ) ) {
562 FolderModel::setFoldersForPosts( $ids, $folder );
563 wp_send_json_success();
564 }
565 wp_send_json_error(
566 array(
567 'mess' => __( 'Validation failed', 'filebird' ),
568 )
569 );
570 }
571 public function ajaxUpdateTree( $request ) {
572 $tree = $request->get_param( 'tree' );
573
574 $tree = isset( $tree ) ? sanitize_text_field( $tree ) : '';
575 if ( $tree != '' ) {
576 $tree = preg_replace( '#[^0-9,()]#', '', $tree );
577 FolderModel::rawInsert( '(id, ord, parent) VALUES ' . $tree . ' ON DUPLICATE KEY UPDATE ord=VALUES(ord),parent=VALUES(parent)' );
578 wp_send_json_success(
579 array(
580 'mess' => __( 'Folder tree has been updated.', 'filebird' ),
581 )
582 );
583 }
584 wp_send_json_error(
585 array(
586 'mess' => __( 'Validation failed', 'filebird' ),
587 )
588 );
589 }
590 public function ajaxGetRelations() {
591 wp_send_json_success(
592 array(
593 'relations' => FolderModel::getRelations(),
594 )
595 );
596 }
597 public function ajaxSetSettings( $request ) {
598 $folder_id = $request->get_param( 'folder_id' );
599
600 $folder_id = isset( $folder_id ) ? intval( $folder_id ) : -1;
601 $this->userSettings->setDefaultSelectedFolder( $folder_id );
602 wp_send_json_success();
603 }
604
605 public function attachment_fields_to_edit( $form_fields, $post ) {
606 $screen = null;
607 if ( function_exists( 'get_current_screen' ) ) {
608 $screen = get_current_screen();
609
610 if ( ! is_null( $screen ) && 'attachment' === $screen->id ) {
611 return $form_fields;
612 }
613 }
614
615 $fbv_folder = FolderModel::getFolderFromPostId( $post->ID );
616 $fbv_folder = count( $fbv_folder ) > 0 ? $fbv_folder[0] : (object) array(
617 'folder_id' => 0,
618 'name' => __( 'Uncategorized', 'filebird' ),
619 );
620 $form_fields['fbv'] = array(
621 'html' => "<div class='fbv-attachment-edit-wrapper' data-folder-id='{$fbv_folder->folder_id}' data-attachment-id='{$post->ID}'><input readonly type='text' value='{$fbv_folder->name}'/></div>",
622 'label' => esc_html__( 'FileBird folder:', 'filebird' ),
623 'helps' => esc_html__( 'Click on the button to move this file to another folder', 'filebird' ),
624 'input' => 'html',
625 );
626
627 return $form_fields;
628 }
629
630 public function attachment_fields_to_save( $post, $attachment ) {
631 if ( isset( $attachment['fbv'] ) ) {
632 FolderModel::setFoldersForPosts( $post['ID'], $attachment['fbv'] );
633 }
634 return $post;
635 }
636
637 private static function addFolderToZip( &$zip, $children, $parent_dir = '' ) {
638 foreach ( $children as $k => $v ) {
639 $folder_name = $v->name;
640 $folder_id = $v->id;
641
642 // $folder_name = sanitize_title( $folder_name );
643
644 $attachment_ids = Helpers::getAttachmentIdsByFolderId( $folder_id );
645 $empty_dir = $parent_dir != '' ? $parent_dir . '/' . $folder_name : $folder_name;
646 $zip->addEmptyDir( $empty_dir );
647
648 foreach ( $attachment_ids as $k => $id ) {
649 $file = get_attached_file( $id );
650 if ( $file ) {
651 $zip->addFile( $file, $empty_dir . '/' . \basename( $file ) );
652 }
653 }
654 if ( \is_array( $v->children ) ) {
655 self::addFolderToZip( $zip, $v->children, $empty_dir );
656 }
657 }
658 }
659 public function actionPluploadUi() {
660 $this->loadView( 'particle/folder_dropdown' );
661 }
662 private function _buildQuery( $tree, $parent ) {
663 $results = array();
664 $ord = 0;
665 foreach ( $tree as $k => $v ) {
666 // if($v['key'] < 1) continue;
667 $results[] = sprintf( '(%1$d, %2$d, %3$d)', $v['id'], $ord, $parent );
668 if ( isset( $v['children'] ) && is_array( $v['children'] ) && count( $v['children'] ) > 0 ) {
669 $children = $this->_buildQuery( $v['children'], $v['id'] );
670 foreach ( $children as $k2 => $v2 ) {
671 $results[] = $v2;
672 }
673 }
674 $ord++;
675 }
676 return $results;
677 }
678
679 public function getFlatTree( $data = array(), $parent = 0, $default = null, $level = 0 ) {
680 $tree = is_null( $default ) ? array() : $default;
681 foreach ( $data as $k => $v ) {
682 if ( $v->parent == $parent ) {
683 $node = array(
684 'title' => str_repeat( '-', $level ) . $v->name,
685 'value' => $v->id,
686 );
687 $tree[] = $node;
688 $children = $this->getFlatTree( $data, $v->id, null, $level + 1 );
689 foreach ( $children as $k2 => $child ) {
690 $tree[] = $child;
691 }
692 }
693 }
694 return $tree;
695 }
696
697 public function exportCSV() {
698 global $wpdb;
699
700 $folders = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbv", ARRAY_A );
701 $attachmentIds = $wpdb->get_results( "SELECT folder_id, GROUP_CONCAT(attachment_id SEPARATOR '|') as attachment_ids FROM {$wpdb->prefix}fbv_attachment_folder GROUP BY (folder_id)", OBJECT_K );
702
703 foreach ( $folders as $key => $folder ) {
704 $folders[ $key ]['attachment_ids'] = $attachmentIds[ $folder['id'] ]->attachment_ids ? $attachmentIds[ $folder['id'] ]->attachment_ids : '';
705 }
706
707 return new \WP_REST_Response( array( 'folders' => $folders ) );
708 }
709
710 public function buildTree( array &$data, $parentId = 0 ) {
711 $tree = array();
712 foreach ( $data as &$node ) {
713 if ( $node['parent'] == $parentId ) {
714 $children = $this->buildTree( $data, $node['id'] );
715 if ( $children ) {
716 $node['children'] = $children;
717 }
718 $tree[] = $node;
719 unset( $node );
720 }
721 }
722 return $tree;
723 }
724
725 public function run_import_folders( $folders, $parent = 0 ) {
726 $folders_created = array();
727
728 foreach ( $folders as $folder ) {
729 $new_folder_id = FolderModel::newOrGet( $folder['name'], $parent );
730 $attachment_ids = ! empty( $folder['attachment_ids'] ) ? explode( '|', $folder['attachment_ids'] ) : false;
731 array_push( $folders_created, $folder['id'] );
732
733 if ( $attachment_ids && false !== $new_folder_id ) {
734 FolderModel::setFoldersForPosts( $attachment_ids, $new_folder_id );
735 }
736
737 if ( isset( $folder['children'] ) && count( $folder['children'] ) > 0 ) {
738 $new_child_folders = $this->run_import_folders( $folder['children'], $new_folder_id );
739 $folders_created = array_merge( $folders_created, $new_child_folders );
740 }
741 }
742
743 return $folders_created;
744 }
745
746 public function restoreFolderStructure( $folders ) {
747 $tree = $this->buildTree( $folders );
748 $folders_created = $this->run_import_folders( $tree );
749
750 $mess = sprintf( __( 'Congratulations! We imported successfully %d folders into <strong>FileBird.</strong>', 'filebird' ), count( $folders_created ) );
751
752 return new \WP_REST_Response( array( 'mess' => $mess ) );
753 }
754
755 public function readCSV( \WP_REST_Request $request ) {
756 $params = $request->get_file_params();
757 $handle = \fopen( $params['file']['tmp_name'], 'r' );
758 $data = array();
759 $columns = array();
760 if ( false !== $handle ) {
761 $count = 1;
762 while ( 1 ) {
763 $row = fgetcsv( $handle, 0 );
764 if ( 1 === $count ) {
765 $columns = $row;
766 $count++;
767 continue;
768 }
769 if ( false === $row ) {
770 break;
771 }
772 foreach ( $columns as $key => $col ) {
773 $tmp[ $col ] = $row[ $key ];
774 }
775 $data[] = $tmp;
776 }
777 }
778 \fclose( $handle );
779
780 $check = array_diff(
781 $columns,
782 array(
783 'id',
784 'name',
785 'parent',
786 'type',
787 'ord',
788 'created_by',
789 'attachment_ids',
790 )
791 );
792
793 if ( count( $check ) > 0 ) {
794 return new \WP_REST_Response(
795 array(
796 'success' => false,
797 'message' => __( 'The uploaded file was not generated by FileBird. Please check again.', 'filebird' ),
798 )
799 );
800 }
801
802 return $data;
803 }
804
805 public function importCSV( \WP_REST_Request $request ) {
806 $data = $this->readCSV( $request );
807 $createdBy = intval( $request->get_param( 'userId' ) );
808
809 if ( $createdBy != '-1' ) {
810 $data = array_filter(
811 $data,
812 function( $item ) use ( $createdBy ) {
813 return $item['created_by'] == $createdBy;
814 }
815 );
816 }
817
818 $result = $this->restoreFolderStructure( $data );
819
820 return new \WP_REST_Response( array( 'success' => $result ) );
821 }
822
823 public function getImportCSVDetail( \WP_REST_Request $request ) {
824 $data = $this->readCSV( $request );
825
826 $users = \get_users(
827 array(
828 'include' => array_unique( array_column( $data, 'created_by' ) ),
829 )
830 );
831
832 $usersReturn = array();
833
834 foreach ( $users as $user ) {
835 $usersReturn[ $user->ID ] = $user->data->display_name . ' ' . __( 'folders', 'filebird' );
836 }
837
838 return new \WP_REST_Response( $usersReturn );
839 }
840
841 public function deleted_user( $id, $reassign, $user ) {
842 if ( $reassign === null ) {
843 FolderModel::deleteByAuthor( $id );
844 } else {
845 FolderModel::updateAuthor( $id, (int) $reassign );
846 }
847 }
848 public function users_have_additional_content( $users_have_content, $userids ) {
849 global $wpdb;
850 if ( $userids && ! $users_have_content ) {
851 if ( $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fbv WHERE created_by IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
852 $users_have_content = true;
853 }
854 }
855 return $users_have_content;
856 }
857 }
858