Skip to content

Commit 3b8e09f

Browse files
committed
Users Menu: add new column listing the linked guest author.
When a guest author has been linked to a user, display that in the Users menu. This makes it clearer that the post count column shows numbers for that guest author.
1 parent a9bbf98 commit 3b8e09f

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

co-authors-plus.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,24 @@ function _filter_manage_posts_custom_column( $column_name ) {
477477
}
478478

479479
/**
480-
* Unset the post count column because it's going to be inaccurate and provide our own
480+
* Create 2 custom columns in the Users table:
481+
* - A custom table listing linked guest authors.
482+
* - A custom post count columm to replace the existing one.
483+
*
484+
* @since 2.6.1
485+
* @since 3.4.0 Added column to display the linked guest author.
486+
*
487+
* @param array $columns An array of column name ⇒ label.
481488
*/
482489
function _filter_manage_users_columns( $columns ) {
483490

484491
$new_columns = array();
485-
// Unset and add our column while retaining the order of the columns
492+
// Unset the default post count columns and add our own while retaining the order of the columns.
486493
foreach ( $columns as $column_name => $column_title ) {
487-
if ( 'posts' == $column_name ) {
494+
if ( 'posts' === $column_name ) {
495+
// Add new guest author column.
496+
$new_columns['coauthors_linked_author'] = __( 'Linked Guest Author', 'co-authors-plus' );
497+
// Add our custom post count columm.
488498
$new_columns['coauthors_post_count'] = __( 'Posts', 'co-authors-plus' );
489499
} else {
490500
$new_columns[ $column_name ] = $column_title;
@@ -494,22 +504,54 @@ function _filter_manage_users_columns( $columns ) {
494504
}
495505

496506
/**
497-
* Provide an accurate count when looking up the number of published posts for a user
507+
* Add 2 custom columns to the Users table:
508+
* - 1st column replaces default posts count and displays an accurate count when looking up the number of published posts for a user.
509+
* - 2nd column displays the name of the linked guest author if one is set.
510+
*
511+
* @since 2.6.1
512+
* @since 3.4.0 Added column to display the linked guest author.
513+
*
514+
* @param string $value Custom column output. Default empty.
515+
* @param string $column_name Column name.
516+
* @param int $user_id ID of the currently-listed user.
498517
*/
499518
function _filter_manage_users_custom_column( $value, $column_name, $user_id ) {
500-
if ( 'coauthors_post_count' != $column_name ) {
501-
return $value;
502-
}
503-
// We filter count_user_posts() so it provides an accurate number
504-
$numposts = count_user_posts( $user_id );
505-
$user = get_user_by( 'id', $user_id );
506-
if ( $numposts > 0 ) {
507-
$value .= "<a href='edit.php?author_name=$user->user_nicename' title='" . esc_attr__( 'View posts by this author', 'co-authors-plus' ) . "' class='edit'>";
508-
$value .= $numposts;
509-
$value .= '</a>';
510-
} else {
511-
$value .= 0;
519+
/**
520+
* Display the number of posts per user, with custom link to filtered posts page.
521+
*/
522+
if ( 'coauthors_post_count' === $column_name ) {
523+
// We filter count_user_posts() so it provides an accurate number
524+
$numposts = count_user_posts( $user_id );
525+
$user = get_user_by( 'id', $user_id );
526+
if ( $numposts > 0 ) {
527+
$value .= sprintf(
528+
'<a href="edit.php?author_name=%1$s" title="%2$s" class="edit">%3$d</a>',
529+
esc_attr( $user->user_nicename ),
530+
esc_attr__( 'View posts by this author', 'co-authors-plus' ),
531+
absint( $numposts )
532+
);
533+
} else {
534+
$value .= 0;
535+
}
536+
} elseif ( 'coauthors_linked_author' === $column_name ) {
537+
/**
538+
* Display the name of the linked guest author if one is set.
539+
*/
540+
$author_info = $this->get_coauthor_by( 'id', $user_id, false );
541+
if (
542+
$author_info
543+
&& 'guest-author' === $author_info->type
544+
) {
545+
$value .= sprintf(
546+
'<a href="post.php?post=%1$d&action=edit" title="%2$s" class="edit">%3$s</a>',
547+
absint( $author_info->ID ),
548+
esc_attr__( 'Edit Guest Author', 'co-authors-plus' ),
549+
esc_html( $author_info->display_name )
550+
);
551+
}
512552
}
553+
554+
// Fallback.
513555
return $value;
514556
}
515557

0 commit comments

Comments
 (0)