This commit is contained in:
Javier Casares 2026-06-03 06:25:27 +00:00
commit 650e69349f
21 changed files with 2919 additions and 268 deletions

View file

@ -47,13 +47,14 @@ class MediaListTable extends \WP_List_Table {
*/
public function get_columns() {
return array(
'cb' => '<input type="checkbox" />',
'thumbnail' => __( 'Thumbnail', 'robotstxt-mediaaudit' ),
'filename' => __( 'Filename', 'robotstxt-mediaaudit' ),
'attachment_id' => __( 'ID', 'robotstxt-mediaaudit' ),
'usage_count' => __( 'Usage Count', 'robotstxt-mediaaudit' ),
'usages' => __( 'Usages', 'robotstxt-mediaaudit' ),
'external_status' => __( 'External Status', 'robotstxt-mediaaudit' ),
'cb' => '<input type="checkbox" />',
'thumbnail' => __( 'Thumbnail', 'robotstxt-mediaaudit' ),
'filename' => __( 'Filename', 'robotstxt-mediaaudit' ),
'attachment_id' => __( 'ID', 'robotstxt-mediaaudit' ),
'usage_count' => __( 'Usage Count', 'robotstxt-mediaaudit' ),
'usages' => __( 'Usages', 'robotstxt-mediaaudit' ),
'external_status' => __( 'External Status', 'robotstxt-mediaaudit' ),
'external_scanned_at' => __( 'Last Scanned', 'robotstxt-mediaaudit' ),
);
}
@ -64,8 +65,9 @@ class MediaListTable extends \WP_List_Table {
*/
protected function get_sortable_columns() {
return array(
'attachment_id' => array( 'attachment_id', true ),
'usage_count' => array( 'usage_count', false ),
'attachment_id' => array( 'attachment_id', true ),
'usage_count' => array( 'usage_count', false ),
'external_scanned_at' => array( 'external_scanned_at', false ),
);
}
@ -79,6 +81,7 @@ class MediaListTable extends \WP_List_Table {
'run_external_scan' => __( 'Run External Scan', 'robotstxt-mediaaudit' ),
'purge_external_data' => __( 'Purge External Data', 'robotstxt-mediaaudit' ),
'export_csv' => __( 'Export CSV', 'robotstxt-mediaaudit' ),
'export_external_csv' => __( 'Export External Results CSV', 'robotstxt-mediaaudit' ),
);
}
@ -309,6 +312,47 @@ class MediaListTable extends \WP_List_Table {
return $out;
}
/**
* Renders the external scan date column.
*
* Shows a human-readable time difference (e.g. "3 days ago") with the full
* date/time as a tooltip. Displays "" when the attachment has never been scanned.
*
* @param array<string, mixed> $item Row data.
*
* @return string
*/
protected function column_external_scanned_at( $item ) {
$raw = $item['external_scanned_at'];
if ( ! is_string( $raw ) || '' === $raw ) {
return '&mdash;';
}
$ts = strtotime( $raw );
if ( false === $ts ) {
return '&mdash;';
}
$date_fmt = get_option( 'date_format' );
$time_fmt = get_option( 'time_format' );
$format = ( is_string( $date_fmt ) ? $date_fmt : 'Y-m-d' )
. ' '
. ( is_string( $time_fmt ) ? $time_fmt : 'H:i' );
$full_val = wp_date( $format, $ts );
$full = is_string( $full_val ) ? $full_val : $raw;
return sprintf(
'<abbr title="%s">%s</abbr>',
esc_attr( $full ),
sprintf(
/* translators: %s: human-readable time difference, e.g. "3 days" */
esc_html__( '%s ago', 'robotstxt-mediaaudit' ),
esc_html( human_time_diff( $ts ) )
)
);
}
/**
* Renders filter controls above the table.
*
@ -391,7 +435,7 @@ class MediaListTable extends \WP_List_Table {
$offset = ( $paged - 1 ) * $per_page;
// Sorting.
$allowed_orderby = array( 'attachment_id', 'usage_count' );
$allowed_orderby = array( 'attachment_id', 'usage_count', 'external_scanned_at' );
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$orderby_raw = isset( $_REQUEST['orderby'] ) && is_string( $_REQUEST['orderby'] ) ? sanitize_key( wp_unslash( $_REQUEST['orderby'] ) ) : '';
$orderby = in_array( $orderby_raw, $allowed_orderby, true ) ? $orderby_raw : 'attachment_id';
@ -438,6 +482,9 @@ class MediaListTable extends \WP_List_Table {
if ( 'usage_count' === $orderby ) {
$order_sql = "(SELECT COUNT(*) FROM {$wpdb->prefix}mra_media_usage mu WHERE mu.attachment_id = i.attachment_id) {$order}";
} elseif ( 'external_scanned_at' === $orderby ) {
// NULLs always last, regardless of sort direction.
$order_sql = "i.external_scanned_at IS NULL ASC, i.external_scanned_at {$order}";
} else {
$order_sql = "i.attachment_id {$order}";
}