self::SINGULAR,
'plural' => self::PLURAL,
'ajax' => false,
)
);
$this->queue = $queue;
$this->tab = ( Queue::TAB_PEOPLE === $tab ) ? Queue::TAB_PEOPLE : Queue::TAB_AUTOMATION;
$this->per_page = max( 1, (int) $per_page );
}
/**
* Current tab from the request (default: automation).
*
* @return string
*/
public static function current_tab(): string {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation state.
$tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : '';
return ( Queue::TAB_PEOPLE === $tab ) ? Queue::TAB_PEOPLE : Queue::TAB_AUTOMATION;
}
/**
* Tab accessor.
*
* @return string
*/
public function tab(): string {
return $this->tab;
}
/* ------------------------------------------------------------------ */
/* Table definition */
/* ------------------------------------------------------------------ */
/**
* Columns.
*
* @return array
*/
public function get_columns() {
return array(
'cb' => '',
'time' => __( 'Time', 'agent-lane' ),
'form' => __( 'Form', 'agent-lane' ),
'class' => __( 'Class', 'agent-lane' ),
'score' => __( 'Score', 'agent-lane' ),
'reasons' => __( 'Reasons', 'agent-lane' ),
'fields' => __( 'Fields', 'agent-lane' ),
'ip' => __( 'IP', 'agent-lane' ),
'actions' => __( 'Actions', 'agent-lane' ),
);
}
/**
* Sortable columns.
*
* @return array
*/
public function get_sortable_columns() {
return array(
'time' => array( 'created_at', true ),
'score' => array( 'score', false ),
'class' => array( 'class', false ),
'form' => array( 'form_source', false ),
);
}
/**
* Bulk actions per tab.
*
* @return array
*/
public function get_bulk_actions() {
$actions = array();
if ( Queue::TAB_AUTOMATION === $this->tab ) {
$actions['release'] = __( 'Release', 'agent-lane' );
}
$actions['mark_bot'] = __( 'Mark as bot', 'agent-lane' );
$actions['delete'] = __( 'Delete', 'agent-lane' );
return $actions;
}
/**
* Extra CSS classes on the table element.
*
* @return array
*/
protected function get_table_classes() {
return array( 'widefat', 'fixed', 'striped', 'agentlane-table', 'agentlane-tab-' . $this->tab );
}
/**
* Loads one page of items.
*
* @return void
*/
public function prepare_items() {
$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns(), 'time' );
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only listing state.
$orderby = isset( $_REQUEST['orderby'] ) ? sanitize_key( wp_unslash( $_REQUEST['orderby'] ) ) : 'created_at';
$order = isset( $_REQUEST['order'] ) ? strtoupper( sanitize_key( wp_unslash( $_REQUEST['order'] ) ) ) : 'DESC';
$search = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
// phpcs:enable
if ( 'time' === $orderby ) {
$orderby = 'created_at';
}
$result = $this->queue->list(
array(
'tab' => $this->tab,
'paged' => $this->get_pagenum(),
'per_page' => $this->per_page,
'orderby' => $orderby,
'order' => ( 'ASC' === $order ) ? 'ASC' : 'DESC',
'search' => $search,
)
);
$this->items = $result['items'];
$this->set_pagination_args(
array(
'total_items' => (int) $result['total'],
'per_page' => $this->per_page,
'total_pages' => (int) ceil( $result['total'] / $this->per_page ),
)
);
}
/**
* Empty-state text per tab.
*
* @return void
*/
public function no_items() {
if ( Queue::TAB_PEOPLE === $this->tab ) {
esc_html_e( 'No submissions from people yet. Delivered submissions show up here as a light copy (no field contents).', 'agent-lane' );
} else {
esc_html_e( 'Nothing is held. Bots, AI agents and scripted submissions will appear here.', 'agent-lane' );
}
}
/**
* Adds a per-class CSS class to each row.
*
* @param array $item Row.
* @return void
*/
public function single_row( $item ) {
printf( '
', esc_attr( (string) $item['class'] ) );
$this->single_row_columns( $item );
echo '
';
}
/* ------------------------------------------------------------------ */
/* Columns */
/* ------------------------------------------------------------------ */
/**
* Checkbox column.
*
* @param array $item Row.
* @return string
*/
public function column_cb( $item ) {
$id = (int) $item['id'];
return sprintf(
'',
$id,
/* translators: %d: row id */
esc_html( sprintf( __( 'Select submission %d', 'agent-lane' ), $id ) )
);
}
/**
* Time column: relative time with the exact local time on hover, plus status.
*
* @param array $item Row.
* @return string
*/
public function column_time( $item ) {
$created = (string) $item['created_at'];
$ts = strtotime( $created . ' +0000' );
$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
$local = get_date_from_gmt( $created, $format );
if ( false === $ts ) {
$html = esc_html( $local );
} else {
/* translators: %s: human-readable time difference */
$ago = sprintf( __( '%s ago', 'agent-lane' ), human_time_diff( $ts, time() ) );
$html = sprintf( '%2$s', esc_attr( $local ), esc_html( $ago ) );
}
if ( Queue::STATUS_RELEASED === $item['status'] || 'block' === $item['action'] ) {
$label = ( Queue::STATUS_RELEASED === $item['status'] )
? Queue::status_label( Queue::STATUS_RELEASED )
: __( 'Blocked', 'agent-lane' );
$html .= '
' . esc_html( $label ) . '';
}
return $html;
}
/**
* Form column: plugin label + form id.
*
* @param array $item Row.
* @return string
*/
public function column_form( $item ) {
$html = esc_html( Queue::form_label( (string) $item['form_source'] ) );
if ( '' !== (string) $item['form_id'] ) {
$html .= '
#' . esc_html( (string) $item['form_id'] ) . '';
}
return $html;
}
/**
* Class column: badge + agent name.
*
* @param array $item Row.
* @return string
*/
public function column_class( $item ) {
$class = (string) $item['class'];
$html = sprintf(
'%2$s',
esc_attr( $class ),
esc_html( Queue::class_label( $class ) )
);
if ( '' !== (string) $item['agent_name'] ) {
$html .= '
' . esc_html( (string) $item['agent_name'] ) . '';
}
return $html;
}
/**
* Score column: number with a small bar.
*
* @param array $item Row.
* @return string
*/
public function column_score( $item ) {
$score = max( 0, min( 100, (int) $item['score'] ) );
$level = 'low';
if ( $score >= 90 ) {
$level = 'high';
} elseif ( $score >= 55 ) {
$level = 'mid';
}
return sprintf(
'%2$d',
esc_attr( $level ),
$score
);
}
/**
* Reasons column: top 3 by weight.
*
* @param array $item Row.
* @return string
*/
public function column_reasons( $item ) {
$reasons = is_array( $item['reasons'] ) ? $item['reasons'] : array();
if ( empty( $reasons ) ) {
return '' . esc_html__( 'No signals', 'agent-lane' ) . '';
}
usort(
$reasons,
static function ( $a, $b ) {
return (int) ( $b['weight'] ?? 0 ) <=> (int) ( $a['weight'] ?? 0 );
}
);
$top = array_slice( $reasons, 0, 3 );
$html = '';
foreach ( $top as $reason ) {
$code = (string) ( $reason['code'] ?? '' );
$weight = (int) ( $reason['weight'] ?? 0 );
$detail = (string) ( $reason['detail'] ?? '' );
if ( '' === $detail ) {
$detail = $code;
}
if ( Queue::REASON_TEXT_REPLACED === $code ) {
$detail = __( 'Characters the database could not store were replaced.', 'agent-lane' );
}
$html .= sprintf(
'- %2$s %3$s
',
esc_attr( $code ),
esc_html( ( $weight >= 0 ? '+' : '' ) . $weight ),
esc_html( $detail )
);
}
$rest = count( $reasons ) - count( $top );
if ( in_array( Queue::REASON_TEXT_REPLACED, array_column( $reasons, 'code' ), true ) && ! in_array( Queue::REASON_TEXT_REPLACED, array_column( $top, 'code' ), true ) ) {
// The second-try marker always shows: the stored text differs from what was sent.
$html .= '- ' . esc_html__( 'Characters the database could not store were replaced.', 'agent-lane' ) . '
';
--$rest;
}
if ( $rest > 0 ) {
/* translators: %d: number of additional reasons */
$html .= '- ' . esc_html( sprintf( __( '+%d more', 'agent-lane' ), $rest ) ) . '
';
}
$html .= '
';
return $html;
}
/**
* Fields column: first two fields truncated, full list in a disclosure.
*
* @param array $item Row.
* @return string
*/
public function column_fields( $item ) {
$fields = $item['fields'];
if ( null === $fields ) {
return '' . esc_html__( 'Not stored for delivered submissions', 'agent-lane' ) . '';
}
if ( ! is_array( $fields ) || empty( $fields ) ) {
return '' . esc_html__( 'Empty', 'agent-lane' ) . '';
}
$html = '';
$shown = 0;
foreach ( $fields as $name => $value ) {
if ( $shown >= 2 ) {
break;
}
$html .= sprintf(
'
%1$s %2$s
',
esc_html( (string) $name ),
esc_html( self::truncate( (string) $value ) )
);
++$shown;
}
$html .= '
';
$html .= '';
/* translators: %d: number of fields */
$html .= esc_html( sprintf( _n( 'Show %d field', 'Show all %d fields', count( $fields ), 'agent-lane' ), count( $fields ) ) );
$html .= '
';
foreach ( $fields as $name => $value ) {
$html .= '- ' . esc_html( (string) $name ) . '
- ' . esc_html( (string) $value ) . '
';
}
$html .= '
';
return $html;
}
/**
* IP column with the user agent on hover.
*
* @param array $item Row.
* @return string
*/
public function column_ip( $item ) {
$ip = (string) $item['ip'];
$ua = (string) $item['ua'];
if ( '' === $ip ) {
$ip = '?';
}
if ( '' === $ua ) {
return '' . esc_html( $ip ) . '';
}
return sprintf( '%2$s', esc_attr( $ua ), esc_html( $ip ) );
}
/**
* Actions column: Release (held only), Mark as bot, Delete. Each link is nonce-protected.
*
* @param array $item Row.
* @return string
*/
public function column_actions( $item ) {
$id = (int) $item['id'];
$links = array();
if ( Queue::STATUS_HELD === $item['status'] ) {
$links[] = sprintf(
'%2$s',
esc_url( $this->action_url( 'release', $id ) ),
esc_html__( 'Release', 'agent-lane' )
);
}
$links[] = sprintf(
'%2$s',
esc_url( $this->action_url( 'mark_bot', $id ) ),
esc_html__( 'Mark as bot', 'agent-lane' )
);
$links[] = sprintf(
'%2$s',
esc_url( $this->action_url( 'delete', $id ) ),
esc_html__( 'Delete', 'agent-lane' )
);
return '' . implode( ' ', $links ) . '
';
}
/**
* Fallback for unknown columns.
*
* @param array $item Row.
* @param string $column_name Column id.
* @return string
*/
public function column_default( $item, $column_name ) {
return isset( $item[ $column_name ] ) && is_scalar( $item[ $column_name ] ) ? esc_html( (string) $item[ $column_name ] ) : '';
}
/* ------------------------------------------------------------------ */
/* Helpers */
/* ------------------------------------------------------------------ */
/**
* Nonce-protected URL for a row action.
*
* @param string $action release|delete|mark_bot.
* @param int $id Row id.
* @return string
*/
private function action_url( $action, $id ) {
$url = add_query_arg(
array(
'action' => $action,
'id' => (int) $id,
),
Admin::queue_url( $this->tab )
);
return wp_nonce_url( $url, 'agentlane_row_' . (int) $id );
}
/**
* Single-line truncation for previews.
*
* @param string $value Text.
* @return string
*/
private static function truncate( $value ) {
$value = trim( preg_replace( '/\s+/u', ' ', $value ) );
if ( mb_strlen( $value ) > self::PREVIEW_LENGTH ) {
$value = rtrim( mb_substr( $value, 0, self::PREVIEW_LENGTH - 3 ) ) . '...';
}
return $value;
}
}