PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.4
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.4
4.3.2 4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 All 165 releases
adminify / Inc / Modules / Folders / Folders.php

Folders.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.4, at Inc/Modules/Folders/Folders.php

616 lines 24.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\Modules\Folders;
4
5 use WPAdminify\Inc\Utils ;
6 use WPAdminify\Inc\Admin\AdminSettings ;
7 if ( !defined( 'ABSPATH' ) ) {
8 exit;
9 }
10 class Folders
11 {
12 private static $postIds ;
13 public $options ;
14 public function __construct()
15 {
16 $needed_keys = [ 'folders_user_roles', 'folders_enable_for', 'folders_media' ];
17 $this->options = (array) array_intersect_key( AdminSettings::get_instance()->get(), array_flip( $needed_keys ) );
18 add_action( 'init', [ $this, 'register_folders_terms' ], 999 );
19 add_action( 'admin_footer', [ $this, 'add_footer_markup' ] );
20 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 100 );
21 add_action( 'wp_ajax_adminify_folder', [ $this, 'handle_adminify_folder' ] );
22 add_action( 'ajax_query_attachments_args', [ $this, 'filter_grid' ] );
23 add_filter( 'pre_get_posts', [ $this, 'filter_list' ] );
24 $this->modify_columns_actions();
25 }
26
27 public function get_post_types()
28 {
29 $post_types = get_post_types( [
30 'public' => true,
31 ] );
32 // @todo add more configurations here
33 return $post_types;
34 }
35
36 public function filter_list( $query )
37 {
38 global $typenow ;
39 global $pagenow ;
40 $post_type = $typenow;
41 if ( empty($post_type) ) {
42
43 if ( $pagenow == 'edit.php' ) {
44 $post_type = 'post';
45 } else {
46 if ( $pagenow == 'upload.php' ) {
47 $post_type = 'attachment';
48 }
49 }
50
51 }
52 if ( !isset( $query->query['post_type'] ) ) {
53 return $query;
54 }
55 $taxonomy = self::get_post_type_taxonomy( $post_type );
56 if ( !isset( $_REQUEST[$taxonomy] ) ) {
57 return $query;
58 }
59 $term = sanitize_text_field( $_REQUEST[$taxonomy] );
60 if ( $term != '-1' ) {
61 return $query;
62 }
63 unset( $query->query_vars[$taxonomy] );
64 $tax_query = array(
65 'taxonomy' => $taxonomy,
66 'operator' => 'NOT EXISTS',
67 );
68 $query->set( 'tax_query', array( $tax_query ) );
69 $query->tax_query = new \WP_Tax_Query( array( $tax_query ) );
70 return $query;
71 }
72
73 public function filter_grid( $args )
74 {
75 $taxonomy = self::get_post_type_taxonomy( 'attachment' );
76 if ( !isset( $args[$taxonomy] ) ) {
77 return $args;
78 }
79 $term = sanitize_text_field( $args[$taxonomy] );
80 if ( $term != "-1" ) {
81 return $args;
82 }
83 unset( $args[$taxonomy] );
84 $args['tax_query'] = array( array(
85 'taxonomy' => $taxonomy,
86 'operator' => 'NOT EXISTS',
87 ) );
88 return $args;
89 }
90
91 public function is_module_active( $post_type = null )
92 {
93 $current_screen = get_current_screen();
94 if ( empty($current_screen->base) || !in_array( $current_screen->base, [ 'upload', 'edit' ] ) ) {
95 return false;
96 }
97 if ( empty($post_type) ) {
98 if ( empty($post_type = $current_screen->post_type) ) {
99 return false;
100 }
101 }
102 if ( $post_type == 'attachment' ) {
103 return (bool) $this->options['folders_media'];
104 }
105 $default_post_types = [ 'post', 'page' ];
106 $folders_enable_for = (array) $this->options['folders_enable_for'];
107 $folders_enable_for = array_intersect( $folders_enable_for, $default_post_types );
108 if ( in_array( $post_type, $folders_enable_for ) ) {
109 return true;
110 }
111 return false;
112 }
113
114 public function add_footer_markup()
115 {
116 if ( $this->is_module_active() ) {
117 echo '<div id="wp-adminify--folder-app"></div>' ;
118 }
119 }
120
121 public function get_uncategorized_posts( $post_type )
122 {
123 global $wpdb ;
124 $post_table = $wpdb->prefix . "posts";
125 $term_table = $wpdb->prefix . "term_relationships";
126 $term_taxonomy_table = $wpdb->prefix . "term_taxonomy";
127 $post_type_tax = self::get_post_type_taxonomy( $post_type );
128
129 if ( $post_type != "attachment" ) {
130 $query = "SELECT COUNT(DISTINCT({$post_table}.ID)) AS total_records FROM {$post_table} WHERE 1=1 AND (\n NOT EXISTS (\n SELECT 1\n FROM {$term_table}\n INNER JOIN {$term_taxonomy_table}\n ON {$term_taxonomy_table}.term_taxonomy_id = {$term_table}.term_taxonomy_id\n WHERE {$term_taxonomy_table}.taxonomy = '%s'\n AND {$term_table}.object_id = {$post_table}.ID\n )\n ) AND {$post_table}.post_type = '%s' AND (({$post_table}.post_status = 'publish' OR {$post_table}.post_status = 'future' OR {$post_table}.post_status = 'draft' OR {$post_table}.post_status = 'private'))";
131 } else {
132 $query = "SELECT COUNT(DISTINCT({$post_table}.ID)) AS total_records FROM {$post_table} WHERE 1=1 AND (\n NOT EXISTS (\n SELECT 1\n FROM {$term_table}\n INNER JOIN {$term_taxonomy_table}\n ON {$term_taxonomy_table}.term_taxonomy_id = {$term_table}.term_taxonomy_id\n WHERE {$term_taxonomy_table}.taxonomy = '%s'\n AND {$term_table}.object_id = {$post_table}.ID\n )\n ) AND {$post_table}.post_type = '%s' AND {$post_table}.post_status = 'inherit'";
133 }
134
135 $query = $wpdb->prepare( $query, $post_type_tax, $post_type );
136 $total = $wpdb->get_var( $query );
137 return ( empty($total) ? 0 : $total );
138 }
139
140 public function get_folders( $post_type, $post_type_tax )
141 {
142 $folders = get_terms( $post_type_tax, array(
143 'hide_empty' => false,
144 'pad_counts' => true,
145 ) );
146 if ( is_wp_error( $folders ) ) {
147 return [];
148 }
149 foreach ( $folders as $folder ) {
150 $count = $this->items_in_taxonomy( $folder->term_id, $post_type, $post_type_tax );
151 $color = get_term_meta( $folder->term_id, '_wp_adminify_fodler_color', true );
152 if ( empty($color) ) {
153 $color = '';
154 }
155 $folder->color = $color;
156 $folder->count = $count;
157 }
158 return $folders;
159 }
160
161 public function enqueue_scripts()
162 {
163 if ( !$this->is_module_active() ) {
164 return;
165 }
166 $current_screen = get_current_screen();
167 wp_enqueue_style(
168 'wp-adminify--folder',
169 WP_ADMINIFY_URL . 'assets/admin/css/wp-adminify--folder.css',
170 [],
171 WP_ADMINIFY_VER
172 );
173 $post_type = $current_screen->post_type;
174 $post_type_tax = self::get_post_type_taxonomy( $post_type );
175 $data = [
176 'adminurl' => admin_url(),
177 'ajaxurl' => admin_url( 'admin-ajax.php' ),
178 'post_type' => $post_type,
179 'nonce' => wp_create_nonce( '__adminify-folder-secirity__' ),
180 'post_type_tax' => $post_type_tax,
181 'is_pro' => wp_validate_boolean( jltwp_adminify()->can_use_premium_code__premium_only() ),
182 'pro_notice' => Utils::adminify_upgrade_pro(),
183 ];
184 $data = array_merge( $data, $this->refreshed_folder_data( $post_type, $post_type_tax ) );
185 wp_localize_script( 'wp-adminify--folder', 'wp_adminify__folder_data', $data );
186 wp_enqueue_script( 'wp-adminify--folder' );
187 }
188
189 public function handle_adminify_folder()
190 {
191 check_ajax_referer( '__adminify-folder-secirity__' );
192
193 if ( !empty($_POST['route']) ) {
194 $route_handler = 'handle_' . $_POST['route'];
195 if ( is_callable( get_class( $this ), $route_handler ) ) {
196 $this->{$route_handler}( $_POST );
197 }
198 }
199
200 wp_send_json_error( [
201 'message' => __( 'Something is wrong, no route found' ),
202 ], 400 );
203 }
204
205 public function handle_delete_folders( $data )
206 {
207 if ( empty($data['term_ids']) || empty($data['post_type']) || empty($data['post_type_tax']) ) {
208 wp_send_json_error( [
209 'message' => __( 'Something is wrong, few args are missing' ),
210 ], 400 );
211 }
212 $term_ids = $data['term_ids'];
213 $post_type = $data['post_type'];
214 $post_type_tax = $data['post_type_tax'];
215 foreach ( $term_ids as $term_id ) {
216 wp_delete_term( $term_id, $post_type_tax );
217 }
218 $data = $this->refreshed_folder_data( $post_type, $post_type_tax );
219 wp_send_json_success( $data );
220 }
221
222 public function handle_rename_folder( $data )
223 {
224 if ( empty($data['term_id']) || empty($data['post_type']) || empty($data['post_type_tax']) || empty($data['folder_name']) || empty($data['folder_color_tag']) ) {
225 wp_send_json_error( [
226 'message' => __( 'Something is wrong, few args are missing' ),
227 ], 400 );
228 }
229 $term_id = $data['term_id'];
230 $folder_name = $data['folder_name'];
231 $post_type = $data['post_type'];
232 $post_type_tax = $data['post_type_tax'];
233 $folder_color_tag = $data['folder_color_tag'];
234 $update = wp_update_term( $term_id, $post_type_tax, [
235 'name' => $folder_name,
236 ] );
237 if ( is_wp_error( $update ) ) {
238 wp_send_json_success( [
239 'message' => $update->get_error_message(),
240 ], 202 );
241 }
242 update_term_meta( $term_id, '_wp_adminify_fodler_color', $folder_color_tag );
243 $data = $this->refreshed_folder_data( $post_type, $post_type_tax );
244 wp_send_json_success( $data );
245 }
246
247 public function handle_move_to_folder( $data )
248 {
249 if ( empty($data['post_ids']) || empty($data['folder_id']) || empty($data['post_type']) || empty($data['post_type_tax']) ) {
250 wp_send_json_error( [
251 'message' => __( 'Something is wrong, few args are missing' ),
252 ], 400 );
253 }
254 $post_ids = (array) $data['post_ids'];
255 $folder_id = sanitize_text_field( $data['folder_id'] );
256 $post_type = sanitize_text_field( $data['post_type'] );
257 $post_type_tax = sanitize_text_field( $data['post_type_tax'] );
258 global $mode ;
259 $mode = ( empty($data['mode']) ? 'list' : sanitize_text_field( $data['mode'] ) );
260 foreach ( $post_ids as $post_id ) {
261
262 if ( $folder_id == 'uncategorized' ) {
263 wp_set_object_terms(
264 $post_id,
265 '',
266 $post_type_tax,
267 false
268 );
269 continue;
270 }
271
272 $term = get_term( $folder_id );
273 if ( !empty($term) && isset( $term->slug ) ) {
274 wp_set_object_terms(
275 $post_id,
276 $term->slug,
277 $post_type_tax,
278 true
279 );
280 }
281 }
282 $updated_rows = '';
283
284 if ( $post_type == 'attachment' ) {
285 global $wp_query ;
286 $args = array(
287 'posts_per_page' => count( $post_ids ),
288 'orderby' => 'title',
289 'order' => 'ASC',
290 'post_type' => $post_type,
291 'post_status' => 'any',
292 'post__in' => $post_ids,
293 );
294 $wp_query = new \WP_Query( $args );
295 $wp_list_table = \_get_list_table( 'WP_Media_List_Table', array(
296 'screen' => $_POST['screen'],
297 ) );
298 foreach ( $post_ids as $post_id ) {
299 ob_start();
300 $wp_list_table->display_rows();
301 $updated_rows = ob_get_clean();
302 }
303 } else {
304 $wp_list_table = \_get_list_table( 'WP_Posts_List_Table', array(
305 'screen' => $_POST['screen'],
306 ) );
307 foreach ( $post_ids as $post_id ) {
308 $level = 0;
309
310 if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) {
311 $request_post = [ get_post( $post_id ) ];
312 $parent = $request_post[0]->post_parent;
313 while ( $parent > 0 ) {
314 $parent_post = get_post( $parent );
315 $parent = $parent_post->post_parent;
316 $level++;
317 }
318 }
319
320 ob_start();
321 $wp_list_table->display_rows( [ get_post( $post_id ) ], $level );
322 $updated_rows .= ob_get_clean();
323 }
324 }
325
326 $data = [
327 'updated_rows' => $updated_rows,
328 ];
329 $data = array_merge( $data, $this->refreshed_folder_data( $post_type, $post_type_tax ) );
330 wp_send_json_success( $data );
331 }
332
333 public function handle_refresh_folders( $data )
334 {
335 if ( empty($data['post_type']) || empty($data['post_type_tax']) ) {
336 wp_send_json_error( [
337 'message' => __( 'Something is wrong, few args are missing' ),
338 ], 400 );
339 }
340 $post_type = $data['post_type'];
341 $post_type_tax = $data['post_type_tax'];
342 wp_send_json_success( $this->refreshed_folder_data( $post_type, $post_type_tax ) );
343 }
344
345 public function handle_create_new_folder( $data )
346 {
347 if ( empty($data['post_type']) || empty($data['post_type_tax']) ) {
348 wp_send_json_error( [
349 'message' => __( 'Something is wrong, few args are missing' ),
350 ], 400 );
351 }
352 if ( empty($data['new_folder_name']) || empty($data['folder_color_tag']) ) {
353 wp_send_json_error( [
354 'message' => __( 'Something is wrong, few args are missing' ),
355 ], 400 );
356 }
357 $post_type = $data['post_type'];
358 $post_type_tax = $data['post_type_tax'];
359 $new_folder_name = $data['new_folder_name'];
360 $folder_color_tag = $data['folder_color_tag'];
361 $parent_term_id = 0;
362 if ( jltwp_adminify()->can_use_premium_code__premium_only() && !empty($data['parent_folder']) ) {
363 $parent_term_id = $data['parent_folder'];
364 }
365 $insert_data = wp_insert_term( $new_folder_name, $post_type_tax, [
366 'parent' => $parent_term_id,
367 ] );
368
369 if ( !is_wp_error( $insert_data ) ) {
370 update_term_meta( $insert_data['term_id'], '_wp_adminify_fodler_color', $folder_color_tag );
371 wp_send_json_success( $this->refreshed_folder_data( $post_type, $post_type_tax ) );
372 } else {
373 wp_send_json_success( [
374 'message' => $insert_data->get_error_message(),
375 ], 202 );
376 }
377
378 }
379
380 function refreshed_folder_data( $post_type, $post_type_tax )
381 {
382 remove_filter( 'pre_get_posts', [ $this, 'filter_list' ] );
383 return [
384 'folders' => $this->get_folders( $post_type, $post_type_tax ),
385 'folder_hierarchy' => _get_term_hierarchy( $post_type_tax ),
386 'total_posts' => wp_count_posts( $post_type ),
387 'total_uncat_posts' => $this->get_uncategorized_posts( $post_type ),
388 ];
389 }
390
391 function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false )
392 {
393 $post_ID = (int) $post_ID;
394 $post_type = get_post_type( $post_ID );
395 $post_status = get_post_status( $post_ID );
396 // If $post_categories isn't already an array, make it one.
397 $post_categories = (array) $post_categories;
398
399 if ( empty($post_categories) ) {
400 /**
401 * Filters post types (in addition to 'post') that require a default category.
402 *
403 * @since 5.5.0
404 *
405 * @param string[] $post_types An array of post type names. Default empty array.
406 */
407 $default_category_post_types = apply_filters( 'default_category_post_types', array() );
408 // Regular posts always require a default category.
409 $default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
410
411 if ( in_array( $post_type, $default_category_post_types, true ) && is_object_in_taxonomy( $post_type, 'category' ) && 'auto-draft' !== $post_status ) {
412 $post_categories = array( get_option( 'default_category' ) );
413 $append = false;
414 } else {
415 $post_categories = array();
416 }
417
418 } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) {
419 return true;
420 }
421
422 return wp_set_post_terms(
423 $post_ID,
424 $post_categories,
425 'category',
426 $append
427 );
428 }
429
430 public function items_in_taxonomy( $term_id, $post_type, $taxonomy )
431 {
432 $posts = get_posts( array(
433 'numberposts' => -1,
434 'post_status' => 'any',
435 'post_type' => $post_type,
436 'fields' => 'ids',
437 'tax_query' => array( array(
438 'operator' => 'IN',
439 'taxonomy' => $taxonomy,
440 'field' => 'term_id',
441 'terms' => $term_id,
442 ) ),
443 ) );
444 return count( $posts );
445 }
446
447 public static function get_post_type_taxonomy( $post_type )
448 {
449 if ( $post_type == 'page' ) {
450 return 'folder';
451 }
452 if ( $post_type == 'attachment' ) {
453 $post_type = 'media';
454 }
455 return $post_type . '_folder';
456 }
457
458 public function register_folders_terms()
459 {
460 $post_types = $this->get_post_types();
461 foreach ( $post_types as $post_type ) {
462 $labels = array(
463 'name' => esc_html__( 'Folders', 'wp-adminify' ),
464 'singular_name' => esc_html__( 'Folder', 'wp-adminify' ),
465 'all_items' => esc_html__( 'All Folders', 'wp-adminify' ),
466 'edit_item' => esc_html__( 'Edit Folder', 'wp-adminify' ),
467 'update_item' => esc_html__( 'Update Folder', 'wp-adminify' ),
468 'add_new_item' => esc_html__( 'Add New Folder', 'wp-adminify' ),
469 'new_item_name' => esc_html__( 'Add folder name', 'wp-adminify' ),
470 'menu_name' => esc_html__( 'Folders', 'wp-adminify' ),
471 'search_items' => esc_html__( 'Search Folders', 'wp-adminify' ),
472 'parent_item' => esc_html__( 'Parent Folder', 'wp-adminify' ),
473 );
474 $args = array(
475 'label' => esc_html__( 'Folder', 'wp-adminify' ),
476 'labels' => $labels,
477 'show_tagcloud' => false,
478 'hierarchical' => true,
479 'public' => false,
480 'show_ui' => false,
481 'show_in_menu' => false,
482 'show_in_rest' => true,
483 'show_admin_column' => true,
484 'query_var' => true,
485 'rewrite' => false,
486 'capabilities' => array(
487 'manage_terms' => 'manage_categories',
488 'edit_terms' => 'manage_categories',
489 'delete_terms' => 'manage_categories',
490 'assign_terms' => 'manage_categories',
491 ),
492 );
493 $taxonomy = self::get_post_type_taxonomy( $post_type );
494 register_taxonomy( $taxonomy, $post_type, $args );
495 }
496 }
497
498 public function modify_columns_actions()
499 {
500 $post_types = $this->get_post_types();
501 foreach ( $post_types as $post_type ) {
502
503 if ( $post_type == 'post' ) {
504 add_filter( 'manage_edit-post_columns', [ $this, 'manage_columns_head' ] );
505 add_filter( 'manage_posts_columns', [ $this, 'manage_columns_head' ] );
506 add_action(
507 'manage_posts_custom_column',
508 [ $this, 'manage_columns_content' ],
509 10,
510 2
511 );
512 add_filter( 'bulk_actions-edit-post', [ $this, 'custom_bulk_action' ] );
513 } else {
514
515 if ( $post_type == 'page' ) {
516 add_filter( 'manage_edit-page_columns', [ $this, 'manage_columns_head' ] );
517 add_filter( 'manage_page_posts_columns', [ $this, 'manage_columns_head' ] );
518 add_action(
519 'manage_page_posts_custom_column',
520 [ $this, 'manage_columns_content' ],
521 10,
522 2
523 );
524 add_filter( 'bulk_actions-edit-page', [ $this, 'custom_bulk_action' ] );
525 } else {
526
527 if ( $post_type == 'attachment' ) {
528 add_filter( 'manage_media_columns', [ $this, 'manage_columns_head' ] );
529 add_action(
530 'manage_media_custom_column',
531 [ $this, 'manage_columns_content' ],
532 10,
533 2
534 );
535 } else {
536 add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'manage_columns_head' ], 99999 );
537 add_action(
538 'manage_' . $post_type . '_posts_custom_column',
539 [ $this, 'manage_columns_content' ],
540 2,
541 2
542 );
543 add_filter( 'bulk_actions-edit-' . $post_type, [ $this, 'custom_bulk_action' ] );
544 }
545
546 }
547
548 }
549
550 }
551 }
552
553 public static function sanitize_options( $value, $type = '' )
554 {
555 $value = stripslashes( $value );
556 if ( $type == "int" ) {
557 return filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
558 }
559 if ( $type == "email" ) {
560 return filter_var( $value, FILTER_SANITIZE_EMAIL );
561 }
562 return filter_var( $value, FILTER_SANITIZE_STRING );
563 }
564
565 function manage_columns_head( $posts_columns )
566 {
567 $post_type = null;
568 if ( isset( $_REQUEST['post_type'] ) ) {
569 $post_type = sanitize_text_field( $_REQUEST['post_type'] );
570 }
571 if ( $this->is_module_active( $post_type ) ) {
572 return [
573 'adminify_move' => '<div class="adminify-move-multiple adminify-col" title="' . esc_attr__( 'Move selected items', 'wp-adminify' ) . '"><span class="dashicons dashicons-move"></span><div class="adminify-items"></div></div>',
574 ] + $posts_columns;
575 }
576 return $posts_columns;
577 }
578
579 function manage_columns_content( $column_name, $post_ID )
580 {
581 $post_type = null;
582 if ( isset( $_REQUEST['post_type'] ) ) {
583 $post_type = sanitize_text_field( $_REQUEST['post_type'] );
584 }
585 $postIDs = self::$postIds;
586 if ( !is_array( $postIDs ) ) {
587 $postIDs = array();
588 }
589
590 if ( !in_array( $post_ID, $postIDs ) ) {
591 $postIDs[] = $post_ID;
592 self::$postIds = $postIDs;
593 // global $typenow;
594 // $type = $typenow;
595 if ( $this->is_module_active( $post_type ) ) {
596
597 if ( $column_name == 'adminify_move' ) {
598 $title = get_the_title();
599 if ( strlen( $title ) > 20 ) {
600 $title = substr( $title, 0, 20 ) . "...";
601 }
602 echo "<div class='adminify-move-file' data-id='{$post_ID}'><span class='adminify-move dashicons dashicons-move' data-id='{$post_ID}'></span><span class='adminify-move-file--title'>{$title}</span></div>" ;
603 }
604
605 }
606 }
607
608 }
609
610 public function custom_bulk_action( $bulk_actions )
611 {
612 $bulk_actions['move_to_folder'] = __( 'Move to Folder', 'wp-adminify' );
613 return $bulk_actions;
614 }
615
616 }