active_tab = (isset($_GET['tab'])) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'default'; $this->setting_key = 'pisol_aww_transactions'; if($this->this_tab == $this->active_tab){ add_action('pisol_'.$this->plugin_name.'_tab_content', array($this,'tab_content')); } // Add AJAX handler for user search add_action('wp_ajax_pisol_aww_search_users', array($this, 'ajax_search_users')); // Add AJAX handler for soft delete transaction add_action('wp_ajax_pisol_aww_soft_delete_transaction', array($this, 'ajax_soft_delete_transaction')); add_action('pisol_'.$this->plugin_name.'_tab', array($this,'tab'), 10); } /** * Add transactions tab to plugin settings page * * @param array $tabs Existing tabs * @return array Modified tabs */ function tab(){ $this->tab_name = __("Transactions",'advanced-wallet-for-woocommerce'); ?> tab_name ); ?> 0 && isset($_GET['transactions_nonce'])) { $nonce = sanitize_text_field(wp_unslash($_GET['transactions_nonce'])); if (!wp_verify_nonce($nonce, 'pisol_aww_transactions_view_nonce_' . $user_id)) { wp_die(esc_html__('Invalid or expired nonce. Please access the transaction history from the user profile page.', 'advanced-wallet-for-woocommerce')); } } ?>

'; // Get users for filter $users_with_transactions = $this->get_users_with_transactions(); // Display the filter form $this->display_filter_form($user_id, $users_with_transactions); // Get transactions $transactions = $this->get_transactions($wallet_manager, $user_id); // Display transactions $this->display_transactions($transactions, $user_id, $wallet_manager); echo ''; } /** * Get list of users with wallet transactions * * @return array List of users with transactions */ private function get_users_with_transactions() { // Fetch all users using WordPress functions instead of direct DB queries $args = array( 'orderby' => 'display_name', 'order' => 'ASC', 'fields' => array('ID', 'user_login', 'display_name'), ); $user_objects = get_users($args); $users = array(); foreach ($user_objects as $user) { $users[] = array( 'ID' => $user->ID, 'user_login' => $user->user_login, 'display_name' => $user->display_name, ); } return $users; } /** * Display user filter form * * @param int $selected_user_id Currently selected user ID * @param array $users List of users to display */ private function display_filter_form($selected_user_id, $users) { // Prepare selected user label $selected_user_name = ''; if ($selected_user_id) { $user_data = get_userdata($selected_user_id); if ($user_data) { $selected_user_name = $user_data->display_name . ' (' . $user_data->user_email . ')'; } } $current_url = admin_url('admin.php?page=advanced-wallet-for-woocommerce&tab=pisol_aww_transactions'); ?>
get_user_balance($selected_user_id, $currency); echo '

'; printf( /* translators: 1: user display name, 2: wallet balance */ esc_html__('Showing transactions for %1$s. Current balance: %2$s', 'advanced-wallet-for-woocommerce'), '' . esc_html($user_data->display_name) . '', '' . wp_kses_post( wc_price( $wallet_balance ) ) . '' ); // Add link to edit user echo ' '; esc_html_e('Edit User', 'advanced-wallet-for-woocommerce'); echo ''; echo '

