diff --git a/src/wp-admin/includes/screen.php b/src/wp-admin/includes/screen.php index 42a19ec91e21..1770ca9963ec 100644 --- a/src/wp-admin/includes/screen.php +++ b/src/wp-admin/includes/screen.php @@ -10,8 +10,8 @@ * Get the column headers for a screen * * @since 2.7.0 - * - * @staticvar array $column_headers + * @since 5.5.0 Removed a static var that made it difficult to instantiate list + * tables after the page header had been rendered. * * @param string|WP_Screen $screen The screen you want the headers for * @return string[] The column header labels keyed by column ID. @@ -21,25 +21,19 @@ function get_column_headers( $screen ) { $screen = convert_to_screen( $screen ); } - static $column_headers = array(); - - if ( ! isset( $column_headers[ $screen->id ] ) ) { - /** - * Filters the column headers for a list table on a specific screen. - * - * The dynamic portion of the hook name, `$screen->id`, refers to the - * ID of a specific screen. For example, the screen ID for the Posts - * list table is edit-post, so the filter for that screen would be - * manage_edit-post_columns. - * - * @since 3.0.0 - * - * @param string[] $columns The column header labels keyed by column ID. - */ - $column_headers[ $screen->id ] = apply_filters( "manage_{$screen->id}_columns", array() ); - } - - return $column_headers[ $screen->id ]; + /** + * Filters the column headers for a list table on a specific screen. + * + * The dynamic portion of the hook name, `$screen->id`, refers to the + * ID of a specific screen. For example, the screen ID for the Posts + * list table is edit-post, so the filter for that screen would be + * manage_edit-post_columns. + * + * @since 3.0.0 + * + * @param string[] $columns The column header labels keyed by column ID. + */ + return apply_filters( "manage_{$screen->id}_columns", array() ); } /** diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 9edbea179335..bc22e1b7f07a 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -293,4 +293,26 @@ public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comm $this->assertNotContains( 'id="delete_all"', $output ); } + + /** + * Test that can a list can display with no dependency on core screen context. + * + * @ticket 15386 + */ + public function test_table_list_renders() { + $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'my-plugin-page' ) ); + + // At least one comment needed. + $this->factory->comment->create_many( 10 ); + + ob_start(); + + $table->views(); + $table->prepare_items(); + $table->display(); + $output = ob_get_clean(); + + // CSS class only exists if the table has at least one comment. + $this->assertContains( 'class="submitted-on"', $output ); + } }