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