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