PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.2.1
Adminify – White Label, Admin Menu Editor, Login Customizer v3.2.1
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 4.1.17 All 164 releases
adminify / Inc / Modules / AdminColumns / AdminColumnsData.php

AdminColumnsData.php in Adminify – White Label, Admin Menu Editor, Login Customizer 3.2.1, at Inc/Modules/AdminColumns/AdminColumnsData.php

591 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // define all helper functions to create list table
4 function jltwp_adminify_admin_columns_create_post_table( $screen_id ) {
5 require_once ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php';
6
7 return new WP_Posts_List_Table( [ 'screen' => $screen_id ] );
8 }
9
10 function jltwp_adminify_admin_columns_create_user_table( $screen_id ) {
11 require_once ABSPATH . 'wp-admin/includes/class-wp-users-list-table.php';
12
13 return new WP_Users_List_Table( [ 'screen' => $screen_id ] );
14 }
15
16 function jltwp_adminify_admin_columns_create_comment_table( $screen_id ) {
17 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
18
19 $table = new WP_Comments_List_Table( [ 'screen' => $screen_id ] );
20
21 // Since 4.4 the `floated_admin_avatar` filter is added in the constructor of the `\WP_Comments_List_Table` class.
22 remove_filter( 'comment_author', [ $table, 'floated_admin_avatar' ] );
23
24 return $table;
25 }
26
27 function jltwp_adminify_admin_columns_create_media_table( $screen_id ) {
28 require_once ABSPATH . 'wp-admin/includes/class-wp-media-list-table.php';
29
30 return new WP_Media_List_Table( [ 'screen' => $screen_id ] );
31 }
32
33 function jltwp_adminify_admin_columns_create_taxonomy_table( $screen_id ) {
34 require_once ABSPATH . 'wp-admin/includes/class-wp-terms-list-table.php';
35
36 return new WP_Terms_List_Table( [ 'screen' => $screen_id ] );
37 }
38 function jltwp_adminify_admin_columns_create_network_user_table( $screen_id ) {
39 require_once ABSPATH . 'wp-admin/includes/class-wp-ms-users-list-table.php';
40
41 return new WP_MS_Users_List_Table( [ 'screen' => $screen_id ] );
42 }
43
44
45
46 // get the columns list of any post or users, comments etc.
47 function jltwp_adminify_admin_columns_data( $type ) {
48 // $type can be post, page, product, user, shop_order, shop_coupon, 'edit-comments', 'users', 'upload'
49
50 $builtin_non_posttype_data = [ 'users', 'edit-comments', 'upload' ];
51 $table = '';
52
53 if ( in_array( $type, $builtin_non_posttype_data ) ) {
54 switch ( $type ) {
55 case 'users':
56 $table = jltwp_adminify_admin_columns_create_user_table( $type );
57 break;
58 case 'edit-comments':
59 $table = jltwp_adminify_admin_columns_create_comment_table( $type );
60 break;
61 case 'upload':
62 $table = jltwp_adminify_admin_columns_create_media_table( $type );
63 break;
64 }
65 } else {
66 $type_post = post_type_exists( $type );
67 $type_tax = taxonomy_exists( str_replace( 'edit-', '', $type ) );
68
69 if ( $type_post ) {
70 $table = jltwp_adminify_admin_columns_create_post_table( $type ); // for all kind of post_type
71 }
72 if ( $type_tax ) {
73 $table = jltwp_adminify_admin_columns_create_taxonomy_table( $type ); // for all kind of taxonomy
74 }
75 }
76
77 // handle woocommerce
78 if ( function_exists( 'WC' ) ) {
79 switch ( $type ) {
80 case 'product':
81 include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/list-tables/class-wc-admin-list-table-products.php';
82 new WC_Admin_List_Table_Products();
83 break;
84 case 'shop_order':
85 include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/list-tables/class-wc-admin-list-table-orders.php';
86 new WC_Admin_List_Table_Orders();
87 break;
88 case 'shop_coupon':
89 include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/list-tables/class-wc-admin-list-table-coupons.php';
90 new WC_Admin_List_Table_Coupons();
91 break;
92 }
93 }
94
95 $col_names = [];
96
97 if ( $table instanceof WP_List_Table ) {
98 $col_names = $table->get_columns();
99 if ( array_key_exists( 'cb', $col_names ) ) {
100 unset( $col_names['cb'] );
101 }
102 }
103
104 return (array) $col_names;
105 }
106
107 // add_action('current_screen', 'jltwp_adminify_admin_columns_data', 99);
108
109
110 function adminify_post_types() {
111 $sections = [];
112
113 $update_column_settings = get_option( '_wpadminify_admin_columns_settings' );
114
115 foreach ( $update_column_settings as $p => $post_type ) {
116 if ( $post_type == 'attachment' ) {
117 continue;
118 }
119
120 $defaults = [];
121
122 foreach ( $post_type as $column => $column_label ) {
123 $column_meta = adminify_get_column_meta( $post_type, $column, $column_label );
124
125 $defaults[] = [
126 'type' => $column,
127 'label' => $column_label,
128 'width' => $column_meta['width'],
129 ];
130 }
131
132 $sections[] = [
133 'title' => WPAdminify\Inc\Utils::id_to_string( $p ),
134 'fields' => [
135 [
136 'id' => 'admin-columns-group-' . $p,
137 'type' => 'group',
138 'title' => '',
139 'fields' => [
140
141 'field_type' => [
142 'id' => 'field_type',
143 'type' => 'select',
144 'title' => 'Column Type',
145 'chosen' => true,
146 'placeholder' => 'Select Column Type',
147 'options' => [
148 'title' => esc_html__( 'Title', 'adminify' ),
149 'author' => esc_html__( 'Author', 'adminify' ),
150 'categories' => esc_html__( 'Categories', 'adminify' ),
151 'tags' => esc_html__( 'Tags', 'adminify' ),
152 'comments' => esc_html__( 'Comments', 'adminify' ),
153 'date' => esc_html__( 'Date', 'adminify' ),
154 'id' => esc_html__( 'ID', 'adminify' ),
155 ],
156 ],
157
158 'label' => [
159 'id' => 'label',
160 'type' => 'text',
161 'title' => esc_html__( 'Label', 'adminify' ),
162 ],
163
164 'width' => [
165 'id' => 'width',
166 'type' => 'slider',
167 'title' => esc_html__( 'Width', 'adminify' ),
168 'unit' => '%',
169 ],
170
171 ],
172
173 'default' => $defaults,
174
175 ],
176 ],
177 ];
178 }
179
180 return $sections;
181 }
182
183
184
185 /*
186 * Get registered post_types
187 */
188 function adminify__get_post_types() {
189 $post_types = [];
190
191 foreach ( WPAdminify\Inc\Utils::get_post_types() as $post_type ) {
192 if ( $post_type->name == 'attachment' ) {
193 continue;
194 }
195
196 $post_types[ $post_type->name ] = $post_type->labels->singular_name;
197 }
198
199 return $post_types;
200 }
201
202 /*
203 * Get registered post_types
204 */
205 function adminify__get_taxonomies() {
206 $taxonomies = [];
207
208 foreach ( WPAdminify\Inc\Utils::get_taxonomies() as $taxonomy ) {
209 $taxonomies[ $taxonomy->name ] = $taxonomy->labels->singular_name;
210 }
211
212 return $taxonomies;
213 }
214
215 /*
216 * Get saved version of both visible & non visible columns of a specific post_type
217 */
218 function adminify__get_post_type_all_columns( $post_type ) {
219 return (array) get_option( '_adminify_admin_columns_' . $post_type, [] );
220 }
221
222 /*
223 * Get both visible & non visible columns of a specific post_type
224 */
225 function _adminify__get_post_type_all_columns( $post_type ) {
226 $column_groups = [
227 [
228 'group' => esc_html__( 'Default', 'adminify' ),
229 'options' => (array) jltwp_adminify_admin_columns_data( $post_type ),
230 ],
231 ];
232
233 // Set Default Columns for First Installation
234 update_option( '_adminify_admin_columns_' . $post_type, $column_groups );
235
236 // Added custom taxonomy support
237 $taxonomies = get_taxonomies( [ 'object_type' => [ $post_type ] ], 'objects' );
238 $taxonomies = wp_list_pluck( $taxonomies, 'label', 'name' );
239 $taxonomy_columns = [];
240 if ( ! empty( $taxonomies ) ) {
241 foreach ( $taxonomies as $tax_name => $tax_label ) {
242
243 // Skip the default category & tag for post
244 if ( $post_type == 'post' && ( $tax_name == 'category' || $tax_name == 'post_tag' ) ) {
245 continue;
246 }
247
248 $_tax_name = 'taxonomy-' . $tax_name;
249 if ( ! in_array( $_tax_name, $taxonomy_columns ) ) {
250 $taxonomy_columns[ $_tax_name ] = $tax_label;
251 }
252 }
253 }
254
255 $column_groups[] = [
256 'group' => esc_html__( 'Taxonomy', 'adminify' ),
257 'options' => $taxonomy_columns,
258 ];
259
260 $custom_admin_columns = adminify_get_custom_admin_columns();
261 $_custom_admin_columns = [];
262
263 foreach ( $custom_admin_columns as $column ) {
264 $_custom_admin_columns[ $column['name'] ] = $column['label'];
265 }
266
267 $column_groups[] = [
268 'group' => esc_html__( 'Custom', 'adminify' ),
269 'options' => $_custom_admin_columns,
270 ];
271
272 $acf_fields = WPAdminify\Inc\Modules\AdminColumns\AdminColumns::get_acf_fields( $post_type );
273 if ( ! empty( $acf_fields ) ) {
274 $column_groups[] = [
275 'group' => esc_html__( 'ACF', 'adminify' ),
276 'options' => $acf_fields,
277 ];
278 }
279
280 $pods_fields = WPAdminify\Inc\Modules\AdminColumns\AdminColumns::get_pods_fields( $post_type );
281 if ( ! empty( $pods_fields ) ) {
282 $column_groups[] = [
283 'group' => esc_html__( 'Pods', 'adminify' ),
284 'options' => $pods_fields,
285 ];
286 }
287
288 $column_groups[] = [
289 'title' => esc_html__( 'Function', 'adminify' ),
290 'name' => 'function',
291 ];
292
293 $column_groups[] = [
294 'title' => esc_html__( 'Shortcode', 'adminify' ),
295 'name' => 'shortcode',
296 ];
297
298 return $column_groups;
299 }
300
301 /*
302 * Get saved version of both visible & non visible columns of a specific taxonomy
303 */
304 function adminify__get_taxonomy_all_columns( $taxonomy ) {
305 return (array) get_option( '_adminify_admin_taxonomy_columns_' . $taxonomy, [] );
306 }
307
308 /*
309 * Get both visible & non visible columns of a specific taxonomy
310 */
311 function _adminify__get_taxonomy_all_columns( $taxonomy ) {
312 $columns = (array) jltwp_adminify_admin_columns_data( 'edit-' . $taxonomy );
313 update_option( '_adminify_admin_taxonomy_columns_' . esc_attr( $taxonomy ), $columns );
314 return $columns;
315 }
316
317 /*
318 * Get visible columns of all post_types
319 */
320 function adminify__get_post_types_columns() {
321 $post_types = adminify__get_post_types();
322
323 $post_types_columns = [];
324
325 foreach ( $post_types as $post_type => $post_type_title ) {
326 $column_data = [
327 'name' => $post_type,
328 'title' => $post_type_title,
329 'columns' => adminify_columns_group_to_options( _adminify__get_post_type_all_columns( $post_type ) ),
330 'display_columns' => adminify_prepare_post_type_column_meta( $post_type ),
331 'fields' => adminify_get_post_type_fields( $post_type ),
332 ];
333
334 if ( ! in_array( $post_type, [ 'post', 'page' ] ) ) {
335 $column_data['is_pro'] = true;
336 }
337
338 $post_types_columns[] = $column_data;
339 }
340
341 return $post_types_columns;
342 }
343
344 function _adminify__get_taxonomy_post_type( $taxonomy ) {
345 $tax = get_taxonomy( $taxonomy );
346
347 if ( $tax && ! empty( $tax->object_type ) ) {
348 return $tax->object_type[0];
349 }
350
351 return '';
352 }
353
354 /*
355 * Get visible columns of all taxonomies
356 */
357 function adminify__get_taxonomies_columns() {
358 $taxonomies = adminify__get_taxonomies();
359
360 $taxonomies_columns = [];
361
362 foreach ( $taxonomies as $taxonomy => $taxonomy_title ) {
363 $column_data = [
364 'name' => $taxonomy,
365 'title' => $taxonomy_title,
366 'object_type' => _adminify__get_taxonomy_post_type( $taxonomy ),
367 'columns' => _adminify__get_taxonomy_all_columns( $taxonomy ),
368 'display_columns' => adminify_prepare_taxonomy_column_meta( $taxonomy ),
369 'fields' => adminify_get_taxonomy_fields( $taxonomy ),
370 ];
371
372 if ( ! in_array( $taxonomy, [ 'category', 'post_tag' ] ) ) {
373 $column_data['is_pro'] = true;
374 }
375
376 $taxonomies_columns[] = $column_data;
377 }
378
379 return $taxonomies_columns;
380 }
381
382 /*
383 * Get specific column meta data
384 */
385 function adminify_get_column_meta( $post_type, $column, $column_label ) {
386 $column_default_meta = [
387 'label' => $column_label,
388 'width' => [
389 'value' => 'auto',
390 'unit' => '%',
391 ],
392 'fields' => [ 'type', 'label', 'width' ],
393 ];
394
395 /*
396 * You can extend the fields based on post type and column
397 * Make sure you have added the new field type in this function: adminify_get_post_type_fields
398 */
399
400 // if ( $post_type == 'page' && $column == 'taxonomy-folder' ) {
401 // $column_default_meta['fields'][] = 'new';
402 // }
403
404 $data = (array) get_option( '_adminify_admin_columns_meta_data', [] );
405
406 $column_meta = [];
407
408 if ( ! empty( $data ) && ! empty( $data[ $post_type ] ) && ! empty( $data[ $post_type ][ $column ] ) ) {
409 $column_meta = $data[ $post_type ][ $column ];
410 }
411
412 return array_merge_recursive( $column_default_meta, $column_meta );
413 }
414
415 function adminify_columns_validation( $columns_meta ) {
416 foreach ( $columns_meta as &$column_meta ) {
417 $column_meta['label'] = wp_kses_post( $column_meta['label'] );
418 $column_meta['name'] = sanitize_key( $column_meta['name'] );
419 if ( ! empty( $column_meta['width'] ) ) {
420 $column_meta['width'] = array_map( 'sanitize_text_field', $column_meta['width'] );
421 }
422 }
423 return $columns_meta;
424 }
425
426 /*
427 * Get post_type columns meta
428 */
429 function adminify_prepare_post_type_column_meta( $post_type ) {
430 $columns_meta = get_option( '_adminify_admin_columns_meta_' . $post_type, null );
431
432 if ( ! is_null( $columns_meta ) ) {
433 $columns_meta = adminify_columns_validation( $columns_meta );
434 return $columns_meta;
435 }
436
437 $columns = adminify_columns_group_to_options( adminify__get_post_type_all_columns( $post_type ) );
438
439 $_columns = [];
440
441 foreach ( $columns as $column => $column_label ) {
442 $_columns[] = [ 'name' => $column ] + (array) adminify_get_column_meta( $post_type, $column, $column_label );
443 }
444
445 return $_columns;
446 }
447
448 /*
449 * Get taxonomy columns meta
450 */
451 function adminify_prepare_taxonomy_column_meta( $taxonomy ) {
452 $columns_meta = get_option( '_adminify_admin_taxonomy_columns_meta_' . $taxonomy, null );
453
454 if ( ! is_null( $columns_meta ) ) {
455 $columns_meta = adminify_columns_validation( $columns_meta );
456 return (array) $columns_meta;
457 }
458
459 $columns = adminify__get_taxonomy_all_columns( $taxonomy );
460
461 $_columns = [];
462
463 foreach ( $columns as $column => $column_label ) {
464 $_columns[] = [ 'name' => $column ] + (array) adminify_get_column_meta( $taxonomy, $column, $column_label );
465 }
466
467 return $_columns;
468 }
469
470 /*
471 * Get post_type fields
472 */
473 function adminify_get_post_type_fields( $post_type ) {
474 $columns = _adminify__get_post_type_all_columns( $post_type );
475
476 return [
477 [
478 'id' => 'type',
479 'title' => esc_html__( 'Column Type', 'adminify' ),
480 'chosen' => true,
481 'placeholder' => 'Select Column Type',
482 'options' => $columns,
483 ],
484 [
485 'id' => 'field_name',
486 'title' => esc_html__( 'Field Name', 'adminify' ),
487 ],
488 [
489 'id' => 'function_name',
490 'title' => esc_html__( 'Function Name', 'adminify' ),
491 ],
492 [
493 'id' => 'shortcode_name',
494 'title' => esc_html__( 'Shortcode Name', 'adminify' ),
495 ],
496 [
497 'id' => 'label',
498 'title' => esc_html__( 'Label', 'adminify' ),
499 ],
500 [
501 'id' => 'width',
502 'title' => esc_html__( 'Width', 'adminify' ),
503 'unit' => '%',
504 ],
505
506 ];
507 }
508
509 /*
510 * Get taxonomy fields
511 */
512 function adminify_get_taxonomy_fields( $taxonomy ) {
513 $columns = array_map( 'sanitize_text_field', _adminify__get_taxonomy_all_columns( $taxonomy ) );
514
515 return [
516 [
517 'id' => 'type',
518 'type' => 'select',
519 'title' => esc_html__( 'Column Type', 'adminify' ),
520 'chosen' => true,
521 'placeholder' => esc_html__( 'Select Column Type', 'adminify' ),
522 'options' => $columns,
523 ],
524 [
525 'id' => 'label',
526 'type' => 'text',
527 'title' => esc_html__( 'Label', 'adminify' ),
528 ],
529 [
530 'id' => 'width',
531 'type' => 'slider',
532 'title' => esc_html__( 'Width', 'adminify' ),
533 'unit' => '%',
534 ],
535
536 /*
537 * Register additional fields
538 */
539 // [
540 // 'id' => 'new',
541 // 'type' => 'text',
542 // 'title' => 'New'
543 // ]
544
545 ];
546 }
547
548 function adminify_columns_group_to_options( array $data ) {
549 $_data = [];
550 foreach ( $data as $group ) {
551 if ( ! empty( $group['group'] ) ) {
552 $_data = array_merge( $_data, $group['options'] );
553 } else {
554 $_data[ $group['name'] ] = $group['title'];
555 }
556 }
557 return $_data;
558 }
559
560 function adminify_get_custom_admin_columns() {
561 return [
562 [
563 'name' => 'word_cound',
564 'label' => esc_html__( 'Word Count', 'adminify' ),
565 'callback' => 'adminify_admin_column__word_count',
566 ],
567 [
568 'name' => 'permalink',
569 'label' => esc_html__( 'Permalink', 'adminify' ),
570 'callback' => 'adminify_admin_column__permalink',
571 ],
572 [
573 'name' => 'status',
574 'label' => esc_html__( 'Status', 'adminify' ),
575 'callback' => 'adminify_admin_column__status',
576 ],
577 ];
578 }
579
580 function adminify_admin_column__word_count( $object_id ) {
581 return str_word_count( get_the_content( $object_id ) );
582 }
583
584 function adminify_admin_column__permalink( $object_id ) {
585 return get_the_permalink( $object_id );
586 }
587
588 function adminify_admin_column__status( $object_id ) {
589 return get_post_status( $object_id );
590 }
591