Skip to content

Commit aac250c

Browse files
committed
feat(admin): author and taxonomy columns on picker table
Matches core edit.php's column set: shows Author and every taxonomy registered on the source blog with show_admin_column=true (Categories and Tags for posts, plus any CPT taxonomies that opt in). Terms are fetched inside switch_to_blog so they reflect the source post's categorization, not an accidental lookup on the target blog. Taxonomy list is cached per request; get_columns and prepare_items share the same underlying WP_Taxonomy objects so header labels and rendered data can never drift.
1 parent 924e124 commit aac250c

1 file changed

Lines changed: 79 additions & 13 deletions

File tree

includes/MslsTranslationPickerTable.php

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class MslsTranslationPickerTable extends \WP_List_Table {
3232

3333
protected string $search;
3434

35+
/** @var array<string, \WP_Taxonomy>|null */
36+
protected ?array $taxonomies_cache = null;
37+
3538
public function __construct( int $source_blog_id, string $post_type, string $search = '' ) {
3639
parent::__construct(
3740
array(
@@ -48,13 +51,45 @@ public function __construct( int $source_blog_id, string $post_type, string $sea
4851
}
4952

5053
public function get_columns(): array {
51-
return array(
52-
'cb' => '<input type="checkbox" />',
53-
'title' => __( 'Title', 'multisite-language-switcher' ),
54-
'status' => __( 'Status', 'multisite-language-switcher' ),
55-
'date' => __( 'Date', 'multisite-language-switcher' ),
56-
'actions' => __( 'Actions', 'multisite-language-switcher' ),
54+
$cols = array(
55+
'cb' => '<input type="checkbox" />',
56+
'title' => __( 'Title', 'multisite-language-switcher' ),
57+
'author' => __( 'Author', 'multisite-language-switcher' ),
5758
);
59+
60+
foreach ( $this->get_admin_column_taxonomies() as $name => $tax ) {
61+
$cols[ 'taxonomy-' . $name ] = $tax->labels->name ?? (string) $tax->label;
62+
}
63+
64+
$cols['status'] = __( 'Status', 'multisite-language-switcher' );
65+
$cols['date'] = __( 'Date', 'multisite-language-switcher' );
66+
$cols['actions'] = __( 'Actions', 'multisite-language-switcher' );
67+
68+
return $cols;
69+
}
70+
71+
/**
72+
* Returns taxonomies registered for the post type that declared
73+
* show_admin_column => true — same signal core edit.php uses to decide
74+
* which taxonomy columns to render.
75+
*
76+
* @return array<string, \WP_Taxonomy>
77+
*/
78+
protected function get_admin_column_taxonomies(): array {
79+
if ( null !== $this->taxonomies_cache ) {
80+
return $this->taxonomies_cache;
81+
}
82+
83+
switch_to_blog( $this->source_blog_id );
84+
$this->taxonomies_cache = array();
85+
foreach ( get_object_taxonomies( $this->post_type, 'objects' ) as $tax ) {
86+
if ( ! empty( $tax->show_admin_column ) ) {
87+
$this->taxonomies_cache[ $tax->name ] = $tax;
88+
}
89+
}
90+
restore_current_blog();
91+
92+
return $this->taxonomies_cache;
5893
}
5994

6095
protected function get_bulk_actions(): array {
@@ -87,16 +122,25 @@ public function prepare_items(): void {
87122
$args['s'] = $this->search;
88123
}
89124

90-
$query = new \WP_Query( $args );
91-
$items = array();
125+
$query = new \WP_Query( $args );
126+
$items = array();
127+
$taxonomies = array_keys( $this->get_admin_column_taxonomies() );
92128

93129
foreach ( $query->posts as $post ) {
130+
$terms_by_tax = array();
131+
foreach ( $taxonomies as $tax_name ) {
132+
$terms = get_the_terms( $post->ID, $tax_name );
133+
$terms_by_tax[ $tax_name ] = is_array( $terms ) ? wp_list_pluck( $terms, 'name' ) : array();
134+
}
135+
94136
$items[] = array(
95-
'ID' => (int) $post->ID,
96-
'title' => get_the_title( $post ),
97-
'status' => $post->post_status,
98-
'date' => get_the_date( '', $post ),
99-
'permalink' => get_permalink( $post ),
137+
'ID' => (int) $post->ID,
138+
'title' => get_the_title( $post ),
139+
'status' => $post->post_status,
140+
'date' => get_the_date( '', $post ),
141+
'permalink' => get_permalink( $post ),
142+
'author' => (string) get_the_author_meta( 'display_name', (int) $post->post_author ),
143+
'taxonomies' => $terms_by_tax,
100144
);
101145
}
102146

@@ -159,6 +203,28 @@ protected function column_date( $item ): string {
159203
return esc_html( (string) $item['date'] );
160204
}
161205

206+
/**
207+
* @param array<string, mixed> $item
208+
*/
209+
protected function column_author( $item ): string {
210+
return '' !== (string) $item['author'] ? esc_html( (string) $item['author'] ) : '';
211+
}
212+
213+
/**
214+
* Fallback for the dynamic taxonomy-* columns.
215+
*
216+
* @param array<string, mixed> $item
217+
* @param string $column_name
218+
*/
219+
protected function column_default( $item, $column_name ): string {
220+
if ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
221+
$tax = substr( $column_name, strlen( 'taxonomy-' ) );
222+
$names = $item['taxonomies'][ $tax ] ?? array();
223+
return empty( $names ) ? '' : esc_html( implode( ', ', $names ) );
224+
}
225+
return '';
226+
}
227+
162228
/**
163229
* @param array<string, mixed> $item
164230
*/

0 commit comments

Comments
 (0)