PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3
FileBird – WordPress Media Library Folders & File Manager v6.3
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 / Tree.php

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

195 lines 6.1 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 defined( 'ABSPATH' ) || exit;
5
6 use FileBird\Model\Folder as FolderModel;
7 use FileBird\Model\SettingModel;
8
9 class Tree {
10 private $order = null;
11 private $order_by = null;
12 private $search = null;
13 private $settingModel;
14
15 public function __construct( $orderby, $order, $search ) {
16 $this->settingModel = SettingModel::getInstance();
17 $orderSetting = $this->settingModel->get( 'DEFAULT_SORT_FOLDERS' );
18 $this->search = $search;
19 if ( 'reset' === $order ) {
20 $this->settingModel->setSettings(
21 array(
22 'DEFAULT_SORT_FOLDERS' => null,
23 )
24 );
25 } elseif ( $order && $orderby ) {
26 $this->order = $order;
27 $this->order_by = $orderby;
28 $this->settingModel->setSettings(
29 array(
30 'DEFAULT_SORT_FOLDERS' => array(
31 'orderby' => $orderby,
32 'order' => $order,
33 ),
34 )
35 );
36 } elseif ( is_array( $orderSetting ) ) {
37 $this->order = $orderSetting['order'];
38 $this->order_by = $orderSetting['orderby'];
39 }
40 }
41
42 public function get( $flat = false ) {
43 $folders_from_db = FolderModel::allFolders( '*', null, $this->order_by, $this->order, $this->search );
44 $folder_colors = get_option( 'fbv_folder_colors', array() );
45 $tree = array();
46 $folders_from_db = self::prepareTreeData( $folders_from_db, $folder_colors );
47 $groups = self::groupByParent( $folders_from_db );
48
49 if ( ! empty( $this->search ) ) {
50 return $folders_from_db;
51 }
52
53 if ( $flat === true ) {
54 $tree = self::getFlatTreeByGroups( $groups, 0 );
55 } else {
56 $tree = self::getTreeByGroups( $groups, 0 );
57 }
58
59 return $tree;
60 }
61
62 public static function getCount( $folder_id, $lang = null ) {
63 global $wpdb;
64
65 $select = "SELECT COUNT(*) FROM {$wpdb->posts} as posts WHERE ";
66 $where = array( "post_type = 'attachment'" );
67
68 // With $folder_id == -1. We get all
69 $where[] = "(posts.post_status = 'inherit' OR posts.post_status = 'private')";
70
71 // with specific folder
72 if ( $folder_id > 0 && ! apply_filters( 'fbv_speedup_get_count_query', false ) ) {
73 $post__in = $wpdb->get_col( "SELECT `attachment_id` FROM {$wpdb->prefix}fbv_attachment_folder WHERE `folder_id` = " . (int) $folder_id );
74 if ( count( $post__in ) == 0 ) {
75 $post__in = array( 0 );
76 }
77 $where[] = '(ID IN (' . implode( ', ', $post__in ) . '))';
78 } elseif ( $folder_id == 0 ) {
79 return 0;//return 0 if this is uncategorized folder
80 }
81
82 $where = apply_filters( 'fbv_get_count_where_query', $where );
83 $query = apply_filters( 'fbv_get_count_query', $select . implode( ' AND ', $where ), $folder_id, $lang );
84 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
85 return (int) $wpdb->get_var( $query );
86 }
87
88 public static function getAllFoldersAndCount( $lang = null ) {
89 global $wpdb;
90 $query = $wpdb->prepare(
91 "SELECT fbva.folder_id, count(fbva.attachment_id) as count FROM {$wpdb->prefix}fbv_attachment_folder as fbva
92 INNER JOIN {$wpdb->prefix}fbv as fbv ON fbv.id = fbva.folder_id
93 INNER JOIN {$wpdb->posts} as posts ON fbva.attachment_id = posts.ID
94 WHERE (posts.post_status = 'inherit' OR posts.post_status = 'private')
95 AND (posts.post_type = 'attachment')
96 AND fbv.created_by = %d
97 GROUP BY fbva.folder_id",
98 apply_filters( 'fbv_folder_created_by', '0' )
99 );
100 $query = apply_filters( 'fbv_all_folders_and_count', $query, $lang );
101
102 $results = $wpdb->get_results( $query );
103 $return = array();
104 if ( is_array( $results ) ) {
105 foreach ( $results as $k => $v ) {
106 $return[ $v->folder_id ] = $v->count;
107 }
108 }
109 return $return;
110 }
111
112 public static function getFolders( $order_by = null, $order = null, $flat = false ) {
113 $settings = SettingModel::getInstance()->get( 'THEME' );
114 $folders_from_db = FolderModel::allFolders( '*', null, $order_by, $order );
115 $folder_colors = get_option( 'fbv_folder_colors', array() );
116 $folder_default_color = $settings['colors'];
117 $tree = array();
118
119 $folders_from_db = self::prepareTreeData( $folders_from_db, $folder_colors, $folder_default_color );
120 $groups = self::groupByParent( $folders_from_db );
121 if ( $flat === true ) {
122 $tree = self::getFlatTreeByGroups( $groups, 0 );
123 } else {
124 $tree = self::getTreeByGroups( $groups, 0 );
125 }
126 return $tree;
127 }
128
129 public static function getFolder( $folder_id, $order_by = null, $order = null ) {
130 $tree = self::getFolders( $order_by, $order );
131 return Helpers::findFolder( $folder_id, $tree );
132 }
133
134 private static function groupByParent( $data ) {
135 $group = array();
136 if ( is_array( $data ) ) {
137 foreach ( $data as $v ) {
138 if ( ! isset( $group[ $v['parent'] ] ) ) {
139 $group[ $v['parent'] ] = array();
140 }
141 $group[ $v['parent'] ][] = $v;
142 }
143 }
144 return $group;
145 }
146
147 private static function getTreeByGroups( $groups, $parent = 0 ) {
148 $tree = array();
149 if ( isset( $groups[ $parent ] ) && is_array( $groups[ $parent ] ) ) {
150 foreach ( $groups[ $parent ] as $node ) {
151 $node['children'] = isset( $groups[ $node['id'] ] ) ? self::getTreeByGroups( $groups, $node['id'] ) : array();
152 $tree[] = $node;
153 }
154 }
155
156 return $tree;
157 }
158
159 private static function getFlatTreeByGroups( $groups, $parent = 0, $level = 0 ) {
160 $tree = array();
161 if ( isset( $groups[ $parent ] ) && is_array( $groups[ $parent ] ) ) {
162 foreach ( $groups[ $parent ] as $node ) {
163 $node['text'] = str_repeat( '-', $level ) . $node['text'];
164 $tree[] = $node;
165 if ( isset( $groups[ $node['id'] ] ) ) {
166 $tree = array_merge( $tree, self::getFlatTreeByGroups( $groups, $node['id'], $level + 1 ) );
167 }
168 }
169 }
170
171 return $tree;
172 }
173
174 private static function prepareTreeData( $data, $folder_colors = array() ) {
175 if ( ! is_array( $data ) ) {
176 return array();
177 }
178 foreach ( $data as $k => $v ) {
179 $data[ $k ] = array(
180 'key' => (int) $v->id,
181 'id' => (int) $v->id,
182 'children' => array(),
183 'text' => $v->name,
184 'title' => $v->name,
185 'color' => ( isset( $folder_colors[ $v->id ] ) ? sanitize_hex_color( $folder_colors[ $v->id ] ) : '' ),
186 'data-id' => (int) $v->id,
187 'parent' => (int) $v->parent,
188 'data-count' => 0,
189 'ord' => $v->ord,
190 );
191 }
192 return $data;
193 }
194 }
195