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