'; } } ?> $wpdb->base_prefix . 'pi_aww_wallet_credits', 'debits' => $wpdb->base_prefix . 'pi_aww_wallet_debits' ]; // Properly escape table names $credits_table = esc_sql($tables['credits']); $debits_table = esc_sql($tables['debits']); // Get all user IDs with wallet activity using properly escaped table names // Table names can't be parameters in prepared statements // We have already escaped the table names earlier with esc_sql() // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names can't be parameterized $user_ids = $wpdb->get_col( "SELECT DISTINCT user_id FROM ( SELECT user_id FROM {$credits_table} UNION SELECT user_id FROM {$debits_table} ) as users" ); $all_transactions = []; // Loop through each user and get their transactions foreach ($user_ids as $user_id) { // Using the existing Wallet_Manager method to get user transactions $user_transactions = $wallet_manager->get_user_transactions($user_id, $args); $all_transactions = array_merge($all_transactions, $user_transactions ?: []); } // Sort transactions by date usort($all_transactions, function($a, $b) use ($args) { $order = ($args['order'] === 'DESC') ? -1 : 1; return $order * (strtotime($a['created_at']) - strtotime($b['created_at'])); }); // Limit after merging return array_slice($all_transactions, 0, $args['limit']); } /** * Display transactions in a table * * @param array $transactions List of transactions * @param int $user_id Current user ID filter * @param Wallet_Manager $wallet_manager Wallet manager instance */ private function display_transactions($transactions_data, $user_id, $wallet_manager) { // Extract data from the transaction results $transactions = isset($transactions_data['transactions']) ? $transactions_data['transactions'] : []; $total_count = isset($transactions_data['total_count']) ? $transactions_data['total_count'] : 0; $total_pages = isset($transactions_data['total_pages']) ? $transactions_data['total_pages'] : 1; $current_page = isset($transactions_data['current_page']) ? $transactions_data['current_page'] : 1; $per_page = isset($transactions_data['per_page']) ? $transactions_data['per_page'] : 10; if(empty($transactions)) { echo '

'; esc_html_e('No transactions found.', 'advanced-wallet-for-woocommerce'); echo '

