Skip to content
Open
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
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 @@ -77,7 +80,7 @@
if ( $this->args['admin_cols'] ) {
add_filter( "manage_edit-{$this->taxo->taxonomy}_columns", [ $this, '_log_default_cols' ], 0 );
add_filter( "manage_edit-{$this->taxo->taxonomy}_columns", [ $this, 'cols' ] );
add_filter( "manage_{$this->taxo->taxonomy}_custom_column", [ $this, 'col' ], 10, 3 );

Check failure on line 83 in src/TaxonomyAdmin.php

View workflow job for this annotation

GitHub Actions / PHP / PHPStan on PHP 8.4

Filter callback return statement is missing.

Check failure on line 83 in src/TaxonomyAdmin.php

View workflow job for this annotation

GitHub Actions / PHP / PHPStan on PHP 7.4

Filter callback return statement is missing.
}

/**
Expand Down Expand Up @@ -224,37 +227,44 @@
* @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 ] );

Check failure on line 255 in src/TaxonomyAdmin.php

View workflow job for this annotation

GitHub Actions / PHP / PHPStan on PHP 8.4

Parameter #1 $term of method ExtCPTs\TaxonomyAdmin::col_term_meta() expects WP_Term, WP_Error|WP_Term given.

Check failure on line 255 in src/TaxonomyAdmin.php

View workflow job for this annotation

GitHub Actions / PHP / PHPStan on PHP 7.4

Parameter #1 $term of method ExtCPTs\TaxonomyAdmin::col_term_meta() expects WP_Term, WP_Error|WP_Term given.
}

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 @@
}

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
Loading