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