logger = $logger;
}
/**
* Register hooks.
*/
public function register(): void {
add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'wp_ajax_aipn_export_csv', array( $this, 'ajax_export_csv' ) );
}
/**
* Add analytics page under AI Negotiator menu.
*/
public function add_menu_page(): void {
add_submenu_page(
'aipn-settings',
__( 'Negotiation Analytics', 'ai-price-negotiator-for-woocommerce' ),
__( 'Analytics & Chat Logs', 'ai-price-negotiator-for-woocommerce' ),
'manage_woocommerce',
'aipn-analytics',
array( $this, 'render_page' )
);
}
/**
* Enqueue analytics assets.
*/
public function enqueue_assets( string $hook ): void {
if ( 'ai-negotiator_page_aipn-analytics' !== $hook ) {
return;
}
wp_enqueue_style(
'aipn-analytics',
AIPN_PLUGIN_URL . 'pro/assets/css/analytics-dashboard.css',
array(),
AIPN_VERSION
);
wp_enqueue_script(
'aipn-analytics',
AIPN_PLUGIN_URL . 'pro/assets/js/analytics-dashboard.js',
array( 'jquery' ),
AIPN_VERSION,
true
);
wp_localize_script( 'aipn-analytics', 'aipnAnalytics', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aipn_export_csv' ),
'strings' => array(
'exporting' => __( 'Exporting...', 'ai-price-negotiator-for-woocommerce' ),
'exportBtn' => __( 'Export CSV', 'ai-price-negotiator-for-woocommerce' ),
'view' => __( 'View', 'ai-price-negotiator-for-woocommerce' ),
'hide' => __( 'Hide', 'ai-price-negotiator-for-woocommerce' ),
),
) );
}
/**
* Render the analytics page.
*/
public function render_page(): void {
$period = 30;
if ( isset( $_GET['_aipn_nonce'], $_GET['period'] ) && wp_verify_nonce( sanitize_key( $_GET['_aipn_nonce'] ), 'aipn_analytics_period' ) ) {
$period = (int) $_GET['period'];
}
$stats = $this->logger->get_stats( $period );
$breakdown = $this->logger->get_status_breakdown( $period );
$top_products = $this->logger->get_top_products( $period, 5 );
$recent = $this->logger->get_negotiations( array( 'limit' => 20 ) );
$currency = html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' );
?>
array( 'label' => __( 'Accepted', 'ai-price-negotiator-for-woocommerce' ), 'color' => 'green' ),
'expired' => array( 'label' => __( 'Expired', 'ai-price-negotiator-for-woocommerce' ), 'color' => 'yellow' ),
'abandoned' => array( 'label' => __( 'Abandoned', 'ai-price-negotiator-for-woocommerce' ), 'color' => 'gray' ),
);
foreach ( $statuses as $status_key => $status_meta ) :
$count = $breakdown[ $status_key ];
$pct = $breakdown['total'] > 0 ? round( ( $count / $breakdown['total'] ) * 100, 1 ) : 0;
?>
|
|
|
|
|
|
|
|
|
|
|
$row ) : ?>
|
' . esc_html( $customer_email ) . '';
}
} elseif ( $customer_id > 0 ) {
$user = get_userdata( $customer_id );
echo $user ? esc_html( $user->display_name ) : esc_html( '#' . $customer_id );
} else {
esc_html_e( 'Guest', 'ai-price-negotiator-for-woocommerce' );
}
?>
|
|
|
|
|
|
— |
—
|
|
|
__( 'Permission denied.', 'ai-price-negotiator-for-woocommerce' ) ) );
}
$period = isset( $_POST['period'] ) ? (int) $_POST['period'] : 30;
$rows = $this->logger->get_export_data( $period );
$csv_rows = array();
$csv_rows[] = $this->csv_encode_row( array(
__( 'Date', 'ai-price-negotiator-for-woocommerce' ),
__( 'Name', 'ai-price-negotiator-for-woocommerce' ),
__( 'Email', 'ai-price-negotiator-for-woocommerce' ),
__( 'Status', 'ai-price-negotiator-for-woocommerce' ),
__( 'Cart Total', 'ai-price-negotiator-for-woocommerce' ),
__( 'Final Price', 'ai-price-negotiator-for-woocommerce' ),
__( 'Discount', 'ai-price-negotiator-for-woocommerce' ),
__( 'Turns', 'ai-price-negotiator-for-woocommerce' ),
__( 'Coupon Code', 'ai-price-negotiator-for-woocommerce' ),
) );
foreach ( $rows as $row ) {
$customer_name = $row['customer_name'] ?? '';
$customer_email = $row['customer_email'] ?? '';
// Fallback to WP user data if name/email columns are empty (old records).
if ( $customer_name === '' ) {
$customer_id = (int) ( $row['customer_id'] ?? 0 );
if ( $customer_id > 0 ) {
$user = get_userdata( $customer_id );
$customer_name = $user ? $user->display_name : '#' . $customer_id;
$customer_email = $customer_email ?: ( $user ? $user->user_email : '' );
} else {
$customer_name = __( 'Guest', 'ai-price-negotiator-for-woocommerce' );
}
}
$csv_rows[] = $this->csv_encode_row( array(
$row['created_at'],
$customer_name,
$customer_email,
ucfirst( $row['status'] ),
$row['cart_total'],
$row['final_price'],
$row['discount_amount'],
$row['turn_count'],
$row['coupon_code'],
) );
}
$csv_content = implode( "\n", $csv_rows );
wp_send_json_success( array(
'csv' => $csv_content,
'filename' => 'negotiations-' . gmdate( 'Y-m-d' ) . '.csv',
) );
}
/**
* Encode a single row as a CSV line without fopen/fputcsv.
*
* @param array $fields Row values.
* @return string CSV-formatted line.
*/
private function csv_encode_row( array $fields ): string {
$escaped = array_map( static function ( $value ) {
$value = (string) $value;
return '"' . str_replace( '"', '""', $value ) . '"';
}, $fields );
return implode( ',', $escaped );
}
}