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
39 changes: 39 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,45 @@ public function __construct( $args = array() ) {
)
);

$user_id = get_current_user_id();

/**
* 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,
$user_id
);

/**
* Filters the number of posts authored by the current user for a specific post type.
*
* The dynamic portion of the hook name, `$post_type`, refers to the post type.
*
* Possible hook names include:
*
* - `user_posts_post_count`
* - `user_posts_page_count`
*
* @since 7.1.0
*
* @param int $user_posts_count Number of posts authored by the current user.
* @param int $user_id The current user ID.
*/
$this->user_posts_count = (int) apply_filters(
"user_posts_{$post_type}_count",
$this->user_posts_count,
$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
44 changes: 44 additions & 0 deletions tests/phpunit/tests/admin/wpPostsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,48 @@ public function test_checkbox_label_includes_no_title_excerpt() {

$this->assertStringContainsString( 'Select (no title) Hello world example excerpt.', $output );
}

/**
* @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;
};
$post_type_filter = function ( $count, $current_user_id ) use ( $user_id ) {
$this->assertSame( 1, $count );
$this->assertSame( $user_id, $current_user_id );

return 2;
};

add_filter( 'user_posts_count', $filter, 10, 3 );
add_filter( 'user_posts_page_count', $post_type_filter, 10, 2 );
$table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) );
remove_filter( 'user_posts_count', $filter );
remove_filter( 'user_posts_page_count', $post_type_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">(2)</span></a>',
$actual['mine']
);
}
}
Loading