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'),3);
}
/**
* 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');
?>
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);
}
/**
* 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);
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 '
';
}
}
?>
get_paginated_transactions_combined($user_id, $current_page, $per_page);
}
/**
* Combined function to get paginated transactions for either specific user or all users
*
* @param int $user_id User ID (0 for all users)
* @param int $current_page Current page number
* @param int $per_page Items per page
* @return array Associative array with transactions, pagination data, and total count
*/
private function get_paginated_transactions_combined($user_id = 0, $current_page = 1, $per_page = 10) {
global $wpdb;
$tables = [
'credits' => $wpdb->base_prefix . 'pi_aww_wallet_credits',
'debits' => $wpdb->base_prefix . 'pi_aww_wallet_debits'
];
// Calculate offset
$offset = ($current_page - 1) * $per_page;
// Build the WHERE clause for filtering by user_id if needed
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $user_where_credits is safely prepared above
$user_where_credits = $user_id > 0 ? $wpdb->prepare("WHERE user_id = %d", $user_id) : "";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $user_where_debits is safely prepared above
$user_where_debits = $user_id > 0 ? $wpdb->prepare("WHERE user_id = %d", $user_id) : "";
// Prepare the table names - we can't use placeholders for table names
$credits_table = esc_sql($tables['credits']);
$debits_table = esc_sql($tables['debits']);
// Count total transactions using a UNION query
if ($user_id > 0) {
// If filtering by user, use a prepared statement with the user ID
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names can't be parameterized
$total_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM (
SELECT credit_id FROM {$credits_table} WHERE user_id = %d
UNION ALL
SELECT debit_id FROM {$debits_table} WHERE user_id = %d
) AS all_transactions", $user_id, $user_id));
} else {
// If not filtering by user
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names can't be parameterized
$total_count = $wpdb->get_var("SELECT COUNT(*) FROM (
SELECT credit_id FROM {$credits_table}
UNION ALL
SELECT debit_id FROM {$debits_table}
) AS all_transactions");
}
// Calculate total pages
$total_pages = ceil($total_count / $per_page);
// Build and execute query safely
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names can't be parameterized
$transactions = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM (
SELECT
credit_id as id,
user_id,
amount,
source,
note,
used_amount,
is_deleted,
created_at,
expiry_date,
'credit' as type
FROM {$credits_table}
" . $user_where_credits . "
UNION ALL
SELECT
debit_id as id,
user_id,
amount,
COALESCE(reference_type, '') as source,
note,
0 as used_amount,
is_deleted,
created_at,
NULL as expiry_date,
'debit' as type
FROM {$debits_table}
" . $user_where_debits . "
) AS all_transactions
ORDER BY created_at DESC
LIMIT %d OFFSET %d", $per_page, $offset),
ARRAY_A
);
return [
'transactions' => $transactions ?: [],
'total_count' => $total_count,
'total_pages' => $total_pages,
'current_page' => $current_page,
'per_page' => $per_page
];
}
/**
* Get all transactions across all users (legacy method - replaced by pagination)
*
* @param Wallet_Manager $wallet_manager Wallet manager instance
* @param array $args Query arguments
* @return array All transactions
*/
private function get_all_users_transactions($wallet_manager, $args) {
global $wpdb;
$tables = [
'credits' => $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, null, 1, $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;
}
?>