'; echo '
'; // Close wrap div return; } // Build the pagination base URL $base_url = admin_url('admin.php?page=advanced-wallet-for-woocommerce&tab=pisol_aww_transactions'); if ($user_id > 0) { $base_url .= '&user_id=' . $user_id; } ?> $transaction['currency'])); if($transaction['used_amount'] > 0) { $amount .= ' (' . wc_price($transaction['amount'], array('currency' => $transaction['currency'])) . ' ' . esc_html__('total', 'advanced-wallet-for-woocommerce') . ')'; } } else { $amount = wc_price($transaction['amount'], array('currency' => $transaction['currency'])); } // Format the description $description = $transaction['note'] ?: '—'; // Format the source $source = $transaction['source'] ?: '—'; // Reference links for orders if($transaction['type'] === 'debit' && $transaction['source'] === 'order' && !empty($transaction['note'])) { if(preg_match('/\#(\d+)/', $transaction['note'], $matches)) { $order_id = $matches[1]; $source = ''; $source .= esc_html__('Order', 'advanced-wallet-for-woocommerce') . ' #' . esc_html($order_id); $source .= ''; } } // Special format for cashback credits if($transaction['type'] === 'credit' && $transaction['source'] === 'cashback') { $source = esc_html__('Cashback', 'advanced-wallet-for-woocommerce'); } // Get and format expiry date (only for credits) $expiry_html = '—'; if($transaction['type'] === 'credit' && !empty($transaction['expiry_date'])) { $today = current_time('Y-m-d'); $expiry_date = $transaction['expiry_date']; // Format the date for display $formatted_expiry = date_i18n(get_option('date_format'), strtotime($expiry_date)); // Show different formatting based on expiry status if($expiry_date < $today) { // Expired $expiry_html = '' . $formatted_expiry . ' (' . esc_html__('Expired', 'advanced-wallet-for-woocommerce') . ')'; } elseif($expiry_date === $today) { // Expires today $expiry_html = '' . $formatted_expiry . ' (' . esc_html__('Today', 'advanced-wallet-for-woocommerce') . ')'; } else { // Future expiry $days_left = (int)((strtotime($expiry_date) - strtotime($today)) / DAY_IN_SECONDS); if($days_left <= 7) { // Highlight if expiring within 7 days $expiry_html = '' . $formatted_expiry . ' (' . sprintf( /* translators: %d: number of days */ _n('%d day left', '%d days left', $days_left, 'advanced-wallet-for-woocommerce'), $days_left) . ')'; } else { $expiry_html = $formatted_expiry; } } } // Status $status_html = ($transaction['is_deleted'] == 1) ? '' . esc_html__('Deleted', 'advanced-wallet-for-woocommerce') . '' : '' . esc_html__('Active', 'advanced-wallet-for-woocommerce') . ''; // Get user info if showing all users $user_html = ''; if(!$user_id && isset($transaction['user_id'])) { $user_data = get_userdata($transaction['user_id']); if($user_data) { $user_html = ''; $user_html .= esc_html($user_data->display_name); $user_html .= ''; } else { $user_html = esc_html__('Unknown User', 'advanced-wallet-for-woocommerce') . ' (ID: ' . esc_html($transaction['user_id']) . ')'; } } ?>
-
1): ?>
esc_like($term) . '%'; // For multisite: join with usermeta to ensure we only get users from current blog $join_statement = ''; $where_statement = ''; if (is_multisite()) { $cap_meta_key = $wpdb->prefix . 'capabilities'; $join_statement = $wpdb->prepare( " INNER JOIN {$wpdb->usermeta} AS um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value != ''", // Ensure user has some role $cap_meta_key ); } // SQL query to find users by email, login or display name // Run the query // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $join_statement is already prepared $users = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT u.ID, u.user_email, u.display_name FROM {$wpdb->users} AS u {$join_statement} WHERE ( u.user_email LIKE %s OR u.user_login LIKE %s OR u.display_name LIKE %s ) ORDER BY CASE WHEN u.user_email LIKE %s THEN 1 WHEN u.user_login LIKE %s THEN 2 ELSE 3 END, u.display_name ASC LIMIT 20", $like_term, $like_term, $like_term, $like_term, $like_term )); // Format the results $results = array(); foreach ($users as $user) { $results[] = array( 'id' => $user->ID, 'text' => sprintf('%s (%s)', $user->display_name, $user->user_email), ); } wp_send_json($results); } /** * Handle AJAX request to soft delete or restore a transaction */ public function ajax_soft_delete_transaction() { // Verify nonce for security check_ajax_referer('pisol_aww_soft_delete_transaction', 'security'); // Check user capability if (!current_user_can('manage_options')) { wp_send_json_error(array( 'message' => __('You do not have permission to perform this action.', 'advanced-wallet-for-woocommerce') )); return; } // Get transaction details and properly unslash before sanitizing $transaction_id = isset($_POST['transaction_id']) ? absint($_POST['transaction_id']) : 0; $transaction_type = isset($_POST['transaction_type']) ? sanitize_text_field(wp_unslash($_POST['transaction_type'])) : ''; $action = isset($_POST['action_type']) ? sanitize_text_field(wp_unslash($_POST['action_type'])) : 'delete'; // 'delete' or 'undelete' if (!$transaction_id || empty($transaction_type)) { wp_send_json_error(array( 'message' => __('Invalid transaction details.', 'advanced-wallet-for-woocommerce') )); return; } // Get wallet manager $wallet_manager = \PISOL\AWW\CLASSES\Wallet_Manager::get_instance(); $success = false; if ($action === 'delete') { // Soft delete based on type if ($transaction_type === 'credit') { $success = $wallet_manager->soft_delete_credit($transaction_id); } elseif ($transaction_type === 'debit') { $success = $wallet_manager->soft_delete_debit($transaction_id); } $message = $success ? __('Transaction has been soft deleted successfully.', 'advanced-wallet-for-woocommerce') : __('Failed to delete the transaction. It may be in use or already deleted.', 'advanced-wallet-for-woocommerce'); } else { // Restore based on type if ($transaction_type === 'credit') { $success = $wallet_manager->restore_credit($transaction_id); } elseif ($transaction_type === 'debit') { $success = $wallet_manager->restore_debit($transaction_id); } $message = $success ? __('Transaction has been restored successfully.', 'advanced-wallet-for-woocommerce') : __('Failed to restore the transaction.', 'advanced-wallet-for-woocommerce'); } if ($success) { wp_send_json_success(array( 'message' => $message )); } else { wp_send_json_error(array( 'message' => $message )); } } }