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