'event',
'plural' => 'events',
'ajax' => false
]);
}
/**
* Get columns
*/
public function get_columns() {
return [
'occurred_at' => __('Time', 'valuespace'),
'event_type' => __('Event Type', 'valuespace'),
'object' => __('Product or Page', 'valuespace'),
'visitor' => __('User / Session', 'valuespace'),
'audience' => __('Country / Language', 'valuespace'),
'delivery_status' => __('ValueSpace Delivery', 'valuespace'),
'event_data' => __('Event Data', 'valuespace'),
];
}
/**
* Get sortable columns
*/
public function get_sortable_columns() {
return [
'occurred_at' => ['occurred_at', true],
'event_type' => ['event_type', false],
];
}
/**
* Column default
*/
public function column_default($item, $column_name) {
switch ($column_name) {
case 'occurred_at':
$occurred_at = $item['occurred_at'] ?? '';
$event_timestamp = is_numeric($occurred_at) ? (int) $occurred_at : strtotime((string) $occurred_at);
return $event_timestamp
? esc_html(wp_date(get_option('date_format') . ' ' . get_option('time_format'), $event_timestamp))
: '—';
case 'object':
return $this->render_object_column($item);
case 'visitor':
return $this->render_visitor_column($item);
case 'audience':
$country = isset($item['country_code'])
? strtoupper((string) $item['country_code'])
: (isset($item['country']) ? strtoupper((string) $item['country']) : '');
$language = isset($item['language']) ? (string) $item['language'] : '';
$audience = array_filter([$country, $language], 'strlen');
return empty($audience) ? '—' : esc_html(implode(' / ', $audience));
case 'delivery_status':
$status = sanitize_key((string) ($item['delivery_status'] ?? 'pending'));
$labels = [
'pending' => __('Waiting', 'valuespace'),
'acknowledged' => __('Sent to ValueSpace', 'valuespace'),
'discarded' => __('Not sent', 'valuespace'),
'ignored' => __('Ignored', 'valuespace'),
];
$label = $labels[$status] ?? $labels['pending'];
return ''
. esc_html($label) . '';
case 'timestamp':
// Show raw timestamp value for debugging
$raw_value = '';
if (isset($item['timestamp'])) {
$raw_value = $item['timestamp'];
} elseif (isset($item['recorded_at'])) {
$raw_value = gmdate('Y-m-d H:i:s', (int)$item['recorded_at']);
}
// Display raw value as string (no conversions)
return '' . esc_html($raw_value) . '';
case 'event_type':
return '' . esc_html($item['event_type']) . '';
case 'wp_user_id':
$user_id = $item['wp_user_id'] ?? 0;
if ($user_id && $user_id > 0) {
$user = get_userdata($user_id);
if ($user) {
return sprintf(
'%s (#%d)',
esc_url(get_edit_user_link($user_id)),
esc_html($user->user_login),
$user_id
);
}
return '#' . intval($user_id);
}
return '' . esc_html__('Guest', 'valuespace') . '';
case 'session_id':
$session_id = $item['session_id'] ?? 'unknown';
return '' . esc_html($session_id) . '';
case 'event_data':
$event_data = $item['data'] ?? [];
$data_summary = [];
// Handle cart_snapshot specially
if (isset($event_data['cart_snapshot'])) {
$snapshot = $event_data['cart_snapshot'];
$cart_info = [];
if (isset($snapshot['total_items'])) {
$cart_info[] = sprintf(
/* translators: %s: Number of items in the recorded cart snapshot. */
__('Items: %s', 'valuespace'),
$snapshot['total_items']
);
}
if (isset($snapshot['total'])) {
$cart_info[] = sprintf(
/* translators: %s: Total value of the recorded cart snapshot. */
__('Total: %s', 'valuespace'),
$snapshot['total']
);
}
if (isset($snapshot['currency'])) {
$cart_info[] = sprintf(
/* translators: %s: Currency code of the recorded cart snapshot. */
__('Currency: %s', 'valuespace'),
$snapshot['currency']
);
}
$cart_summary = !empty($cart_info)
? sprintf(
/* translators: %s: Comma-separated cart snapshot details. */
__('Cart: %s', 'valuespace'),
implode(', ', $cart_info)
)
: __('Cart data available', 'valuespace');
$data_summary[] = 'cart_snapshot: ' . esc_html($cart_summary) . '';
}
// Handle other scalar values
foreach ($event_data as $key => $value) {
if ($key === 'cart_snapshot') continue; // Already handled above
if (is_scalar($value)) {
$display_value = strlen($value) > 50 ? substr($value, 0, 50) . '...' : $value;
$data_summary[] = '' . esc_html($key) . ': ' . esc_html($display_value);
} elseif (is_array($value) && !empty($value)) {
$array_summary = [];
foreach ($value as $sub_key => $sub_value) {
if (is_scalar($sub_value) && strlen($sub_value) < 20) {
$array_summary[] = $sub_key . ':' . $sub_value;
}
}
if (!empty($array_summary)) {
$data_summary[] = '' . esc_html($key) . ': {' . esc_html(implode(', ', array_slice($array_summary, 0, 2))) . '...}';
} else {
$data_summary[] = '' . esc_html($key) . ': ' . esc_html(sprintf(
/* translators: %d: Number of items in an event-data array. */
_n('Array with %d item', 'Array with %d items', count($value), 'valuespace'),
count($value)
));
}
}
}
if (!empty($data_summary)) {
$full_data = wp_json_encode($event_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$short_summary = implode('
', array_slice($data_summary, 0, 2));
return '
'search-submit')); ?>
' . $primary . ' ' . esc_html__('(opens in a new tab)', 'valuespace') . ''; } $meta = []; if ($type !== '') { $meta[] = $type === 'product' ? __('Product', 'valuespace') : __('Page', 'valuespace'); } if ($id !== '') { $meta[] = '#' . $id; } $secondary = $default_title !== '' && $default_title !== $title ? '' . sprintf( /* translators: %s: Default title of the event object. */ esc_html__('Default: %s', 'valuespace'), esc_html($default_title) ) . '' : ''; return '' . $primary . '' . (!empty($meta) ? '' . esc_html(implode(' ', $meta)) . '' : '') . $secondary; } /** * Render user and session identity. */ private function render_visitor_column($item) { $user_id = isset($item['wp_user_id']) ? (int) $item['wp_user_id'] : 0; $session_id = isset($item['session_id']) ? (string) $item['session_id'] : ''; $html = ''; if ($user_id > 0) { $user = get_userdata($user_id); $html = $user ? '' . esc_html($user->display_name ?: $user->user_login) . ' #' . $user_id . '' : sprintf( /* translators: %d: WordPress user ID. */ esc_html__('User #%d', 'valuespace'), $user_id ); } else { $html = '' . esc_html__('Guest', 'valuespace') . ''; } if ($session_id !== '') { $html .= '' . esc_html($session_id) . '';
}
return $html;
}
}