list-table.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Helper functions for displaying a list of items in an ajaxified HTML table.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Fetches an instance of a WP_List_Table class.
  11. *
  12. * @since 3.1.0
  13. *
  14. * @global string $hook_suffix
  15. *
  16. * @param string $class_name The type of the list table, which is the class name.
  17. * @param array $args Optional. Arguments to pass to the class. Accepts 'screen'.
  18. * @return WP_List_Table|false List table object on success, false if the class does not exist.
  19. */
  20. function _get_list_table( $class_name, $args = array() ) {
  21. $core_classes = array(
  22. // Site Admin.
  23. 'WP_Posts_List_Table' => 'posts',
  24. 'WP_Media_List_Table' => 'media',
  25. 'WP_Terms_List_Table' => 'terms',
  26. 'WP_Users_List_Table' => 'users',
  27. 'WP_Comments_List_Table' => 'comments',
  28. 'WP_Post_Comments_List_Table' => array( 'comments', 'post-comments' ),
  29. 'WP_Links_List_Table' => 'links',
  30. 'WP_Plugin_Install_List_Table' => 'plugin-install',
  31. 'WP_Themes_List_Table' => 'themes',
  32. 'WP_Theme_Install_List_Table' => array( 'themes', 'theme-install' ),
  33. 'WP_Plugins_List_Table' => 'plugins',
  34. 'WP_Application_Passwords_List_Table' => 'application-passwords',
  35. // Network Admin.
  36. 'WP_MS_Sites_List_Table' => 'ms-sites',
  37. 'WP_MS_Users_List_Table' => 'ms-users',
  38. 'WP_MS_Themes_List_Table' => 'ms-themes',
  39. // Privacy requests tables.
  40. 'WP_Privacy_Data_Export_Requests_List_Table' => 'privacy-data-export-requests',
  41. 'WP_Privacy_Data_Removal_Requests_List_Table' => 'privacy-data-removal-requests',
  42. );
  43. if ( isset( $core_classes[ $class_name ] ) ) {
  44. foreach ( (array) $core_classes[ $class_name ] as $required ) {
  45. require_once ABSPATH . 'wp-admin/includes/class-wp-' . $required . '-list-table.php';
  46. }
  47. if ( isset( $args['screen'] ) ) {
  48. $args['screen'] = convert_to_screen( $args['screen'] );
  49. } elseif ( isset( $GLOBALS['hook_suffix'] ) ) {
  50. $args['screen'] = get_current_screen();
  51. } else {
  52. $args['screen'] = null;
  53. }
  54. /**
  55. * Filters the list table class to instantiate.
  56. *
  57. * @since 6.1.0
  58. *
  59. * @param string $class_name The list table class to use.
  60. * @param array $args An array containing _get_list_table() arguments.
  61. */
  62. $custom_class_name = apply_filters( 'wp_list_table_class_name', $class_name, $args );
  63. if ( is_string( $custom_class_name ) && class_exists( $custom_class_name ) ) {
  64. $class_name = $custom_class_name;
  65. }
  66. return new $class_name( $args );
  67. }
  68. return false;
  69. }
  70. /**
  71. * Register column headers for a particular screen.
  72. *
  73. * @see get_column_headers(), print_column_headers(), get_hidden_columns()
  74. *
  75. * @since 2.7.0
  76. *
  77. * @param string $screen The handle for the screen to register column headers for. This is
  78. * usually the hook name returned by the `add_*_page()` functions.
  79. * @param string[] $columns An array of columns with column IDs as the keys and translated
  80. * column names as the values.
  81. */
  82. function register_column_headers( $screen, $columns ) {
  83. new _WP_List_Table_Compat( $screen, $columns );
  84. }
  85. /**
  86. * Prints column headers for a particular screen.
  87. *
  88. * @since 2.7.0
  89. *
  90. * @param string|WP_Screen $screen The screen hook name or screen object.
  91. * @param bool $with_id Whether to set the ID attribute or not.
  92. */
  93. function print_column_headers( $screen, $with_id = true ) {
  94. $wp_list_table = new _WP_List_Table_Compat( $screen );
  95. $wp_list_table->print_column_headers( $with_id );
  96. }