-
Notifications
You must be signed in to change notification settings - Fork 475
/
class-edd-fees.php
executable file
·342 lines (279 loc) · 8.16 KB
/
class-edd-fees.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Fees
*
* This class is for adding arbitrary fees to the cart. Fees can be positive or negative (discounts)
*
* @package EDD
* @subpackage Classes/Fees
* @copyright Copyright (c) 2015, Pippin Williamson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.5
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* EDD_Fees Class
*
* @since 1.5
*/
class EDD_Fees {
/**
* Setup the EDD Fees
*
* @since 1.5
*/
public function __construct() {
add_filter( 'edd_payment_meta', array( $this, 'record_fees' ), 10, 2 );
}
/**
* Adds a new Fee
*
* @since 1.5
*
* @param array $args Fee arguments
*
* @uses EDD_Fees::get_fees()
* @uses EDD_Session::set()
*
* @return array The fees.
*/
public function add_fee( $args = array() ) {
// Backwards compatibility with pre 2.0
if ( func_num_args() > 1 ) {
$args = func_get_args();
$amount = $args[0];
$label = isset( $args[1] ) ? $args[1] : '';
$id = isset( $args[2] ) ? $args[2] : '';
$type = 'fee';
$args = array(
'amount' => $amount,
'label' => $label,
'id' => $id,
'type' => $type,
'no_tax' => false,
'download_id' => 0,
'price_id' => NULL
);
} else {
$defaults = array(
'amount' => 0,
'label' => '',
'id' => '',
'no_tax' => false,
'type' => 'fee',
'download_id' => 0,
'price_id' => NULL
);
$args = wp_parse_args( $args, $defaults );
if( $args['type'] != 'fee' && $args['type'] != 'item' ) {
$args['type'] = 'fee';
}
}
// If the fee is for an "item" but we passed in a download id
if( 'item' === $args['type'] && ! empty( $args['download_id'] ) ) {
unset( $args['download_id'] );
unset( $args['price_id'] );
}
if ( ! empty( $args['download_id'] ) ) {
$options = isset( $args['price_id'] ) ? array( 'price_id' => $args['price_id'] ) : array();
if ( ! edd_item_in_cart( $args['download_id'], $options ) ) {
return false;
}
}
$fees = $this->get_fees( 'all' );
// Determine the key
$key = empty( $args['id'] ) ? sanitize_key( $args['label'] ) : sanitize_key( $args['id'] );
// Remove the unneeded id key
unset( $args['id'] );
// Sanitize the amount
$args['amount'] = edd_sanitize_amount( $args['amount'] );
// Force the amount to have the proper number of decimal places.
$args['amount'] = number_format( (float) $args['amount'], edd_currency_decimal_filter(), '.', '' );
// Force no_tax to true if the amount is negative
if( $args['amount'] < 0 ) {
$args['no_tax'] = true;
}
// Set the fee
$fees[ $key ] = apply_filters( 'edd_fees_add_fee', $args, $this );
// Allow 3rd parties to process the fees before storing them in the session
$fees = apply_filters( 'edd_fees_set_fees', $fees, $this );
// Update fees
EDD()->session->set( 'edd_cart_fees', $fees );
do_action( 'edd_post_add_fee', $fees, $key, $args );
return $fees;
}
/**
* Remove an Existing Fee
*
* @since 1.5
* @param string $id Fee ID
* @uses EDD_Fees::get_fees()
* @uses EDD_Session::set()
* @return array Remaining fees
*/
public function remove_fee( $id = '' ) {
$fees = $this->get_fees( 'all' );
if ( isset( $fees[ $id ] ) ) {
unset( $fees[ $id ] );
EDD()->session->set( 'edd_cart_fees', $fees );
do_action( 'edd_post_remove_fee', $fees, $id );
}
return $fees;
}
/**
* Check if any fees are present
*
* @since 1.5
* @param string $type Fee type, "fee" or "item"
* @uses EDD_Fees::get_fees()
* @return bool True if there are fees, false otherwise
*/
public function has_fees( $type = 'fee' ) {
if( 'all' == $type || 'fee' == $type ) {
if( ! edd_get_cart_contents() ) {
$type = 'item';
}
}
$fees = $this->get_fees( $type );
return ! empty( $fees ) && is_array( $fees );
}
/**
* Retrieve all active fees
*
* @since 1.5
* @param string $type Fee type, "fee" or "item"
* @param int $download_id The download ID whose fees to retrieve
* @param int $price_id The variable price ID whose fees to retrieve
* @uses EDD_Session::get()
* @return array|bool List of fees when available, false when there are no fees
*/
public function get_fees( $type = 'fee', $download_id = 0, $price_id = NULL ) {
$fees = EDD()->session->get( 'edd_cart_fees' );
if ( EDD()->cart->is_empty() ) {
// We can only get item type fees when the cart is empty
$type = 'item';
}
if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) {
foreach ( $fees as $key => $fee ) {
if ( ! empty( $fee['type'] ) && $type != $fee['type'] ) {
unset( $fees[ $key ] );
}
}
}
if ( ! empty( $fees ) && ! empty( $download_id ) ) {
// Remove fees that don't belong to the specified Download
$applied_fees = array();
foreach ( $fees as $key => $fee ) {
if ( empty( $fee['download_id'] ) || (int) $download_id !== (int) $fee['download_id'] ) {
unset( $fees[ $key ] );
}
$fee_hash = md5( $fee['amount'] . $fee['label'] . $fee['type'] );
if ( in_array( $fee_hash, $applied_fees ) ) {
unset( $fees[ $key ] );
}
$applied_fees[] = $fee_hash;
}
}
// Now that we've removed any fees that are for other Downloads, lets also remove any fees that don't match this price id
if ( ! empty( $fees ) && ! empty( $download_id ) && ! is_null( $price_id ) ) {
// Remove fees that don't belong to the specified Download AND Price ID
foreach ( $fees as $key => $fee ) {
if ( is_null( $fee['price_id'] ) ) {
continue;
}
if ( (int) $price_id !== (int) $fee['price_id'] ){
unset( $fees[ $key ] );
}
}
}
if ( ! empty( $fees ) ) {
// Remove fees that belong to a specific download but are not in the cart
foreach ( $fees as $key => $fee ) {
if ( empty( $fee['download_id'] ) ) {
continue;
}
if ( ! edd_item_in_cart( $fee['download_id'] ) ) {
unset( $fees[ $key ] );
}
}
}
// Allow 3rd parties to process the fees before returning them
return apply_filters( 'edd_fees_get_fees', ! empty( $fees ) ? $fees : array(), $this );
}
/**
* Retrieve a specific fee
*
* @since 1.5
*
* @param string $id ID of the fee to get
* @return array|bool The fee array when available, false otherwise
*/
public function get_fee( $id = '' ) {
$fees = $this->get_fees( 'all' );
if ( ! isset( $fees[ $id ] ) )
return false;
return $fees[ $id ];
}
/**
* Calculate the total fee amount for a specific fee type
*
* Can be negative
*
* @since 2.0
* @param string $type Fee type, "fee" or "item"
* @uses EDD_Fees::get_fees()
* @uses EDD_Fees::has_fees()
* @return float Total fee amount
*/
public function type_total( $type = 'fee' ) {
$fees = $this->get_fees( $type );
$total = (float) 0.00;
if ( $this->has_fees( $type ) ) {
foreach ( $fees as $fee ) {
$total += edd_sanitize_amount( $fee['amount'] );
}
}
return edd_sanitize_amount( $total );
}
/**
* Calculate the total fee amount
*
* Can be negative
*
* @since 1.5
* @uses EDD_Fees::get_fees()
* @uses EDD_Fees::has_fees()
* @param int $download_id The download ID whose fees to retrieve
* @return float Total fee amount
*/
public function total( $download_id = 0 ) {
$fees = $this->get_fees( 'all', $download_id );
$total = (float) 0.00;
if ( $this->has_fees( 'all' ) ) {
foreach ( $fees as $fee ) {
$total += edd_sanitize_amount( $fee['amount'] );
}
}
return edd_sanitize_amount( $total );
}
/**
* Stores the fees in the payment meta
*
* @since 1.5
* @uses EDD_Session::set()
* @param array $payment_meta The meta data to store with the payment
* @param array $payment_data The info sent from process-purchase.php
* @return array Return the payment meta with the fees added
*/
public function record_fees( $payment_meta, $payment_data ) {
if ( $this->has_fees( 'all' ) ) {
$payment_meta['fees'] = $this->get_fees( 'all' );
// Only clear fees from session when status is not pending
if( ! empty( $payment_data['status'] ) && 'pending' !== strtolower( $payment_data['status'] ) ) {
EDD()->session->set( 'edd_cart_fees', null );
}
}
return $payment_meta;
}
}