| 1 |
<?php |
| 2 |
/** |
| 3 |
* Recount all donor stats. |
| 4 |
* |
| 5 |
* This class handles batch processing of recounting all donor stats. |
| 6 |
* |
| 7 |
* @subpackage Admin/Tools/Give_Tools_Recount_Donor_Stats |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.5 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Give_Tools_Recount_Donor_Stats Class |
| 19 |
* |
| 20 |
* @since 1.5 |
| 21 |
*/ |
| 22 |
class Give_Tools_Recount_Donor_Stats extends Give_Batch_Export { |
| 23 |
|
| 24 |
/** |
| 25 |
* Our export type. Used for export-type specific filters/actions. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
* @since 1.5 |
| 29 |
*/ |
| 30 |
public $export_type = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Allows for a non-form batch processing to be run. |
| 34 |
* |
| 35 |
* @since 1.5 |
| 36 |
* @var boolean |
| 37 |
*/ |
| 38 |
public $is_void = true; |
| 39 |
|
| 40 |
/** |
| 41 |
* Sets the number of items to pull on each step |
| 42 |
* |
| 43 |
* @since 1.5 |
| 44 |
* @var integer |
| 45 |
*/ |
| 46 |
public $per_step = 5; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor. |
| 50 |
*/ |
| 51 |
public function __construct( $_step = 1 ) { |
| 52 |
parent::__construct( $_step ); |
| 53 |
|
| 54 |
$this->is_writable = true; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the Export Data |
| 59 |
* |
| 60 |
* @access public |
| 61 |
* @since 1.5 |
| 62 |
* |
| 63 |
* @return array|bool $data The data for the CSV file |
| 64 |
*/ |
| 65 |
public function get_data() { |
| 66 |
|
| 67 |
$args = array( |
| 68 |
'number' => $this->per_step, |
| 69 |
'offset' => $this->per_step * ( $this->step - 1 ), |
| 70 |
'orderby' => 'id', |
| 71 |
'order' => 'DESC', |
| 72 |
); |
| 73 |
|
| 74 |
$donors = Give()->donors->get_donors( $args ); |
| 75 |
|
| 76 |
if ( $donors ) { |
| 77 |
|
| 78 |
$allowed_payment_status = apply_filters( 'give_recount_donors_donation_statuses', give_get_payment_status_keys() ); |
| 79 |
|
| 80 |
foreach ( $donors as $donor ) { |
| 81 |
|
| 82 |
$attached_payment_ids = explode( ',', $donor->payment_ids ); |
| 83 |
|
| 84 |
$attached_args = array( |
| 85 |
'post__in' => $attached_payment_ids, |
| 86 |
'number' => - 1, |
| 87 |
'status' => $allowed_payment_status, |
| 88 |
); |
| 89 |
|
| 90 |
$attached_payments = (array) give_get_payments( $attached_args ); |
| 91 |
|
| 92 |
$unattached_args = array( |
| 93 |
'post__not_in' => $attached_payment_ids, |
| 94 |
'number' => - 1, |
| 95 |
'status' => $allowed_payment_status, |
| 96 |
'meta_query' => array( |
| 97 |
array( |
| 98 |
'key' => '_give_payment_donor_email', |
| 99 |
'value' => $donor->email, |
| 100 |
'compare' => '=', |
| 101 |
), |
| 102 |
), |
| 103 |
); |
| 104 |
|
| 105 |
$unattached_payments = give_get_payments( $unattached_args ); |
| 106 |
|
| 107 |
$payments = array_merge( $attached_payments, $unattached_payments ); |
| 108 |
|
| 109 |
$purchase_value = 0.00; |
| 110 |
$purchase_count = 0; |
| 111 |
$payment_ids = array(); |
| 112 |
|
| 113 |
if ( $payments ) { |
| 114 |
|
| 115 |
foreach ( $payments as $payment ) { |
| 116 |
|
| 117 |
$should_process_payment = 'publish' == $payment->post_status ? true : false; |
| 118 |
$should_process_payment = apply_filters( 'give_donor_recount_should_process_donation', $should_process_payment, $payment ); |
| 119 |
|
| 120 |
if ( true === $should_process_payment ) { |
| 121 |
|
| 122 |
if ( apply_filters( 'give_donor_recount_should_increase_value', true, $payment ) ) { |
| 123 |
$purchase_value += (float) give_donation_amount( $payment->ID, array( 'type' => 'stats' ) ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( apply_filters( 'give_donor_recount_should_increase_count', true, $payment ) ) { |
| 127 |
$purchase_count ++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$payment_ids[] = $payment->ID; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$payment_ids = implode( ',', $payment_ids ); |
| 136 |
|
| 137 |
$donor_update_data = array( |
| 138 |
'purchase_count' => $purchase_count, |
| 139 |
'purchase_value' => $purchase_value, |
| 140 |
'payment_ids' => $payment_ids, |
| 141 |
); |
| 142 |
|
| 143 |
$donor_instance = new Give_Donor( $donor->id ); |
| 144 |
$donor_instance->update( $donor_update_data ); |
| 145 |
|
| 146 |
}// End foreach(). |
| 147 |
|
| 148 |
return true; |
| 149 |
}// End if(). |
| 150 |
|
| 151 |
return false; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Return the calculated completion percentage |
| 157 |
* |
| 158 |
* @since 1.5 |
| 159 |
* @return int |
| 160 |
*/ |
| 161 |
public function get_percentage_complete() { |
| 162 |
|
| 163 |
$args = array( |
| 164 |
'number' => - 1, |
| 165 |
'orderby' => 'id', |
| 166 |
'order' => 'DESC', |
| 167 |
); |
| 168 |
|
| 169 |
$donors = Give()->donors->get_donors( $args ); |
| 170 |
$total = count( $donors ); |
| 171 |
|
| 172 |
$percentage = 100; |
| 173 |
|
| 174 |
if ( $total > 0 ) { |
| 175 |
$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $percentage > 100 ) { |
| 179 |
$percentage = 100; |
| 180 |
} |
| 181 |
|
| 182 |
return $percentage; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Set the properties specific to the payments export |
| 187 |
* |
| 188 |
* @since 1.5 |
| 189 |
* |
| 190 |
* @param array $request The Form Data passed into the batch processing |
| 191 |
*/ |
| 192 |
public function set_properties( $request ) { |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Process a step |
| 197 |
* |
| 198 |
* @since 1.5 |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
public function process_step() { |
| 202 |
|
| 203 |
if ( ! $this->can_export() ) { |
| 204 |
wp_die( |
| 205 |
esc_html__( 'You do not have permission to recount stats.', 'give' ), |
| 206 |
esc_html__( 'Error', 'give' ), |
| 207 |
array( |
| 208 |
'response' => 403, |
| 209 |
) |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
$had_data = $this->get_data(); |
| 214 |
|
| 215 |
if ( $had_data ) { |
| 216 |
$this->done = false; |
| 217 |
|
| 218 |
return true; |
| 219 |
} else { |
| 220 |
$this->done = true; |
| 221 |
$this->message = esc_html__( 'Donor stats have been successfully recounted.', 'give' ); |
| 222 |
|
| 223 |
return false; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Headers |
| 229 |
*/ |
| 230 |
public function headers() { |
| 231 |
give_ignore_user_abort(); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Perform the export |
| 236 |
* |
| 237 |
* @access public |
| 238 |
* @since 1.5 |
| 239 |
* @return void |
| 240 |
*/ |
| 241 |
public function export() { |
| 242 |
|
| 243 |
// Set headers |
| 244 |
$this->headers(); |
| 245 |
|
| 246 |
give_die(); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|