process_bulk_action(); } /** * Render the orders list, view, or edit screen depending on the request. */ public static function render() { global $wpdb; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing check, no state change. if ( isset( $_GET['view'] ) ) { include AIMSLMS_PLUGIN_DIR . 'includes/admin/views/order-details.php'; return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing check, no state change. if ( isset( $_GET['edit'] ) ) { include AIMSLMS_PLUGIN_DIR . 'includes/admin/edits/order-edit.php'; return; } require_once AIMSLMS_PLUGIN_DIR . 'includes/admin/orders/class-aimslms-orders-table.php'; $table = new AIMSLMS_Orders_Table(); $table->prepare_items(); include AIMSLMS_PLUGIN_DIR . 'includes/admin/views/orders.php'; } /** * CSV export handler for orders. */ public static function export_orders() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only routing check, no state change. if ( ! is_admin() || ! isset( $_GET['page'], $_GET['action'] ) || 'aimslms-orders' !== $_GET['page'] || 'export_orders' !== $_GET['action'] ) { return; } // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Permission denied' ); } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $orders = $wpdb->get_results( "SELECT o.*, p.post_title, u.display_name, u.user_email, pay.currency AS payment_currency, pay.payment_method, pay.payment_gateway FROM {$wpdb->prefix}aimslms_course_orders o JOIN {$wpdb->users} u ON u.ID = o.user_id JOIN {$wpdb->posts} p ON p.ID = o.course_id LEFT JOIN {$wpdb->prefix}aimslms_course_payment pay ON pay.order_id = o.id ORDER BY o.id DESC", ARRAY_A ); if ( empty( $orders ) ) { wp_die( 'No orders found' ); } // 🚨 NO OUTPUT BEFORE THIS nocache_headers(); header( 'Content-Type: text/csv; charset=UTF-8' ); header( 'Content-Disposition: attachment; filename=orders-' . gmdate( 'Y-m-d' ) . '.csv' ); $output = fopen( 'php://output', 'w' ); fputcsv( $output, array( 'Order ID', 'Customer', 'Email', 'Course', 'Amount', 'Currency', 'Status', 'Payment', 'Transaction ID', 'Date', ) ); foreach ( $orders as $order ) { $first = get_user_meta( $order['user_id'], 'first_name', true ); $last = get_user_meta( $order['user_id'], 'last_name', true ); $full_name = trim( $first . ' ' . $last ); $name = $full_name ? $full_name : $order['display_name']; $amount = 'razorpay' === $order['payment_gateway'] ? $order['order_total'] / 100 : $order['order_total']; $total = aimslms_format_price( $order['order_total'], array( 'gateway' => $order['payment_gateway'], 'currency' => $order['currency'] ?? null, ) ); fputcsv( $output, array( '#' . $order['id'], aimslms_csv_safe( $name ), aimslms_csv_safe( $order['user_email'] ), aimslms_csv_safe( $order['post_title'] ), number_format( $amount, 2, '.', '' ), // 166.00. $order['currency'] ?? 'INR', // INR. ucfirst( $order['order_status'] ), aimslms_csv_safe( $order['payment_method_title'] ), aimslms_csv_safe( $order['transaction_id'] ), $order['created_at'], ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- streaming CSV directly to php://output, not a filesystem operation. fclose( $output ); exit; } /** * Update order status and note only. */ public static function update_order() { if ( empty( $_POST['submit'] ) || empty( $_POST['order_id'] ) ) { return; } $order_id = absint( $_POST['order_id'] ); // Verify nonce. if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aimslms_update_order_' . $order_id ) ) { wp_die( esc_html__( 'Security check failed.', 'aims-infosoft-lms' ) ); } // Permission check. if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized action.', 'aims-infosoft-lms' ) ); } global $wpdb; // Sanitize inputs. $order_status = isset( $_POST['order_status'] ) ? sanitize_text_field( wp_unslash( $_POST['order_status'] ) ) : ''; $customer_note = isset( $_POST['customer_note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['customer_note'] ) ) : ''; // Allowed statuses. $allowed_statuses = array( 'pending', 'completed', 'cancelled', 'refunded' ); if ( ! in_array( $order_status, $allowed_statuses, true ) ) { $order_status = 'pending'; } // Update ONLY allowed fields. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( "{$wpdb->prefix}aimslms_course_orders", array( 'order_status' => $order_status, 'customer_note' => $customer_note, 'updated_at' => current_time( 'mysql' ), ), array( 'id' => $order_id ), array( '%s', '%s', '%s' ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'status' => $order_status, ), array( 'order_id' => $order_id ) ); // Redirect back safely. wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&edit=' . $order_id . '&updated=1' ) ); exit; } } AIMSLMS_Admin_Orders::init();