PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.2.5
FileBird – WordPress Media Library Folders & File Manager v6.2.5
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.2.5, at includes/Model/Folder.php

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