Skip to content

Commit

Permalink
Fix missing taxonomy column feature parity with post
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Mar 9, 2022
1 parent d6d83bb commit 49ab937
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/TaxonomyAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use WP_Post;
use WP_Taxonomy;
use WP_Term;
use DateTime;
use Exception;

class TaxonomyAdmin {

Expand Down Expand Up @@ -224,37 +227,44 @@ public function cols( array $cols ): array {
* @param string $string Blank string.
* @param string $col Name of the column.
* @param int $term_id Term ID.
* @return string Blank string.
*/
public function col( string $string, string $col, int $term_id ): string {
public function col( string $string, string $col, int $term_id ): void {
# Shorthand:
$c = $this->args['admin_cols'];

# We're only interested in our custom columns:
$custom_cols = array_filter( array_keys( $c ) );

if ( ! in_array( $col, $custom_cols, true ) ) {
return $string;
return;
}

if ( isset( $c[ $col ]['term_cap'] ) && ! current_user_can( $c[ $col ]['term_cap'], get_the_ID() ) ) {
return;
}

$term = get_term( $term_id );

if ( ! $term ) {
return;
}

if ( isset( $c[ $col ]['function'] ) ) {
call_user_func( $c[ $col ]['function'], $term_id );
call_user_func( $c[ $col ]['function'], $term );
} elseif ( isset( $c[ $col ]['meta_key'] ) ) {
$this->col_term_meta( $c[ $col ]['meta_key'], $c[ $col ], $term_id );
$this->col_term_meta( $term, $c[ $col ]['meta_key'], $c[ $col ] );
}

return $string;
}

/**
* Output column data for a term meta field.
*
* @param WP_Term $term The term object.
* @param string $meta_key The term meta key.
* @param array<string,mixed> $args Array of arguments for this field.
* @param int $term_id Term ID.
*/
public function col_term_meta( string $meta_key, array $args, int $term_id ): void {
$vals = get_term_meta( $term_id, $meta_key, false );
public function col_term_meta( WP_Term $term, string $meta_key, array $args ): void {
$vals = get_term_meta( $term->term_id, $meta_key, false );
$echo = [];

sort( $vals );
Expand All @@ -265,14 +275,25 @@ public function col_term_meta( string $meta_key, array $args, int $term_id ): vo
}

foreach ( $vals as $val ) {
try {
$val_time = ( new DateTime( '@' . $val ) )->format( 'U' );
} catch ( Exception $e ) {
$val_time = strtotime( $val );
}

if ( false !== $val_time ) {
$val = $val_time;
}

if ( is_numeric( $val ) ) {
$echo[] = date( $args['date_format'], (int) $val );
$echo[] = date_i18n( $args['date_format'], (int) $val );
} elseif ( ! empty( $val ) ) {
$echo[] = mysql2date( $args['date_format'], $val );
}
}
} else {
foreach ( $vals as $val ) {

if ( ! empty( $val ) || ( '0' === $val ) ) {
$echo[] = $val;
}
Expand Down

0 comments on commit 49ab937

Please sign in to comment.