Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/wp-admin/includes/class-wp-posts-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] )
Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/tests/admin/wpPostsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<a href="edit.php?post_type=page&#038;author=' . $user_id . '">Mine <span class="count">(1)</span></a>',
$actual['mine']
);
}
}
Loading