'order', 'plural' => 'orders', 'ajax' => false, ) ); } /** * Get the bulk actions available for the current status view. * * @return array Bulk action labels keyed by action slug. */ protected function get_bulk_actions() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $status = isset( $_GET['order_status'] ) ? sanitize_text_field( wp_unslash( $_GET['order_status'] ) ) : ''; if ( 'trash' === $status ) { return array( 'restore' => __( 'Restore', 'aims-infosoft-lms' ), 'delete_permanently' => __( 'Delete Permanently', 'aims-infosoft-lms' ), ); } return array( 'trash' => __( 'Move to Trash', 'aims-infosoft-lms' ), ); } /** =============================== * TABLE COLUMNS * =============================== */ public function get_columns() { return array( 'cb' => '', 'id' => __( 'Order', 'aims-infosoft-lms' ), 'item_name' => __( 'Item', 'aims-infosoft-lms' ), 'created_at' => __( 'Date', 'aims-infosoft-lms' ), 'order_status' => __( 'Status', 'aims-infosoft-lms' ), 'order_total' => __( 'Total', 'aims-infosoft-lms' ), 'payment_method_title' => __( 'Payment', 'aims-infosoft-lms' ), 'invoice' => __( 'Invoice', 'aims-infosoft-lms' ), // ✅ NEW ); } /** * Get the sortable columns for the table. * * @return array Sortable column definitions. */ protected function get_sortable_columns() { return array( 'id' => array( 'id', true ), 'created_at' => array( 'created_at', true ), 'order_total' => array( 'order_total', false ), ); } /** * Render the checkbox column. * * @param array $item The current order row. * @return string The checkbox HTML. */ protected function column_cb( $item ) { return ''; } /** * Render the order ID and customer name column. * * ORDER ID + CUSTOMER NAME. * * @param array $item The current order row. * @return string The column HTML. */ protected function column_id( $item ) { $order_id = absint( $item['id'] ); $view_url = admin_url( 'admin.php?page=aimslms-orders&view=' . $order_id ); $edit_url = admin_url( 'admin.php?page=aimslms-orders&edit=' . $order_id ); $trash_url = wp_nonce_url( admin_url( 'admin.php?page=aimslms-orders&action=trash&order_id=' . $order_id ), 'aimslms_trash_order_' . $order_id ); if ( 'trash' === $item['order_status'] ) { $restore_url = wp_nonce_url( admin_url( 'admin.php?page=aimslms-orders&action=restore&order_id=' . $order_id ), 'aimslms_restore_order_' . $order_id ); $delete_url = wp_nonce_url( admin_url( 'admin.php?page=aimslms-orders&action=delete&order_id=' . $order_id ), 'aimslms_delete_order_' . $order_id ); $actions = array( 'restore' => '' . __( 'Restore', 'aims-infosoft-lms' ) . '', 'delete' => '' . __( 'Delete Permanently', 'aims-infosoft-lms' ) . '', ); } else { $trash_url = wp_nonce_url( admin_url( 'admin.php?page=aimslms-orders&action=trash&order_id=' . $order_id ), 'aimslms_trash_order_' . $order_id ); $actions = array( 'edit' => '' . __( 'Edit', 'aims-infosoft-lms' ) . '', 'trash' => '' . __( 'Trash', 'aims-infosoft-lms' ) . '', ); } $user_id = $item['user_id']; $first = get_user_meta( $user_id, 'first_name', true ); $last = get_user_meta( $user_id, 'last_name', true ); if ( $first || $last ) { $name = trim( $first . ' ' . $last ); } else { $user = get_userdata( $user_id ); $name = $user ? $user->display_name : '-'; } return sprintf( '#%2$d %3$s %4$s', esc_url( $view_url ), $order_id, esc_html( $name ), $this->row_actions( $actions ) ); } /** * Render the item name column. * * @param array $item The current order row. * @return string The escaped item name. */ protected function column_item_name( $item ) { return esc_html( $item['item_name'] ?? '-' ); } /** * Render the created date column. * * @param array $item The current order row. * @return string The escaped, localized order date. */ protected function column_created_at( $item ) { return esc_html( date_i18n( get_option( 'date_format' ), strtotime( $item['created_at'] ) ) ); } /** * Render the order status column. * * @param array $item The current order row. * @return string The order status badge HTML. */ protected function column_order_status( $item ) { $status = sanitize_key( $item['order_status'] ); return sprintf( '%2$s', esc_attr( $status ), esc_html( ucfirst( $status ) ) ); } /** * Render the order total column. * * @param array $item The current order row. * @return string The formatted order total HTML. */ protected function column_order_total( $item ) { $amount = (float) $item['order_total']; $gateway = $item['payment_gateway'] ?? ''; $currency = $item['payment_currency'] ?? get_option( 'aimslms_currency', 'USD' ); // Razorpay stores amount in paise. if ( 'razorpay' === $gateway ) { $amount = $amount / 100; $currency = 'INR'; } return '' . aimslms_format_price( $amount, array( 'currency' => $currency, ) ) . ''; } /** * Render the payment method column. * * @param array $item The current order row. * @return string The escaped payment method title. */ protected function column_payment_method_title( $item ) { return esc_html( $item['payment_method_title'] ); } /** * Render a default column value. * * @param array $item The current order row. * @param string $column_name The column being rendered. * @return mixed The column value, or an empty string if not set. */ protected function column_default( $item, $column_name ) { return $item[ $column_name ] ?? ''; } /** * Render the invoice download column. * * @param array $item The current order row. * @return string The invoice download link HTML. */ protected function column_invoice( $item ) { $order_id = absint( $item['id'] ); $url = add_query_arg( array( 'aimslms_invoice' => 1, 'order_id' => $order_id, '_nonce' => wp_create_nonce( 'aimslms_invoice_' . $order_id ), ), home_url( '/' ) ); return sprintf( ' ', esc_url( $url ), esc_attr__( 'Download Invoice', 'aims-infosoft-lms' ) ); } /** * Build the SELECT query used by the table and CSV export. * * QUERY BUILDER (TABLE + EXPORT). * * @param bool $with_limit Whether to append a LIMIT/OFFSET clause for pagination. * @return string The assembled SQL query. */ public static function build_query( $with_limit = true ) { global $wpdb; $where = self::build_where(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $wpdb->prefix, $wpdb->users, $wpdb->posts are derived from $wpdb->prefix, not user input; $where is built entirely from $wpdb->prepare() calls in build_where(). $sql = " SELECT o.*, u.display_name, u.user_email, p.post_title AS item_name, MAX(pay.payment_gateway) AS payment_gateway, MAX(pay.currency) AS payment_currency FROM {$wpdb->prefix}aimslms_course_orders o JOIN {$wpdb->users} u ON u.ID = o.user_id LEFT JOIN {$wpdb->posts} p ON p.ID = o.course_id LEFT JOIN {$wpdb->prefix}aimslms_course_payment pay ON pay.order_id = o.id $where GROUP BY o.id "; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $orderby = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : 'id'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $order = isset( $_GET['order'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ) ) ) : 'DESC'; $allowed_orderby = array( 'id', 'created_at', 'order_total' ); if ( ! in_array( $orderby, $allowed_orderby, true ) ) { $orderby = 'id'; } $order = ( 'ASC' === $order ) ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $orderby is allowlist-validated above and $order is restricted to ASC/DESC, not user input passed through. $sql .= " ORDER BY o.$orderby $order "; if ( $with_limit ) { $per_page = 10; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $paged = max( 1, absint( $_GET['paged'] ?? 1 ) ); $offset = ( $paged - 1 ) * $per_page; $sql .= $wpdb->prepare( ' LIMIT %d OFFSET %d', $per_page, $offset ); } return $sql; } /** =============================== * PREPARE ITEMS * =============================== */ public function prepare_items() { global $wpdb; $per_page = 10; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $paged = max( 1, absint( $_GET['paged'] ?? 1 ) ); $offset = ( $paged - 1 ) * $per_page; // Get table data. $sql = self::build_query( true ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql is assembled in build_query(), which already annotates its own interpolation as safe; custom table, no WP API alternative. $this->items = $wpdb->get_results( $sql, ARRAY_A ); // Column headers. $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns(), ); // Build COUNT query. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $wpdb->prefix, $wpdb->users, $wpdb->posts are derived from $wpdb->prefix, not user input; build_where() is built entirely from $wpdb->prepare() calls. $count_sql = " SELECT COUNT(DISTINCT o.id) FROM {$wpdb->prefix}aimslms_course_orders o JOIN {$wpdb->users} u ON u.ID = o.user_id LEFT JOIN {$wpdb->posts} p ON p.ID = o.course_id LEFT JOIN {$wpdb->prefix}aimslms_course_payment pay ON pay.order_id = o.id " . self::build_where(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $count_sql is assembled above from safe, allowlisted pieces (see annotation); custom table, no WP API alternative. $total_items = $wpdb->get_var( $count_sql ); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, ) ); } /** =============================== * WHERE CLAUSE BUILDER * =============================== */ private static function build_where() { global $wpdb; // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen; every value below is either allowlist-compared or passed through $wpdb->prepare(). $where = ' WHERE 1=1 '; // Status filter. $status = isset( $_GET['order_status'] ) ? sanitize_text_field( wp_unslash( $_GET['order_status'] ) ) : ''; if ( 'trash' === $status ) { $where .= " AND o.order_status = 'trash' "; } else { $where .= " AND o.order_status != 'trash' "; } // Status from. if ( ! empty( $_GET['status'] ) ) { $where .= $wpdb->prepare( ' AND o.order_status = %s ', sanitize_text_field( wp_unslash( $_GET['status'] ) ) ); } // Date from. if ( ! empty( $_GET['date_from'] ) ) { $date_from = sanitize_text_field( wp_unslash( $_GET['date_from'] ) ); $where .= $wpdb->prepare( ' AND DATE(o.created_at) >= %s ', $date_from ); } // Date to. if ( ! empty( $_GET['date_to'] ) ) { $date_to = sanitize_text_field( wp_unslash( $_GET['date_to'] ) ); $where .= $wpdb->prepare( ' AND DATE(o.created_at) <= %s ', $date_to ); } // Search. if ( ! empty( $_GET['s'] ) ) { $search = '%' . $wpdb->esc_like( sanitize_text_field( wp_unslash( $_GET['s'] ) ) ) . '%'; $where .= $wpdb->prepare( ' AND (u.display_name LIKE %s OR u.user_email LIKE %s OR p.post_title LIKE %s)', $search, $search, $search ); } // phpcs:enable WordPress.Security.NonceVerification.Recommended return $where; } /** * Render the extra filter controls above the list table. * * @param string $which Whether this is the 'top' or 'bottom' table nav. */ protected function extra_tablenav( $which ) { if ( 'top' !== $which ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; $date_from = isset( $_GET['date_from'] ) ? sanitize_text_field( wp_unslash( $_GET['date_from'] ) ) : ''; $date_to = isset( $_GET['date_to'] ) ? sanitize_text_field( wp_unslash( $_GET['date_to'] ) ) : ''; $reset_url = admin_url( 'admin.php?page=aimslms-orders' ); $empty_trash_order_url = wp_nonce_url( admin_url( 'admin.php?page=aimslms-orders&action=empty_trash' ), 'aimslms_empty_trash' ); ?>
current_action(); if ( ! $action ) { $action = isset( $_REQUEST['action2'] ) ? sanitize_key( wp_unslash( $_REQUEST['action2'] ) ) : ''; } if ( ! $action ) { // Plain page view (no bulk action submitted) - nothing to do, // and nothing to verify a nonce against. return; } if ( 'empty_trash' !== $action ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized immediately below via absint()/array_map( 'absint', ... ). $raw_order_id = isset( $_REQUEST['order_id'] ) ? wp_unslash( $_REQUEST['order_id'] ) : array(); if ( ! is_array( $raw_order_id ) && in_array( $action, array( 'trash', 'restore', 'delete' ), true ) ) { // Single-row action link (column_id()), each with its own // per-order nonce ('aimslms_{action}_order_{id}'). check_admin_referer( 'aimslms_' . $action . '_order_' . absint( $raw_order_id ) ); } else { // The bulk-action dropdown/submit form is rendered by // WP_List_Table with a 'bulk-orders' nonce (from the // 'plural' arg passed to parent::__construct()). check_admin_referer( 'bulk-orders' ); } $order_ids = array_map( 'absint', (array) $raw_order_id ); if ( empty( $order_ids ) ) { return; } } // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom tables, no WP API alternative. if ( 'trash' === $action ) { foreach ( $order_ids as $order_id ) { $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_orders SET previous_status = order_status, order_status = 'trash' WHERE id = %d", $order_id ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_payment SET previous_status = status, status = 'trash' WHERE order_id = %d", $order_id, ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_enrollments SET status = 'deactive' WHERE order_id = %d", $order_id, ) ); } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&message=trashed' ) ); exit; } if ( 'restore' === $action ) { foreach ( $order_ids as $order_id ) { $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_orders SET order_status = COALESCE(previous_status,'pending'), previous_status = NULL WHERE id = %d", $order_id ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_payment SET status = COALESCE(previous_status,'pending'), previous_status = NULL WHERE order_id = %d", $order_id, ) ); $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}aimslms_course_enrollments SET status = 'active' WHERE order_id = %d", $order_id, ) ); } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&message=restored' ) ); exit; } if ( 'delete_permanently' === $action ) { foreach ( $order_ids as $order_id ) { $wpdb->delete( $wpdb->prefix . 'aimslms_course_orders', array( 'id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_enrollments', array( 'order_id' => $order_id ) ); } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&order_status=trash&message=deleted' ) ); exit; } if ( 'delete' === $action ) { foreach ( $order_ids as $order_id ) { $wpdb->delete( $wpdb->prefix . 'aimslms_course_orders', array( 'id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_enrollments', array( 'order_id' => $order_id ) ); } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&order_status=trash&message=deleted' ) ); exit; } if ( 'empty_trash' === $action ) { // Security check. if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'aimslms_empty_trash' ) ) { wp_die( esc_html__( 'Security check failed', 'aims-infosoft-lms' ) ); } // Delete all trashed orders. $trashed_orders = $wpdb->get_col( "SELECT id FROM {$wpdb->prefix}aimslms_course_orders WHERE order_status = 'trash'" ); if ( ! empty( $trashed_orders ) ) { foreach ( $trashed_orders as $order_id ) { $wpdb->delete( $wpdb->prefix . 'aimslms_course_orders', array( 'id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id ) ); $wpdb->delete( $wpdb->prefix . 'aimslms_course_enrollments', array( 'order_id' => $order_id ) ); } } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-orders&message=emptied' ) ); exit; } // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get the status views (All / Trash) shown above the list table. * * @return array View link HTML keyed by view slug. */ public function get_views() { global $wpdb; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. $current = isset( $_GET['order_status'] ) ? sanitize_key( wp_unslash( $_GET['order_status'] ) ) : 'all'; $base_url = admin_url( 'admin.php?page=aimslms-orders' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $wpdb->prefix is derived from $wpdb->prefix, not user input; custom table, no WP API alternative. $all = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_orders WHERE order_status != 'trash'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $wpdb->prefix is derived from $wpdb->prefix, not user input; custom table, no WP API alternative. $trash = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}aimslms_course_orders WHERE order_status = 'trash'" ); $views = array(); $views['all'] = sprintf( '%s (%d)', esc_url( $base_url ), 'all' === $current ? 'current' : '', __( 'All', 'aims-infosoft-lms' ), $all ); if ( $trash > 0 ) { $views['trash'] = sprintf( '%s (%d)', esc_url( add_query_arg( 'order_status', 'trash', $base_url ) ), 'trash' === $current ? 'current' : '', __( 'Trash', 'aims-infosoft-lms' ), $trash ); } return $views; } } add_action( 'admin_notices', function () { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display notice on a manage_options-gated admin screen. if ( ! isset( $_GET['message'] ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display notice on a manage_options-gated admin screen. $message = sanitize_text_field( wp_unslash( $_GET['message'] ) ); $messages = array( 'trashed' => __( 'Order moved to trash.', 'aims-infosoft-lms' ), 'restored' => __( 'Order restored successfully.', 'aims-infosoft-lms' ), 'deleted' => __( 'Order deleted permanently.', 'aims-infosoft-lms' ), 'emptied' => __( 'Trash emptied successfully.', 'aims-infosoft-lms' ), ); if ( isset( $messages[ $message ] ) ) { echo '
'; echo '

' . esc_html( $messages[ $message ] ) . '

'; echo '
'; } } );