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 / Folder.php

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

527 lines 16.9 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\Classes\Helpers;
5
6 defined( 'ABSPATH' ) || exit;
7
8 class Folder {
9 private static $folder_table = 'fbv';
10 private static $relation_table = 'fbv_attachment_folder';
11
12 const ALL_CATEGORIES = -1;
13 const UN_CATEGORIZED = 0;
14 const PREVIOUS_FOLDER = -2;
15
16 public static function allFolders( $select = '*', $prepend_default = null, $order_by = null, $order = null, $search = null ) {
17 //TODO need to convert ord to number using +0
18 global $wpdb;
19
20 $conditions = array(
21 '1 = 1',
22 'created_by = ' . apply_filters( 'fbv_folder_created_by', 0 ),
23 );
24
25 if ( ! empty( $search ) ) {
26 $conditions[] = "name LIKE '%" . $wpdb->esc_like( $search ) . "%'";
27 }
28 $conditions = implode( ' AND ', $conditions );
29 $sql = "SELECT $select FROM " . self::getTable( self::$folder_table ) . ' WHERE ' . $conditions . ' ORDER BY `ord` ASC';
30
31 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
32 $folders = $wpdb->get_results( $sql );
33
34 if ( 'name' === $order_by && in_array( $order, array( 'asc', 'desc' ), true ) ) {
35 usort( $folders, array( __CLASS__, "sort_natural_$order" ) );
36 }
37
38 if ( is_array( $prepend_default ) ) {
39 $all = new \stdClass();
40 $all->{$prepend_default[0]} = -1;
41 $all->{$prepend_default[1]} = __( 'All Folders', 'filebird' );
42
43 $uncategorized = new \stdClass();
44 $uncategorized->{$prepend_default[0]} = 0;
45 $uncategorized->{$prepend_default[1]} = __( 'Uncategorized', 'filebird' );
46
47 array_unshift( $folders, $all, $uncategorized );
48 }
49 return $folders;
50 }
51 private static function sort_natural_asc( $a, $b ) {
52 return strnatcasecmp( $a->name, $b->name );
53 }
54 private static function sort_natural_desc( $a, $b ) {
55 return strnatcasecmp( $a->name, $b->name ) * -1;
56 }
57
58 public static function countFolder() {
59 global $wpdb;
60 return intval( $wpdb->get_var( 'SELECT count(*) as c FROM ' . self::getTable( self::$folder_table ) ) );
61 }
62
63 public static function updateOrdAndParent( $id, $new_ord, $new_parent ) {
64 global $wpdb;
65 $wpdb->update(
66 self::getTable( self::$folder_table ),
67 array(
68 'parent' => $new_parent,
69 'ord' => $new_ord,
70 ),
71 array( 'id' => $id ),
72 array( '%d', '%d' ),
73 array( '%d' )
74 );
75 }
76 public static function verifyAuthor( $folder_id, $current_user_id, $folder_per_user = false ) {
77 global $wpdb;
78 if ( $folder_per_user ) {
79 $created_by = (int) $wpdb->get_var( $wpdb->prepare( "SELECT `created_by` FROM {$wpdb->prefix}fbv WHERE `id` = %d", $folder_id ) );
80 return $created_by == $current_user_id;
81 }
82 return true;
83 }
84 public static function updateAuthor( $from_author, $to_author ) {
85 global $wpdb;
86 $wpdb->update(
87 self::getTable( self::$folder_table ),
88 array(
89 'created_by' => $to_author,
90 ),
91 array( 'created_by' => $from_author ),
92 array( '%d' ),
93 array( '%d' )
94 );
95 }
96 public static function deleteByAuthor( $author ) {
97 global $wpdb;
98 $wpdb->query( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder WHERE folder_id IN (SELECT id FROM {$wpdb->prefix}fbv WHERE created_by = " . (int) $author . ')' );
99 $wpdb->query( "DELETE FROM {$wpdb->prefix}fbv WHERE created_by = " . (int) $author );
100 }
101
102 public static function rawInsert( $query ) {
103 global $wpdb;
104 $wpdb->query( 'INSERT INTO ' . self::getTable( self::$folder_table ) . ' ' . $query );
105 }
106
107 public static function getFoldersOfPost( $post_id ) {
108 global $wpdb;
109 return $wpdb->get_col( 'SELECT `folder_id` FROM ' . self::getTable( self::$relation_table ) . ' WHERE `attachment_id` = ' . (int) $post_id . ' GROUP BY `folder_id`' );
110 }
111
112 public static function getFolderFromPostId( $post_id ) {
113 global $wpdb;
114
115 $created = 0;
116 $user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
117 if ( $user_has_own_folder ) {
118 $created = get_current_user_id();
119 }
120 return $wpdb->get_results(
121 $wpdb->prepare(
122 "SELECT `folder_id`,`name` FROM {$wpdb->prefix}fbv as fbv
123 JOIN {$wpdb->prefix}fbv_attachment_folder as fbva ON fbv.id = fbva.folder_id
124 WHERE `attachment_id` = %d AND `created_by` = %d GROUP BY `folder_id`",
125 $post_id,
126 $created
127 ),
128 OBJECT
129 );
130 }
131
132 private static function getNestedFolder() {
133 global $wpdb;
134
135 $counterType = UserSettingModel::getInstance()->get( 'FOLDER_COUNTER_TYPE' );
136 $isUsed = apply_filters( 'fbv_counter_type', $counterType ) === 'counter_file_in_folder_and_sub';
137
138 if ( ! $isUsed ) {
139 return array();
140 }
141
142 $query = $wpdb->prepare(
143 "SELECT parent,GROUP_CONCAT(id) as child
144 FROM {$wpdb->prefix}fbv
145 WHERE created_by = %d
146 GROUP BY parent",
147 apply_filters( 'fbv_folder_created_by', 0 )
148 );
149
150 $result = $wpdb->get_results( $query );
151 $nestedFolder = array();
152
153 foreach ( $result as $v ) {
154 if ( $v->parent !== $v->child ) {
155 $nestedFolder[ $v->parent ] = explode( ',', $v->child );
156 }
157 }
158
159 return $nestedFolder;
160 }
161
162 private static function getNestedCountAttachments( $counters, $nestedFolder, $folder_id ) {
163 $c = 0;
164
165 if ( isset( $counters[ $folder_id ] ) ) {
166 $c = intval( $counters[ $folder_id ]->counter );
167 }
168
169 if ( ! isset( $nestedFolder[ $folder_id ] ) ) {
170 return $c;
171 }
172
173 $total = $c;
174
175 foreach ( $nestedFolder[ $folder_id ] as $folder_id => $children_id ) {
176 $total += self::getNestedCountAttachments( $counters, $nestedFolder, $children_id );
177 }
178
179 return $total;
180 }
181
182 public static function countAttachments( $lang = null ) {
183 global $wpdb;
184
185 $query = $wpdb->prepare(
186 "SELECT folder_id, count(attachment_id) as counter
187 FROM {$wpdb->prefix}posts AS `posts`
188 INNER JOIN {$wpdb->prefix}fbv_attachment_folder AS `fbva` ON (fbva.attachment_id = posts.ID AND posts.post_type = 'attachment')
189 INNER JOIN {$wpdb->prefix}fbv AS `fbv` ON (fbva.folder_id = fbv.id AND fbv.created_by = %d)
190 WHERE posts.post_status != 'trash'
191 GROUP BY folder_id",
192 apply_filters( 'fbv_folder_created_by', 0 )
193 );
194
195 $nestedFolder = self::getNestedFolder();
196 $query = apply_filters( 'fbv_all_folders_and_count', $query, $lang );
197
198 $counters = $wpdb->get_results( $query, OBJECT_K );
199 $formattedCounter = array();
200 $actualCounter = array();
201
202 foreach ( $nestedFolder as $folder_id => $counter ) {
203 $formattedCounter[ $folder_id ] = self::getNestedCountAttachments( $counters, $nestedFolder, $folder_id );
204 }
205
206 foreach ( $counters as $counter ) {
207 $actualCounter[ $counter->folder_id ] = $counter->counter;
208 }
209
210 return array(
211 'display' => array_replace( $actualCounter, $formattedCounter ),
212 'actual' => $actualCounter,
213 );
214 }
215
216 public static function assignFolder( int $folderId, array $attachmentIds, string $lang ) {
217 global $wpdb;
218
219 $ids = implode( ',', $attachmentIds );
220
221 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
222 $cleanQuery = $wpdb->prepare(
223 "DELETE `fbva` FROM {$wpdb->prefix}fbv_attachment_folder AS `fbva`
224 INNER JOIN {$wpdb->prefix}fbv AS `fbv`
225 ON (fbva.folder_id = fbv.id AND fbv.created_by = %d)
226 WHERE attachment_id IN ({$ids})",
227 apply_filters( 'fbv_folder_created_by', 0 )
228 );
229
230 $wpdb->query( $cleanQuery );
231
232 if ( $folderId > 0 ) {
233 $prepareInsert = '';
234
235 foreach ( $attachmentIds as $attachmentId ) {
236 $prepareInsert .= "($folderId, $attachmentId),";
237 }
238
239 $prepareInsert = rtrim( $prepareInsert, ',' );
240
241 $insertQuery = "INSERT INTO {$wpdb->prefix}fbv_attachment_folder ( folder_id, attachment_id ) VALUES {$prepareInsert}";
242
243 $wpdb->query( $insertQuery );
244 }
245 do_action( 'fbv_after_assign_folder', $folderId, $attachmentIds );
246 // Clean cache in wordpress.com
247 if ( count( $attachmentIds ) > 0 ) {
248 clean_post_cache( $attachmentIds[0] );
249 }
250
251 return self::countAttachments( $lang );
252 }
253
254 public static function setFoldersForPosts( $post_ids, $folder_ids, $has_action = true ) {
255 global $wpdb;
256 if ( ! is_array( $post_ids ) ) {
257 $post_ids = array( $post_ids );
258 }
259 if ( ! is_array( $folder_ids ) ) {
260 $folder_ids = array( $folder_ids );
261 }
262 $user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
263 $current_user_id = get_current_user_id();
264
265 foreach ( $folder_ids as $k => $folder_id ) {
266 $folder_id = (int) $folder_id;
267 foreach ( $post_ids as $k2 => $post_id ) {
268 do_action( 'fbv_before_setting_folder', (int) $post_id, $folder_id );
269 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder WHERE `attachment_id` = %d AND `folder_id` IN (SELECT `id` FROM {$wpdb->prefix}fbv WHERE `created_by` = %d)", (int) $post_id, $user_has_own_folder ? $current_user_id : 0 ) );
270 if ( $folder_id > 0 ) {
271 $wpdb->insert(
272 self::getTable( self::$relation_table ),
273 array(
274 'attachment_id' => (int) $post_id,
275 'folder_id' => $folder_id,
276 ),
277 array( '%d', '%d' )
278 );
279 }
280 if ( $has_action === true ) {
281 do_action( 'fbv_after_set_folder', $post_id, $folder_id );
282 }
283 }
284 }
285 if ( count( $post_ids ) > 0 ) {
286 clean_post_cache( $post_ids[0] );
287 }
288 }
289 public static function detail( $name, $parent ) {
290 global $wpdb;
291 $name = wp_kses_post( $name );
292 $user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
293 if ( $user_has_own_folder ) {
294 $created = get_current_user_id();
295 } else {
296 $created = 0;
297 }
298 return $wpdb->get_row(
299 $wpdb->prepare(
300 "SELECT * FROM {$wpdb->prefix}fbv WHERE `name` = %s AND `parent` = %d AND `created_by` = %d",
301 $name,
302 $parent,
303 $created
304 )
305 );
306 }
307 public static function findById( $folder_id, $select = 'id' ) {
308 global $wpdb;
309 $query = 'SELECT ' . $select . ' FROM ' . self::getTable( self::$folder_table ) . " WHERE `id` = '" . (int) $folder_id . "'";
310 return $wpdb->get_row( $query );
311 }
312
313 public static function isFolderExist( $folder_id ) {
314 return self::findById( $folder_id ) !== null;
315 }
316
317 public static function updateFolderName( $new_name, $parent, $folder_id ) {
318 global $wpdb;
319 $new_name = sanitize_text_field( wp_unslash( wp_kses_post( $new_name ) ) );
320 $new_name = Helpers::sanitize_for_excel( $new_name );
321 $exist_name = $wpdb->get_row(
322 $wpdb->prepare(
323 "SELECT * FROM {$wpdb->prefix}fbv WHERE id != %d AND name = %s AND parent = %d",
324 $folder_id,
325 $new_name,
326 $parent
327 )
328 );
329
330 if ( \is_null( $exist_name ) ) {
331 $wpdb->update(
332 self::getTable( self::$folder_table ),
333 array( 'name' => $new_name ),
334 array( 'id' => $folder_id ),
335 array( '%s' ),
336 array( '%d' )
337 );
338 do_action( 'fbv_after_folder_renamed', $folder_id, $new_name );
339 return true;
340 }
341
342 return false;
343 }
344 public static function updateParent( $folder_id, $new_parent ) {
345 global $wpdb;
346 $wpdb->update(
347 self::getTable( self::$folder_table ),
348 array( 'parent' => $new_parent ),
349 array( 'id' => $folder_id ),
350 array( '%d' ),
351 array( '%d' )
352 );
353 do_action( 'fbv_after_parent_updated', $folder_id, $new_parent );
354 }
355 public static function deleteAll() {
356 global $wpdb;
357 $wpdb->query( "DELETE FROM {$wpdb->prefix}fbv" );
358 $wpdb->query( "DELETE FROM {$wpdb->prefix}fbv_attachment_folder" );
359 do_action( 'fbv_after_delete_all' );
360 }
361 public static function deleteFolderAndItsChildren( $id ) {
362 global $wpdb;
363 $wpdb->delete( self::getTable( self::$folder_table ), array( 'id' => $id ), array( '%d' ) );
364 $wpdb->delete( self::getTable( self::$relation_table ), array( 'folder_id' => $id ), array( '%d' ) );
365 $folder_colors = get_option( 'fbv_folder_colors', array() );
366
367 if ( ! empty( $folder_colors ) && isset( $folder_colors[ $id ] ) ) {
368 unset( $folder_colors[ $id ] );
369 update_option( 'fbv_folder_colors', $folder_colors );
370 }
371
372 //delete it's children
373 $children = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fbv WHERE parent = %d", $id ) );
374 foreach ( $children as $k => $child ) {
375 self::deleteFolderAndItsChildren( $child );
376 }
377 }
378
379 public static function newFolder( $name, $parent = 0 ) {
380 global $wpdb;
381
382 $ord = $wpdb->get_var( $wpdb->prepare( "SELECT MAX(ord) FROM {$wpdb->prefix}fbv WHERE parent = %d AND created_by = %d", $parent, apply_filters( 'fbv_folder_created_by', 0 ) ) );
383
384 $name = sanitize_text_field( wp_kses_post( Helpers::sanitize_for_excel( $name ) ) );
385
386 $data = array(
387 'name' => $name,
388 'parent' => $parent,
389 'type' => 0,
390 'created_by' => apply_filters( 'fbv_folder_created_by', 0 ),
391 'ord' => is_null( $ord ) ? 0 : ( intval( $ord ) + 1 ),
392 );
393
394 $inserted = $wpdb->insert( self::getTable( self::$folder_table ), $data );
395
396 if ( $inserted ) {
397 $insertId = $wpdb->insert_id;
398
399 $data = array(
400 'title' => $name,
401 'id' => $insertId,
402 'key' => $insertId,
403 'type' => 0,
404 'parent' => $parent,
405 'children' => array(),
406 'data-count' => 0,
407 'data-id' => $insertId,
408 );
409 do_action( 'fbv_after_folder_created', $insertId, $data );
410 return $data;
411 }
412
413 return false;
414 }
415
416 public static function newOrGet( $name, $parent, $return_id_if_exist = true ) {
417 $check = self::detail( $name, $parent );
418 if ( is_null( $check ) ) {
419 return self::newFolder( $name, $parent );
420 } else {
421 return $return_id_if_exist ? array( 'id' => (int) $check->id ) : false;
422 }
423 }
424
425 public static function deleteFoldersOfPost( $post_id ) {
426 global $wpdb;
427 $wpdb->delete(
428 self::getTable( self::$relation_table ),
429 array( 'attachment_id' => $post_id ),
430 array( '%d' )
431 );
432 }
433
434 public static function getChildrenOfFolder( $folder_id, $index = 0 ) {
435 global $wpdb;
436 $detail = null;
437 if ( $index == 0 ) {
438 $detail = $wpdb->get_results( 'SELECT name, id FROM ' . $wpdb->prefix . 'fbv WHERE id = ' . (int) $folder_id );
439 }
440 $children = $wpdb->get_results( 'SELECT name, id FROM ' . $wpdb->prefix . 'fbv WHERE parent = ' . (int) $folder_id );
441 foreach ( $children as $k => $v ) {
442 $children[ $k ]->children = self::getChildrenOfFolder( $v->id, $index + 1 );
443 }
444 if ( $detail != null ) {
445 $return = new \stdClass();
446 $return->id = $detail[0]->id;
447 $return->name = $detail[0]->name;
448 $return->children = $children;
449
450 return $return;
451 }
452 return $children;
453 }
454
455 private static function getTable( $table ) {
456 global $wpdb;
457 return $wpdb->prefix . $table;
458 }
459
460 public static function getRelationsWithFolderUser( $clauses ) {
461 global $wpdb;
462
463 $attachment_in_folder = $wpdb->prepare(
464 "SELECT attachment_id
465 FROM {$wpdb->prefix}fbv_attachment_folder AS fbva
466 JOIN {$wpdb->prefix}fbv AS fbv ON fbva.folder_id = fbv.id
467 GROUP BY attachment_id
468 HAVING FIND_IN_SET(%d, GROUP_CONCAT(created_by))",
469 apply_filters( 'fbv_folder_created_by', 0 )
470 );
471
472 $clauses['where'] .= " AND {$wpdb->posts}.ID NOT IN ($attachment_in_folder) ";
473
474 return $clauses;
475 }
476
477 public static function delete( array $ids, $lang ) {
478 $folderColors = get_option( 'fbv_folder_colors', array() );
479
480 self::_delete( $ids, $folderColors );
481 update_option( 'fbv_folder_colors', $folderColors );
482
483 return self::countAttachments( $lang );
484 }
485
486 public static function getChildrenIds( int $id ) {
487 global $wpdb;
488
489 return $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fbv WHERE parent = %d", $id ) );
490 }
491
492 public static function _delete( array $ids, array $folderColors ) {
493 global $wpdb;
494
495 foreach ( $ids as $id ) {
496 if ( self::isFolderExist( $id ) && apply_filters( 'fbv_can_delete_folder', true, $id ) ) {
497 $wpdb->delete( $wpdb->prefix . 'fbv', array( 'id' => $id ), array( '%d' ) );
498 $wpdb->delete( $wpdb->prefix . 'fbv_attachment_folder', array( 'folder_id' => $id ), array( '%d' ) );
499
500 do_action( 'fbv_after_folder_deleted', $id );
501
502 if ( ! empty( $folderColors ) && isset( $folderColors[ $id ] ) ) {
503 unset( $folderColors[ $id ] );
504 }
505
506 $childrenIds = self::getChildrenIds( $id );
507 self::_delete( $childrenIds, $folderColors );
508 }
509 }
510 }
511 public static function exportAll() {
512 global $wpdb;
513
514 $folders = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}fbv", ARRAY_A );
515 $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 );
516
517 foreach ( $folders as $key => $folder ) {
518 $folders[ $key ]['name'] = '"' . Helpers::sanitize_for_excel( $folder['name'] ) . '"';
519 if ( isset( $folders[ $key ] ) && isset( $attachmentIds[ $folder['id'] ] ) ) {
520 $folders[ $key ]['attachment_ids'] = $attachmentIds[ $folder['id'] ]->attachment_ids ? $attachmentIds[ $folder['id'] ]->attachment_ids : '';
521 } else {
522 $folders[ $key ]['attachment_ids'] = '';
523 }
524 }
525 return $folders;
526 }
527 }