PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.0.2
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.0.2
3.1.9 3.1.8 3.1.7 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 trunk 1.3.7 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9 2.9.1 2.9.2
folders / includes / folders.class.php
folders / includes Last commit date
folders.class.php 7 years ago form.class.php 7 years ago plugin.updates.php 7 years ago tree.class.php 7 years ago
folders.class.php
1157 lines
1 <?php
2
3 class WCP_Folders
4 {
5
6 private static $instance;
7
8 public $license_key_data = null;
9
10 private static $folders;
11
12 public $total_folders = 0;
13
14 private function __construct()
15 {
16 spl_autoload_register(array($this, 'autoload'));
17 add_action('init', array($this, 'create_folder_terms'), 15);
18 add_action('admin_init', array($this, 'folders_register_settings'));
19 add_action('admin_menu', array($this, 'admin_menu'), 10000);
20 add_action('admin_enqueue_scripts', array($this, 'folders_admin_styles'));
21 add_action('admin_enqueue_scripts', array($this, 'folders_admin_scripts'));
22 add_filter('plugin_action_links_' . WCP_FOLDERS_PLUGIN_BASE, [$this, 'plugin_action_links']);
23 add_action('admin_footer', array($this, 'admin_footer'));
24 add_action('parse_tax_query', array($this, 'taxonomy_archive_exclude_children'));
25
26 /* Save Data */
27 add_action('wp_ajax_nopriv_wcp_add_new_folder', array($this, 'wcp_add_new_folder'));
28 add_action('wp_ajax_wcp_add_new_folder', array($this, 'wcp_add_new_folder'));
29
30 /* Update Data */
31 add_action('wp_ajax_nopriv_wcp_update_folder', array($this, 'wcp_update_folder'));
32 add_action('wp_ajax_wcp_update_folder', array($this, 'wcp_update_folder'));
33
34 /* Remove Data */
35 add_action('wp_ajax_nopriv_wcp_remove_folder', array($this, 'wcp_remove_folder'));
36 add_action('wp_ajax_wcp_remove_folder', array($this, 'wcp_remove_folder'));
37
38 /* Save State Data */
39 add_action('wp_ajax_nopriv_save_wcp_folder_state', array($this, 'save_wcp_folder_state'));
40 add_action('wp_ajax_save_wcp_folder_state', array($this, 'save_wcp_folder_state'));
41
42 /* Save State Data */
43 add_action('wp_ajax_nopriv_wcp_save_parent_data', array($this, 'wcp_save_parent_data'));
44 add_action('wp_ajax_wcp_save_parent_data', array($this, 'wcp_save_parent_data'));
45
46 /* Update Parent Data */
47 add_action('wp_ajax_nopriv_wcp_update_parent_information', array($this, 'wcp_update_parent_information'));
48 add_action('wp_ajax_wcp_update_parent_information', array($this, 'wcp_update_parent_information'));
49
50 /* Update Parent Data */
51 add_action('wp_ajax_nopriv_wcp_save_folder_order', array($this, 'wcp_save_folder_order'));
52 add_action('wp_ajax_wcp_save_folder_order', array($this, 'wcp_save_folder_order'));
53
54 /* Update Parent Data */
55 add_action('wp_ajax_nopriv_wcp_mark_un_mark_folder', array($this, 'wcp_mark_un_mark_folder'));
56 add_action('wp_ajax_wcp_mark_un_mark_folder', array($this, 'wcp_mark_un_mark_folder'));
57
58 /* Update Parent Data */
59 add_action('wp_ajax_nopriv_wcp_change_post_folder', array($this, 'wcp_change_post_folder'));
60 add_action('wp_ajax_wcp_change_post_folder', array($this, 'wcp_change_post_folder'));
61
62 /* Update Parent Data */
63 add_action('wp_ajax_nopriv_wcp_change_multiple_post_folder', array($this, 'wcp_change_multiple_post_folder'));
64 add_action('wp_ajax_wcp_change_multiple_post_folder', array($this, 'wcp_change_multiple_post_folder'));
65
66 /* Update width Data */
67 add_action('wp_ajax_nopriv_wcp_change_post_width', array($this, 'wcp_change_post_width'));
68 add_action('wp_ajax_wcp_change_post_width', array($this, 'wcp_change_post_width'));
69
70 /* Update width Data */
71 add_action('wp_ajax_nopriv_wcp_change_all_status', array($this, 'wcp_change_all_status'));
72 add_action('wp_ajax_wcp_change_all_status', array($this, 'wcp_change_all_status'));
73
74 /* Update width Data */
75 add_action('wp_ajax_nopriv_wcp_folder_activate_key', array($this, 'wcp_folder_activate_key'));
76 add_action('wp_ajax_wcp_folder_activate_key', array($this, 'wcp_folder_activate_key'));
77
78 add_action('admin_notices', array($this, 'wcp_admin_notice'));
79
80 self::$folders = 10;
81 }
82
83
84 public function check_has_valid_key()
85 {
86 return 0;
87 }
88
89 public function total_term_folders()
90 {
91 $post_types = get_option(WCP_FOLDER_VAR);
92 $total = 0;
93 foreach ($post_types as $post_type) {
94 $post_type = self::get_custom_post_type($post_type);
95 $total += wp_count_terms($post_type);
96 }
97 return $total;
98 }
99
100 public function wcp_admin_notice()
101 {
102
103 }
104
105 public function get_license_key_information($licenseKey)
106 {
107 return array();
108 }
109
110 public function get_license_key_data($licenseKey = '')
111 {
112 return array();
113 }
114
115 public function check_for_license_key()
116 {
117 return false;
118 }
119
120 public function wcp_remove_post_item()
121 {
122 $response = array();
123 $response['status'] = 0;
124 $response['error'] = 0;
125 $response['data'] = array();
126 $response['message'] = "";
127 $postData = $_POST;
128 if (isset($postData['post_id']) && !empty($postData['post_id'])) {
129 wp_delete_post($postData['post_id']);
130 $response['status'] = 1;
131 }
132 echo json_encode($response);
133 die;
134 }
135
136 public function wcp_change_all_status()
137 {
138 $response = array();
139 $response['status'] = 0;
140 $response['error'] = 0;
141 $response['data'] = array();
142 $response['message'] = "";
143 $postData = $_POST;
144 if (isset($postData['folders']) && !empty($postData['folders'])) {
145 $status = (isset($postData['status']) && $postData['status'] == 1) ? 1 : 0;
146 $folders = $postData['folders'];
147 $folders = trim($folders, ",");
148 $folders = explode(",", $folders);
149 foreach ($folders as $folder) {
150 update_term_meta($folder, "is_active", $status);
151 }
152 }
153 echo json_encode($response);
154 die;
155 }
156
157 public function wcp_change_post_width()
158 {
159 $response = array();
160 $response['status'] = 0;
161 $response['error'] = 0;
162 $response['data'] = array();
163 $response['message'] = "";
164 $postData = $_POST;
165 if (isset($postData['width']) && isset($postData['type'])) {
166 $optionName = "wcp_dynamic_width_for_" . $postData['type'];
167 update_option($optionName, $postData['width']);
168 $response['status'] = 1;
169 }
170 echo json_encode($response);
171 die;
172 }
173
174 public function wcp_change_multiple_post_folder()
175 {
176 $response = array();
177 $response['status'] = 0;
178 $response['error'] = 0;
179 $response['data'] = array();
180 $response['message'] = "";
181 $postData = $_POST;
182 if (isset($postData['post_ids']) && isset($postData['folder_id'])) {
183 $postID = $postData['post_ids'];
184 $postID = trim($postID, ",");
185 $folderID = $postData['folder_id'];
186 $postArray = explode(",", $postID);
187 if (is_array($postArray)) {
188 foreach ($postArray as $post) {
189 $post_type = self::get_custom_post_type($postData['type']);
190 wp_set_post_terms($post, $folderID, $post_type, false);
191 }
192 }
193 $response['status'] = 1;
194 }
195 echo json_encode($response);
196 die;
197 }
198
199 public function wcp_change_post_folder()
200 {
201 $response = array();
202 $response['status'] = 0;
203 $response['error'] = 0;
204 $response['data'] = array();
205 $response['message'] = "";
206 $postData = $_POST;
207 if (isset($postData['post_id']) && isset($postData['folder_id'])) {
208 $postID = $postData['post_id'];
209 $folderID = $postData['folder_id'];
210 $folder_post_type = self::get_custom_post_type($postData['type']);
211 $terms = get_the_terms($postID, $folder_post_type);
212 if (!empty($terms)) {
213 foreach ($terms as $term) {
214 wp_remove_object_terms($postID, $term->term_id, $folder_post_type);
215 }
216 }
217 wp_set_post_terms($postID, $folderID, $folder_post_type);
218 $response['status'] = 1;
219 }
220 echo json_encode($response);
221 die;
222 }
223
224 public function wcp_mark_un_mark_folder()
225 {
226 $response = array();
227 $response['status'] = 0;
228 $response['error'] = 0;
229 $response['data'] = array();
230 $response['message'] = "";
231 $postData = $_POST;
232 $status = 0;
233 if (isset($postData['term_id'])) {
234 $status = get_term_meta($postData['term_id'], "is_highlighted", true);
235 if ($status == 1) {
236 update_term_meta($postData['term_id'], "is_highlighted", 0);
237 $status = 0;
238 } else {
239 update_term_meta($postData['term_id'], "is_highlighted", 1);
240 $status = 1;
241 }
242 $response['marked'] = $status;
243 $response['id'] = $postData['term_id'];
244 $response['status'] = 1;
245 }
246 echo json_encode($response);
247 die;
248 }
249
250 public function wcp_save_folder_order()
251 {
252 $response = array();
253 $response['status'] = 0;
254 $response['error'] = 0;
255 $response['data'] = array();
256 $response['message'] = "";
257 $postData = $_POST;
258 if (isset($postData['term_ids'])) {
259 $termIds = $postData['term_ids'];
260 $termIds = trim($termIds, ",");
261 $termArray = explode(",", $termIds);
262 $order = 1;
263 foreach ($termArray as $term) {
264 if (!empty($term)) {
265 update_term_meta($term, "wcp_custom_order", $order);
266 $order++;
267 }
268 }
269 $response['status'] = 1;
270 $folder_type = self::get_custom_post_type($postData['type']);
271 $response['options'] = WCP_Tree::get_option_data_for_select($folder_type);
272 }
273 echo json_encode($response);
274 die;
275 }
276
277 public function save_wcp_folder_state()
278 {
279 $response = array();
280 $response['status'] = 0;
281 $response['error'] = 0;
282 $response['data'] = array();
283 $response['message'] = "";
284 $postData = $_POST;
285 if (isset($postData['term_id'])) {
286 if (isset($postData['is_active']) && $postData['is_active'] == 1) {
287 update_term_meta($postData['term_id'], "is_active", 1);
288 } else {
289 update_term_meta($postData['term_id'], "is_active", 0);
290 }
291 }
292 echo json_encode($response);
293 die;
294 }
295
296 public function wcp_update_parent_information()
297 {
298 $response = array();
299 $response['status'] = 0;
300 $response['error'] = 0;
301 $response['data'] = array();
302 $response['message'] = "";
303 $postData = $_POST;
304 if (isset($postData['type']) && isset($postData['term_id']) && isset($postData['parent_id'])) {
305 $folder_type = self::get_custom_post_type($postData['type']);
306 wp_update_term($postData['term_id'], $folder_type, array(
307 'parent' => $postData['parent_id']
308 ));
309 update_term_meta($postData['parent_id'], "is_active", 1);
310 }
311 echo json_encode($response);
312 die;
313 }
314
315 public function wcp_save_parent_data()
316 {
317 $response = array();
318 $response['status'] = 0;
319 $response['error'] = 0;
320 $response['data'] = array();
321 $response['message'] = "";
322 $postData = $_POST;
323 if (isset($postData['type'])) {
324 $postType = $postData['type'];
325 $optionName = $postType . "_parent_status";
326 if (isset($postData['is_active']) && $postData['is_active'] == 1) {
327 update_option($optionName, 1);
328 } else {
329 update_option($optionName, 0);
330 }
331 }
332 echo json_encode($response);
333 die;
334 }
335
336 public function wcp_remove_folder()
337 {
338 $response = array();
339 $response['status'] = 0;
340 $response['error'] = 0;
341 $response['data'] = array();
342 $response['message'] = "";
343 $postData = $_POST;
344 $errorCounter = 0;
345 if (!isset($postData['term_id']) || empty($postData['term_id'])) {
346 $error = "Invalid request";
347 $errorCounter++;
348 } else if (!isset($postData['type']) || empty($postData['type'])) {
349 $error = "Invalid request";
350 $errorCounter++;
351 }
352 if ($errorCounter == 0) {
353 // $fol
354 self::remove_folder_child_items($postData['term_id'], $postData['type']);
355 $response['status'] = 1;
356 $is_active = 1;
357 $folders = -1;
358 if (!self::check_has_valid_key()) {
359 $is_active = 0;
360 $folders = self::total_term_folders();
361 }
362 $response['folders'] = $folders;
363 $response['is_key_active'] = $is_active;
364 } else {
365 $response['error'] = 1;
366 $response['message'] = $error;
367 }
368 echo json_encode($response);
369 die;
370 }
371
372 public function remove_folder_child_items($term_id, $post_type)
373 {
374 $folder_type = self::get_custom_post_type($post_type);
375 $terms = get_terms($folder_type, array(
376 'hide_empty' => false,
377 'parent' => $term_id
378 ));
379
380 if (!empty($terms)) {
381 foreach ($terms as $term) {
382 self::remove_folder_child_items($term->term_id, $post_type);
383 }
384 wp_delete_term($term_id, $folder_type);
385 } else {
386 wp_delete_term($term_id, $folder_type);
387 }
388 }
389
390 public function wcp_update_folder()
391 {
392 $response = array();
393 $response['status'] = 0;
394 $response['error'] = 0;
395 $response['data'] = array();
396 $response['message'] = "";
397 $postData = $_REQUEST;
398 $errorCounter = 0;
399 if (!isset($postData['name']) || empty($postData['name'])) {
400 $error = "Folder name can no be empty";
401 $errorCounter++;
402 } else if (!isset($postData['type']) || empty($postData['type'])) {
403 $error = "Invalid request";
404 $errorCounter++;
405 }
406 if ($errorCounter == 0) {
407 $folder_type = self::get_custom_post_type($postData['type']);
408 $parent = isset($postData['parent_id']) && !empty($postData['parent_id']) ? $postData['parent_id'] : 0;
409 $term = term_exists($postData['name'], $folder_type, $parent);
410 //if (!(0 !== $term && null !== $term )) {
411 $result = wp_update_term(
412 $postData['term_id'], // the term
413 $folder_type, // the taxonomy
414 array(
415 'name' => $postData['name'],
416 )
417 );
418 if (!empty($result)) {
419 $response['id'] = $result['term_id'];
420 $response['status'] = 1;
421 $response['term_title'] = $postData['name'];
422 } else {
423 $response['message'] = "Error during server request";
424 }
425 // } else {
426 // $response['message'] = "Folder name already exists";
427 // }
428 } else {
429 $response['error'] = 1;
430 $response['message'] = $error;
431 }
432 echo json_encode($response);
433 die;
434 }
435
436 public function create_slug_from_string($str)
437 {
438 $a = array('À', 'Á', 'Â', 'Ã', 'Ä', '�
439 ', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'Ð', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', '?', '?', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', '?', '?', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', '?', 'O', 'o', 'O', 'o', 'O', 'o', 'Œ', 'œ', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'Š', 'š', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Ÿ', 'Z', 'z', 'Z', 'z', 'Ž', 'ž', '?', 'ƒ', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', '?', '?', '?', '?', '?', '?');
440 $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
441 return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), str_replace($a, $b, $str)));
442 }
443
444 public function wcp_add_new_folder()
445 {
446 $response = array();
447 $response['status'] = 0;
448 $response['error'] = 0;
449 $response['data'] = array();
450 $response['message'] = "";
451 $postData = $_REQUEST;
452 $errorCounter = 0;
453 if (!isset($postData['name']) || empty($postData['name'])) {
454 $error = "Folder name can no be empty";
455 $errorCounter++;
456 } else if (!isset($postData['type']) || empty($postData['type'])) {
457 $error = "Invalid request";
458 $errorCounter++;
459 }
460 if ($errorCounter == 0) {
461 $parent = isset($postData['parent_id']) && !empty($postData['parent_id']) ? $postData['parent_id'] : 0;
462 $folder_type = self::get_custom_post_type($postData['type']);
463 $term = term_exists($postData['name'], $folder_type, $parent);
464 if (!(0 !== $term && null !== $term)) {
465 $slug = self::create_slug_from_string($postData['name']) . "-" . time();
466 $result = wp_insert_term(
467 $postData['name'], // the term
468 $folder_type, // the taxonomy
469 array(
470 'parent' => $parent,
471 'slug' => $slug
472 )
473 );
474 if (!empty($result)) {
475 $response['id'] = $result['term_id'];
476 $response['status'] = 1;
477 $order = isset($postData['order']) ? $postData['order'] : 0;
478 update_term_meta($result['term_id'], "wcp_custom_order", $order);
479 if ($parent != 0) {
480 update_term_meta($parent, "is_active", 1);
481 }
482 $string = "<li data-slug='{$result['term_id']}' class='ui-state-default route' id='wcp_folder_{$result['term_id']}' data-folder-id='{$result['term_id']}'><h3 class='title' id='title_{$result['term_id']}'><span class='title-text'>{$postData['name']}</span> <span class='update-inline-record'></span><span class='star-icon'></span> </h3><span class='nav-icon'><i class='wcp-icon folder-icon-arrow_right'></i></span><span class='ui-icon'><i class='wcp-icon folder-icon-folder'></i></span> <ul class='space' id='space_{$result['term_id']}'>";
483 $string .= "</ul></li>";
484 $response['term_data'] = $string;
485 $response['parent_id'] = $parent;
486
487 $is_active = 1;
488 $folders = -1;
489 if (!self::check_has_valid_key()) {
490 $is_active = 0;
491 $folders = self::total_term_folders();
492 }
493 $response['is_key_active'] = $is_active;
494 $response['folders'] = $folders;
495 } else {
496 $response['message'] = "Error during server request";
497 }
498 } else {
499 $response['error'] = 1;
500 $response['message'] = "Folder name already exists";
501 }
502 } else {
503 $response['error'] = 1;
504 $response['message'] = $error;
505 }
506 echo json_encode($response);
507 die;
508 }
509
510 public function is_for_this_post_type($post_type)
511 {
512 $post_types = get_option(WCP_FOLDER_VAR);
513 return in_array($post_type, $post_types);
514 }
515
516 public function is_active_for_screen()
517 {
518
519 global $typenow, $current_screen;
520
521 if ((isset($_POST['action']) && $_POST['action'] == 'inline-save') && (isset($_POST['post_type']) && self::is_for_this_post_type($_POST['post_type']))) {
522 return true;
523 }
524 global $current_screen;
525
526 if (self::is_for_this_post_type($typenow) && ('edit' == $current_screen->base || 'upload' == $current_screen->base)) {
527 return true;
528 }
529
530 $post_types = get_option(WCP_FOLDER_VAR);
531
532 if(empty($typenow) && 'upload' == $current_screen->base ) {
533 $typenow = "attachment";
534 if (self::is_for_this_post_type($typenow)) {
535 return true;
536 }
537 }
538 return false;
539 }
540
541 public function is_add_update_screen()
542 {
543 global $current_screen;
544 $current_type = $current_screen->base;
545 $action = $current_screen->action;
546 $post_types = get_option(WCP_FOLDER_VAR);
547 global $typenow;
548 if (in_array($current_type, $post_types) && in_array($action, array("add", ""))) {
549 $license_data = self::get_license_key_data();
550
551 $is_active = 1;
552 $folders = -1;
553 if (!self::check_has_valid_key()) {
554 $is_active = 0;
555 $folders = self::total_term_folders();
556 }
557 $response['folders'] = $folders;
558 $response['is_key_active'] = $is_active;
559
560 wp_register_script('wcp-folders-inner', plugin_dir_url(dirname(__FILE__)) . 'assets/js/inner.js');
561
562 $is_rtl = 0;
563 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
564 $is_rtl = 1;
565 }
566
567 wp_localize_script('wcp-folders-inner', 'wcp_inner_settings', array(
568 'ajax_url' => admin_url('admin-ajax.php'),
569 'post_type' => $typenow,
570 'ajax_image' => plugin_dir_url(dirname(__FILE__)) . "assets/images/ajax-loader.gif",
571 'is_valid' => $is_active,
572 'folders' => $folders,
573 'isRTL' => $is_rtl
574 ));
575 wp_enqueue_script('wcp-folders-inner');
576 }
577 }
578
579 public function get_custom_post_type($post_type)
580 {
581 if ($post_type == "post") {
582 return "post_folder";
583 } else if ($post_type == "page") {
584 return "folder";
585 } else if ($post_type == "attachment") {
586 return "media_folder";
587 }
588 return $post_type . '_folder';
589 }
590
591 public function admin_footer()
592 {
593 if (self::is_active_for_screen()) {
594 global $typenow;
595
596 $total_posts = self::get_total_posts($typenow);
597
598 $folder_type = self::get_custom_post_type($typenow);
599 $terms_data = WCP_Tree::get_full_tree_data($folder_type);
600 $terms_html = WCP_Tree::get_option_data_for_select($folder_type);
601 $form_html = WCP_Forms::get_form_html($terms_html);
602 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "admin-content.php";
603 }
604 }
605
606 public function get_total_posts($post_type = "")
607 {
608 global $typenow;
609 if ($post_type == "") {
610 $post_type = $typenow;
611 }
612 if ($typenow == "attachment") {
613 return wp_count_posts($post_type)->inherit;
614 } else {
615 return wp_count_posts($post_type)->publish + wp_count_posts($post_type)->draft + wp_count_posts($post_type)->future + wp_count_posts($post_type)->private;
616 }
617 }
618
619 public function autoload()
620 {
621 $files = array(
622 'WCP_Tree_View' => WCP_DS . "includes" . WCP_DS . "tree.class.php",
623 'WCP_Form_View' => WCP_DS . "includes" . WCP_DS . "form.class.php",
624 );
625
626 foreach ($files as $file) {
627 if (file_exists(dirname(dirname(__FILE__)) . $file)) {
628 include_once dirname(dirname(__FILE__)) . $file;
629 }
630 }
631 }
632
633 public function create_folder_terms()
634 {
635 $options = get_option(WCP_FOLDER_VAR);
636 $old_plugin_status = 0;
637 $posts = array();
638 if (!empty($options)) {
639 foreach ($options as $option) {
640 if (!(strpos($option, 'folder4') === false) && $old_plugin_status == 0) {
641 $old_plugin_status = 1;
642 }
643 if (in_array($option, array("page", "post", "attachment"))) {
644 $posts[] = str_replace("folder4", "", $option);
645 }
646 }
647 if(!empty($posts)) {
648 update_option(WCP_FOLDER_VAR, $posts);
649 }
650 }
651 if ($old_plugin_status == 1) {
652 update_option("folders_show_in_menu", "on");
653 $old_plugin_var = get_option("folder_old_plugin_status");
654 if (empty($old_plugin_var) || $old_plugin_var == null) {
655 update_option("folder_old_plugin_status", "1");
656 }
657 }
658 $posts = get_option(WCP_FOLDER_VAR);
659 if (!empty($posts)) {
660 foreach ($posts as $post_type) {
661 $labels = array(
662 'name' => __('Folders', WCP_FOLDER),
663 'singular_name' => __('Folder', WCP_FOLDER),
664 'all_items' => __('All Folders', WCP_FOLDER),
665 'edit_item' => __('Edit Folder', WCP_FOLDER),
666 'update_item' => __('Update Folder', WCP_FOLDER),
667 'add_new_item' => __('Add New Folder', WCP_FOLDER),
668 'new_item_name' => __('Add Folder Name', WCP_FOLDER),
669 'menu_name' => __('Folders', WCP_FOLDER),
670 'search_items' => __('Search Folders', WCP_FOLDER),
671 'parent_item' => __('Parent Folder', WCP_FOLDER),
672 );
673
674 $args = array(
675 'label' => __('Folder'),
676 'labels' => $labels,
677 'show_tagcloud' => false,
678 'hierarchical' => true,
679 'public' => false,
680 'show_ui' => true,
681 'show_in_menu' => false,
682 'show_in_rest' => true,
683 'show_admin_column' => true,
684 'update_count_callback' => '_update_generic_term_count',
685 'query_var' => true,
686 'rewrite' => false,
687 );
688
689 $folder_post_type = self::get_custom_post_type($post_type);
690
691 register_taxonomy(
692 $folder_post_type,
693 $post_type,
694 $args
695 );
696 }
697 }
698
699
700 if (isset($_POST['folders_show_in_menu']) && !empty($_POST['folders_show_in_menu'])) {
701 $show_menu = "off";
702 if($_POST['folders_show_in_menu'] == "on") {
703 $show_menu = "on";
704 }
705 update_option("folders_show_in_menu", $show_menu);
706 }
707
708 if(isset($_POST['folders_settings1'])) {
709 $posts = array();
710 if(isset($_POST['folders_settings']) && is_array($_POST['folders_settings'])) {
711 foreach($_POST['folders_settings'] as $key=>$val) {
712 $posts[] = $val;
713 }
714 }
715 update_option("folders_settings", $posts);
716 }
717
718 $old_version = get_option("folder_old_plugin_status");
719 if($old_version !== false && $old_version == 1) {
720 $total_folders = get_option("folder_old_plugin_folder_status");
721 if($total_folders === false) {
722 $total = self::total_term_folders();
723 if($total <= 10) {
724 $total = 10;
725 };
726 update_option("folder_old_plugin_folder_status", $total);
727 self::$folders = $total;
728 } else {
729 self::$folders = $total_folders;
730 }
731 }
732
733 $total_folders = get_option("folder_old_plugin_folder_status");
734 if($total_folders === false) {
735 self::$folders = 10;
736 } else {
737 self::$folders = $total_folders;
738 }
739 }
740
741 function searchForId($id, $menu)
742 {
743 if ($menu) {
744 foreach ($menu as $key => $val) {
745 if (array_key_exists(2, $val)) {
746 $stripVal = explode('=', $val[2]);
747 }
748 if (array_key_exists(1, $stripVal)) {
749 $stripVal = $stripVal[1];
750 }
751 if ($stripVal === $id) {
752 return $key;
753 }
754 }
755 }
756 }
757
758 function create_menu_for_folders()
759 {
760 global $menu;
761 self::check_and_set_post_type();
762
763 $folder_types = get_option("folders_settings");
764 if (empty($folder_types)) {
765 return;
766 }
767
768 foreach ($folder_types as $type) {
769 $itemKey = self::searchForId($type, $menu);
770 switch (true) {
771 case ($type == 'attachment'):
772 $itemKey = 10;
773 $edit = 'upload.php';
774 break;
775 case ($type === 'post'):
776 $edit = 'edit.php';
777 $itemKey = 5;
778 break;
779 default:
780 $edit = 'edit.php';
781 break;
782 }
783
784 $folder = $type == 'attachment' ? 'media' : $type;
785 $upper = $type == 'attachment' ? 'Media' : ucwords(str_replace(array('-', '_'), ' ', $type));
786 if ($type == 'page') {
787 $tax_slug = 'folder';
788 } else {
789 $tax_slug = $folder . '_folder';
790 }
791
792
793 if ($type == 'attachment') {
794 add_menu_page('Media Folders', 'Media Folders', 'publish_pages', "{$edit}?type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
795 } else {
796 add_menu_page($upper . ' Folders', "{$upper} Folders", 'publish_pages', "{$edit}?post_type={$type}&type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
797 }
798 $terms = get_terms($tax_slug, array(
799 'hide_empty' => true,
800 'parent' => 0,
801 'orderby' => 'meta_value_num',
802 'order' => 'ASC',
803 'hierarchical' => false,
804 'meta_query' => [[
805 'key' => 'wcp_custom_order',
806 'type' => 'NUMERIC',
807 ]]
808 )
809 );
810
811 if ($terms) {
812 foreach ($terms as $term) {
813 if ($type == 'attachment') {
814 add_submenu_page("{$edit}?type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type=attachment&media_folder={$term->slug}", false);
815 } else {
816 add_submenu_page("{$edit}?post_type={$type}&type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type={$type}&{$tax_slug}={$term->slug}", false);
817 }
818 }
819 }
820 }
821 }
822
823 function folders_admin_styles()
824 {
825 if (self::is_active_for_screen()) {
826 wp_register_style('wcp-folders-fa', plugin_dir_url(dirname(__FILE__)) . 'assets/css/folder-icon.css');
827 wp_enqueue_style('wcp-folders-fa');
828 wp_register_style('wcp-folders-admin', plugin_dir_url(dirname(__FILE__)) . 'assets/css/design.css');
829 wp_enqueue_style('wcp-folders-admin');
830 }
831 wp_register_style('wcp-css-handle', false);
832 wp_enqueue_style('wcp-css-handle');
833 $css = "
834 .wcp-folder-upgrade-button {color: #FF5983; font-weight: bold;}
835 ";
836 if (self::is_active_for_screen()) {
837 global $typenow;
838 $width = get_option("wcp_dynamic_width_for_" . $typenow);
839 if (!empty($width)) {
840 $css .= ".wcp-content{width:{$width}px}";
841 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
842 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:" . ($width + 20) . "px}";
843 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}";
844 } else {
845 $css .= "body.wp-admin #wpcontent {padding-left:" . ($width + 20) . "px}";
846 }
847
848 }
849 }
850 wp_add_inline_style('wcp-css-handle', $css);
851
852 if (self::is_active_for_screen()) {
853 global $typenow;
854 add_filter('views_edit-' . $typenow, array($this, 'wcp_check_for_child_folders'));
855 }
856 }
857
858 function wcp_check_for_child_folders()
859 {
860 $termId = 0;
861 global $typenow;
862 $post_type = self::get_custom_post_type($typenow);
863 if (isset($_GET[$post_type]) && !empty($_GET[$post_type])) {
864 $term = $_GET[$post_type];
865 $term = get_term_by("slug", $term, $post_type);
866 if (!empty($term)) {
867 $termId = $term->term_id;
868 }
869 }
870 $terms = get_terms($post_type, array(
871 'hide_empty' => false,
872 'parent' => $termId,
873 'orderby' => 'meta_value_num',
874 'order' => 'ASC',
875 'hierarchical' => false,
876 'update_count_callback' => '_update_generic_term_count',
877 'meta_query' => [[
878 'key' => 'wcp_custom_order',
879 'type' => 'NUMERIC',
880 ]]
881 ));
882 echo '<div class="tree-structure" id="list-folder-' . $termId . '" data-id="' . $termId . '">';
883 echo '<ul>';
884 foreach ($terms as $term) {
885 $status = get_term_meta($term->term_id, "is_highlighted", true);
886 ?>
887 <li class="grid-view" data-id="<?php echo $term->term_id ?>" id="folder_<?php echo $term->term_id ?>">
888 <div class="folder-item is-folder" data-id="<?php echo $term->term_id ?>">
889 <a title='<?php echo $term->name ?>' id="folder_view_<?php echo $term->term_id ?>"
890 class="folder-view <?php echo ($status == 1) ? "is-high" : "" ?>"
891 data-id="<?php echo $term->term_id ?>">
892 <span class="folder item-name"><span id="wcp_folder_text_<?php echo $term->term_id ?>"
893 class="folder-title"><?php echo $term->name ?></span></span>
894 <!--<span class="folder-option"></span>-->
895 </a>
896 </div>
897 </li>
898 <?php
899 }
900 echo '</ul>';
901 echo '<div class="clear clearfix"></div>';
902 echo '</div>';
903 }
904
905 function folders_admin_scripts()
906 {
907 if (self::is_active_for_screen()) {
908 global $typenow;
909 wp_register_script('wcp-folders-alert', plugin_dir_url(dirname(__FILE__)) . 'assets/js/sweetalert.all.min.js');
910 wp_register_script('wcp-folders-custom', plugin_dir_url(dirname(__FILE__)) . 'assets/js/custom.js', array('jquery', 'jquery-ui-resizable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'backbone'));
911
912 if ($typenow == "attachment") {
913 $admin_url = admin_url("upload.php?post_type=attachment&media_folder=");
914 } else {
915 $admin_url = admin_url("edit.php?post_type=" . $typenow);
916 if (isset($_GET['s']) && !empty($_GET['s'])) {
917 $admin_url .= "&s=" . $_GET['s'];
918 }
919 $post_type = self::get_custom_post_type($typenow);
920 $admin_url .= "&{$post_type}=";
921 }
922
923 $is_active = 1;
924 $folders = -1;
925 if (!self::check_has_valid_key()) {
926 $is_active = 0;
927 $folders = self::total_term_folders();
928 }
929 $register_url = admin_url("admin.php?page=wcp_folders_upgrade");
930
931 $is_rtl = 0;
932 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
933 $is_rtl = 1;
934 }
935
936 wp_localize_script('wcp-folders-custom', 'wcp_settings', array(
937 'ajax_url' => admin_url('admin-ajax.php'),
938 'post_type' => $typenow,
939 'page_url' => $admin_url,
940 'ajax_image' => plugin_dir_url(dirname(__FILE__)) . "assets/images/ajax-loader.gif",
941 'is_key_active' => $is_active,
942 'folders' => $folders,
943 'register_url' => $register_url,
944 'isRTL' => $is_rtl
945 ));
946 wp_enqueue_script('wcp-folders-alert');
947 wp_enqueue_script('wcp-folders-custom');
948
949 } else {
950 self::is_add_update_screen();
951 }
952 }
953
954 public function plugin_action_links($links)
955 {
956 array_unshift($links, '<a href="' . admin_url("admin.php?page=wcp_folders_settings") . '" >' . __('Settings', WCP_FOLDER) . '</a>');
957 $links['pro'] = '<a class="wcp-folder-upgrade-button" href="'.admin_url("admin.php?page=wcp_folders_upgrade").'" >'.__( 'Upgrade', WCP_FOLDER ).'</a>';
958 return $links;
959 }
960
961 public static function get_instance()
962 {
963 if (empty(self::$instance)) {
964 self::$instance = new WCP_Folders();
965 }
966 return self::$instance;
967 }
968
969 public function check_and_set_post_type() {
970 $options = get_option(WCP_FOLDER_VAR);
971 $old_plugin_status = 0;
972 $post_array = array();
973 if (!empty($options)) {
974 foreach ($options as $key=>$val) {
975 if (!(strpos($key, 'folders4') === false) && $old_plugin_status == 0) {
976 $old_plugin_status = 1;
977 }
978 if (in_array($key, array("folders4page", "folders4post", "folders4attachment"))) {
979 $post_array[] = str_replace("folders4", "", $key);
980 }
981 }
982 } else {
983 $post_array = array("page", "post", "attachment");
984 }
985 if ($old_plugin_status == 1) {
986 update_option("folders_show_in_menu", "on");
987 $old_plugin_var = get_option("folder_old_plugin_status");
988 if (empty($old_plugin_var) || $old_plugin_var == null) {
989 update_option("folder_old_plugin_status", "1");
990 }
991 update_option(WCP_FOLDER_VAR, $post_array);
992 self::set_default_values_if_not_exists();
993 }
994 if (!empty($post_array) && get_option(WCP_FOLDER_VAR) === false) {
995 update_option(WCP_FOLDER_VAR, $post_array);
996 update_option("folders_show_in_menu", "off");
997 }
998 }
999
1000 public static function activate()
1001 {
1002 update_option("folders_show_in_menu", "off");
1003 update_option("folder_redirect_status", 1);
1004 }
1005
1006 public static function get_total_term_folders()
1007 {
1008 return self::total_term_folders();
1009 }
1010
1011 function folders_register_settings()
1012 {
1013 register_setting('folders_settings', 'folders_settings1', 'folders_settings_validate');
1014
1015 self::check_and_set_post_type();
1016
1017 $options = get_option("folders_settings");
1018
1019 if (in_array("post", $options)) {
1020 add_filter('manage_posts_columns', array($this, 'wcp_manage_columns_head'));
1021 add_action('manage_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1022 }
1023
1024 if (in_array("page", $options)) {
1025 add_filter('manage_page_posts_columns', array($this, 'wcp_manage_columns_head'));
1026 add_action('manage_page_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1027 }
1028
1029 if (in_array("attachment", $options)) {
1030 add_filter('manage_media_columns', array($this, 'wcp_manage_columns_head'));
1031 add_action('manage_media_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1032 }
1033
1034 foreach ($options as $option) {
1035 if ($option != "post" && $option != "page" && $option != "attachment") {
1036 add_filter('manage_edit-' . $option . '_columns', 'wcp_manage_columns_head');
1037 add_action('manage_' . $option . '_posts_custom_column', 'my_manage_movie_columns', 10, 2);
1038 }
1039 }
1040
1041 $option = get_option("folder_redirect_status", true);
1042 if ($option == 1) {
1043 update_option("folder_redirect_status", 0);
1044 wp_redirect(admin_url("admin.php?page=wcp_folders_settings"));
1045 exit;
1046 }
1047 }
1048
1049 function wcp_manage_columns_head($defaults)
1050 {
1051 global $typenow;
1052 $options = get_option("folders_settings");
1053 if (is_array($options) && in_array($typenow, $options)) {
1054 $columns = array(
1055 'wcp_move' => '<div class="wcp-move-multiple wcp-col" title="' . __('Move selected items', WCP_FOLDER) . '"><span class="dashicons dashicons-move"></span><div class="wcp-items"></div></div>',
1056 ) + $defaults;
1057 return $columns;
1058 }
1059 return $defaults;
1060 }
1061
1062 function wcp_manage_columns_content($column_name, $post_ID)
1063 {
1064 global $typenow;
1065 $options = get_option("folders_settings");
1066 if (is_array($options) && in_array($typenow, $options)) {
1067 if ($column_name == 'wcp_move') {
1068 $title = get_the_title();
1069 if (strlen($title) > 20) {
1070 $title = substr($title, 0, 20) . "...";
1071 }
1072 echo "<div class='wcp-move-file' data-id='{$post_ID}'><span class='wcp-move dashicons dashicons-move' data-id='{$post_ID}'></span><span class='wcp-item' data-object-id='{$post_ID}'>" . $title . "</span></div>";
1073 }
1074 }
1075 }
1076
1077 function taxonomy_archive_exclude_children($query)
1078 {
1079 $options = get_option("folders_settings");
1080 if (!empty($options)) {
1081 $taxonomy_slugs = array();
1082 foreach ($options as $option) {
1083 $taxonomy_slugs[] = self::get_custom_post_type($option);
1084 }
1085 if (!empty($taxonomy_slugs)) {
1086 if ($query->is_main_query() && is_tax($taxonomy_slugs)) {
1087 foreach ($query->tax_query->queries as &$tax_query_item) {
1088 if (empty($taxonomy_slugs) || in_array($tax_query_item['taxonomy'], $taxonomy_slugs)) {
1089 $tax_query_item['include_children'] = 0;
1090 }
1091 }
1092 }
1093 }
1094 }
1095 }
1096
1097 public function admin_menu()
1098 {
1099 // Add menu item for settings page
1100 $page_title = __('Folders', WCP_FOLDER);
1101 $menu_title = __('Folders Settings', WCP_FOLDER);
1102 $capability = 'manage_options';
1103 $menu_slug = 'wcp_folders_settings';
1104 $callback = array($this, "wcp_folders_settings");
1105 $icon_url = 'dashicons-category';
1106 $position = 99;
1107
1108 add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $position);
1109
1110 add_submenu_page(
1111 $menu_slug,
1112 __('Upgrade to Pro', WCP_FOLDER),
1113 __('Upgrade to Pro', WCP_FOLDER),
1114 'manage_options',
1115 'wcp_folders_upgrade',
1116 array($this, 'wcp_folders_upgrade')
1117 );
1118
1119 self::check_and_set_post_type();
1120
1121 $show_menu = get_option("folders_show_in_menu", true);
1122 if ($show_menu == "on") {
1123 self::create_menu_for_folders();
1124 }
1125 }
1126
1127 public function wcp_folders_upgrade()
1128 {
1129 self::set_default_values_if_not_exists();
1130 include_once dirname(dirname(__FILE__)) . "/templates/admin/upgrade-to-pro.php";
1131 }
1132
1133 public function wcp_folders_settings()
1134 {
1135 self::set_default_values_if_not_exists();
1136 include_once dirname(dirname(__FILE__)) . "/templates/admin/general-settings.php";
1137 }
1138
1139 public function set_default_values_if_not_exists()
1140 {
1141 $options = get_option(WCP_FOLDER_VAR);
1142 foreach ($options as $option) {
1143 $post_type = self::get_custom_post_type($option);
1144 $terms = get_terms($post_type, array(
1145 'hide_empty' => false,
1146 )
1147 );
1148 foreach ($terms as $term) {
1149 $order = get_term_meta($term->term_id, "wcp_custom_order", true);
1150 if (empty($order) || $order == null) {
1151 update_term_meta($term->term_id, "wcp_custom_order", "1");
1152 }
1153
1154 }
1155 }
1156 }
1157 }