From d0d090f448ed6e8c1dcc5ff527bca25fc2390d61 Mon Sep 17 00:00:00 2001 From: ArkaPrabhaChowdhury Date: Thu, 25 Jun 2026 13:07:33 +0530 Subject: [PATCH] Administration: Allow filtering the current user's post count --- .../includes/class-wp-posts-list-table.php | 16 +++++++++ .../phpunit/tests/admin/wpPostsListTable.php | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+) 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 18c76169eb81c..ea92e8555db23 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -101,6 +101,22 @@ public function __construct( $args = array() ) { ) ); + /** + * Filters the number of posts authored by the current user. + * + * @since 7.1.0 + * + * @param int $user_posts_count Number of posts authored by the current user. + * @param string $post_type The post type. + * @param int $user_id The current user ID. + */ + $this->user_posts_count = (int) apply_filters( + 'user_posts_count', + $this->user_posts_count, + $post_type, + get_current_user_id() + ); + if ( $this->user_posts_count && ! current_user_can( $post_type_object->cap->edit_others_posts ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['all_posts'] ) diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php index 9d2482a034af7..8988b2894791c 100644 --- a/tests/phpunit/tests/admin/wpPostsListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -330,4 +330,40 @@ public function test_get_views_should_return_views_by_default() { $this->assertSame( $expected, $actual ); } + + /** + * @ticket 47640 + * + * @covers WP_Posts_List_Table::__construct + * @covers WP_Posts_List_Table::get_views + */ + public function test_get_views_should_use_filtered_user_posts_count() { + global $avail_post_stati; + + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user_id ); + + $filter = function ( $count, $post_type, $current_user_id ) use ( $user_id ) { + $this->assertSame( 0, $count ); + $this->assertSame( 'page', $post_type ); + $this->assertSame( $user_id, $current_user_id ); + + return 1; + }; + + add_filter( 'user_posts_count', $filter, 10, 3 ); + $table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) ); + remove_filter( 'user_posts_count', $filter ); + + $avail_post_stati_backup = $avail_post_stati; + $avail_post_stati = get_available_post_statuses(); + + $actual = $table->get_views(); + $avail_post_stati = $avail_post_stati_backup; + + $this->assertSame( + 'Mine (1)', + $actual['mine'] + ); + } }