function don_donation_custom_columns($column)
{
    global $post;
    switch ($column) {
        case "amount":
            foreach (unserialize(DON_CURRENCIES) as $don_curr_key => $don_curr_value) {
                if (get_post_meta($post->ID, 'donation_currency', true) == $don_curr_key) {
                    echo $don_curr_value[1];
                }
            }
            echo don_format_price(get_post_meta($post->ID, 'donation_amount', true));
            break;
        case "status":
            echo htmlentities(get_post_meta($post->ID, 'donation_status', true));
            break;
        case "email":
            echo htmlentities(get_post_meta($post->ID, 'donor_email', true));
            break;
    }
}
function don_options_stats_page()
{
    // Date vars
    if (isset($_POST['don-month'])) {
        $don_month = $_POST['don-month'];
    } else {
        $don_month = date('m') * 1;
    }
    if (isset($_POST['don-year'])) {
        $don_year = $_POST['don-year'];
    } else {
        $don_year = date('Y') * 1;
    }
    // Total donations
    $don_meta_query = new WP_Query('post_type=donation&meta_value=Completed&year=' . $don_year . '&monthnum=' . $don_month);
    $don_total_donations = $don_meta_query->found_posts;
    wp_reset_postdata();
    // Get total donation amounts
    $don_donations = get_posts('meta_value=Completed&posts_per_page=9999999&post_type=donation&year=' . $don_year . '&monthnum=' . $don_month);
    if (count($don_donations)) {
        $don_totals = array();
        foreach (unserialize(DON_CURRENCIES) as $don_curr_key => $don_curr_value) {
            $don_totals[$don_curr_key] = 0;
        }
        foreach ($don_donations as $don_donation) {
            foreach (unserialize(DON_CURRENCIES) as $don_curr_key => $don_curr_value) {
                if (get_post_meta($don_donation->ID, 'donation_currency', true) == $don_curr_key) {
                    $don_totals[$don_curr_key] += (int) get_post_meta($don_donation->ID, 'donation_amount', true);
                }
            }
        }
    }
    ?>

<div class="wrap">

	<?php 
    echo $successful;
    ?>

	<h2><?php 
    _e('Stats', 'donator');
    ?>
</h2>

	<form action="#" method="post">

		<table class="form-table">
			
			<tr valign="top">
				<th scope="row"><label><?php 
    _e('Month/Year', 'donator');
    ?>
</label></th>
				<td>
					<select name="don-month">
						<option value="1" <?php 
    echo $_POST['don-month'] == '1' ? 'selected="selected"' : (date('m') * 1 == 1 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('January', 'donator');
    ?>
</option>
						<option value="2" <?php 
    echo $_POST['don-month'] == '2' ? 'selected="selected"' : (date('m') * 1 == 2 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('February', 'donator');
    ?>
</option>
						<option value="3" <?php 
    echo $_POST['don-month'] == '3' ? 'selected="selected"' : (date('m') * 1 == 3 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('March', 'donator');
    ?>
</option>
						<option value="4" <?php 
    echo $_POST['don-month'] == '4' ? 'selected="selected"' : (date('m') * 1 == 4 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('April', 'donator');
    ?>
</option>
						<option value="5" <?php 
    echo $_POST['don-month'] == '5' ? 'selected="selected"' : (date('m') * 1 == 5 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('May', 'donator');
    ?>
</option>
						<option value="6" <?php 
    echo $_POST['don-month'] == '6' ? 'selected="selected"' : (date('m') * 1 == 6 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('June', 'donator');
    ?>
</option>
						<option value="7" <?php 
    echo $_POST['don-month'] == '7' ? 'selected="selected"' : (date('m') * 1 == 7 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('July', 'donator');
    ?>
</option>
						<option value="8" <?php 
    echo $_POST['don-month'] == '8' ? 'selected="selected"' : (date('m') * 1 == 8 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('August', 'donator');
    ?>
</option>
						<option value="9" <?php 
    echo $_POST['don-month'] == '9' ? 'selected="selected"' : (date('m') * 1 == 9 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('September', 'donator');
    ?>
</option>
						<option value="10" <?php 
    echo $_POST['don-month'] == '10' ? 'selected="selected"' : (date('m') * 1 == 10 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('October', 'donator');
    ?>
</option>
						<option value="11" <?php 
    echo $_POST['don-month'] == '11' ? 'selected="selected"' : (date('m') * 1 == 11 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('November', 'donator');
    ?>
</option>
						<option value="12" <?php 
    echo $_POST['don-month'] == '12' ? 'selected="selected"' : (date('m') * 1 == 12 && !isset($_POST['don-month']) ? 'selected="selected"' : '');
    ?>
><?php 
    _e('December', 'donator');
    ?>
</option>
					</select>&nbsp;<select name="don-year">
						<option value="<?php 
    echo date('Y') - 1;
    ?>
" <?php 
    echo $_POST['don-year'] == date('Y') - 1 ? 'selected="selected"' : '';
    ?>
><?php 
    echo date('Y') - 1;
    ?>
</option>
						<option value="<?php 
    echo date('Y');
    ?>
" <?php 
    echo $_POST['don-year'] == date('Y') ? 'selected="selected"' : (!isset($_POST['don-year']) ? 'selected="selected"' : '');
    ?>
><?php 
    echo date('Y');
    ?>
</option>
						<option value="<?php 
    echo date('Y') + 1;
    ?>
" <?php 
    echo $_POST['don-year'] == date('Y') + 1 ? 'selected="selected"' : '';
    ?>
><?php 
    echo date('Y') + 1;
    ?>
</option>
					</select>&nbsp;<input type="submit" value="Submit" class="button button-primary" id="submit" name="submit">
				</td>
			</tr>

			<?php 
    foreach (unserialize(DON_CURRENCIES) as $don_curr_key => $don_curr_value) {
        ?>
				<tr valign="top">
					<th scope="row"><label><?php 
        echo sprintf(__('Total %s', 'donator'), $don_curr_key);
        ?>
</label></th>
					<td><?php 
        echo don_format_price($don_totals[$don_curr_key]);
        ?>
</td>
				</tr>
				<?php 
    }
    ?>

			<tr valign="top">
				<th scope="row"><label><?php 
    _e('Total donations', 'donator');
    ?>
</label></th>
				<td><?php 
    echo $don_total_donations;
    ?>
</td>
			</tr>

			<tr valign="top">
				<th scope="row"><label><?php 
    _e('Export donations', 'donator');
    ?>
</label></th>
				<td><a href="<?php 
    echo plugins_url('donator-export.php?export=1&amp;month=' . $don_month . '&amp;year=' . $don_year, __FILE__);
    ?>
" class="button button-primary"><?php 
    _e('Export', 'donator');
    ?>
</a></td>
			</tr>

			<tr valign="top">
				<th scope="row"><label><?php 
    _e('Export gift aid donations', 'donator');
    ?>
</label></th>
				<td><a href="<?php 
    echo plugins_url('donator-export-ga.php?export=1&amp;month=' . $don_month . '&amp;year=' . $don_year, __FILE__);
    ?>
" class="button button-primary"><?php 
    _e('Export', 'donator');
    ?>
</a></td>
			</tr>

	</form>

</div>

<?php 
}
function don_donate_process()
{
    if (isset($_POST['don-submit'])) {
        $don_donate_error = false;
        $don_currency = trim($_POST['don-currency']);
        $don_fixedamount = (int) $_POST['don-fixed-amount'];
        $don_amount = (int) $_POST['don-amount'];
        $don_occurrence = trim($_POST['don-occurrence']);
        if (isset($_POST['don-type'])) {
            $don_type = (int) $_POST['don-type'];
        } else {
            $don_type = 0;
        }
        $don_title = trim($_POST['don-title']);
        $don_firstname = trim(preg_replace('/\\PL/u', '', $_POST['don-firstname']));
        $don_lastname = trim(preg_replace('/\\PL/u', '', $_POST['don-lastname']));
        $don_houseno = trim($_POST['don-houseno']);
        $don_street = trim($_POST['don-street']);
        $don_town = trim($_POST['don-town']);
        $don_city = trim($_POST['don-city']);
        $don_postcode = trim($_POST['don-postcode']);
        $don_country = trim($_POST['don-country']);
        $don_email = trim($_POST['don-email']);
        $don_giftaid = trim($_POST['don-giftaid']);
        // Validation
        if (strlen($don_title) < 10 && strlen($don_firstname) && strlen($don_lastname) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$/', $don_email) && strlen($don_houseno) && strlen($don_street) && strlen($don_town) && strlen($don_city) && strlen($don_postcode) && strlen($don_country) == 2) {
            // Amount
            if (is_numeric($don_amount)) {
                if ($don_amount > 0) {
                    $donate_amount = $don_amount;
                } else {
                    if (is_numeric($don_fixedamount)) {
                        if ($don_fixedamount > 0) {
                            $donate_amount = $don_fixedamount;
                        } else {
                            $don_donate_error_msg = __('Please enter a correct donation amount', 'donator');
                            $don_donate_error = true;
                        }
                    } else {
                        $don_donate_error_msg = __('Please enter a correct donation amount', 'donator');
                        $don_donate_error = true;
                    }
                }
            }
            // Currency
            if (in_array($don_currency, unserialize(DON_CURRENCIES_SINGLE))) {
                $donate_currency = $don_currency;
            } else {
                $donate_currency = 'GBP';
            }
            // Occurrence
            if (in_array($don_occurrence, array('monthly', 'oneoff'))) {
                $donate_occurrence = $don_occurrence;
            } else {
                $donate_occurrence = 'monthly';
            }
            // Check type
            if ($don_type == 0) {
                // Do nothing
            } else {
                if (!get_term($don_type, 'donation-type')) {
                    $don_type = 0;
                }
            }
            // Gift aid
            if ($don_giftaid == 'true') {
                $donate_giftaid = 'true';
            } else {
                $donate_giftaid = 'false';
            }
            // Process donation
            if (!$don_donate_error) {
                // Donor details
                $donor_title = $don_title;
                $donor_firstname = $don_firstname;
                $donor_lastname = $don_lastname;
                $donor_houseno = $don_houseno;
                $donor_street = $don_street;
                $donor_town = $don_town;
                $donor_city = $don_city;
                $donor_postcode = $don_postcode;
                $donor_country = $don_country;
                $donor_email = $don_email;
                // Create new donation post
                $don_donation_details = array('post_title' => ($donate_occurrence == 'monthly' ? __('Monthly', 'donator') : __('One-off', 'donator')) . ' - ' . $donor_firstname . ' ' . $donor_lastname, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'donation');
                if ($don_type == 0) {
                    // Don't do anything
                } else {
                    $don_donation_details['tax_input'] = array($don_type);
                }
                $don_donation_id = wp_insert_post($don_donation_details);
                // Save details in post
                update_post_meta($don_donation_id, 'donation_status', 'Incomplete');
                update_post_meta($don_donation_id, 'donation_currency', $donate_currency);
                update_post_meta($don_donation_id, 'donation_amount', $donate_amount);
                update_post_meta($don_donation_id, 'donation_occurrence', $donate_occurrence);
                update_post_meta($don_donation_id, 'donation_giftaid', $donate_giftaid);
                update_post_meta($don_donation_id, 'donor_title', $donor_title);
                update_post_meta($don_donation_id, 'donor_firstname', $donor_firstname);
                update_post_meta($don_donation_id, 'donor_lastname', $donor_lastname);
                update_post_meta($don_donation_id, 'donor_houseno', $donor_houseno);
                update_post_meta($don_donation_id, 'donor_street', $donor_street);
                update_post_meta($don_donation_id, 'donor_town', $donor_town);
                update_post_meta($don_donation_id, 'donor_city', $donor_city);
                update_post_meta($don_donation_id, 'donor_postcode', $donor_postcode);
                update_post_meta($don_donation_id, 'donor_country', $donor_country);
                update_post_meta($don_donation_id, 'donor_email', $donor_email);
                // Send to paypal
                if (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$/', get_option('don-d-paypal-email'))) {
                    // Paypal setup
                    $paypal_email = trim(get_option('don-d-paypal-email'));
                    $paypal_return_url = get_page((int) get_option('don-d-paypal-confirm-page'));
                    $paypal_return_url = get_permalink($paypal_return_url->ID);
                    $paypal_cancel_url = get_page((int) get_option('don-d-paypal-cancel-page'));
                    $paypal_cancel_url = get_permalink($paypal_cancel_url->ID);
                    $paypal_notify_url = plugins_url('donator-ipn.php', __FILE__);
                    // Variables
                    $paypal_name = get_bloginfo('name') . ': ' . __('Donation', 'donator');
                    $paypal_amount = don_format_price($donate_amount);
                    $paypal_id = 'DON' . str_pad($don_donation_id, 8, '0', STR_PAD_LEFT);
                    $paypal_custom = $don_donation_id;
                    // Build query string
                    $paypal_querystring = array('business' => $paypal_email, 'return' => $paypal_return_url, 'cancel_return' => $paypal_cancel_url, 'notify_url' => $paypal_notify_url, 'item_name' => $paypal_name, 'item_number' => $paypal_id, 'custom' => $paypal_custom, 'currency_code' => $donate_currency, 'no_note' => 1, 'bn' => get_bloginfo('name') . '_Donation_Website', 'payer_email' => $don_email, 'email' => $don_email, 'first_name' => $don_firstname, 'last_name' => $don_lastname, 'city' => $don_city, 'address1' => $don_houseno . ' ' . $don_street, 'country' => $don_country);
                    if ($donate_occurrence == 'monthly') {
                        $paypal_querystring['a3'] = $paypal_amount;
                        $paypal_querystring['p3'] = 1;
                        $paypal_querystring['t3'] = 'M';
                        $paypal_querystring['src'] = 1;
                        $paypal_querystring['sra'] = 1;
                        $paypal_querystring['cmd'] = '_xclick-subscriptions';
                    } else {
                        $paypal_querystring['cmd'] = '_donations';
                        $paypal_querystring['amount'] = $paypal_amount;
                    }
                    $paypal_querystring = http_build_query($paypal_querystring);
                    // Send to Paypal
                    if (get_option('don-d-paypal-status') == 'live') {
                        header('Location: https://www.paypal.com/cgi-bin/webscr?' . $paypal_querystring);
                        exit;
                    } else {
                        header('Location: https://www.sandbox.paypal.com/cgi-bin/webscr?' . $paypal_querystring);
                        exit;
                    }
                }
            } else {
                $don_donate_error_msg = __('Please fill in the form correctly', 'donator');
                $don_donate_error = true;
            }
        } else {
            $don_donate_error_msg = __('Please fill in the form correctly', 'donator');
            $don_donate_error = true;
        }
        if ($don_donate_error) {
            session_start();
            $_SESSION['don_donate_error_msg'] = $don_donate_error_msg;
        }
    }
}