| 1 |
<?php |
| 2 |
|
| 3 |
// define all helper functions to create list table |
| 4 |
function jltwp_adminify_admin_columns_create_post_table($screen_id) |
| 5 |
{ |
| 6 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php'); |
| 7 |
|
| 8 |
return new WP_Posts_List_Table(['screen' => $screen_id]); |
| 9 |
} |
| 10 |
|
| 11 |
function jltwp_adminify_admin_columns_create_user_table($screen_id) |
| 12 |
{ |
| 13 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-users-list-table.php'); |
| 14 |
|
| 15 |
return new WP_Users_List_Table(['screen' => $screen_id]); |
| 16 |
} |
| 17 |
|
| 18 |
function jltwp_adminify_admin_columns_create_comment_table($screen_id) |
| 19 |
{ |
| 20 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php'); |
| 21 |
|
| 22 |
$table = new WP_Comments_List_Table(['screen' => $screen_id]); |
| 23 |
|
| 24 |
// Since 4.4 the `floated_admin_avatar` filter is added in the constructor of the `\WP_Comments_List_Table` class. |
| 25 |
remove_filter('comment_author', [$table, 'floated_admin_avatar']); |
| 26 |
|
| 27 |
return $table; |
| 28 |
} |
| 29 |
|
| 30 |
function jltwp_adminify_admin_columns_create_media_table($screen_id) |
| 31 |
{ |
| 32 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-media-list-table.php'); |
| 33 |
|
| 34 |
return new WP_Media_List_Table(['screen' => $screen_id]); |
| 35 |
} |
| 36 |
|
| 37 |
function jltwp_adminify_admin_columns_create_taxonomy_table($screen_id) |
| 38 |
{ |
| 39 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-terms-list-table.php'); |
| 40 |
|
| 41 |
return new WP_Terms_List_Table(['screen' => $screen_id]); |
| 42 |
} |
| 43 |
function jltwp_adminify_admin_columns_create_network_user_table($screen_id) |
| 44 |
{ |
| 45 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-ms-users-list-table.php'); |
| 46 |
|
| 47 |
return new WP_MS_Users_List_Table(['screen' => $screen_id]); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
// get the columns list of any post or users, comments etc. |
| 53 |
function jltwp_adminify_admin_columns_data($type) |
| 54 |
{ |
| 55 |
// $data_type_for_fetching_column can be post, page, product, user, shop_order, shop_coupon, 'edit-comments', 'users', 'upload' |
| 56 |
|
| 57 |
$data_type_for_fetching_column = $type; |
| 58 |
|
| 59 |
$builtin_non_posttype_data = ['users', 'edit-comments', 'upload', 'edit-category', 'edit-post_tag']; |
| 60 |
$table = ''; |
| 61 |
|
| 62 |
|
| 63 |
if (in_array($data_type_for_fetching_column, $builtin_non_posttype_data)) { |
| 64 |
switch ($data_type_for_fetching_column) { |
| 65 |
case 'users': |
| 66 |
$table = jltwp_adminify_admin_columns_create_user_table($data_type_for_fetching_column); |
| 67 |
break; |
| 68 |
case 'edit-comments': |
| 69 |
$table = jltwp_adminify_admin_columns_create_comment_table($data_type_for_fetching_column); |
| 70 |
break; |
| 71 |
case 'upload': |
| 72 |
$table = jltwp_adminify_admin_columns_create_media_table($data_type_for_fetching_column); |
| 73 |
break; |
| 74 |
case 'edit-category': |
| 75 |
case 'edit-post_tag': |
| 76 |
$table = jltwp_adminify_admin_columns_create_taxonomy_table($data_type_for_fetching_column); |
| 77 |
break; |
| 78 |
} |
| 79 |
} else { |
| 80 |
if (!empty($data_type_for_fetching_column)) { |
| 81 |
$table = jltwp_adminify_admin_columns_create_post_table($data_type_for_fetching_column); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// handle woocommerce |
| 86 |
if ( function_exists('WC') ) { |
| 87 |
|
| 88 |
switch ($data_type_for_fetching_column) { |
| 89 |
case 'product': |
| 90 |
include_once dirname(WC_PLUGIN_FILE) . '/includes/admin/list-tables/class-wc-admin-list-table-products.php'; |
| 91 |
new WC_Admin_List_Table_Products(); |
| 92 |
break; |
| 93 |
case 'shop_order': |
| 94 |
include_once dirname(WC_PLUGIN_FILE) . '/includes/admin/list-tables/class-wc-admin-list-table-orders.php'; |
| 95 |
new WC_Admin_List_Table_Orders(); |
| 96 |
break; |
| 97 |
case 'shop_coupon': |
| 98 |
include_once dirname(WC_PLUGIN_FILE) . '/includes/admin/list-tables/class-wc-admin-list-table-coupons.php'; |
| 99 |
new WC_Admin_List_Table_Coupons(); |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$col_names = []; |
| 105 |
|
| 106 |
if ( $table instanceof WP_List_Table ) { |
| 107 |
$col_names = $table->get_columns(); |
| 108 |
|
| 109 |
if ( array_key_exists('cb', $col_names) ) { |
| 110 |
unset($col_names['cb']); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
return (array) $col_names; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
// add_action('current_screen', 'jltwp_adminify_admin_columns_data', 99); |
| 120 |
|
| 121 |
|
| 122 |
function adminify_post_types() { |
| 123 |
|
| 124 |
$sections = []; |
| 125 |
|
| 126 |
$update_column_settings = get_option('_wpadminify_admin_columns_settings'); |
| 127 |
|
| 128 |
foreach ( $update_column_settings as $p => $post_type ) { |
| 129 |
|
| 130 |
if ( $post_type == 'attachment' ) continue; |
| 131 |
|
| 132 |
$defaults = []; |
| 133 |
|
| 134 |
foreach ( $post_type as $column => $column_label ) { |
| 135 |
|
| 136 |
$column_meta = adminify_get_column_meta( $post_type, $column, $column_label ); |
| 137 |
|
| 138 |
$defaults[] = array( |
| 139 |
'type' => $column, |
| 140 |
'label' => $column_label, |
| 141 |
'width' => $column_meta['width'] |
| 142 |
); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
$sections[] = array( |
| 147 |
'title' => WPAdminify\Inc\Utils::id_to_string($p), |
| 148 |
'fields' => array( |
| 149 |
array( |
| 150 |
'id' => 'admin-columns-group-' . $p, |
| 151 |
'type' => 'group', |
| 152 |
'title' => '', |
| 153 |
'fields' => array( |
| 154 |
|
| 155 |
'field_type' => array( |
| 156 |
'id' => 'field_type', |
| 157 |
'type' => 'select', |
| 158 |
'title' => 'Column Type', |
| 159 |
'chosen' => true, |
| 160 |
'placeholder' => 'Select Column Type', |
| 161 |
'options' => array( |
| 162 |
'title' => 'Title', |
| 163 |
'author' => 'Author', |
| 164 |
'categories' => 'Categories', |
| 165 |
'tags' => 'Tags', |
| 166 |
'comments' => 'Comments', |
| 167 |
'date' => 'Date', |
| 168 |
'id' => 'ID', |
| 169 |
), |
| 170 |
), |
| 171 |
|
| 172 |
'label' => array( |
| 173 |
'id' => 'label', |
| 174 |
'type' => 'text', |
| 175 |
'title' => 'Label', |
| 176 |
), |
| 177 |
|
| 178 |
'width' => array( |
| 179 |
'id' => 'width', |
| 180 |
'type' => 'slider', |
| 181 |
'title' => 'Width', |
| 182 |
'unit' => '%' |
| 183 |
), |
| 184 |
|
| 185 |
), |
| 186 |
|
| 187 |
'default' => $defaults |
| 188 |
|
| 189 |
) |
| 190 |
) |
| 191 |
); |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
return $sections; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
/* |
| 202 |
* Get registered post_types |
| 203 |
*/ |
| 204 |
function adminify__get_post_types() { |
| 205 |
|
| 206 |
$post_types = []; |
| 207 |
|
| 208 |
foreach ( WPAdminify\Inc\Utils::get_post_types() as $post_type ) { |
| 209 |
|
| 210 |
if ( $post_type->name == 'attachment' ) continue; |
| 211 |
|
| 212 |
$post_types[ $post_type->name ] = $post_type->labels->singular_name; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
return $post_types; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* Get registered post_types |
| 222 |
*/ |
| 223 |
function adminify__get_taxonomies() { |
| 224 |
|
| 225 |
$taxonomies = []; |
| 226 |
|
| 227 |
foreach ( WPAdminify\Inc\Utils::get_taxonomies() as $taxonomy ) { |
| 228 |
$taxonomies[ $taxonomy->name ] = $taxonomy->labels->singular_name; |
| 229 |
} |
| 230 |
|
| 231 |
return $taxonomies; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* Get both visible & non visible columns of a specific post_type |
| 237 |
*/ |
| 238 |
function adminify__get_post_type_all_columns( $post_type ) { |
| 239 |
|
| 240 |
return jltwp_adminify_admin_columns_data( $post_type ); // @todo implement the function |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* Get both visible & non visible columns of a specific taxonomy |
| 246 |
*/ |
| 247 |
function adminify__get_taxonomy_all_columns( $taxonomy ) { |
| 248 |
|
| 249 |
return jltwp_adminify_admin_columns_data( $taxonomy ); // @todo implement the function |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* Get visible columns of all post_types |
| 255 |
*/ |
| 256 |
function adminify__get_post_types_columns() { |
| 257 |
|
| 258 |
$post_types = adminify__get_post_types(); |
| 259 |
|
| 260 |
$post_types_columns = []; |
| 261 |
|
| 262 |
foreach ( $post_types as $post_type => $post_type_title ) { |
| 263 |
|
| 264 |
$column_data = [ |
| 265 |
'name' => $post_type, |
| 266 |
'title' => $post_type_title, |
| 267 |
'columns' => adminify__get_post_type_all_columns( $post_type ), |
| 268 |
'display_columns' => adminify_prepare_post_type_column_meta( $post_type ), |
| 269 |
'fields' => adminify_get_post_type_fields( $post_type ), |
| 270 |
]; |
| 271 |
|
| 272 |
if ( ! in_array($post_type, ['post', 'page']) ) { |
| 273 |
$column_data['is_pro'] = true; |
| 274 |
} |
| 275 |
|
| 276 |
$post_types_columns[] = $column_data; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
return $post_types_columns; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
/* |
| 285 |
* Get visible columns of all taxonomies |
| 286 |
*/ |
| 287 |
function adminify__get_taxonomies_columns() { |
| 288 |
|
| 289 |
$taxonomies = adminify__get_taxonomies(); |
| 290 |
|
| 291 |
$taxonomies_columns = []; |
| 292 |
|
| 293 |
foreach ( $taxonomies as $taxonomy => $taxonomy_title ) { |
| 294 |
|
| 295 |
$column_data = [ |
| 296 |
'name' => $taxonomy, |
| 297 |
'title' => $taxonomy_title, |
| 298 |
'columns' => adminify__get_taxonomy_all_columns( 'edit-' . $taxonomy ), |
| 299 |
'display_columns' => adminify_prepare_taxonomy_column_meta( $taxonomy ), |
| 300 |
'fields' => adminify_get_taxonomy_fields( $taxonomy ) |
| 301 |
]; |
| 302 |
|
| 303 |
if ( ! in_array($taxonomy, ['category', 'post_tag']) ) { |
| 304 |
$column_data['is_pro'] = true; |
| 305 |
} |
| 306 |
|
| 307 |
$taxonomies_columns[] = $column_data; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
return $taxonomies_columns; |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
/* |
| 316 |
* Get specific column meta data |
| 317 |
*/ |
| 318 |
function adminify_get_column_meta( $post_type, $column, $column_label ) { |
| 319 |
|
| 320 |
$column_default_meta = [ |
| 321 |
'label' => $column_label, |
| 322 |
'width' => [ |
| 323 |
'value' => 'auto', |
| 324 |
'unit' => '%' |
| 325 |
], |
| 326 |
'fields' => ['type', 'label', 'width'] |
| 327 |
]; |
| 328 |
|
| 329 |
/* |
| 330 |
* You can extend the fields based on post type and column |
| 331 |
* Make sure you have added the new field type in this function: adminify_get_post_type_fields |
| 332 |
*/ |
| 333 |
|
| 334 |
// if ( $post_type == 'page' && $column == 'taxonomy-folder' ) { |
| 335 |
// $column_default_meta['fields'][] = 'new'; |
| 336 |
// } |
| 337 |
|
| 338 |
$data = (array) get_option( '_adminify_admin_columns_meta_data', [] ); |
| 339 |
|
| 340 |
$column_meta = []; |
| 341 |
|
| 342 |
if ( !empty($data) && !empty($data[$post_type]) && !empty($data[$post_type][$column]) ) { |
| 343 |
$column_meta = $data[$post_type][$column]; |
| 344 |
} |
| 345 |
|
| 346 |
return array_merge_recursive( $column_default_meta, $column_meta ); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* Get post_type columns meta |
| 352 |
*/ |
| 353 |
function adminify_prepare_post_type_column_meta( $post_type ) { |
| 354 |
|
| 355 |
$columns_meta = get_option( '_adminify_admin_columns_meta_' . $post_type, null ); |
| 356 |
|
| 357 |
if ( ! is_null($columns_meta) ) return (array) $columns_meta; |
| 358 |
|
| 359 |
$columns = adminify__get_post_type_all_columns( $post_type ); |
| 360 |
|
| 361 |
$_columns = []; |
| 362 |
|
| 363 |
foreach ( $columns as $column => $column_label ) { |
| 364 |
$_columns[] = [ 'name' => $column ] + (array) adminify_get_column_meta( $post_type, $column, $column_label ); |
| 365 |
} |
| 366 |
|
| 367 |
return $_columns; |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
/* |
| 372 |
* Get taxonomy columns meta |
| 373 |
*/ |
| 374 |
function adminify_prepare_taxonomy_column_meta( $taxonomy ) { |
| 375 |
|
| 376 |
$columns_meta = get_option( '_adminify_admin_taxonomy_columns_meta_' . $taxonomy, null ); |
| 377 |
|
| 378 |
if ( ! is_null($columns_meta) ) return (array) $columns_meta; |
| 379 |
|
| 380 |
$columns = adminify__get_taxonomy_all_columns( 'edit-' . $taxonomy ); |
| 381 |
|
| 382 |
$_columns = []; |
| 383 |
|
| 384 |
foreach ( $columns as $column => $column_label ) { |
| 385 |
$_columns[] = [ 'name' => $column ] + (array) adminify_get_column_meta( $taxonomy, $column, $column_label ); |
| 386 |
} |
| 387 |
|
| 388 |
return $_columns; |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
/* |
| 393 |
* Get post_type fields |
| 394 |
*/ |
| 395 |
function adminify_get_post_type_fields( $post_type ) { |
| 396 |
|
| 397 |
$columns = array_map( 'sanitize_text_field', adminify__get_post_type_all_columns($post_type) ); |
| 398 |
|
| 399 |
return [ |
| 400 |
[ |
| 401 |
'id' => 'type', |
| 402 |
'type' => 'select', |
| 403 |
'title' => 'Column Type', |
| 404 |
'chosen' => true, |
| 405 |
'placeholder' => 'Select Column Type', |
| 406 |
'options' => $columns, |
| 407 |
], |
| 408 |
[ |
| 409 |
'id' => 'label', |
| 410 |
'type' => 'text', |
| 411 |
'title' => 'Label', |
| 412 |
], |
| 413 |
[ |
| 414 |
'id' => 'width', |
| 415 |
'type' => 'slider', |
| 416 |
'title' => 'Width', |
| 417 |
'unit' => '%' |
| 418 |
], |
| 419 |
|
| 420 |
/* |
| 421 |
* Register additional fields |
| 422 |
*/ |
| 423 |
// [ |
| 424 |
// 'id' => 'new', |
| 425 |
// 'type' => 'text', |
| 426 |
// 'title' => 'New' |
| 427 |
// ] |
| 428 |
|
| 429 |
]; |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
/* |
| 434 |
* Get taxonomy fields |
| 435 |
*/ |
| 436 |
function adminify_get_taxonomy_fields( $taxonomy ) { |
| 437 |
|
| 438 |
$columns = array_map( 'sanitize_text_field', adminify__get_taxonomy_all_columns( 'edit-' . $taxonomy ) ); |
| 439 |
|
| 440 |
return [ |
| 441 |
[ |
| 442 |
'id' => 'type', |
| 443 |
'type' => 'select', |
| 444 |
'title' => 'Column Type', |
| 445 |
'chosen' => true, |
| 446 |
'placeholder' => 'Select Column Type', |
| 447 |
'options' => $columns, |
| 448 |
], |
| 449 |
[ |
| 450 |
'id' => 'label', |
| 451 |
'type' => 'text', |
| 452 |
'title' => 'Label', |
| 453 |
], |
| 454 |
[ |
| 455 |
'id' => 'width', |
| 456 |
'type' => 'slider', |
| 457 |
'title' => 'Width', |
| 458 |
'unit' => '%' |
| 459 |
], |
| 460 |
|
| 461 |
/* |
| 462 |
* Register additional fields |
| 463 |
*/ |
| 464 |
// [ |
| 465 |
// 'id' => 'new', |
| 466 |
// 'type' => 'text', |
| 467 |
// 'title' => 'New' |
| 468 |
// ] |
| 469 |
|
| 470 |
]; |
| 471 |
|
| 472 |
} |