init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); } /** * Add admin menu. * Note: Dashboard is now handled by AfriDirect_Payment_Admin_Menu. * This method is kept for backward compatibility but does nothing. */ public function add_admin_menu() { // Menu is now created by AfriDirect_Payment_Admin_Menu. // This method is kept for compatibility but does nothing. } /** * Render dashboard. */ public function render_dashboard() { $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); if ( ! current_user_can( $menu_manager->get_capability() ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'afridirect-payment' ) ); } // Handle bulk actions. $this->handle_bulk_actions(); // Get orders pending review (only afridirect-proof status). $orders = wc_get_orders( array( 'status' => array( 'adp-proof' ), 'payment_method' => 'afridirect_payment', 'limit' => 100, 'orderby' => 'date', 'order' => 'DESC', ) ); include AFRIDIRECT_PAYMENT_PLUGIN_DIR . 'admin/views/dashboard.php'; } /** * Handle bulk actions. */ private function handle_bulk_actions() { if ( ! isset( $_POST['afridirect_bulk_action'] ) || ! isset( $_POST['afridirect_order_ids'] ) ) { return; } check_admin_referer( 'afridirect-bulk-actions', 'afridirect_bulk_nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Unauthorized access.', 'afridirect-payment' ) ); } $action = sanitize_text_field( wp_unslash( $_POST['afridirect_bulk_action'] ) ); $order_ids = array_map( 'absint', $_POST['afridirect_order_ids'] ); if ( empty( $order_ids ) ) { return; } $processed = 0; foreach ( $order_ids as $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || 'adp-proof' !== $order->get_status() ) { continue; } if ( 'approve' === $action ) { $this->process_approve( $order_id, '' ); $processed++; } elseif ( 'reject' === $action ) { $note = isset( $_POST['afridirect_bulk_reject_note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['afridirect_bulk_reject_note'] ) ) : ''; if ( $note ) { $this->process_reject( $order_id, $note ); $processed++; } } } if ( $processed > 0 ) { add_action( 'admin_notices', function() use ( $processed, $action ) { echo '

'; /* translators: 1: Number of orders, 2: Action verb (e.g., "approved", "rejected") */ echo esc_html( sprintf( _n( '%1$d order %2$s successfully.', '%1$d orders %2$s successfully.', $processed, 'afridirect-payment' ), $processed, $action . 'ed' ) ); echo '

'; } ); } } /** * Process approve action. * * @param int $order_id Order ID. * @param string $notes Admin notes. */ private function process_approve( $order_id, $notes = '' ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } if ( $notes ) { $order->update_meta_data( '_afridirect_admin_note', $notes ); } // Generate receipt number if not exists. $receipt_number = $order->get_meta( '_afridirect_receipt_no' ); if ( ! $receipt_number ) { $receipt_generator = AfriDirect_Payment_Receipt_Generator::instance(); $receipt_number = $receipt_generator->generate_receipt_number( $order_id ); $order->update_meta_data( '_afridirect_receipt_no', $receipt_number ); } $order->update_status( 'adp-approved', __( 'Payment proof approved by admin.', 'afridirect-payment' ) ); $order->save(); do_action( 'afridirect_order_approved', $order_id ); } /** * Process reject action. * * @param int $order_id Order ID. * @param string $note Rejection note. */ private function process_reject( $order_id, $note ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $order->update_meta_data( '_afridirect_admin_note', $note ); $order->update_status( 'adp-rejected', __( 'Payment proof rejected by admin.', 'afridirect-payment' ) ); $order->save(); do_action( 'afridirect_order_rejected', $order_id ); } /** * Get order review modal content. * * @param WC_Order $order Order object. * @return string */ public function get_order_review_modal( $order ) { ob_start(); include AFRIDIRECT_PAYMENT_PLUGIN_DIR . 'admin/views/order-review.php'; return ob_get_clean(); } }