PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.2.3
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.2.3
3.1.9 3.1.8 3.1.7 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 trunk 1.3.7 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9 2.9.1 2.9.2
folders / includes / folders.class.php
folders / includes Last commit date
folders.class.php 6 years ago form.class.php 6 years ago plugin.updates.php 6 years ago tree.class.php 6 years ago
folders.class.php
2136 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 = "gal@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 = "none@none.none";
654 if (isset($postData['email_id']) && !empty($postData['email_id']) && filter_var($postData['email_id'], FILTER_VALIDATE_EMAIL)) {
655 $email = $postData['email_id'];
656 }
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 esc_attr(WCP_FOLDER_VERSION) ?></td>
675 </tr>
676 <tr>
677 <th>Domain</th>
678 <td><?php echo esc_attr($domain) ?></td>
679 </tr>
680 <tr>
681 <th>Email</th>
682 <td><?php echo esc_attr($email) ?></td>
683 </tr>
684 <tr>
685 <th>Comment</th>
686 <td><?php echo esc_attr(nl2br($reason)) ?></td>
687 </tr>
688 <tr>
689 <th>WordPress Version</th>
690 <td><?php echo esc_attr(get_bloginfo('version')) ?></td>
691 </tr>
692 <tr>
693 <th>PHP Version</th>
694 <td><?php echo esc_attr(PHP_VERSION) ?></td>
695 </tr>
696 </table>
697 <?php
698 $content = ob_get_clean();
699 $email_id = "gal@premio.io";
700 wp_mail($email_id, $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 = filter_input_array(INPUT_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 = filter_input_array(INPUT_POST);
763 $errorCounter = 0;
764 if (!isset($postData['type']) || empty($postData['type'])) {
765 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
766 $errorCounter++;
767 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
768 $response['message'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
809 $errorCounter = 0;
810 if (!isset($postData['width']) || empty($postData['width'])) {
811 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
812 $errorCounter++;
813 } else if (!isset($postData['type']) || empty($postData['type'])) {
814 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
815 $errorCounter++;
816 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
817 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
818 $errorCounter++;
819 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
820 $response['message'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
852 $errorCounter = 0;
853 if (!isset($postData['post_ids']) || empty($postData['post_ids'])) {
854 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
855 $errorCounter++;
856 } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) {
857 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
858 $errorCounter++;
859 } else if (!isset($postData['type']) || empty($postData['type'])) {
860 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
861 $errorCounter++;
862 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
863 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
864 $errorCounter++;
865 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
866 $response['message'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
917 $errorCounter = 0;
918 if (!isset($postData['post_id']) || empty($postData['post_id'])) {
919 $errorCounter++;
920 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
921 } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) {
922 $errorCounter++;
923 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
924 } else if (!isset($postData['type']) || empty($postData['type'])) {
925 $errorCounter++;
926 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
927 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
928 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
929 $errorCounter++;
930 } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) {
931 $response['message'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
980 $errorCounter = 0;
981 if (!current_user_can("manage_categories")) {
982 $response['message'] = esc_html__("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'] = esc_html__("Your request is not valid", WCP_FOLDER);
987 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
988 $response['message'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
1023 $errorCounter = 0;
1024 if (!current_user_can("manage_categories")) {
1025 $response['message'] = esc_html__("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'] = esc_html__("Your request is not valid", WCP_FOLDER);
1030 } else if (!isset($postData['type']) || empty($postData['type'])) {
1031 $errorCounter++;
1032 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
1033 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1034 $response['message'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
1072 $errorCounter = 0;
1073 if (!current_user_can("manage_categories")) {
1074 $response['message'] = esc_html__("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'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
1112 $errorCounter = 0;
1113 if (!current_user_can("manage_categories")) {
1114 $response['message'] = esc_html__("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'] = esc_html__("Your request is not valid", WCP_FOLDER);
1118 $errorCounter++;
1119 } else if (!isset($postData['type']) || empty($postData['type'])) {
1120 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
1121 $errorCounter++;
1122 } else if (!isset($postData['parent_id'])) {
1123 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
1124 $errorCounter++;
1125 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1126 $response['message'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
1158 $errorCounter = 0;
1159 if (!isset($postData['type']) || empty($postData['type'])) {
1160 $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER);
1161 $errorCounter++;
1162 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1163 $response['message'] = esc_html__("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'] = esc_html__("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 = filter_input_array(INPUT_POST);
1196 $errorCounter = 0;
1197 if (!current_user_can("manage_categories")) {
1198 $error = esc_html__("You have not permission to remove folder", WCP_FOLDER);
1199 $errorCounter++;
1200 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
1201 $error = esc_html__("Your request is not valid", WCP_FOLDER);
1202 $errorCounter++;
1203 } else if (!isset($postData['type']) || empty($postData['type'])) {
1204 $error = esc_html__("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 = esc_html__("You have not permission to update folder", WCP_FOLDER);
1266 $errorCounter++;
1267 } else if (!isset($postData['term_id']) || empty($postData['term_id'])) {
1268 $error = esc_html__("Unable to rename folder, Your request is not valid", WCP_FOLDER);
1269 $errorCounter++;
1270 } else if (!isset($postData['name']) || empty($postData['name'])) {
1271 $error = esc_html__("Folder name can no be empty", WCP_FOLDER);
1272 $errorCounter++;
1273 } else if (!isset($postData['type']) || empty($postData['type'])) {
1274 $error = esc_html__("Your request is not valid", WCP_FOLDER);
1275 $errorCounter++;
1276 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1277 $error = esc_html__("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'] = esc_html__("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 = esc_html__("You have not permission to add folder", WCP_FOLDER);
1345 $errorCounter++;
1346 } else if (!isset($postData['name']) || empty($postData['name'])) {
1347 $error = esc_html__("Folder name can no be empty", WCP_FOLDER);
1348 $errorCounter++;
1349 } else if (!isset($postData['type']) || empty($postData['type'])) {
1350 $error = esc_html__("Your request is not valid", WCP_FOLDER);
1351 $errorCounter++;
1352 } else if (!isset($postData['nonce']) || empty($postData['nonce'])) {
1353 $response['login'] = 0;
1354 $error = esc_html__("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 = esc_html__("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'] = esc_html__("Error during server request", WCP_FOLDER);
1409 }
1410 } else {
1411 $response['error'] = 1;
1412 $response['message'] = esc_html__("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 $postData = filter_input_array(INPUT_POST);
1435
1436 if ((isset($postData['action']) && $postData['action'] == 'inline-save') && (isset($postData['post_type']) && self::is_for_this_post_type($postData['post_type']))) {
1437 return true;
1438 }
1439 global $current_screen;
1440
1441 if (self::is_for_this_post_type($typenow) && ('edit' == $current_screen->base || 'upload' == $current_screen->base)) {
1442 return true;
1443 }
1444
1445 $post_types = get_option(WCP_FOLDER_VAR);
1446 $post_types = is_array($post_types)?$post_types:array();
1447
1448 if(empty($typenow) && 'upload' == $current_screen->base ) {
1449 $typenow = "attachment";
1450 if (self::is_for_this_post_type($typenow)) {
1451 return true;
1452 }
1453 }
1454 return false;
1455 }
1456
1457 public function is_add_update_screen()
1458 {
1459 global $current_screen;
1460 $current_type = $current_screen->base;
1461 $action = $current_screen->action;
1462 $post_types = get_option(WCP_FOLDER_VAR);
1463 $post_types = is_array($post_types)?$post_types:array();
1464 global $typenow;
1465 if (in_array($current_type, $post_types) && in_array($action, array("add", ""))) {
1466 $license_data = self::get_license_key_data();
1467
1468 $is_active = 1;
1469 $folders = -1;
1470 if (!self::check_has_valid_key()) {
1471 $is_active = 0;
1472 $folders = self::total_term_folders();
1473 }
1474 $response['folders'] = $folders;
1475 $response['is_key_active'] = $is_active;
1476 }
1477 }
1478
1479 public static function get_custom_post_type($post_type)
1480 {
1481 if ($post_type == "post") {
1482 return "post_folder";
1483 } else if ($post_type == "page") {
1484 return "folder";
1485 } else if ($post_type == "attachment") {
1486 return "media_folder";
1487 }
1488 return $post_type . '_folder';
1489 }
1490
1491 public function admin_footer()
1492 {
1493
1494 if (self::is_active_for_screen()) {
1495 global $typenow;
1496
1497 $total_posts = self::get_total_posts($typenow);
1498
1499 $total_empty = self::get_total_empty_posts($typenow);
1500
1501 $folder_type = self::get_custom_post_type($typenow);
1502 $terms_data = WCP_Tree::get_full_tree_data($folder_type);
1503 $terms_html = WCP_Tree::get_option_data_for_select($folder_type);
1504 $form_html = WCP_Forms::get_form_html($terms_html);
1505 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "admin-content.php";
1506 }
1507
1508 global $pagenow;
1509 if ( 'plugins.php' !== $pagenow ) {
1510
1511 } else {
1512 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "folder-deactivate-form.php";
1513 }
1514 }
1515
1516 public function get_total_posts($post_type = "")
1517 {
1518 global $typenow;
1519 if ($post_type == "") {
1520 $post_type = $typenow;
1521 }
1522 if ($typenow == "attachment") {
1523 return wp_count_posts($post_type)->inherit;
1524 } else {
1525 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;
1526 }
1527 }
1528
1529 public function get_total_empty_posts($post_type = "")
1530 {
1531 $taxonomy = self::get_custom_post_type($post_type);
1532 $args = array(
1533 'posts_per_page' => -1,
1534 'post_type' => $post_type,
1535 'post_status' => 'inherit'
1536 );
1537 if ($post_type != "attachment") {
1538 $args['post_status'] = array('publish', 'draft', 'future', 'private');
1539 }
1540 $args['tax_query'] = array(
1541 array(
1542 'taxonomy' => $taxonomy,
1543 'operator' => 'NOT EXISTS',
1544 ),
1545 );
1546 $result = get_posts($args);
1547
1548 return (count($result));
1549 }
1550
1551 public function autoload()
1552 {
1553 $files = array(
1554 'WCP_Tree_View' => WCP_DS . "includes" . WCP_DS . "tree.class.php",
1555 'WCP_Form_View' => WCP_DS . "includes" . WCP_DS . "form.class.php",
1556 );
1557
1558 foreach ($files as $file) {
1559 if (file_exists(dirname(dirname(__FILE__)) . $file)) {
1560 include_once dirname(dirname(__FILE__)) . $file;
1561 }
1562 }
1563 }
1564
1565 public function create_folder_terms()
1566 {
1567 $options = get_option(WCP_FOLDER_VAR);
1568 $options = is_array($options)?$options:array();
1569 $old_plugin_status = 0;
1570 $posts = array();
1571 if (!empty($options)) {
1572 foreach ($options as $option) {
1573 if (!(strpos($option, 'folder4') === false) && $old_plugin_status == 0) {
1574 $old_plugin_status = 1;
1575 }
1576 if (in_array($option, array("page", "post", "attachment"))) {
1577 $posts[] = str_replace("folder4", "", $option);
1578 } else {
1579 $posts[] = $option;
1580 }
1581 }
1582 if(!empty($posts)) {
1583 update_option(WCP_FOLDER_VAR, $posts);
1584 }
1585 }
1586 if ($old_plugin_status == 1) {
1587 update_option("folders_show_in_menu", "on");
1588 $old_plugin_var = get_option("folder_old_plugin_status");
1589 if (empty($old_plugin_var) || $old_plugin_var == null) {
1590 update_option("folder_old_plugin_status", "1");
1591 }
1592 }
1593 $posts = get_option(WCP_FOLDER_VAR);
1594 if (!empty($posts)) {
1595 foreach ($posts as $post_type) {
1596 $labels = array(
1597 'name' => esc_html__('Folders', WCP_FOLDER),
1598 'singular_name' => esc_html__('Folder', WCP_FOLDER),
1599 'all_items' => esc_html__('All Folders', WCP_FOLDER),
1600 'edit_item' => esc_html__('Edit Folder', WCP_FOLDER),
1601 'update_item' => esc_html__('Update Folder', WCP_FOLDER),
1602 'add_new_item' => esc_html__('Add New Folder', WCP_FOLDER),
1603 'new_item_name' => esc_html__('Add Folder Name', WCP_FOLDER),
1604 'menu_name' => esc_html__('Folders', WCP_FOLDER),
1605 'search_items' => esc_html__('Search Folders', WCP_FOLDER),
1606 'parent_item' => esc_html__('Parent Folder', WCP_FOLDER),
1607 );
1608
1609 $args = array(
1610 'label' => esc_html__('Folder', WCP_FOLDER),
1611 'labels' => $labels,
1612 'show_tagcloud' => false,
1613 'hierarchical' => true,
1614 'public' => false,
1615 'show_ui' => true,
1616 'show_in_menu' => false,
1617 'show_in_rest' => true,
1618 'show_admin_column' => true,
1619 'update_count_callback' => '_update_generic_term_count',
1620 'query_var' => true,
1621 'rewrite' => false,
1622 );
1623
1624 $folder_post_type = self::get_custom_post_type($post_type);
1625
1626 register_taxonomy(
1627 $folder_post_type,
1628 $post_type,
1629 $args
1630 );
1631 }
1632 }
1633
1634 $postData = filter_input_array(INPUT_POST);
1635
1636 if(current_user_can("manage_categories")) {
1637 if (isset($postData['folders_show_in_menu']) && !empty($postData['folders_show_in_menu'])) {
1638 $show_menu = "off";
1639 if ($postData['folders_show_in_menu'] == "on") {
1640 $show_menu = "on";
1641 }
1642 update_option("folders_show_in_menu", $show_menu);
1643 }
1644
1645 if (isset($postData['folders_settings1'])) {
1646 $posts = array();
1647 if (isset($postData['folders_settings']) && is_array($postData['folders_settings'])) {
1648 foreach ($postData['folders_settings'] as $key => $val) {
1649 $posts[] = $val;
1650 }
1651 }
1652 update_option("folders_settings", $posts);
1653 }
1654 }
1655
1656 $old_version = get_option("folder_old_plugin_status");
1657 if($old_version !== false && $old_version == 1) {
1658 $total_folders = get_option("folder_old_plugin_folder_status");
1659 if($total_folders === false) {
1660 $total = self::total_term_folders();
1661 if($total <= 10) {
1662 $total = 10;
1663 };
1664 update_option("folder_old_plugin_folder_status", $total);
1665 self::$folders = $total;
1666 } else {
1667 self::$folders = $total_folders;
1668 }
1669 }
1670
1671 $total_folders = get_option("folder_old_plugin_folder_status");
1672 if($total_folders === false) {
1673 self::$folders = 10;
1674 } else {
1675 self::$folders = $total_folders;
1676 }
1677 }
1678
1679 function searchForId($id, $menu)
1680 {
1681 if ($menu) {
1682 foreach ($menu as $key => $val) {
1683 if (array_key_exists(2, $val)) {
1684 $stripVal = explode('=', $val[2]);
1685 }
1686 if (array_key_exists(1, $stripVal)) {
1687 $stripVal = $stripVal[1];
1688 }
1689 if ($stripVal === $id) {
1690 return $key;
1691 }
1692 }
1693 }
1694 }
1695
1696 function create_menu_for_folders()
1697 {
1698 global $menu;
1699 self::check_and_set_post_type();
1700
1701 $folder_types = get_option("folders_settings");
1702 if (empty($folder_types)) {
1703 return;
1704 }
1705
1706 foreach ($folder_types as $type) {
1707 $itemKey = self::searchForId($type, $menu);
1708 switch (true) {
1709 case ($type == 'attachment'):
1710 $itemKey = 10;
1711 $edit = 'upload.php';
1712 break;
1713 case ($type === 'post'):
1714 $edit = 'edit.php';
1715 $itemKey = 5;
1716 break;
1717 default:
1718 $edit = 'edit.php';
1719 break;
1720 }
1721
1722 $folder = $type == 'attachment' ? 'media' : $type;
1723 $upper = $type == 'attachment' ? 'Media' : ucwords(str_replace(array('-', '_'), ' ', $type));
1724 if ($type == 'page') {
1725 $tax_slug = 'folder';
1726 } else {
1727 $tax_slug = $folder . '_folder';
1728 }
1729
1730
1731 if ($type == 'attachment') {
1732 add_menu_page('Media Folders', 'Media Folders', 'publish_pages', "{$edit}?type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
1733 } else {
1734 add_menu_page($upper . ' Folders', "{$upper} Folders", 'publish_pages', "{$edit}?post_type={$type}&type=folder", false, 'dashicons-portfolio', "{$itemKey}.5");
1735 }
1736 $terms = get_terms($tax_slug, array(
1737 'hide_empty' => true,
1738 'parent' => 0,
1739 'orderby' => 'meta_value_num',
1740 'order' => 'ASC',
1741 'hierarchical' => false,
1742 'meta_query' => [[
1743 'key' => 'wcp_custom_order',
1744 'type' => 'NUMERIC',
1745 ]]
1746 )
1747 );
1748
1749 if ($terms) {
1750 foreach ($terms as $term) {
1751 if ($type == 'attachment') {
1752 add_submenu_page("{$edit}?type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type=attachment&media_folder={$term->slug}", false);
1753 } else {
1754 add_submenu_page("{$edit}?post_type={$type}&type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type={$type}&{$tax_slug}={$term->slug}", false);
1755 }
1756 }
1757 }
1758 }
1759 }
1760
1761 function folders_admin_styles()
1762 {
1763 if (self::is_active_for_screen()) {
1764 wp_register_style('wcp-folders-fa', plugin_dir_url(dirname(__FILE__)) . 'assets/css/folder-icon.css', array(), WCP_FOLDER_VERSION);
1765 wp_enqueue_style('wcp-folders-fa');
1766 wp_register_style('wcp-folders-admin', plugin_dir_url(dirname(__FILE__)) . 'assets/css/design.css', array(), WCP_FOLDER_VERSION);
1767 wp_enqueue_style('wcp-folders-admin');
1768 }
1769 wp_register_style('wcp-css-handle', false);
1770 wp_enqueue_style('wcp-css-handle');
1771 $css = "
1772 .wcp-folder-upgrade-button {color: #FF5983; font-weight: bold;}
1773 ";
1774 if (self::is_active_for_screen()) {
1775 global $typenow;
1776 $width = get_option("wcp_dynamic_width_for_" . $typenow);
1777 $width = esc_attr($width);
1778 $display_status = "wcp_dynamic_display_status_" . $typenow;
1779 $display_status = get_option($display_status);
1780 if($display_status != "hide") {
1781 if (!empty($width) && is_numeric($width)) {
1782 $css .= ".wcp-content{width:{$width}px}";
1783 if (function_exists('is_rtl') && is_rtl()) {
1784 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:" . ($width + 20) . "px}";
1785 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}";
1786 } else {
1787 $css .= "body.wp-admin #wpcontent {padding-left:" . ($width + 20) . "px}";
1788 }
1789 }
1790 } else {
1791 if (function_exists('is_rtl') && is_rtl()) {
1792 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:20px}";
1793 $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}";
1794 } else {
1795 $css .= "body.wp-admin #wpcontent {padding-left:20px}";
1796 }
1797 }
1798 }
1799 wp_add_inline_style('wcp-css-handle', $css);
1800
1801 if (self::is_active_for_screen()) {
1802 global $typenow;
1803 add_filter('views_edit-' . $typenow, array($this, 'wcp_check_for_child_folders'));
1804 }
1805
1806 }
1807
1808 function wcp_check_for_child_folders($content)
1809 {
1810 $termId = 0;
1811 global $typenow;
1812 $post_type = self::get_custom_post_type($typenow);
1813 if (isset($_GET[$post_type]) && !empty($_GET[$post_type])) {
1814 $term = $_GET[$post_type];
1815 $term = get_term_by("slug", $term, $post_type);
1816 if (!empty($term)) {
1817 $termId = $term->term_id;
1818 }
1819 }
1820 $terms = get_terms($post_type, array(
1821 'hide_empty' => false,
1822 'parent' => $termId,
1823 'orderby' => 'meta_value_num',
1824 'order' => 'ASC',
1825 'hierarchical' => false,
1826 'update_count_callback' => '_update_generic_term_count',
1827 'meta_query' => [[
1828 'key' => 'wcp_custom_order',
1829 'type' => 'NUMERIC',
1830 ]]
1831 ));
1832 echo '<div class="tree-structure" id="list-folder-' . $termId . '" data-id="' . $termId . '">';
1833 echo '<ul>';
1834 foreach ($terms as $term) {
1835 $status = get_term_meta($term->term_id, "is_highlighted", true);
1836 ?>
1837 <li class="grid-view" data-id="<?php echo $term->term_id ?>" id="folder_<?php echo $term->term_id ?>">
1838 <div class="folder-item is-folder" data-id="<?php echo $term->term_id ?>">
1839 <a title='<?php echo $term->name ?>' id="folder_view_<?php echo $term->term_id ?>"
1840 class="folder-view <?php echo ($status == 1) ? "is-high" : "" ?>"
1841 data-id="<?php echo $term->term_id ?>">
1842 <span class="folder item-name"><span id="wcp_folder_text_<?php echo $term->term_id ?>"
1843 class="folder-title"><?php echo $term->name ?></span></span>
1844 <!--<span class="folder-option"></span>-->
1845 </a>
1846 </div>
1847 </li>
1848 <?php
1849 }
1850 echo '</ul>';
1851 echo '<div class="clear clearfix"></div>';
1852 echo '</div>';
1853 if(!empty($content) && is_array($content)) {
1854 echo '<ul class="subsubsub">';
1855 foreach($content as $k=>$v) {
1856 echo "<li class='{$k}'>{$v}</li>";
1857 }
1858 echo '</ul>';
1859 }
1860 }
1861
1862 function folders_admin_scripts()
1863 {
1864 if (self::is_active_for_screen()) {
1865 global $typenow;
1866 wp_register_script('wcp-folders-alert', plugin_dir_url(dirname(__FILE__)) . 'assets/js/livequery.min.js', array(), WCP_FOLDER_VERSION);
1867 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);
1868
1869 if ($typenow == "attachment") {
1870 $admin_url = admin_url("upload.php?post_type=attachment&media_folder=");
1871 } else {
1872 $admin_url = admin_url("edit.php?post_type=" . $typenow);
1873 if (isset($_GET['s']) && !empty($_GET['s'])) {
1874 $admin_url .= "&s=" . $_GET['s'];
1875 }
1876 $post_type = self::get_custom_post_type($typenow);
1877 $admin_url .= "&{$post_type}=";
1878 }
1879
1880 $is_active = 1;
1881 $folders = -1;
1882 if (!self::check_has_valid_key()) {
1883 $is_active = 0;
1884 $folders = self::total_term_folders();
1885 }
1886 $register_url = admin_url("admin.php?page=wcp_folders_upgrade");
1887
1888 $is_rtl = 0;
1889 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
1890 $is_rtl = 1;
1891 }
1892
1893 $can_manage_folder = current_user_can("manage_categories")?1:0;
1894 $width = get_option("wcp_dynamic_width_for_" . $typenow);
1895 $width = empty($width)||!is_numeric($width)?310:$width;
1896 $post_type = self::get_custom_post_type($typenow);
1897 $taxonomy_status = 0;
1898 $selected_taxonomy = "";
1899 if(!isset($_GET[$post_type]) || empty($_GET[$post_type])) {
1900 $taxonomy_status = 1;
1901 } else if(isset($_GET[$post_type]) && !empty($_GET[$post_type])) {
1902 $selected_taxonomy = $_GET[$post_type];
1903 }
1904 wp_localize_script('wcp-folders-custom', 'wcp_settings', array(
1905 'ajax_url' => admin_url('admin-ajax.php'),
1906 'post_type' => $typenow,
1907 'page_url' => $admin_url,
1908 'ajax_image' => plugin_dir_url(dirname(__FILE__)) . "assets/images/ajax-loader.gif",
1909 'is_key_active' => $is_active,
1910 'folders' => $folders,
1911 'register_url' => $register_url,
1912 'isRTL' => $is_rtl,
1913 'nonce' => wp_create_nonce('wcp_folder_nonce_'.$typenow),
1914 'can_manage_folder' => $can_manage_folder,
1915 'folder_width' => $width,
1916 'taxonomy_status' => $taxonomy_status,
1917 'selected_taxonomy' => $selected_taxonomy
1918 ));
1919
1920 wp_enqueue_script('wcp-folders-alert');
1921 wp_enqueue_script('wcp-folders-custom');
1922
1923 } else {
1924 self::is_add_update_screen();
1925 }
1926 }
1927
1928 public function plugin_action_links($links)
1929 {
1930 array_unshift($links, '<a href="' . admin_url("admin.php?page=wcp_folders_settings") . '" >' . esc_html__('Settings', WCP_FOLDER) . '</a>');
1931 $links['pro'] = '<a class="wcp-folder-upgrade-button" href="'.admin_url("admin.php?page=wcp_folders_upgrade").'" >'.__( 'Upgrade', WCP_FOLDER ).'</a>';
1932 return $links;
1933 }
1934
1935 public static function get_instance()
1936 {
1937 if (empty(self::$instance)) {
1938 self::$instance = new WCP_Folders();
1939 }
1940 return self::$instance;
1941 }
1942
1943 public function check_and_set_post_type() {
1944 $options = get_option(WCP_FOLDER_VAR);
1945 $options = is_array($options)?$options:array();
1946 $old_plugin_status = 0;
1947 $post_array = array();
1948 if (!empty($options)) {
1949 foreach ($options as $key=>$val) {
1950 if (!(strpos($key, 'folders4') === false) && $old_plugin_status == 0) {
1951 $old_plugin_status = 1;
1952 }
1953 if (in_array($key, array("folders4page", "folders4post", "folders4attachment"))) {
1954 $post_array[] = str_replace("folders4", "", $key);
1955 }
1956 }
1957 } else {
1958 $post_array = array("page", "post", "attachment");
1959 }
1960 if ($old_plugin_status == 1) {
1961 update_option("folders_show_in_menu", "on");
1962 $old_plugin_var = get_option("folder_old_plugin_status");
1963 if (empty($old_plugin_var) || $old_plugin_var == null) {
1964 update_option("folder_old_plugin_status", "1");
1965 }
1966 update_option(WCP_FOLDER_VAR, $post_array);
1967 self::set_default_values_if_not_exists();
1968 }
1969 if (!empty($post_array) && get_option(WCP_FOLDER_VAR) === false) {
1970 update_option(WCP_FOLDER_VAR, $post_array);
1971 update_option("folders_show_in_menu", "off");
1972 }
1973 }
1974
1975 public static function activate()
1976 {
1977 update_option("folders_show_in_menu", "off");
1978 update_option("folder_redirect_status", 1);
1979 }
1980
1981 public static function get_total_term_folders()
1982 {
1983 return self::total_term_folders();
1984 }
1985
1986 function folders_register_settings()
1987 {
1988 register_setting('folders_settings', 'folders_settings1', 'folders_settings_validate');
1989
1990 self::check_and_set_post_type();
1991
1992
1993
1994 $option = get_option("folder_redirect_status", true);
1995 if ($option == 1) {
1996 update_option("folder_redirect_status", 0);
1997 wp_redirect(admin_url("admin.php?page=wcp_folders_settings"));
1998 exit;
1999 }
2000 }
2001
2002 function wcp_manage_columns_head($defaults, $d = "")
2003 {
2004 global $typenow;
2005 $type = $typenow;
2006 if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'inline-save') {
2007 $type = self::sanitize_options($_REQUEST['post_type']);
2008 }
2009
2010 $options = get_option("folders_settings");
2011
2012 // echo "<pre>"; print_r($defaults);
2013 if (is_array($options) && in_array($type, $options)) {
2014 $columns = array(
2015 '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>',
2016 ) + $defaults;
2017 return $columns;
2018 }
2019 return $defaults;
2020 }
2021
2022 function wcp_manage_columns_content($column_name, $post_ID)
2023 {
2024 $postIDs = self::$postIds;
2025 if(!is_array($postIDs)) {
2026 $postIDs = array();
2027 }
2028 if(!in_array($post_ID, $postIDs)) {
2029 $postIDs[] = $post_ID;
2030 self::$postIds = $postIDs;
2031 global $typenow;
2032 $type = $typenow;
2033 if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'inline-save') {
2034 $type = self::sanitize_options($_REQUEST['post_type']);
2035 }
2036
2037 $options = get_option("folders_settings");
2038 if (is_array($options) && in_array($type, $options)) {
2039 if ($column_name == 'wcp_move') {
2040 $title = get_the_title();
2041 if (strlen($title) > 20) {
2042 $title = substr($title, 0, 20) . "...";
2043 }
2044 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>";
2045 }
2046 }
2047 }
2048 }
2049
2050 function taxonomy_archive_exclude_children($query)
2051 {
2052 $options = get_option("folders_settings");
2053 if (!empty($options)) {
2054 $taxonomy_slugs = array();
2055 foreach ($options as $option) {
2056 $taxonomy_slugs[] = self::get_custom_post_type($option);
2057 }
2058 if (!empty($taxonomy_slugs)) {
2059 $i = 0;
2060 foreach ($query->tax_query->queries as $tax_query_item) {
2061 if (empty($taxonomy_slugs) || (isset($tax_query_item['taxonomy']) && in_array($tax_query_item['taxonomy'], $taxonomy_slugs))) {
2062 $query->tax_query->queries[$i]['include_children'] = 0;
2063 }
2064 }
2065 }
2066 }
2067 }
2068
2069 public function admin_menu()
2070 {
2071 // Add menu item for settings page
2072 $page_title = esc_html__('Folders', WCP_FOLDER);
2073 $menu_title = esc_html__('Folders Settings', WCP_FOLDER);
2074 $capability = 'manage_options';
2075 $menu_slug = 'wcp_folders_settings';
2076 $callback = array($this, "wcp_folders_settings");
2077 $icon_url = 'dashicons-category';
2078 $position = 99;
2079
2080 add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $position);
2081
2082 add_submenu_page(
2083 $menu_slug,
2084 esc_html__('Upgrade to Pro', WCP_FOLDER),
2085 esc_html__('Upgrade to Pro', WCP_FOLDER),
2086 'manage_options',
2087 'wcp_folders_upgrade',
2088 array($this, 'wcp_folders_upgrade')
2089 );
2090
2091 self::check_and_set_post_type();
2092
2093 $show_menu = get_option("folders_show_in_menu", true);
2094 if ($show_menu == "on") {
2095 self::create_menu_for_folders();
2096 }
2097 }
2098
2099 public function wcp_folders_upgrade()
2100 {
2101 self::set_default_values_if_not_exists();
2102 include_once dirname(dirname(__FILE__)) . "/templates/admin/upgrade-to-pro.php";
2103 }
2104
2105 public function wcp_folders_settings()
2106 {
2107 self::set_default_values_if_not_exists();
2108 $is_shown = get_option("folder_update_message");
2109 if($is_shown === false) {
2110 include_once dirname(dirname(__FILE__)) . "/templates/admin/update.php";
2111 } else {
2112 include_once dirname(dirname(__FILE__)) . "/templates/admin/general-settings.php";
2113 }
2114
2115 }
2116
2117 public function set_default_values_if_not_exists()
2118 {
2119 $options = get_option(WCP_FOLDER_VAR);
2120 $options = is_array($options)?$options:array();
2121 foreach ($options as $option) {
2122 $post_type = self::get_custom_post_type($option);
2123 $terms = get_terms($post_type, array(
2124 'hide_empty' => false,
2125 )
2126 );
2127 foreach ($terms as $term) {
2128 $order = get_term_meta($term->term_id, "wcp_custom_order", true);
2129 if (empty($order) || $order == null) {
2130 update_term_meta($term->term_id, "wcp_custom_order", "1");
2131 }
2132
2133 }
2134 }
2135 }
2136 }