Make WordPress Core

Ticket #58824: 58824.diff

File 58824.diff, 3.2 KB (added by bruheshwarinikhil, 3 months ago)

Adds post_list_table_classes filter to WP_Posts_List_Table::get_table_classes() with unit tests.

  • src/wp-admin/includes/class-wp-posts-list-table.php

    diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php
    index c7d10fca21..1c955e71db 100644
    a b class WP_Posts_List_Table extends WP_List_Table { 
    640640
    641641                $mode_class = esc_attr( 'table-view-' . $mode );
    642642
    643                 return array(
    644                         'widefat',
    645                         'fixed',
    646                         'striped',
    647                         $mode_class,
    648                         is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts',
     643                /**
     644                 * Filters the CSS classes applied to the posts list table.
     645                 *
     646                 * @since 6.8.0
     647                 *
     648                 * @param string[] $classes   An array of CSS classes for the posts list table.
     649                 * @param string   $post_type The post type slug.
     650                 */
     651                return apply_filters(
     652                        'post_list_table_classes',
     653                        array(
     654                                'widefat',
     655                                'fixed',
     656                                'striped',
     657                                $mode_class,
     658                                is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts',
     659                        ),
     660                        $this->screen->post_type
    649661                );
    650662        }
    651663
  • tests/phpunit/tests/admin/wpPostsListTable.php

    diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php
    index 9d2482a034..c73370b561 100644
    a b class Tests_Admin_wpPostsListTable extends WP_UnitTestCase { 
    309309                $this->assertStringNotContainsString( 'id="delete_all"', $output );
    310310        }
    311311
     312        /**
     313         * @ticket 58824
     314         *
     315         * @covers WP_Posts_List_Table::get_table_classes
     316         */
     317        public function test_get_table_classes_returns_default_classes() {
     318                $method = new ReflectionMethod( $this->table, 'get_table_classes' );
     319                $method->setAccessible( true );
     320
     321                $classes = $method->invoke( $this->table );
     322
     323                $this->assertContains( 'widefat', $classes );
     324                $this->assertContains( 'fixed', $classes );
     325                $this->assertContains( 'striped', $classes );
     326                $this->assertContains( 'pages', $classes );
     327        }
     328
     329        /**
     330         * @ticket 58824
     331         *
     332         * @covers WP_Posts_List_Table::get_table_classes
     333         */
     334        public function test_get_table_classes_filter_modifies_classes() {
     335                add_filter(
     336                        'post_list_table_classes',
     337                        static function ( $classes ) {
     338                                $classes[] = 'my-custom-class';
     339                                return $classes;
     340                        }
     341                );
     342
     343                $method = new ReflectionMethod( $this->table, 'get_table_classes' );
     344                $method->setAccessible( true );
     345
     346                $classes = $method->invoke( $this->table );
     347
     348                remove_all_filters( 'post_list_table_classes' );
     349
     350                $this->assertContains( 'my-custom-class', $classes );
     351        }
     352
     353        /**
     354         * @ticket 58824
     355         *
     356         * @covers WP_Posts_List_Table::get_table_classes
     357         */
     358        public function test_get_table_classes_filter_receives_post_type() {
     359                $received_post_type = null;
     360
     361                add_filter(
     362                        'post_list_table_classes',
     363                        static function ( $classes, $post_type ) use ( &$received_post_type ) {
     364                                $received_post_type = $post_type;
     365                                return $classes;
     366                        },
     367                        10,
     368                        2
     369                );
     370
     371                $method = new ReflectionMethod( $this->table, 'get_table_classes' );
     372                $method->setAccessible( true );
     373                $method->invoke( $this->table );
     374
     375                remove_all_filters( 'post_list_table_classes' );
     376
     377                $this->assertSame( 'page', $received_post_type );
     378        }
     379
    312380        /**
    313381         * @ticket 42066
    314382         *