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