PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.1.2
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.1.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
1918 lines
1 <?php
2
3 defined('ABSPATH') or die('Nope, not accessing this');
4
5 class WCP_Folders
6 {
7
8 private static $instance;
9
10 public $license_key_data = null;
11
12 private static $folders;
13
14 public $total_folders = 0;
15
16 public function __construct()
17 {
18 spl_autoload_register(array($this, 'autoload'));
19 add_action('init', array($this, 'create_folder_terms'), 15);
20 add_action('admin_init', array($this, 'folders_register_settings'));
21 add_action('admin_menu', array($this, 'admin_menu'), 10000);
22 add_action('admin_enqueue_scripts', array($this, 'folders_admin_styles'));
23 add_action('admin_enqueue_scripts', array($this, 'folders_admin_scripts'));
24 add_filter('plugin_action_links_' . WCP_FOLDERS_PLUGIN_BASE, [$this, 'plugin_action_links']);
25 add_action('admin_footer', array($this, 'admin_footer'));
26 add_action('parse_tax_query', array($this, 'taxonomy_archive_exclude_children'));
27
28 /* Save Data */
29 add_action('wp_ajax_wcp_add_new_folder', array($this, 'wcp_add_new_folder'));
30
31 /* Update Data */
32 add_action('wp_ajax_wcp_update_folder', array($this, 'wcp_update_folder'));
33
34 /* Remove Data */
35 add_action('wp_ajax_wcp_remove_folder', array($this, 'wcp_remove_folder'));
36
37 /* Save State Data */
38 add_action('wp_ajax_save_wcp_folder_state', array($this, 'save_wcp_folder_state'));
39
40 /* Save State Data */
41 add_action('wp_ajax_wcp_save_parent_data', array($this, 'wcp_save_parent_data'));
42
43 /* Update Parent Data */
44 add_action('wp_ajax_wcp_update_parent_information', array($this, 'wcp_update_parent_information'));
45
46 /* Update Parent Data */
47 add_action('wp_ajax_wcp_save_folder_order', array($this, 'wcp_save_folder_order'));
48
49 /* Update Parent Data */
50 add_action('wp_ajax_wcp_mark_un_mark_folder', array($this, 'wcp_mark_un_mark_folder'));
51
52 /* Update Parent Data */
53 add_action('wp_ajax_wcp_change_post_folder', array($this, 'wcp_change_post_folder'));
54
55 /* Update Parent Data */
56 add_action('wp_ajax_wcp_change_multiple_post_folder', array($this, 'wcp_change_multiple_post_folder'));
57
58 /* Update Parent Data */
59 add_action('wp_ajax_wcp_remove_post_folder', array($this, 'wcp_remove_post_folder'));
60
61 /* Update width Data */
62 add_action('wp_ajax_wcp_change_post_width', array($this, 'wcp_change_post_width'));
63
64 /* Update width Data */
65 add_action('wp_ajax_wcp_change_all_status', array($this, 'wcp_change_all_status'));
66
67 self::$folders = 10;
68
69 /* Send message on plugin deactivate */
70 add_action( 'wp_ajax_folder_plugin_deactivate', array( $this, 'folder_plugin_deactivate' ) );
71
72 /* Send message on owner */
73 add_action( 'wp_ajax_wcp_folder_send_message_to_owner', array( $this, 'wcp_folder_send_message_to_owner' ) );
74
75 /* Get default list */
76 add_action( 'wp_ajax_wcp_get_default_list', array( $this, 'wcp_get_default_list' ) );
77
78 /* Auto select folder for new page, post */
79 add_action('new_to_auto-draft', array($this, 'new_to_auto_draft'), 10);
80
81 /* for media */
82 add_action('restrict_manage_posts', array($this, 'output_list_table_filters'), 10, 2);
83 add_filter('pre_get_posts', array($this, 'filter_attachments_list'));
84 add_action('wp_enqueue_media', array($this, 'output_backbone_view_filters'));
85 add_filter('ajax_query_attachments_args', array($this, 'filter_attachments_grid'));
86 add_filter('add_attachment', array($this, 'save_media_terms'));
87
88 /* to filter un assigned items*/
89 add_filter('pre_get_posts', array($this, 'filter_record_list'));
90 }
91
92 public function wcp_remove_post_folder() {
93 $response = array();
94 $response['status'] = 0;
95 $response['error'] = 0;
96 $response['data'] = array();
97 $response['message'] = "";
98 $postData = $_POST;
99 $errorCounter = 0;
100 if (!isset($postData['post_id']) || empty($postData['post_id'])) {
101 $response['message'] = __("Your request is not valid", WCP_FOLDER);
102 $errorCounter++;
103 } else if (!isset($postData['type']) || empty($postData['type'])) {
104 $response['message'] = __("Your request is not valid", WCP_FOLDER);
105 $errorCounter++;
106 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
107 $response['message'] = __("Your request is not valid", WCP_FOLDER);
108 $errorCounter++;
109 } else if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$postData['type'])) {
110 $response['message'] = __("Your request is not valid", WCP_FOLDER);
111 $errorCounter++;
112 }
113 if ($errorCounter == 0) {
114 $type = self::sanitize_options($postData['type']);
115 $post_id = self::sanitize_options($postData['post_id']);
116
117 $post_id = explode(",", $post_id);
118
119 $taxonomy = self::get_custom_post_type($type);
120
121 foreach($post_id as $id) {
122 if(!empty($id) && is_numeric($id) && $id > 0) {
123 wp_delete_object_term_relationships($id, $taxonomy);
124 }
125 }
126
127 $response['status'] = 1;
128 }
129 echo json_encode($response);
130 die;
131 // wp_delete_object_term_relationships
132 }
133
134 public function wcp_get_default_list() {
135 $post_type = 'attachment';
136
137 $total_posts = wp_count_posts($post_type)->inherit;;
138
139 $empty_items = self::get_total_empty_posts('attachment');
140
141 $post_type = self::get_custom_post_type($post_type);
142 $terms_data = WCP_Tree::get_full_tree_data($post_type);
143 $taxonomies = self::get_terms_hierarchical('media_folder');
144
145 $response = array(
146 'status' => 1,
147 'data' => $terms_data,
148 'total_items' => $total_posts,
149 'taxonomies' => $taxonomies,
150 'empty_items' => $empty_items
151 );
152 echo json_encode($response);
153 die;
154 }
155
156 function save_media_terms( $post_id ) {
157 if ( wp_is_post_revision( $post_id ) ) {
158 return;
159 }
160 $post = get_post($post_id);
161 if($post->post_type !== 'attachment') {
162 return;
163 }
164 $post_type = self::get_custom_post_type('attachment');
165 $selected_folder = get_option("selected_{$post_type}_folder");
166 if($selected_folder != null && !empty($selected_folder)) {
167 $terms = get_term($selected_folder);
168 if(!empty($terms) && isset($terms->term_id)) {
169 wp_set_post_terms($post_id, $terms->term_id, $post_type, false);
170 }
171 }
172 }
173
174 public function filter_attachments_grid( $args ) {
175 $taxonomy = 'media_folder';
176 if ( ! isset( $args[ $taxonomy ] ) ) {
177 return $args;
178 }
179 $term = sanitize_text_field( $args[ $taxonomy ] );
180 if ( $term != "-1" ) {
181 return $args;
182 }
183 unset( $args[ $taxonomy ] );
184 $args['tax_query'] = array(
185 array(
186 'taxonomy' => $taxonomy,
187 'operator' => 'NOT EXISTS',
188 ),
189 );
190 $args = apply_filters( 'media_library_organizer_media_filter_attachments_grid', $args );
191 return $args;
192 }
193
194 public function filter_record_list($query) {
195 global $typenow;
196
197 if($typenow == "attachment") {
198 return;
199 }
200
201 if(!self::is_for_this_post_type($typenow)) {
202 return $query;
203 }
204
205 $taxonomy = self::get_custom_post_type($typenow);
206
207 if ( ! isset( $query->query['post_type'] ) ) {
208 return $query;
209 }
210
211 if ( ! isset( $_REQUEST[$taxonomy] ) ) {
212 return $query;
213 }
214
215 $term = sanitize_text_field( $_REQUEST[$taxonomy] );
216 if ( $term != "-1" ) {
217 return $query;
218 }
219
220 unset( $query->query_vars[$taxonomy] );
221
222 $tax_query = array(
223 'taxonomy' => $taxonomy,
224 'operator' => 'NOT EXISTS',
225 );
226
227 $query->set( 'tax_query', array( $tax_query ) );
228 $query->tax_query = new WP_Tax_Query( array( $tax_query ) );
229
230 return $query;
231 }
232
233 public function output_backbone_view_filters() {
234 wp_enqueue_script( 'folders-media', WCP_FOLDER_URL.'assets/js/media.js', array( 'media-editor', 'media-views' ), false, true );
235 wp_localize_script( 'folders-media', 'folders_media_options', array(
236 'terms' => self::get_terms_hierarchical('media_folder'),
237 'taxonomy' => get_taxonomy('media_folder')
238 ));
239 wp_enqueue_style( 'folders-media', WCP_FOLDER_URL . 'assets/css/media.css' );
240 }
241
242 public function get_terms_hierarchical( $taxonomy ) {
243 $terms = get_terms( array(
244 'taxonomy' => $taxonomy,
245 'hide_empty' => false,
246 'parent' => 0,
247 ) );
248
249 if ( empty( $terms ) ) {
250 return false;
251 }
252
253 $hierarchy = _get_term_hierarchy( $taxonomy );
254
255 $hierarchical_terms = array();
256 foreach ( $terms as $term ) {
257 $hierarchical_terms[] = $term;
258 $hierarchical_terms = self::add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $term->term_id, 1 );
259 }
260
261 return $hierarchical_terms;
262 }
263
264 private function add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $current_term_id, $current_depth ) {
265
266 if ( ! isset( $hierarchy[ $current_term_id ] ) ) {
267 return $hierarchical_terms;
268 }
269
270 foreach ( $hierarchy[ $current_term_id ] as $child_term_id ) {
271
272 $child_term = get_term( $child_term_id, $taxonomy );
273
274 $child_term->name = str_pad( '', $current_depth, '-', STR_PAD_LEFT ) . ' ' . $child_term->name;
275
276 $hierarchical_terms[] = $child_term;
277
278 $hierarchical_terms = self::add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $child_term_id, ( $current_depth + 1 ) );
279 }
280
281 return $hierarchical_terms;
282 }
283
284 public function filter_attachments_list( $query ) {
285
286 if ( ! isset( $query->query['post_type'] ) ) {
287 return $query;
288 }
289
290 if ( is_array( $query->query['post_type'] ) && ! in_array( 'attachment', $query->query['post_type'] ) ) {
291 return $query;
292 }
293 if ( ! is_array( $query->query['post_type'] ) && strpos( $query->query['post_type'], 'attachment' ) === false ) {
294 return $query;
295 }
296
297 if ( ! isset( $_REQUEST['media_folder'] ) ) {
298 return $query;
299 }
300
301 $term = sanitize_text_field( $_REQUEST['media_folder'] );
302 if ( $term != "-1" ) {
303 return $query;
304 }
305
306 unset( $query->query_vars['media_folder'] );
307
308 $tax_query = array(
309 'taxonomy' => 'media_folder',
310 'operator' => 'NOT EXISTS',
311 );
312
313 $query->set( 'tax_query', array( $tax_query ) );
314 $query->tax_query = new WP_Tax_Query( array( $tax_query ) );
315
316 $query = apply_filters( 'media_library_organizer_media_filter_attachments', $query, $_REQUEST );
317
318 return $query;
319
320 }
321
322 public function output_list_table_filters( $post_type, $view_name )
323 {
324 if ($post_type != 'attachment') {
325 return;
326 }
327
328 if ($view_name != 'bar') {
329 return;
330 }
331
332 $current_term = false;
333 if ( isset( $_REQUEST['media_folder'] ) ) {
334 $current_term = sanitize_text_field($_REQUEST['media_folder']);
335 }
336
337 wp_dropdown_categories( array(
338 'show_option_all' => __( 'All Folders', WCP_FOLDER ),
339 'show_option_none' => __( '(Unassigned)', WCP_FOLDER ),
340 'option_none_value' => -1,
341 'orderby' => 'name',
342 'order' => 'ASC',
343 'show_count' => true,
344 'hide_empty' => false,
345 'echo' => true,
346 'selected' => $current_term,
347 'hierarchical' => true,
348 'name' => 'media_folder',
349 'id' => '',
350 'class' => '',
351 'taxonomy' => 'media_folder',
352 'value_field' => 'slug',
353 ) );
354
355 }
356
357
358 function new_to_auto_draft($post) {
359
360 $post_type = $post->post_type;
361
362 if(self::is_for_this_post_type($post_type)) {
363
364 $post_type = self::get_custom_post_type($post_type);
365 $selected_folder = get_option("selected_{$post_type}_folder");
366
367 if($selected_folder != null && !empty($selected_folder)) {
368 $terms = get_term($selected_folder);
369 if(!empty($terms) && isset($terms->slug)) {
370 wp_set_object_terms($post->ID, $terms->slug, $post_type );
371 }
372
373 }
374 }
375 }
376
377 public function wcp_folder_send_message_to_owner() {
378 $response = array();
379 $response['status'] = 0;
380 $response['error'] = 0;
381 $response['errors'] = array();
382 $response['message'] = "";
383 $errorArray = [];
384 $errorMessage = __("%s is required", WCP_FOLDER);
385 $postData = $_POST;
386 if(!isset($postData['textarea_text']) || trim($postData['textarea_text']) == "") {
387 $error = array(
388 "key" => "textarea_text",
389 "message" => __("Please enter your message",WCP_FOLDER)
390 );
391 $errorArray[] = $error;
392 }
393 if(!isset($postData['user_email']) || trim($postData['user_email']) == "") {
394 $error = array(
395 "key" => "user_email",
396 "message" => sprintf($errorMessage,__("Email",WCP_FOLDER))
397 );
398 $errorArray[] = $error;
399 } else if(!filter_var($postData['user_email'], FILTER_VALIDATE_EMAIL)) {
400 $error = array(
401 'key' => "user_email",
402 "message" => "Email is not valid"
403 );
404 $errorArray[] = $error;
405 }
406 if(empty($errorArray)) {
407 if(!isset($postData['folder_help_nonce']) || trim($postData['folder_help_nonce']) == "") {
408 $error = array(
409 "key" => "nonce",
410 "message" => __("Your request is not valid", WCP_FOLDER)
411 );
412 $errorArray[] = $error;
413 } else {
414 if(!wp_verify_nonce($postData['folder_help_nonce'], 'wcp_folder_help_nonce')) {
415 $error = array(
416 "key" => "nonce",
417 "message" => __("Your request is not valid", WCP_FOLDER)
418 );
419 $errorArray[] = $error;
420 }
421 }
422 }
423 if(empty($errorArray)) {
424 global $current_user;
425 $text_message = self::sanitize_options($postData['textarea_text']);
426 $email = self::sanitize_options($postData['user_email'],"email");
427 $domain = site_url();
428 $user_name = $current_user->first_name." ".$current_user->last_name;
429 $subject = "Folder request: ".$domain;
430 $headers = "MIME-Version: 1.0\r\n";
431 $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
432 $headers .= 'From: '.$user_name.' <'.$email.'>'.PHP_EOL ;
433 $headers .= 'Reply-To: '.$user_name.' <'.$email.'>'.PHP_EOL ;
434 $headers .= 'X-Mailer: PHP/' . phpversion();
435 ob_start();
436 ?>
437 <table border="0" cellspacing="0" cellpadding="5">
438 <tr>
439 <th>Domain</th>
440 <td><?php echo $domain ?></td>
441 </tr>
442 <tr>
443 <th>Email</th>
444 <td><?php echo $email ?></td>
445 </tr>
446 <tr>
447 <th>Message</th>
448 <td><?php echo nl2br($text_message) ?></td>
449 </tr>
450 </table>
451 <?php
452 $message = ob_get_clean();
453 $to = "contact+fromwp@premio.io";
454 $status = wp_mail($to, $subject, $message, $headers);
455 if($status) {
456 $response['status'] = 1;
457 } else {
458 $response['status'] = 0;
459 $response['message'] = "Not able to send mail";
460 }
461 } else {
462 $response['error'] = 1;
463 $response['errors'] = $errorArray;
464 }
465 echo json_encode($response);
466 }
467
468 public function folder_plugin_deactivate() {
469 global $current_user;
470 $postData = $_POST;
471 $errorCounter = 0;
472 $response = array();
473 $response['status'] = 0;
474 $response['message'] = "";
475 $response['valid'] = 1;
476 if(!isset($postData['reason']) || empty($postData['reason'])) {
477 $errorCounter++;
478 $response['message'] = "Please provide reason";
479 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
480 $response['message'] = __("Your request is not valid", WCP_FOLDER);
481 $errorCounter++;
482 $response['valid'] = 0;
483 } else {
484 $nonce = self::sanitize_options($postData['nonce']);
485 if(!wp_verify_nonce($nonce, 'wcp_folder_deactivate_nonce')) {
486 $response['message'] = __("Your request is not valid", WCP_FOLDER);
487 $errorCounter++;
488 $response['valid'] = 0;
489 }
490 }
491 if($errorCounter == 0) {
492 $reason = $postData['reason'];
493 $email = get_option( 'admin_email' );
494 $domain = site_url();
495 $user_name = $current_user->first_name." ".$current_user->last_name;
496 $subject = "Folders was removed from {$domain}";
497 $headers = "MIME-Version: 1.0\r\n";
498 $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
499 $headers .= 'From: '.$user_name.' <'.$email.'>'.PHP_EOL ;
500 $headers .= 'Reply-To: '.$user_name.' <'.$email.'>'.PHP_EOL ;
501 $headers .= 'X-Mailer: PHP/' . phpversion();
502 ob_start();
503 ?>
504 <table border="0" cellspacing="0" cellpadding="5">
505 <tr>
506 <th>Plugin</th>
507 <td>Folders</td>
508 </tr>
509 <tr>
510 <th>Plugin Version</th>
511 <td><?php echo WCP_FOLDER_VERSION ?></td>
512 </tr>
513 <tr>
514 <th>Domain</th>
515 <td><?php echo $domain ?></td>
516 </tr>
517 <tr>
518 <th>Email</th>
519 <td><?php echo $email ?></td>
520 </tr>
521 <tr>
522 <th>Comment</th>
523 <td><?php echo nl2br($reason) ?></td>
524 </tr>
525 <tr>
526 <th>WordPress Version</th>
527 <td><?php echo get_bloginfo('version') ?></td>
528 </tr>
529 <tr>
530 <th>PHP Version</th>
531 <td><?php echo PHP_VERSION ?></td>
532 </tr>
533 </table>
534 <?php
535 $content = ob_get_clean();
536 $to = "contact+removed@premio.io";
537 wp_mail($to, $subject, $content, $headers);
538 $response['status'] = 1;
539 }
540 echo json_encode($response);
541 die;
542 }
543
544 public function check_has_valid_key()
545 {
546 return 0;
547 }
548
549 public static function total_term_folders()
550 {
551 $post_types = get_option(WCP_FOLDER_VAR);
552 $post_types = is_array($post_types)?$post_types:array();
553 $total = 0;
554 foreach ($post_types as $post_type) {
555 $post_type = self::get_custom_post_type($post_type);
556 $total += wp_count_terms($post_type);
557 }
558 return $total;
559 }
560
561 public function get_license_key_information($licenseKey)
562 {
563 return array();
564 }
565
566 public function get_license_key_data($licenseKey = '')
567 {
568 return array();
569 }
570
571 public function check_for_license_key()
572 {
573 return false;
574 }
575
576 public function wcp_remove_post_item()
577 {
578 $response = array();
579 $response['status'] = 0;
580 $response['error'] = 0;
581 $response['data'] = array();
582 $response['message'] = "";
583 $postData = $_POST;
584 if (isset($postData['post_id']) && !empty($postData['post_id'])) {
585 wp_delete_post($postData['post_id']);
586 $response['status'] = 1;
587 }
588 echo json_encode($response);
589 die;
590 }
591
592 public function wcp_change_all_status()
593 {
594 $response = array();
595 $response['status'] = 0;
596 $response['error'] = 0;
597 $response['data'] = array();
598 $response['message'] = "";
599 $postData = $_POST;
600 $errorCounter = 0;
601 if (!isset($postData['type']) || empty($postData['type'])) {
602 $response['message'] = __("Your request is not valid", WCP_FOLDER);
603 $errorCounter++;
604 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
605 $response['message'] = __("Your request is not valid", WCP_FOLDER);
606 $errorCounter++;
607 } else if (!current_user_can("manage_categories") || ($postData['type'] == "page" && !current_user_can("edit_pages"))) {
608 $response['message'] = __("You have not permission to update width", WCP_FOLDER);
609 $errorCounter++;
610 } else if (!current_user_can("manage_categories") || ($postData['type'] != "page" && !current_user_can("edit_posts"))) {
611 $response['message'] = __("You have not permission to update width", WCP_FOLDER);
612 $errorCounter++;
613 } else {
614 $type = self::sanitize_options($postData['type']);
615 $nonce = self::sanitize_options($postData['nonce']);
616 if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) {
617 $response['message'] = __("Your request is not valid", WCP_FOLDER);
618 $errorCounter++;
619 }
620 }
621 if ($errorCounter == 0) {
622 if (isset($postData['folders']) || !empty($postData['folders'])) {
623 $status = isset($postData['status']) ? $postData['status'] : 0;
624 $status = self::sanitize_options($status);
625 $folders = self::sanitize_options($postData['folders']);
626 $folders = trim($folders, ",");
627 $folders = explode(",", $folders);
628 foreach ($folders as $folder) {
629 update_term_meta($folder, "is_active", $status);
630 }
631 }
632 $response['status'] = 1;
633 }
634 echo json_encode($response);
635 die;
636 }
637
638 public function wcp_change_post_width()
639 {
640 $response = array();
641 $response['status'] = 0;
642 $response['error'] = 0;
643 $response['data'] = array();
644 $response['message'] = "";
645 $postData = $_POST;
646 $errorCounter = 0;
647 if (!isset($postData['width']) || empty($postData['width'])) {
648 $response['message'] = __("Your request is not valid", WCP_FOLDER);
649 $errorCounter++;
650 } else if (!isset($postData['type']) || empty($postData['type'])) {
651 $response['message'] = __("Your request is not valid", WCP_FOLDER);
652 $errorCounter++;
653 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
654 $response['message'] = __("Your request is not valid", WCP_FOLDER);
655 $errorCounter++;
656 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
657 $response['message'] = __("You have not permission to update width", WCP_FOLDER);
658 $errorCounter++;
659 } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) {
660 $response['message'] = __("You have not permission to update width", WCP_FOLDER);
661 $errorCounter++;
662 } else {
663 $type = self::sanitize_options($postData['type']);
664 $nonce = self::sanitize_options($postData['nonce']);
665 if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) {
666 $response['message'] = __("Your request is not valid", WCP_FOLDER);
667 $errorCounter++;
668 }
669 }
670 if ($errorCounter == 0) {
671 $type = self::sanitize_options($postData['type']);
672 $width = self::sanitize_options($postData['width'], "int");
673 $optionName = "wcp_dynamic_width_for_" . $type;
674 update_option($optionName, $width);
675 $response['status'] = 1;
676 }
677 echo json_encode($response);
678 die;
679 }
680
681 public function wcp_change_multiple_post_folder()
682 {
683 $response = array();
684 $response['status'] = 0;
685 $response['error'] = 0;
686 $response['data'] = array();
687 $response['message'] = "";
688 $postData = $_POST;
689 $errorCounter = 0;
690 if (!isset($postData['post_ids']) || empty($postData['post_ids'])) {
691 $response['message'] = __("Your request is not valid", WCP_FOLDER);
692 $errorCounter++;
693 } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) {
694 $response['message'] = __("Your request is not valid", WCP_FOLDER);
695 $errorCounter++;
696 } else if (!isset($postData['type']) || empty($postData['type'])) {
697 $response['message'] = __("Your request is not valid", WCP_FOLDER);
698 $errorCounter++;
699 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
700 $response['message'] = __("Your request is not valid", WCP_FOLDER);
701 $errorCounter++;
702 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
703 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
704 $errorCounter++;
705 } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) {
706 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
707 $errorCounter++;
708 } else {
709 $folder_id = self::sanitize_options($postData['folder_id']);
710 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$folder_id)) {
711 $response['message'] = __("Your request is not valid", WCP_FOLDER);
712 $errorCounter++;
713 }
714 }
715 if ($errorCounter == 0) {
716 $postID = self::sanitize_options($postData['post_ids']);
717 $postID = trim($postID, ",");
718 $folderID = self::sanitize_options($postData['folder_id']);
719 $type = self::sanitize_options($postData['type']);
720 $postArray = explode(",", $postID);
721 if (is_array($postArray)) {
722 foreach ($postArray as $post) {
723 $post_type = self::get_custom_post_type($type);
724 wp_set_post_terms($post, $folderID, $post_type, false);
725 }
726 }
727 $response['status'] = 1;
728 }
729 echo json_encode($response);
730 die;
731 }
732
733 public function wcp_change_post_folder()
734 {
735 $response = array();
736 $response['status'] = 0;
737 $response['error'] = 0;
738 $response['data'] = array();
739 $response['message'] = "";
740 $postData = $_POST;
741 $errorCounter = 0;
742 if (!isset($postData['post_id']) || empty($postData['post_id'])) {
743 $errorCounter++;
744 $response['message'] = __("Your request is not valid", WCP_FOLDER);
745 } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) {
746 $errorCounter++;
747 $response['message'] = __("Your request is not valid", WCP_FOLDER);
748 } else if (!isset($postData['type']) || empty($postData['type'])) {
749 $errorCounter++;
750 $response['message'] = __("Your request is not valid", WCP_FOLDER);
751 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
752 $response['message'] = __("Your request is not valid", WCP_FOLDER);
753 $errorCounter++;
754 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
755 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
756 $errorCounter++;
757 } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) {
758 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
759 $errorCounter++;
760 } else {
761 $term_id = self::sanitize_options($postData['folder_id']);
762 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) {
763 $response['message'] = __("Your request is not valid", WCP_FOLDER);
764 $errorCounter++;
765 }
766 }
767 if ($errorCounter == 0) {
768 $postID = self::sanitize_options($postData['post_id']);
769 $folderID = self::sanitize_options($postData['folder_id']);
770 $type = self::sanitize_options($postData['type']);
771 $folder_post_type = self::get_custom_post_type($type);
772 $terms = get_the_terms($postID, $folder_post_type);
773 if (!empty($terms)) {
774 foreach ($terms as $term) {
775 wp_remove_object_terms($postID, $term->term_id, $folder_post_type);
776 }
777 }
778 wp_set_post_terms($postID, $folderID, $folder_post_type);
779 $response['status'] = 1;
780 }
781 echo json_encode($response);
782 die;
783 }
784
785 public function wcp_mark_un_mark_folder()
786 {
787 $response = array();
788 $response['status'] = 0;
789 $response['error'] = 0;
790 $response['data'] = array();
791 $response['message'] = "";
792 $postData = $_POST;
793 $errorCounter = 0;
794 if (!current_user_can("manage_categories")) {
795 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
796 $errorCounter++;
797 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
798 $errorCounter++;
799 $response['message'] = __("Your request is not valid", WCP_FOLDER);
800 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
801 $response['message'] = __("Your request is not valid", WCP_FOLDER);
802 $errorCounter++;
803 } else {
804 $term_id = self::sanitize_options($postData['term_id']);
805 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_highlight_term_'.$term_id)) {
806 $response['message'] = __("Your request is not valid", WCP_FOLDER);
807 $errorCounter++;
808 }
809 }
810 if ($errorCounter == 0) {
811 $term_id = self::sanitize_options($postData['term_id']);
812 $status = get_term_meta($term_id, "is_highlighted", true);
813 if ($status == 1) {
814 update_term_meta($term_id, "is_highlighted", 0);
815 $status = 0;
816 } else {
817 update_term_meta($term_id, "is_highlighted", 1);
818 $status = 1;
819 }
820 $response['marked'] = $status;
821 $response['id'] = $postData['term_id'];
822 $response['status'] = 1;
823 }
824 echo json_encode($response);
825 die;
826 }
827
828 public function wcp_save_folder_order()
829 {
830 $response = array();
831 $response['status'] = 0;
832 $response['error'] = 0;
833 $response['data'] = array();
834 $response['message'] = "";
835 $postData = $_POST;
836 $errorCounter = 0;
837 if (!current_user_can("manage_categories")) {
838 $response['message'] = __("You have not permission to update folder order", WCP_FOLDER);
839 $errorCounter++;
840 } else if (!isset($postData['term_ids']) || empty($postData['term_ids'])) {
841 $errorCounter++;
842 $response['message'] = __("Your request is not valid", WCP_FOLDER);
843 } else if (!isset($postData['type']) || empty($postData['type'])) {
844 $errorCounter++;
845 $response['message'] = __("Your request is not valid", WCP_FOLDER);
846 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
847 $response['message'] = __("Your request is not valid", WCP_FOLDER);
848 $errorCounter++;
849 } else {
850 $type = self::sanitize_options($postData['type']);
851 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) {
852 $response['message'] = __("Your request is not valid", WCP_FOLDER);
853 $errorCounter++;
854 }
855 }
856 if ($errorCounter == 0) {
857 $termIds = self::sanitize_options(($postData['term_ids']));
858 $type = self::sanitize_options($postData['type']);
859 $termIds = trim($termIds, ",");
860 $termArray = explode(",", $termIds);
861 $order = 1;
862 foreach ($termArray as $term) {
863 if (!empty($term)) {
864 update_term_meta($term, "wcp_custom_order", $order);
865 $order++;
866 }
867 }
868 $response['status'] = 1;
869 $folder_type = self::get_custom_post_type($type);
870 $response['options'] = WCP_Tree::get_option_data_for_select($folder_type);
871
872 }
873 echo json_encode($response);
874 die;
875 }
876
877 public function save_wcp_folder_state()
878 {
879 $response = array();
880 $response['status'] = 0;
881 $response['error'] = 0;
882 $response['data'] = array();
883 $response['message'] = "";
884 $postData = $_POST;
885 $errorCounter = 0;
886 if (!current_user_can("manage_categories")) {
887 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
888 $errorCounter++;
889 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
890 $response['message'] = __("Your request is not valid", WCP_FOLDER);
891 $errorCounter++;
892 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
893 $response['message'] = "Unable to create folder, Your request is not valid";
894 $errorCounter++;
895 } else {
896 $term_id = self::sanitize_options($postData['term_id']);
897 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) {
898 $response['message'] = __("Your request is not valid", WCP_FOLDER);
899 $errorCounter++;
900 }
901 }
902 if ($errorCounter == 0) {
903 $response['status'] = 1;
904 $term_id = self::sanitize_options($postData['term_id']);
905 $is_active = isset($postData['is_active'])?$postData['is_active']:0;
906 $is_active = self::sanitize_options($is_active);
907 if ($is_active == 1) {
908 update_term_meta($term_id, "is_active", 1);
909 } else {
910 update_term_meta($term_id, "is_active", 0);
911 }
912 }
913 echo json_encode($response);
914 die;
915 }
916
917 public function wcp_update_parent_information()
918 {
919 $response = array();
920 $response['status'] = 0;
921 $response['error'] = 0;
922 $response['data'] = array();
923 $response['message'] = "";
924 $postData = $_POST;
925 $errorCounter = 0;
926 if (!current_user_can("manage_categories")) {
927 $response['message'] = __("You have not permission to update folder", WCP_FOLDER);
928 $errorCounter++;
929 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
930 $response['message'] = __("Your request is not valid", WCP_FOLDER);
931 $errorCounter++;
932 } else if (!isset($postData['type']) || empty($postData['type'])) {
933 $response['message'] = __("Your request is not valid", WCP_FOLDER);
934 $errorCounter++;
935 } else if (!isset($postData['parent_id'])) {
936 $response['message'] = __("Your request is not valid", WCP_FOLDER);
937 $errorCounter++;
938 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
939 $response['message'] = __("Your request is not valid", WCP_FOLDER);
940 $errorCounter++;
941 } else {
942 $term_id = self::sanitize_options($postData['term_id']);
943 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) {
944 $response['message'] = __("Your request is not valid", WCP_FOLDER);
945 $errorCounter++;
946 }
947 }
948 if($errorCounter == 0) {
949 $term_id = self::sanitize_options($postData['term_id']);
950 $parent_id = self::sanitize_options($postData['parent_id']);
951 $type = self::sanitize_options($postData['type']);
952 $folder_type = self::get_custom_post_type($type);
953 wp_update_term($term_id, $folder_type, array(
954 'parent' => $parent_id
955 ));
956 update_term_meta($parent_id, "is_active", 1);
957 $response['status'] = 1;
958 }
959 echo json_encode($response);
960 die;
961 }
962
963 public function wcp_save_parent_data()
964 {
965 $response = array();
966 $response['status'] = 0;
967 $response['error'] = 0;
968 $response['data'] = array();
969 $response['message'] = "";
970 $postData = $_POST;
971 $errorCounter = 0;
972 if (!isset($postData['type']) || empty($postData['type'])) {
973 $response['message'] = __("Your request is not valid", WCP_FOLDER);
974 $errorCounter++;
975 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
976 $response['message'] = __("Your request is not valid", WCP_FOLDER);
977 $errorCounter++;
978 } else {
979 $type = self::sanitize_options($postData['type']);
980 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) {
981 $response['message'] = __("Your request is not valid", WCP_FOLDER);
982 $errorCounter++;
983 }
984 }
985 if ($errorCounter == 0) {
986 $type = self::sanitize_options($postData['type']);
987 $optionName = $type . "_parent_status";
988 $response['status'] = 1;
989 $is_active = isset($postData['is_active'])?$postData['is_active']:0;
990 $is_active = self::sanitize_options($is_active);
991 if ($is_active == 1) {
992 update_option($optionName, 1);
993 } else {
994 update_option($optionName, 0);
995 }
996 }
997 echo json_encode($response);
998 die;
999 }
1000
1001 public function wcp_remove_folder()
1002 {
1003 $response = array();
1004 $response['status'] = 0;
1005 $response['error'] = 0;
1006 $response['data'] = array();
1007 $response['message'] = "";
1008 $postData = $_POST;
1009 $errorCounter = 0;
1010 if (!current_user_can("manage_categories")) {
1011 $error = __("You have not permission to remove folder", WCP_FOLDER);
1012 $errorCounter++;
1013 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
1014 $error = __("Your request is not valid", WCP_FOLDER);
1015 $errorCounter++;
1016 } else if (!isset($postData['type']) || empty($postData['type'])) {
1017 $error = __("Your request is not valid", WCP_FOLDER);
1018 $errorCounter++;
1019 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1020 $error = "Unable to delete folder, Your request is not valid";
1021 $errorCounter++;
1022 } else {
1023 $term_id = self::sanitize_options($postData['term_id']);
1024 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_delete_term_'.$term_id)) {
1025 $error = "Unable to delete folder, Your request is not valid";
1026 $errorCounter++;
1027 }
1028 }
1029 if ($errorCounter == 0) {
1030 $term_id = self::sanitize_options($postData['term_id']);
1031 $type = self::sanitize_options($postData['type']);
1032 self::remove_folder_child_items($term_id, $type);
1033 $response['status'] = 1;
1034 $is_active = 1;
1035 $folders = -1;
1036 if (!self::check_has_valid_key()) {
1037 $is_active = 0;
1038 $folders = self::total_term_folders();
1039 }
1040 $response['folders'] = $folders;
1041 $response['is_key_active'] = $is_active;
1042 } else {
1043 $response['error'] = 1;
1044 $response['message'] = $error;
1045 }
1046 echo json_encode($response);
1047 die;
1048 }
1049
1050 public function remove_folder_child_items($term_id, $post_type)
1051 {
1052 $folder_type = self::get_custom_post_type($post_type);
1053 $terms = get_terms($folder_type, array(
1054 'hide_empty' => false,
1055 'parent' => $term_id
1056 ));
1057
1058 if (!empty($terms)) {
1059 foreach ($terms as $term) {
1060 self::remove_folder_child_items($term->term_id, $post_type);
1061 }
1062 wp_delete_term($term_id, $folder_type);
1063 } else {
1064 wp_delete_term($term_id, $folder_type);
1065 }
1066 }
1067
1068 public function wcp_update_folder()
1069 {
1070 $response = array();
1071 $response['status'] = 0;
1072 $response['error'] = 0;
1073 $response['data'] = array();
1074 $response['message'] = "";
1075 $postData = $_REQUEST;
1076 $errorCounter = 0;
1077 if (!current_user_can("manage_categories")) {
1078 $error = __("You have not permission to update folder", WCP_FOLDER);
1079 $errorCounter++;
1080 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
1081 $error = __("Unable to rename folder, Your request is not valid", WCP_FOLDER);
1082 $errorCounter++;
1083 } else if (!isset($postData['name']) || empty($postData['name'])) {
1084 $error = __("Folder name can no be empty", WCP_FOLDER);
1085 $errorCounter++;
1086 } else if (!isset($postData['type']) || empty($postData['type'])) {
1087 $error = __("Your request is not valid", WCP_FOLDER);
1088 $errorCounter++;
1089 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1090 $error = __("Unable to rename folder, Your request is not valid", WCP_FOLDER);
1091 $errorCounter++;
1092 } else {
1093 $term_id = self::sanitize_options($postData['term_id']);
1094 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_rename_term_'.$term_id)) {
1095 $error = _("Unable to rename folder, Your request is not valid", WCP_FOLDER);
1096 $errorCounter++;
1097 }
1098 }
1099 if ($errorCounter == 0) {
1100 $type = self::sanitize_options($postData['type']);
1101 $folder_type = self::get_custom_post_type($type);
1102 $name = self::sanitize_options($postData['name']);
1103 $term_id = self::sanitize_options($postData['term_id']);
1104 $result = wp_update_term(
1105 $term_id,
1106 $folder_type,
1107 array(
1108 'name' => $name,
1109 )
1110 );
1111 if (!empty($result)) {
1112 $response['id'] = $result['term_id'];
1113 $response['status'] = 1;
1114 $response['term_title'] = $postData['name'];
1115 } else {
1116 $response['message'] = __("Unable to rename folder", WCP_FOLDER);
1117 }
1118 } else {
1119 $response['error'] = 1;
1120 $response['message'] = $error;
1121 }
1122 echo json_encode($response);
1123 die;
1124 }
1125
1126 public function create_slug_from_string($str)
1127 {
1128 $a = array('À', 'Á', 'Â', 'Ã', 'Ä', '�
1129 ', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', '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', '?', '?', '?', '?', '?', '?');
1130 $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');
1131 return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), str_replace($a, $b, $str)));
1132 }
1133
1134 public static function sanitize_options($value, $type = "") {
1135 $value = stripslashes($value);
1136 if($type == "int") {
1137 $value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
1138 } else if($type == "email") {
1139 $value = sanitize_email($value);
1140 } else {
1141 $value = sanitize_text_field($value);
1142 }
1143 return $value;
1144 }
1145
1146 public function wcp_add_new_folder()
1147 {
1148 $response = array();
1149 $response['status'] = 0;
1150 $response['error'] = 0;
1151 $response['login'] = 1;
1152 $response['data'] = array();
1153 $response['message'] = "";
1154 $postData = $_REQUEST;
1155 $errorCounter = 0;
1156 if (!current_user_can("manage_categories")) {
1157 $error = __("You have not permission to add folder", WCP_FOLDER);
1158 $errorCounter++;
1159 } else if (!isset($postData['name']) || empty($postData['name'])) {
1160 $error = __("Folder name can no be empty", WCP_FOLDER);
1161 $errorCounter++;
1162 } else if (!isset($postData['type']) || empty($postData['type'])) {
1163 $error = __("Your request is not valid", WCP_FOLDER);
1164 $errorCounter++;
1165 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1166 $response['login'] = 0;
1167 $error = __("Unable to create folder, Your request is not valid", WCP_FOLDER);
1168 $errorCounter++;
1169 } else {
1170 $type = self::sanitize_options($postData['type']);
1171 if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) {
1172 $response['login'] = 0;
1173 $error = __("Unable to create folder, Your request is not valid", WCP_FOLDER);
1174 $errorCounter++;
1175 }
1176 }
1177 if ($errorCounter == 0) {
1178 $parent = isset($postData['parent_id']) && !empty($postData['parent_id']) ? $postData['parent_id'] : 0;
1179 $parent = self::sanitize_options($parent);
1180 $type = self::sanitize_options($postData['type']);
1181 $folder_type = self::get_custom_post_type($type);
1182 $term_name = self::sanitize_options($postData['name']);
1183 $term = term_exists($term_name, $folder_type, $parent);
1184 if (!(0 !== $term && null !== $term)) {
1185 $slug = self::create_slug_from_string($postData['name']) . "-" . time();
1186 $result = wp_insert_term(
1187 $postData['name'], // the term
1188 $folder_type, // the taxonomy
1189 array(
1190 'parent' => $parent,
1191 'slug' => $slug
1192 )
1193 );
1194 if (!empty($result)) {
1195 $response['id'] = $result['term_id'];
1196 $response['status'] = 1;
1197 $order = isset($postData['order']) ? $postData['order'] : 0;
1198 $order = self::sanitize_options($order);
1199 update_term_meta($result['term_id'], "wcp_custom_order", $order);
1200 if ($parent != 0) {
1201 update_term_meta($parent, "is_active", 1);
1202 }
1203 $delete_nonce = wp_create_nonce('wcp_folder_delete_term_'.$result['term_id']);
1204 $rename_nonce = wp_create_nonce('wcp_folder_rename_term_'.$result['term_id']);
1205 $highlight_nonce = wp_create_nonce('wcp_folder_highlight_term_'.$result['term_id']);
1206 $term_nonce = wp_create_nonce('wcp_folder_term_'.$result['term_id']);
1207 $string = "<li data-nonce='{$term_nonce}' data-star='{$highlight_nonce}' data-rename='{$rename_nonce}' data-delete='{$delete_nonce}' 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']}'>";
1208 $string .= "</ul></li>";
1209 $response['term_data'] = $string;
1210 $response['parent_id'] = $parent;
1211
1212 $is_active = 1;
1213 $folders = -1;
1214 if (!self::check_has_valid_key()) {
1215 $is_active = 0;
1216 $folders = self::total_term_folders();
1217 }
1218 $response['is_key_active'] = $is_active;
1219 $response['folders'] = $folders;
1220 } else {
1221 $response['message'] = __("Error during server request", WCP_FOLDER);
1222 }
1223 } else {
1224 $response['error'] = 1;
1225 $response['message'] = __("Folder name already exists", WCP_FOLDER);
1226 }
1227 } else {
1228 $response['error'] = 1;
1229 $response['message'] = $error;
1230 }
1231 echo json_encode($response);
1232 die;
1233 }
1234
1235 public function is_for_this_post_type($post_type)
1236 {
1237 $post_types = get_option(WCP_FOLDER_VAR);
1238 $post_types = is_array($post_types)?$post_types:array();
1239 return in_array($post_type, $post_types);
1240 }
1241
1242 public function is_active_for_screen()
1243 {
1244
1245 global $typenow, $current_screen;
1246
1247 if ((isset($_POST['action']) && $_POST['action'] == 'inline-save') && (isset($_POST['post_type']) && self::is_for_this_post_type($_POST['post_type']))) {
1248 return true;
1249 }
1250 global $current_screen;
1251
1252 if (self::is_for_this_post_type($typenow) && ('edit' == $current_screen->base || 'upload' == $current_screen->base)) {
1253 return true;
1254 }
1255
1256 $post_types = get_option(WCP_FOLDER_VAR);
1257 $post_types = is_array($post_types)?$post_types:array();
1258
1259 if(empty($typenow) && 'upload' == $current_screen->base ) {
1260 $typenow = "attachment";
1261 if (self::is_for_this_post_type($typenow)) {
1262 return true;
1263 }
1264 }
1265 return false;
1266 }
1267
1268 public function is_add_update_screen()
1269 {
1270 global $current_screen;
1271 $current_type = $current_screen->base;
1272 $action = $current_screen->action;
1273 $post_types = get_option(WCP_FOLDER_VAR);
1274 $post_types = is_array($post_types)?$post_types:array();
1275 global $typenow;
1276 if (in_array($current_type, $post_types) && in_array($action, array("add", ""))) {
1277 $license_data = self::get_license_key_data();
1278
1279 $is_active = 1;
1280 $folders = -1;
1281 if (!self::check_has_valid_key()) {
1282 $is_active = 0;
1283 $folders = self::total_term_folders();
1284 }
1285 $response['folders'] = $folders;
1286 $response['is_key_active'] = $is_active;
1287 }
1288 }
1289
1290 public static function get_custom_post_type($post_type)
1291 {
1292 if ($post_type == "post") {
1293 return "post_folder";
1294 } else if ($post_type == "page") {
1295 return "folder";
1296 } else if ($post_type == "attachment") {
1297 return "media_folder";
1298 }
1299 return $post_type . '_folder';
1300 }
1301
1302 public function admin_footer()
1303 {
1304
1305 if (self::is_active_for_screen()) {
1306 global $typenow;
1307
1308 $total_posts = self::get_total_posts($typenow);
1309
1310 $total_empty = self::get_total_empty_posts($typenow);
1311
1312 $folder_type = self::get_custom_post_type($typenow);
1313 $terms_data = WCP_Tree::get_full_tree_data($folder_type);
1314 $terms_html = WCP_Tree::get_option_data_for_select($folder_type);
1315 $form_html = WCP_Forms::get_form_html($terms_html);
1316 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "admin-content.php";
1317 }
1318
1319 global $pagenow;
1320 if ( 'plugins.php' !== $pagenow ) {
1321
1322 } else {
1323 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "folder-deactivate-form.php";
1324 }
1325 }
1326
1327 public function get_total_posts($post_type = "")
1328 {
1329 global $typenow;
1330 if ($post_type == "") {
1331 $post_type = $typenow;
1332 }
1333 if ($typenow == "attachment") {
1334 return wp_count_posts($post_type)->inherit;
1335 } else {
1336 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;
1337 }
1338 }
1339
1340 public function get_total_empty_posts($post_type = "")
1341 {
1342 $taxonomy = self::get_custom_post_type($post_type);
1343 $args = array(
1344 'posts_per_page' => -1,
1345 'post_type' => $post_type,
1346 'post_status' => 'inherit'
1347 );
1348 if ($post_type != "attachment") {
1349 $args['post_status'] = array('publish', 'draft', 'future', 'private');
1350 }
1351 $args['tax_query'] = array(
1352 array(
1353 'taxonomy' => $taxonomy,
1354 'operator' => 'NOT EXISTS',
1355 ),
1356 );
1357 $result = get_posts($args);
1358
1359 return (count($result));
1360 }
1361
1362 public function autoload()
1363 {
1364 $files = array(
1365 'WCP_Tree_View' => WCP_DS . "includes" . WCP_DS . "tree.class.php",
1366 'WCP_Form_View' => WCP_DS . "includes" . WCP_DS . "form.class.php",
1367 );
1368
1369 foreach ($files as $file) {
1370 if (file_exists(dirname(dirname(__FILE__)) . $file)) {
1371 include_once dirname(dirname(__FILE__)) . $file;
1372 }
1373 }
1374 }
1375
1376 public function create_folder_terms()
1377 {
1378 $options = get_option(WCP_FOLDER_VAR);
1379 $options = is_array($options)?$options:array();
1380 $old_plugin_status = 0;
1381 $posts = array();
1382 if (!empty($options)) {
1383 foreach ($options as $option) {
1384 if (!(strpos($option, 'folder4') === false) && $old_plugin_status == 0) {
1385 $old_plugin_status = 1;
1386 }
1387 if (in_array($option, array("page", "post", "attachment"))) {
1388 $posts[] = str_replace("folder4", "", $option);
1389 } else {
1390 $posts[] = $option;
1391 }
1392 }
1393 if(!empty($posts)) {
1394 update_option(WCP_FOLDER_VAR, $posts);
1395 }
1396 }
1397 if ($old_plugin_status == 1) {
1398 update_option("folders_show_in_menu", "on");
1399 $old_plugin_var = get_option("folder_old_plugin_status");
1400 if (empty($old_plugin_var) || $old_plugin_var == null) {
1401 update_option("folder_old_plugin_status", "1");
1402 }
1403 }
1404 $posts = get_option(WCP_FOLDER_VAR);
1405 if (!empty($posts)) {
1406 foreach ($posts as $post_type) {
1407 $labels = array(
1408 'name' => __('Folders', WCP_FOLDER),
1409 'singular_name' => __('Folder', WCP_FOLDER),
1410 'all_items' => __('All Folders', WCP_FOLDER),
1411 'edit_item' => __('Edit Folder', WCP_FOLDER),
1412 'update_item' => __('Update Folder', WCP_FOLDER),
1413 'add_new_item' => __('Add New Folder', WCP_FOLDER),
1414 'new_item_name' => __('Add Folder Name', WCP_FOLDER),
1415 'menu_name' => __('Folders', WCP_FOLDER),
1416 'search_items' => __('Search Folders', WCP_FOLDER),
1417 'parent_item' => __('Parent Folder', WCP_FOLDER),
1418 );
1419
1420 $args = array(
1421 'label' => __('Folder'),
1422 'labels' => $labels,
1423 'show_tagcloud' => false,
1424 'hierarchical' => true,
1425 'public' => false,
1426 'show_ui' => true,
1427 'show_in_menu' => false,
1428 'show_in_rest' => true,
1429 'show_admin_column' => true,
1430 'update_count_callback' => '_update_generic_term_count',
1431 'query_var' => true,
1432 'rewrite' => false,
1433 );
1434
1435 $folder_post_type = self::get_custom_post_type($post_type);
1436
1437 register_taxonomy(
1438 $folder_post_type,
1439 $post_type,
1440 $args
1441 );
1442 }
1443 }
1444
1445 if(current_user_can("manage_categories")) {
1446 if (isset($_POST['folders_show_in_menu']) && !empty($_POST['folders_show_in_menu'])) {
1447 $show_menu = "off";
1448 if ($_POST['folders_show_in_menu'] == "on") {
1449 $show_menu = "on";
1450 }
1451 update_option("folders_show_in_menu", $show_menu);
1452 }
1453
1454 if (isset($_POST['folders_settings1'])) {
1455 $posts = array();
1456 if (isset($_POST['folders_settings']) && is_array($_POST['folders_settings'])) {
1457 foreach ($_POST['folders_settings'] as $key => $val) {
1458 $posts[] = $val;
1459 }
1460 }
1461 update_option("folders_settings", $posts);
1462 }
1463 }
1464
1465 $old_version = get_option("folder_old_plugin_status");
1466 if($old_version !== false && $old_version == 1) {
1467 $total_folders = get_option("folder_old_plugin_folder_status");
1468 if($total_folders === false) {
1469 $total = self::total_term_folders();
1470 if($total <= 10) {
1471 $total = 10;
1472 };
1473 update_option("folder_old_plugin_folder_status", $total);
1474 self::$folders = $total;
1475 } else {
1476 self::$folders = $total_folders;
1477 }
1478 }
1479
1480 $total_folders = get_option("folder_old_plugin_folder_status");
1481 if($total_folders === false) {
1482 self::$folders = 10;
1483 } else {
1484 self::$folders = $total_folders;
1485 }
1486 }
1487
1488 function searchForId($id, $menu)
1489 {
1490 if ($menu) {
1491 foreach ($menu as $key => $val) {
1492 if (array_key_exists(2, $val)) {
1493 $stripVal = explode('=', $val[2]);
1494 }
1495 if (array_key_exists(1, $stripVal)) {
1496 $stripVal = $stripVal[1];
1497 }
1498 if ($stripVal === $id) {
1499 return $key;
1500 }
1501 }
1502 }
1503 }
1504
1505 function create_menu_for_folders()
1506 {
1507 global $menu;
1508 self::check_and_set_post_type();
1509
1510 $folder_types = get_option("folders_settings");
1511 if (empty($folder_types)) {
1512 return;
1513 }
1514
1515 foreach ($folder_types as $type) {
1516 $itemKey = self::searchForId($type, $menu);
1517 switch (true) {
1518 case ($type == 'attachment'):
1519 $itemKey = 10;
1520 $edit = 'upload.php';
1521 break;
1522 case ($type === 'post'):
1523 $edit = 'edit.php';
1524 $itemKey = 5;
1525 break;
1526 default:
1527 $edit = 'edit.php';
1528 break;
1529 }
1530
1531 $folder = $type == 'attachment' ? 'media' : $type;
1532 $upper = $type == 'attachment' ? 'Media' : ucwords(str_replace(array('-', '_'), ' ', $type));
1533 if ($type == 'page') {
1534 $tax_slug = 'folder';
1535 } else {
1536 $tax_slug = $folder . '_folder';
1537 }
1538
1539
1540 if ($type == 'attachment') {
1541 add_menu_page('Media Folders', 'Media Folders', 'publish_pages', "{$edit}?type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
1542 } else {
1543 add_menu_page($upper . ' Folders', "{$upper} Folders", 'publish_pages', "{$edit}?post_type={$type}&type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
1544 }
1545 $terms = get_terms($tax_slug, array(
1546 'hide_empty' => true,
1547 'parent' => 0,
1548 'orderby' => 'meta_value_num',
1549 'order' => 'ASC',
1550 'hierarchical' => false,
1551 'meta_query' => [[
1552 'key' => 'wcp_custom_order',
1553 'type' => 'NUMERIC',
1554 ]]
1555 )
1556 );
1557
1558 if ($terms) {
1559 foreach ($terms as $term) {
1560 if ($type == 'attachment') {
1561 add_submenu_page("{$edit}?type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type=attachment&media_folder={$term->slug}", false);
1562 } else {
1563 add_submenu_page("{$edit}?post_type={$type}&type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type={$type}&{$tax_slug}={$term->slug}", false);
1564 }
1565 }
1566 }
1567 }
1568 }
1569
1570 function folders_admin_styles()
1571 {
1572 if (self::is_active_for_screen()) {
1573 wp_register_style('wcp-folders-fa', plugin_dir_url(dirname(__FILE__)) . 'assets/css/folder-icon.css');
1574 wp_enqueue_style('wcp-folders-fa');
1575 wp_register_style('wcp-folders-admin', plugin_dir_url(dirname(__FILE__)) . 'assets/css/design.css');
1576 wp_enqueue_style('wcp-folders-admin');
1577 }
1578 wp_register_style('wcp-css-handle', false);
1579 wp_enqueue_style('wcp-css-handle');
1580 $css = "
1581 .wcp-folder-upgrade-button {color: #FF5983; font-weight: bold;}
1582 ";
1583 if (self::is_active_for_screen()) {
1584 global $typenow;
1585 $width = get_option("wcp_dynamic_width_for_" . $typenow);
1586 $width = esc_attr($width);
1587 if (!empty($width) && is_numeric($width)) {
1588 $css .= ".wcp-content{width:{$width}px}";
1589 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
1590 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:" . ($width + 20) . "px}";
1591 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}";
1592 } else {
1593 $css .= "body.wp-admin #wpcontent {padding-left:" . ($width + 20) . "px}";
1594 }
1595
1596 }
1597 }
1598 wp_add_inline_style('wcp-css-handle', $css);
1599
1600 if (self::is_active_for_screen()) {
1601 global $typenow;
1602 add_filter('views_edit-' . $typenow, array($this, 'wcp_check_for_child_folders'));
1603 }
1604
1605 }
1606
1607 function wcp_check_for_child_folders($content)
1608 {
1609 $termId = 0;
1610 global $typenow;
1611 $post_type = self::get_custom_post_type($typenow);
1612 if (isset($_GET[$post_type]) && !empty($_GET[$post_type])) {
1613 $term = $_GET[$post_type];
1614 $term = get_term_by("slug", $term, $post_type);
1615 if (!empty($term)) {
1616 $termId = $term->term_id;
1617 }
1618 }
1619 $terms = get_terms($post_type, array(
1620 'hide_empty' => false,
1621 'parent' => $termId,
1622 'orderby' => 'meta_value_num',
1623 'order' => 'ASC',
1624 'hierarchical' => false,
1625 'update_count_callback' => '_update_generic_term_count',
1626 'meta_query' => [[
1627 'key' => 'wcp_custom_order',
1628 'type' => 'NUMERIC',
1629 ]]
1630 ));
1631 echo '<div class="tree-structure" id="list-folder-' . $termId . '" data-id="' . $termId . '">';
1632 echo '<ul>';
1633 foreach ($terms as $term) {
1634 $status = get_term_meta($term->term_id, "is_highlighted", true);
1635 ?>
1636 <li class="grid-view" data-id="<?php echo $term->term_id ?>" id="folder_<?php echo $term->term_id ?>">
1637 <div class="folder-item is-folder" data-id="<?php echo $term->term_id ?>">
1638 <a title='<?php echo $term->name ?>' id="folder_view_<?php echo $term->term_id ?>"
1639 class="folder-view <?php echo ($status == 1) ? "is-high" : "" ?>"
1640 data-id="<?php echo $term->term_id ?>">
1641 <span class="folder item-name"><span id="wcp_folder_text_<?php echo $term->term_id ?>"
1642 class="folder-title"><?php echo $term->name ?></span></span>
1643 <!--<span class="folder-option"></span>-->
1644 </a>
1645 </div>
1646 </li>
1647 <?php
1648 }
1649 echo '</ul>';
1650 echo '<div class="clear clearfix"></div>';
1651 echo '</div>';
1652 if(!empty($content) && is_array($content)) {
1653 echo '<ul class="subsubsub">';
1654 foreach($content as $k=>$v) {
1655 echo "<li class='{$k}'>{$v}</li>";
1656 }
1657 echo '</ul>';
1658 }
1659 }
1660
1661 function folders_admin_scripts()
1662 {
1663 if (self::is_active_for_screen()) {
1664 global $typenow;
1665 wp_register_script('wcp-folders-alert', plugin_dir_url(dirname(__FILE__)) . 'assets/js/sweetalert.all.min.js');
1666 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'));
1667
1668 if ($typenow == "attachment") {
1669 $admin_url = admin_url("upload.php?post_type=attachment&media_folder=");
1670 } else {
1671 $admin_url = admin_url("edit.php?post_type=" . $typenow);
1672 if (isset($_GET['s']) && !empty($_GET['s'])) {
1673 $admin_url .= "&s=" . $_GET['s'];
1674 }
1675 $post_type = self::get_custom_post_type($typenow);
1676 $admin_url .= "&{$post_type}=";
1677 }
1678
1679 $is_active = 1;
1680 $folders = -1;
1681 if (!self::check_has_valid_key()) {
1682 $is_active = 0;
1683 $folders = self::total_term_folders();
1684 }
1685 $register_url = admin_url("admin.php?page=wcp_folders_upgrade");
1686
1687 $is_rtl = 0;
1688 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
1689 $is_rtl = 1;
1690 }
1691
1692 $can_manage_folder = current_user_can("manage_categories")?1:0;
1693 wp_localize_script('wcp-folders-custom', 'wcp_settings', array(
1694 'ajax_url' => admin_url('admin-ajax.php'),
1695 'post_type' => $typenow,
1696 'page_url' => $admin_url,
1697 'ajax_image' => plugin_dir_url(dirname(__FILE__)) . "assets/images/ajax-loader.gif",
1698 'is_key_active' => $is_active,
1699 'folders' => $folders,
1700 'register_url' => $register_url,
1701 'isRTL' => $is_rtl,
1702 'nonce' => wp_create_nonce('wcp_folder_nonce_'.$typenow),
1703 'can_manage_folder' => $can_manage_folder
1704 ));
1705
1706 wp_enqueue_script('wcp-folders-alert');
1707 wp_enqueue_script('wcp-folders-custom');
1708
1709 } else {
1710 self::is_add_update_screen();
1711 }
1712 }
1713
1714 public function plugin_action_links($links)
1715 {
1716 array_unshift($links, '<a href="' . admin_url("admin.php?page=wcp_folders_settings") . '" >' . __('Settings', WCP_FOLDER) . '</a>');
1717 $links['pro'] = '<a class="wcp-folder-upgrade-button" href="'.admin_url("admin.php?page=wcp_folders_upgrade").'" >'.__( 'Upgrade', WCP_FOLDER ).'</a>';
1718 return $links;
1719 }
1720
1721 public static function get_instance()
1722 {
1723 if (empty(self::$instance)) {
1724 self::$instance = new WCP_Folders();
1725 }
1726 return self::$instance;
1727 }
1728
1729 public function check_and_set_post_type() {
1730 $options = get_option(WCP_FOLDER_VAR);
1731 $options = is_array($options)?$options:array();
1732 $old_plugin_status = 0;
1733 $post_array = array();
1734 if (!empty($options)) {
1735 foreach ($options as $key=>$val) {
1736 if (!(strpos($key, 'folders4') === false) && $old_plugin_status == 0) {
1737 $old_plugin_status = 1;
1738 }
1739 if (in_array($key, array("folders4page", "folders4post", "folders4attachment"))) {
1740 $post_array[] = str_replace("folders4", "", $key);
1741 }
1742 }
1743 } else {
1744 $post_array = array("page", "post", "attachment");
1745 }
1746 if ($old_plugin_status == 1) {
1747 update_option("folders_show_in_menu", "on");
1748 $old_plugin_var = get_option("folder_old_plugin_status");
1749 if (empty($old_plugin_var) || $old_plugin_var == null) {
1750 update_option("folder_old_plugin_status", "1");
1751 }
1752 update_option(WCP_FOLDER_VAR, $post_array);
1753 self::set_default_values_if_not_exists();
1754 }
1755 if (!empty($post_array) && get_option(WCP_FOLDER_VAR) === false) {
1756 update_option(WCP_FOLDER_VAR, $post_array);
1757 update_option("folders_show_in_menu", "off");
1758 }
1759 }
1760
1761 public static function activate()
1762 {
1763 update_option("folders_show_in_menu", "off");
1764 update_option("folder_redirect_status", 1);
1765 }
1766
1767 public static function get_total_term_folders()
1768 {
1769 return self::total_term_folders();
1770 }
1771
1772 function folders_register_settings()
1773 {
1774 register_setting('folders_settings', 'folders_settings1', 'folders_settings_validate');
1775
1776 self::check_and_set_post_type();
1777
1778 $options = get_option("folders_settings");
1779
1780 if (in_array("post", $options)) {
1781 add_filter('manage_posts_columns', array($this, 'wcp_manage_columns_head'));
1782 add_action('manage_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1783 }
1784
1785 if (in_array("page", $options)) {
1786 add_filter('manage_page_posts_columns', array($this, 'wcp_manage_columns_head'));
1787 add_action('manage_page_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1788 }
1789
1790 if (in_array("attachment", $options)) {
1791 add_filter('manage_media_columns', array($this, 'wcp_manage_columns_head'));
1792 add_action('manage_media_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1793 }
1794
1795 foreach ($options as $option) {
1796 if ($option != "post" && $option != "page" && $option != "attachment") {
1797 add_filter('manage_media_columns', array($this, 'wcp_manage_columns_head'));
1798 add_action('manage_media_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2);
1799 }
1800 }
1801
1802 $option = get_option("folder_redirect_status", true);
1803 if ($option == 1) {
1804 update_option("folder_redirect_status", 0);
1805 wp_redirect(admin_url("admin.php?page=wcp_folders_settings"));
1806 exit;
1807 }
1808 }
1809
1810 function wcp_manage_columns_head($defaults, $d = "")
1811 {
1812 global $typenow;
1813 $options = get_option("folders_settings");
1814 if (is_array($options) && in_array($typenow, $options)) {
1815 $columns = array(
1816 '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>',
1817 ) + $defaults;
1818 return $columns;
1819 }
1820 return $defaults;
1821 }
1822
1823 function wcp_manage_columns_content($column_name, $post_ID)
1824 {
1825 global $typenow;
1826 $options = get_option("folders_settings");
1827 if (is_array($options) && in_array($typenow, $options)) {
1828 if ($column_name == 'wcp_move') {
1829 $title = get_the_title();
1830 if (strlen($title) > 20) {
1831 $title = substr($title, 0, 20) . "...";
1832 }
1833 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>";
1834 }
1835 }
1836 }
1837
1838 function taxonomy_archive_exclude_children($query)
1839 {
1840 $options = get_option("folders_settings");
1841 if (!empty($options)) {
1842 $taxonomy_slugs = array();
1843 foreach ($options as $option) {
1844 $taxonomy_slugs[] = self::get_custom_post_type($option);
1845 }
1846 if (!empty($taxonomy_slugs)) {
1847 $i = 0;
1848 foreach ($query->tax_query->queries as $tax_query_item) {
1849 if (empty($taxonomy_slugs) || (isset($tax_query_item['taxonomy']) && in_array($tax_query_item['taxonomy'], $taxonomy_slugs))) {
1850 $query->tax_query->queries[$i]['include_children'] = 0;
1851 }
1852 }
1853 }
1854 }
1855 }
1856
1857 public function admin_menu()
1858 {
1859 // Add menu item for settings page
1860 $page_title = __('Folders', WCP_FOLDER);
1861 $menu_title = __('Folders Settings', WCP_FOLDER);
1862 $capability = 'manage_options';
1863 $menu_slug = 'wcp_folders_settings';
1864 $callback = array($this, "wcp_folders_settings");
1865 $icon_url = 'dashicons-category';
1866 $position = 99;
1867
1868 add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $position);
1869
1870 add_submenu_page(
1871 $menu_slug,
1872 __('Upgrade to Pro', WCP_FOLDER),
1873 __('Upgrade to Pro', WCP_FOLDER),
1874 'manage_options',
1875 'wcp_folders_upgrade',
1876 array($this, 'wcp_folders_upgrade')
1877 );
1878
1879 self::check_and_set_post_type();
1880
1881 $show_menu = get_option("folders_show_in_menu", true);
1882 if ($show_menu == "on") {
1883 self::create_menu_for_folders();
1884 }
1885 }
1886
1887 public function wcp_folders_upgrade()
1888 {
1889 self::set_default_values_if_not_exists();
1890 include_once dirname(dirname(__FILE__)) . "/templates/admin/upgrade-to-pro.php";
1891 }
1892
1893 public function wcp_folders_settings()
1894 {
1895 self::set_default_values_if_not_exists();
1896 include_once dirname(dirname(__FILE__)) . "/templates/admin/general-settings.php";
1897 }
1898
1899 public function set_default_values_if_not_exists()
1900 {
1901 $options = get_option(WCP_FOLDER_VAR);
1902 $options = is_array($options)?$options:array();
1903 foreach ($options as $option) {
1904 $post_type = self::get_custom_post_type($option);
1905 $terms = get_terms($post_type, array(
1906 'hide_empty' => false,
1907 )
1908 );
1909 foreach ($terms as $term) {
1910 $order = get_term_meta($term->term_id, "wcp_custom_order", true);
1911 if (empty($order) || $order == null) {
1912 update_term_meta($term->term_id, "wcp_custom_order", "1");
1913 }
1914
1915 }
1916 }
1917 }
1918 }