1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Give Form Functions |
4
|
|
|
* |
5
|
|
|
* @package WordImpress |
6
|
|
|
* @subpackage Includes/Forms |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.1 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Filter: Do not show the Give shortcut button on Give Forms CPT |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
function give_shortcode_button_condition() { |
23
|
|
|
|
24
|
|
|
global $typenow; |
25
|
|
|
|
26
|
|
|
if ( $typenow != 'give_forms' ) { |
|
|
|
|
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
add_filter( 'give_shortcode_button_condition', 'give_shortcode_button_condition' ); |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the form ID from the form $args |
38
|
|
|
* |
39
|
|
|
* @param array $args |
40
|
|
|
* |
41
|
|
|
* @return int|false |
42
|
|
|
*/ |
43
|
|
|
function get_form_id_from_args( $args ) { |
44
|
|
|
|
45
|
|
|
if ( isset( $args['form_id'] ) && $args['form_id'] != 0 ) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
return intval( $args['form_id'] ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Checks whether floating labels is enabled for the form ID in $args |
55
|
|
|
* |
56
|
|
|
* @since 1.1 |
57
|
|
|
* |
58
|
|
|
* @param array $args |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
function give_is_float_labels_enabled( $args ) { |
63
|
|
|
|
64
|
1 |
|
$float_labels = ''; |
65
|
|
|
|
66
|
1 |
|
if ( ! empty( $args['float_labels'] ) ) { |
67
|
|
|
$float_labels = $args['float_labels']; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ( empty( $float_labels ) ) { |
71
|
1 |
|
$float_labels = give_get_meta( $args['form_id'], '_give_form_floating_labels', true ); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
if ( empty( $float_labels ) || ( 'global' === $float_labels ) ) { |
75
|
1 |
|
$float_labels = give_get_option( 'floatlabels', 'disabled' ); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
return give_is_setting_enabled( $float_labels ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Determines if a user can checkout or not |
83
|
|
|
* |
84
|
|
|
* Allows themes and plugins to set donation checkout conditions |
85
|
|
|
* |
86
|
|
|
* @since 1.0 |
87
|
|
|
* |
88
|
|
|
* @return bool Can user checkout? |
89
|
|
|
*/ |
90
|
|
|
function give_can_checkout() { |
91
|
1 |
|
|
92
|
|
|
$can_checkout = true; |
93
|
1 |
|
|
94
|
|
|
return (bool) apply_filters( 'give_can_checkout', $can_checkout ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieve the Success page URI |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* @since 1.0 |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
function give_get_success_page_uri() { |
|
|
|
|
106
|
|
|
$give_options = give_get_settings(); |
107
|
|
|
|
108
|
|
|
$success_page = isset( $give_options['success_page'] ) ? get_permalink( absint( $give_options['success_page'] ) ) : get_bloginfo( 'url' ); |
109
|
|
|
|
110
|
|
|
return apply_filters( 'give_get_success_page_uri', $success_page ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Determines if we're currently on the Success page. |
115
|
|
|
* |
116
|
|
|
* @since 1.0 |
117
|
|
|
* |
118
|
|
|
* @return bool True if on the Success page, false otherwise. |
119
|
|
|
*/ |
120
|
|
|
function give_is_success_page() { |
121
|
|
|
$give_options = give_get_settings(); |
122
|
|
|
$is_success_page = isset( $give_options['success_page'] ) ? is_page( $give_options['success_page'] ) : false; |
123
|
|
|
|
124
|
|
|
return apply_filters( 'give_is_success_page', $is_success_page ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Send To Success Page |
129
|
|
|
* |
130
|
|
|
* Sends the user to the success page. |
131
|
|
|
* |
132
|
|
|
* @param string $query_string |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
* @since 1.0 |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
function give_send_to_success_page( $query_string = null ) { |
139
|
|
|
|
140
|
|
|
$redirect = give_get_success_page_uri(); |
141
|
|
|
|
142
|
|
|
if ( $query_string ) { |
|
|
|
|
143
|
|
|
$redirect .= $query_string; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$gateway = isset( $_REQUEST['give-gateway'] ) ? $_REQUEST['give-gateway'] : ''; |
|
|
|
|
147
|
|
|
|
148
|
|
|
wp_redirect( apply_filters( 'give_success_page_redirect', $redirect, $gateway, $query_string ) ); |
149
|
|
|
give_die(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Send back to donation form. |
155
|
|
|
* |
156
|
|
|
* Used to redirect a user back to the donation form if there are errors present. |
157
|
|
|
* |
158
|
|
|
* @param array $args |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* @since 1.0 |
162
|
|
|
* @return Void |
163
|
|
|
*/ |
164
|
|
|
function give_send_back_to_checkout( $args = array() ) { |
165
|
|
|
|
166
|
|
|
$url = isset( $_POST['give-current-url'] ) ? sanitize_text_field( $_POST['give-current-url'] ) : ''; |
|
|
|
|
167
|
|
|
$form_id = 0; |
168
|
|
|
|
169
|
|
|
// Set the form_id. |
170
|
|
|
if ( isset( $_POST['give-form-id'] ) ) { |
171
|
|
|
$form_id = sanitize_text_field( $_POST['give-form-id'] ); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Need a URL to continue. If none, redirect back to single form. |
175
|
|
|
if ( empty( $url ) ) { |
176
|
|
|
wp_safe_redirect( get_permalink( $form_id ) ); |
177
|
|
|
give_die(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$defaults = array( |
181
|
|
|
'form-id' => (int) $form_id, |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
// Set the $level_id. |
185
|
|
|
if ( isset( $_POST['give-price-id'] ) ) { |
186
|
|
|
$defaults['level-id'] = sanitize_text_field( $_POST['give-price-id'] ); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Check for backward compatibility. |
190
|
|
|
if ( is_string( $args ) ) { |
191
|
|
|
$args = str_replace( '?', '', $args ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$args = wp_parse_args( $args, $defaults ); |
195
|
|
|
|
196
|
|
|
// Merge URL query with $args to maintain third-party URL parameters after redirect. |
197
|
|
|
$url_data = wp_parse_url( $url ); |
198
|
|
|
|
199
|
|
|
// Check if an array to prevent notices before parsing. |
200
|
|
|
if ( isset( $url_data['query'] ) && ! empty( $url_data['query'] ) ) { |
201
|
|
|
parse_str( $url_data['query'], $query ); |
202
|
|
|
|
203
|
|
|
// Precaution: don't allow any CC info. |
204
|
|
|
unset( $query['card_number'] ); |
205
|
|
|
unset( $query['card_cvc'] ); |
206
|
|
|
|
207
|
|
|
} else { |
208
|
|
|
// No $url_data so pass empty array. |
209
|
|
|
$query = array(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$new_query = array_merge( $args, $query ); |
213
|
|
|
$new_query_string = http_build_query( $new_query ); |
214
|
|
|
|
215
|
|
|
// Assemble URL parts. |
216
|
|
|
$redirect = home_url( '/' . $url_data['path'] . '?' . $new_query_string . '#give-form-' . $form_id . '-wrap' ); |
217
|
|
|
|
218
|
|
|
// Redirect them. |
219
|
|
|
wp_safe_redirect( apply_filters( 'give_send_back_to_checkout', $redirect, $args ) ); |
220
|
|
|
give_die(); |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get Success Page URL |
226
|
|
|
* |
227
|
|
|
* Gets the success page URL. |
228
|
|
|
* |
229
|
|
|
* @param string $query_string |
230
|
|
|
* |
231
|
|
|
* @access public |
232
|
|
|
* @since 1.0 |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
function give_get_success_page_url( $query_string = null ) { |
236
|
|
|
|
237
|
|
|
$success_page = give_get_option( 'success_page', 0 ); |
238
|
|
|
$success_page = get_permalink( $success_page ); |
239
|
|
|
|
240
|
|
|
if ( $query_string ) { |
|
|
|
|
241
|
|
|
$success_page .= $query_string; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return apply_filters( 'give_success_page_url', $success_page ); |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the URL of the Failed Donation Page. |
250
|
|
|
* |
251
|
|
|
* @since 1.0 |
252
|
|
|
* |
253
|
|
|
* @param bool $extras Extras to append to the URL. |
254
|
|
|
* |
255
|
|
|
* @return mixed Full URL to the Failed Donation Page, if present, home page if it doesn't exist. |
256
|
|
|
*/ |
257
|
|
|
function give_get_failed_transaction_uri( $extras = false ) { |
258
|
|
|
$give_options = give_get_settings(); |
259
|
|
|
|
260
|
|
|
// Remove question mark. |
261
|
|
|
if ( 0 === strpos( $extras, '?' ) ) { |
262
|
|
|
$extras = substr( $extras, 1 ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$extras_args = wp_parse_args( $extras ); |
266
|
|
|
|
267
|
|
|
// Set nonce if payment id exist in extra params. |
268
|
|
|
if ( array_key_exists( 'payment-id', $extras_args ) ) { |
269
|
|
|
$extras_args['_wpnonce'] = wp_create_nonce( "give-failed-donation-{$extras_args['payment-id']}" ); |
270
|
|
|
$extras = http_build_query( $extras_args ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$uri = ! empty( $give_options['failure_page'] ) ? |
274
|
|
|
trailingslashit( get_permalink( $give_options['failure_page'] ) ) : |
275
|
|
|
home_url(); |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
278
|
|
|
if ( $extras ) { |
279
|
|
|
$uri .= "?{$extras}"; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return apply_filters( 'give_get_failed_transaction_uri', $uri ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Determines if we're currently on the Failed Donation Page. |
287
|
|
|
* |
288
|
|
|
* @since 1.0 |
289
|
|
|
* @return bool True if on the Failed Donation Page, false otherwise. |
290
|
|
|
*/ |
291
|
|
|
function give_is_failed_transaction_page() { |
292
|
|
|
$give_options = give_get_settings(); |
293
|
|
|
$ret = isset( $give_options['failure_page'] ) ? is_page( $give_options['failure_page'] ) : false; |
294
|
|
|
|
295
|
|
|
return apply_filters( 'give_is_failure_page', $ret ); |
296
|
|
|
} |
297
|
|
|
|
298
|
42 |
|
/** |
299
|
|
|
* Mark payments as Failed when returning to the Failed Donation Page |
300
|
|
|
* |
301
|
42 |
|
* @since 1.0 |
302
|
42 |
|
* @since 1.8.16 Add security check |
303
|
42 |
|
* |
304
|
42 |
|
* @return bool |
305
|
42 |
|
*/ |
306
|
|
|
function give_listen_for_failed_payments() { |
307
|
|
|
|
308
|
42 |
|
$failed_page = give_get_option( 'failure_page', 0 ); |
309
|
|
|
$payment_id = ! empty( $_GET['payment-id'] ) ? absint( $_GET['payment-id'] ) : 0; |
|
|
|
|
310
|
42 |
|
$nonce = ! empty( $_GET['_wpnonce'] ) ? give_clean( $_GET['_wpnonce'] ) : false; |
|
|
|
|
311
|
|
|
|
312
|
42 |
|
// Bailout. |
313
|
42 |
|
if ( ! $failed_page || ! is_page( $failed_page ) || ! $payment_id || ! $nonce ) { |
314
|
|
|
return false; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// Security check. |
318
|
|
|
if ( ! wp_verify_nonce( $nonce, "give-failed-donation-{$payment_id}" ) ) { |
319
|
|
|
wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ) ); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// Set payment status to failure |
323
|
|
|
give_update_payment_status( $payment_id, 'failed' ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
add_action( 'template_redirect', 'give_listen_for_failed_payments' ); |
327
|
42 |
|
|
328
|
42 |
|
/** |
329
|
|
|
* Retrieve the Donation History page URI |
330
|
42 |
|
* |
331
|
|
|
* @access public |
332
|
|
|
* @since 1.7 |
333
|
|
|
* |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
function give_get_history_page_uri() { |
|
|
|
|
337
|
|
|
$give_options = give_get_settings(); |
338
|
|
|
|
339
|
|
|
$history_page = isset( $give_options['history_page'] ) ? get_permalink( absint( $give_options['history_page'] ) ) : get_bloginfo( 'url' ); |
340
|
|
|
|
341
|
|
|
return apply_filters( 'give_get_history_page_uri', $history_page ); |
342
|
|
|
} |
343
|
|
|
|
344
|
18 |
|
/** |
345
|
18 |
|
* Check if a field is required |
346
|
|
|
* |
347
|
18 |
|
* @param string $field |
348
|
|
|
* @param int $form_id |
349
|
|
|
* |
350
|
|
|
* @access public |
351
|
|
|
* @since 1.0 |
352
|
|
|
* @return bool |
353
|
|
|
*/ |
354
|
|
|
function give_field_is_required( $field = '', $form_id ) { |
355
|
|
|
|
356
|
|
|
$required_fields = give_get_required_fields( $form_id ); |
357
|
|
|
|
358
|
|
|
return array_key_exists( $field, $required_fields ); |
359
|
|
|
} |
360
|
|
|
|
361
|
42 |
|
/** |
362
|
|
|
* Record Donation In Log |
363
|
42 |
|
* |
364
|
|
|
* Stores log information for a donation. |
365
|
|
|
* |
366
|
|
|
* @since 1.0 |
367
|
|
|
* @global $give_logs Give_Logging |
368
|
|
|
* |
369
|
|
|
* @param int $give_form_id Give Form ID. |
370
|
|
|
* @param int $payment_id Payment ID. |
371
|
|
|
* @param bool|int $price_id Price ID, if any. |
372
|
|
|
* @param string|null $donation_date The date of the donation. |
373
|
|
|
* |
374
|
|
|
* @return void |
375
|
|
|
*/ |
376
|
|
|
function give_record_donation_in_log( $give_form_id = 0, $payment_id, $price_id = false, $donation_date = null ) { |
377
|
18 |
|
global $give_logs; |
378
|
|
|
|
379
|
18 |
|
$log_data = array( |
380
|
|
|
'post_parent' => $give_form_id, |
381
|
|
|
'log_type' => 'sale', |
382
|
|
|
'post_date' => isset( $donation_date ) ? $donation_date : null, |
383
|
|
|
'post_date_gmt' => isset( $donation_date ) ? $donation_date : null, |
384
|
|
|
); |
385
|
|
|
|
386
|
|
|
$log_meta = array( |
387
|
|
|
'payment_id' => $payment_id, |
388
|
|
|
'price_id' => (int) $price_id, |
389
|
|
|
); |
390
|
|
|
|
391
|
|
|
$give_logs->insert_log( $log_data, $log_meta ); |
392
|
|
|
} |
393
|
42 |
|
|
394
|
|
|
|
395
|
42 |
|
/** |
396
|
|
|
* Increases the donation total count of a donation form. |
397
|
|
|
* |
398
|
|
|
* @since 1.0 |
399
|
|
|
* |
400
|
|
|
* @param int $form_id Give Form ID |
401
|
|
|
* @param int $quantity Quantity to increase donation count by |
402
|
|
|
* |
403
|
|
|
* @return bool|int |
404
|
|
|
*/ |
405
|
|
|
function give_increase_donation_count( $form_id = 0, $quantity = 1 ) { |
406
|
|
|
$quantity = (int) $quantity; |
407
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
408
|
|
|
|
409
|
42 |
|
return $form->increase_sales( $quantity ); |
410
|
|
|
} |
411
|
42 |
|
|
412
|
|
|
/** |
413
|
|
|
* Decreases the sale count of a form. Primarily for when a donation is refunded. |
414
|
|
|
* |
415
|
|
|
* @since 1.0 |
416
|
|
|
* |
417
|
|
|
* @param int $form_id Give Form ID |
418
|
|
|
* @param int $quantity Quantity to increase donation count by |
419
|
|
|
* |
420
|
|
|
* @return bool|int |
421
|
|
|
*/ |
422
|
|
|
function give_decrease_donation_count( $form_id = 0, $quantity = 1 ) { |
423
|
|
|
$quantity = (int) $quantity; |
424
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
425
|
11 |
|
|
426
|
11 |
|
return $form->decrease_sales( $quantity ); |
427
|
|
|
} |
428
|
11 |
|
|
429
|
|
|
/** |
430
|
11 |
|
* Increases the total earnings of a form. |
431
|
|
|
* |
432
|
11 |
|
* @since 1.0 |
433
|
|
|
* |
434
|
|
|
* @param int $give_form_id Give Form ID |
435
|
|
|
* @param int $amount Earnings |
436
|
11 |
|
* |
437
|
|
|
* @return bool|int |
438
|
|
|
*/ |
439
|
|
|
function give_increase_earnings( $give_form_id = 0, $amount ) { |
440
|
|
|
$form = new Give_Donate_Form( $give_form_id ); |
|
|
|
|
441
|
|
|
|
442
|
|
|
return $form->increase_earnings( $amount ); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Decreases the total earnings of a form. |
447
|
|
|
* |
448
|
|
|
* Primarily for when a donation is refunded. |
449
|
|
|
* |
450
|
11 |
|
* @since 1.0 |
451
|
11 |
|
* |
452
|
|
|
* @param int $form_id Give Form ID |
453
|
11 |
|
* @param int $amount Earnings |
454
|
|
|
* |
455
|
11 |
|
* @return bool|int |
456
|
|
|
*/ |
457
|
11 |
|
function give_decrease_form_earnings( $form_id = 0, $amount ) { |
458
|
|
|
|
459
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
460
|
|
|
|
461
|
11 |
|
return $form->decrease_earnings( $amount ); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Returns the total earnings for a form. |
467
|
|
|
* |
468
|
|
|
* @since 1.0 |
469
|
|
|
* |
470
|
|
|
* @param int $form_id Give Form ID |
471
|
|
|
* |
472
|
|
|
* @return int $earnings Earnings for a certain form |
473
|
|
|
*/ |
474
|
|
|
function give_get_form_earnings_stats( $form_id = 0 ) { |
475
|
|
|
$give_form = new Give_Donate_Form( $form_id ); |
|
|
|
|
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Filter the form earnings |
479
|
|
|
* |
480
|
34 |
|
* @since 1.8.17 |
481
|
34 |
|
*/ |
482
|
|
|
return apply_filters( 'give_get_form_earnings_stats', $give_form->earnings, $form_id, $give_form ); |
483
|
34 |
|
} |
484
|
|
|
|
485
|
34 |
|
|
486
|
|
|
/** |
487
|
34 |
|
* Return the sales number for a form. |
488
|
34 |
|
* |
489
|
34 |
|
* @since 1.0 |
490
|
|
|
* |
491
|
34 |
|
* @param int $give_form_id Give Form ID |
492
|
|
|
* |
493
|
34 |
|
* @return int $sales Amount of sales for a certain form |
494
|
|
|
*/ |
495
|
|
|
function give_get_form_sales_stats( $give_form_id = 0 ) { |
496
|
34 |
|
$give_form = new Give_Donate_Form( $give_form_id ); |
|
|
|
|
497
|
|
|
|
498
|
|
|
return $give_form->sales; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Retrieves the average monthly sales for a specific donation form |
504
|
|
|
* |
505
|
|
|
* @since 1.0 |
506
|
|
|
* |
507
|
|
|
* @param int $form_id Form ID |
508
|
|
|
* |
509
|
|
|
* @return float $sales Average monthly sales |
510
|
|
|
*/ |
511
|
|
View Code Duplication |
function give_get_average_monthly_form_sales( $form_id = 0 ) { |
|
|
|
|
512
|
|
|
$sales = give_get_form_sales_stats( $form_id ); |
513
|
|
|
$release_date = get_post_field( 'post_date', $form_id ); |
514
|
|
|
|
515
|
|
|
$diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) ); |
516
|
|
|
|
517
|
|
|
$months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication |
518
|
|
|
|
519
|
|
|
if ( $months > 0 ) { |
520
|
|
|
$sales = ( $sales / $months ); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
return $sales; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Retrieves the average monthly earnings for a specific form |
529
|
|
|
* |
530
|
|
|
* @since 1.0 |
531
|
|
|
* |
532
|
|
|
* @param int $form_id Form ID |
533
|
|
|
* |
534
|
|
|
* @return float $earnings Average monthly earnings |
535
|
|
|
*/ |
536
|
|
View Code Duplication |
function give_get_average_monthly_form_earnings( $form_id = 0 ) { |
|
|
|
|
537
|
|
|
$earnings = give_get_form_earnings_stats( $form_id ); |
538
|
|
|
$release_date = get_post_field( 'post_date', $form_id ); |
539
|
|
|
|
540
|
|
|
$diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) ); |
541
|
|
|
|
542
|
|
|
$months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication |
543
|
|
|
|
544
|
|
|
if ( $months > 0 ) { |
545
|
|
|
$earnings = ( $earnings / $months ); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
return $earnings < 0 ? 0 : $earnings; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Get Price Option Name (Text) |
554
|
|
|
* |
555
|
|
|
* Retrieves the name of a variable price option. |
556
|
|
|
* |
557
|
|
|
* @since 1.0 |
558
|
|
|
* |
559
|
|
|
* @param int $form_id ID of the donation form. |
560
|
|
|
* @param int $price_id ID of the price option. |
561
|
|
|
* @param int $payment_id payment ID for use in filters ( optional ). |
562
|
|
|
* @param bool $use_fallback Outputsz the level amount if no level text is provided. |
563
|
|
|
* |
564
|
|
|
* @return string $price_name Name of the price option |
565
|
|
|
*/ |
566
|
|
|
function give_get_price_option_name( $form_id = 0, $price_id = 0, $payment_id = 0, $use_fallback = true ) { |
567
|
|
|
|
568
|
|
|
$prices = give_get_variable_prices( $form_id ); |
569
|
|
|
$price_name = ''; |
570
|
|
|
|
571
|
|
|
if ( false === $prices ) { |
572
|
|
|
return $price_name; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
foreach ( $prices as $price ) { |
576
|
|
|
|
577
|
|
|
if ( intval( $price['_give_id']['level_id'] ) == intval( $price_id ) ) { |
578
|
|
|
|
579
|
|
|
$price_text = isset( $price['_give_text'] ) ? $price['_give_text'] : ''; |
580
|
|
|
$price_fallback = $use_fallback ? give_currency_filter( give_format_amount( $price['_give_amount'], array( 'sanitize' => false ) ), '', true ) : ''; |
581
|
|
|
$price_name = ! empty( $price_text ) ? $price_text : $price_fallback; |
582
|
|
|
|
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
return apply_filters( 'give_get_price_option_name', $price_name, $form_id, $payment_id, $price_id ); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Retrieves a price from from low to high of a variable priced form |
592
|
|
|
* |
593
|
|
|
* @since 1.0 |
594
|
|
|
* |
595
|
|
|
* @param int $form_id ID of the form |
596
|
|
|
* @param bool $formatted Flag to decide which type of price range string return |
597
|
|
|
* |
598
|
|
|
* @return string $range A fully formatted price range |
599
|
|
|
*/ |
600
|
|
|
function give_price_range( $form_id = 0, $formatted = true ) { |
601
|
|
|
$low = give_get_lowest_price_option( $form_id ); |
602
|
|
|
$high = give_get_highest_price_option( $form_id ); |
603
|
|
|
$order_type = ! empty( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'asc'; |
|
|
|
|
604
|
|
|
|
605
|
|
|
$range = sprintf( |
606
|
|
|
'<span class="give_price_range_%1$s">%2$s</span><span class="give_price_range_sep"> – </span><span class="give_price_range_%3$s">%4$s</span>', |
607
|
|
|
'asc' === $order_type ? 'low' : 'high', |
608
|
|
|
'asc' === $order_type ? give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ), |
609
|
|
|
'asc' === $order_type ? 'high' : 'low', |
610
|
|
|
'asc' === $order_type ? give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) ) |
611
|
|
|
|
|
|
|
|
612
|
|
|
); |
613
|
|
|
|
614
|
|
|
if ( ! $formatted ) { |
615
|
|
|
$range = wp_strip_all_tags( $range ); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
return apply_filters( 'give_price_range', $range, $form_id, $low, $high ); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Get Lowest Price ID |
624
|
|
|
* |
625
|
|
|
* Retrieves the ID for the cheapest price option of a variable donation form |
626
|
|
|
* |
627
|
|
|
* @since 1.5 |
628
|
|
|
* |
629
|
|
|
* @param int $form_id ID of the donation |
630
|
|
|
* |
631
|
|
|
* @return int ID of the lowest price |
632
|
|
|
*/ |
633
|
|
|
function give_get_lowest_price_id( $form_id = 0 ) { |
634
|
|
|
|
635
|
|
|
if ( empty( $form_id ) ) { |
636
|
|
|
$form_id = get_the_ID(); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
if ( ! give_has_variable_prices( $form_id ) ) { |
640
|
|
|
return give_get_form_price( $form_id ); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
$prices = give_get_variable_prices( $form_id ); |
644
|
|
|
|
645
|
|
|
$min = $min_id = 0; |
646
|
|
|
|
647
|
|
|
if ( ! empty( $prices ) ) { |
648
|
|
|
|
649
|
|
|
foreach ( $prices as $key => $price ) { |
650
|
|
|
|
651
|
|
|
if ( empty( $price['_give_amount'] ) ) { |
652
|
|
|
continue; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
if ( ! isset( $min ) ) { |
656
|
|
|
$min = $price['_give_amount']; |
657
|
|
|
} else { |
658
|
|
|
$min = min( $min, $price['_give_amount'] ); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
if ( $price['_give_amount'] == $min ) { |
662
|
|
|
$min_id = $price['_give_id']['level_id']; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
return (int) $min_id; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Retrieves cheapest price option of a variable priced form |
672
|
|
|
* |
673
|
|
|
* @since 1.0 |
674
|
12 |
|
* |
675
|
|
|
* @param int $form_id ID of the form |
676
|
|
|
* |
677
|
|
|
* @return float Amount of the lowest price |
678
|
12 |
|
*/ |
679
|
|
View Code Duplication |
function give_get_lowest_price_option( $form_id = 0 ) { |
|
|
|
|
680
|
12 |
|
if ( empty( $form_id ) ) { |
681
|
|
|
$form_id = get_the_ID(); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
if ( ! give_has_variable_prices( $form_id ) ) { |
685
|
|
|
return give_get_form_price( $form_id ); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if ( ! ( $low = get_post_meta( $form_id, '_give_levels_minimum_amount', true ) ) ) { |
689
|
|
|
// Backward compatibility. |
690
|
|
|
$prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' ); |
691
|
|
|
$low = ! empty( $prices ) ? min( $prices ) : 0; |
692
|
|
|
} |
693
|
|
|
|
694
|
1 |
|
return give_maybe_sanitize_amount( $low ); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
1 |
|
* Retrieves most expensive price option of a variable priced form |
699
|
|
|
* |
700
|
1 |
|
* @since 1.0 |
701
|
|
|
* |
702
|
|
|
* @param int $form_id ID of the form |
703
|
|
|
* |
704
|
|
|
* @return float Amount of the highest price |
705
|
|
|
*/ |
706
|
|
View Code Duplication |
function give_get_highest_price_option( $form_id = 0 ) { |
|
|
|
|
707
|
|
|
|
708
|
|
|
if ( empty( $form_id ) ) { |
709
|
|
|
$form_id = get_the_ID(); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
if ( ! give_has_variable_prices( $form_id ) ) { |
713
|
|
|
return give_get_form_price( $form_id ); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
if ( ! ( $high = get_post_meta( $form_id, '_give_levels_maximum_amount', true ) ) ) { |
717
|
|
|
// Backward compatibility. |
718
|
|
|
$prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' ); |
719
|
|
|
$high = ! empty( $prices ) ? max( $prices ) : 0; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
return give_maybe_sanitize_amount( $high ); |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Returns the price of a form, but only for non-variable priced forms. |
727
|
|
|
* |
728
|
|
|
* @since 1.0 |
729
|
|
|
* |
730
|
|
|
* @param int $form_id ID number of the form to retrieve a price for |
731
|
|
|
* |
732
|
|
|
* @return mixed string|int Price of the form |
733
|
|
|
*/ |
734
|
|
|
function give_get_form_price( $form_id = 0 ) { |
735
|
|
|
|
736
|
|
|
if ( empty( $form_id ) ) { |
737
|
|
|
return false; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
741
|
|
|
|
742
|
|
|
return $form->__get( 'price' ); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* Returns the minimum price amount of a form, only enforced for the custom amount input. |
747
|
|
|
* |
748
|
|
|
* @since 1.3.6 |
749
|
|
|
* |
750
|
|
|
* @param int $form_id ID number of the form to retrieve the minimum price for |
751
|
|
|
* |
752
|
|
|
* @return mixed string|int Minimum price of the form |
753
|
|
|
*/ |
754
|
|
|
function give_get_form_minimum_price( $form_id = 0 ) { |
755
|
|
|
|
756
|
|
|
if ( empty( $form_id ) ) { |
757
|
|
|
return false; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
761
|
|
|
|
762
|
|
|
return $form->get_minimum_price(); |
763
|
|
|
|
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* Displays a formatted price for a donation form |
768
|
|
|
* |
769
|
|
|
* @since 1.0 |
770
|
|
|
* |
771
|
|
|
* @param int $form_id ID of the form price to show |
772
|
|
|
* @param bool $echo Whether to echo or return the results |
773
|
|
|
* @param bool|int $price_id Optional price id for variable pricing |
774
|
|
|
* |
775
|
11 |
|
* @return int $formatted_price |
776
|
|
|
*/ |
777
|
11 |
|
function give_price( $form_id = 0, $echo = true, $price_id = false ) { |
778
|
|
|
$price = 0; |
779
|
11 |
|
|
780
|
11 |
|
if ( empty( $form_id ) ) { |
781
|
11 |
|
$form_id = get_the_ID(); |
782
|
11 |
|
} |
783
|
11 |
|
|
784
|
|
|
if ( give_has_variable_prices( $form_id ) ) { |
785
|
11 |
|
|
786
|
|
|
$prices = give_get_variable_prices( $form_id ); |
787
|
|
|
|
788
|
|
|
if ( false !== $price_id ) { |
789
|
|
|
|
790
|
|
|
// loop through multi-prices to see which is default |
791
|
|
View Code Duplication |
foreach ( $prices as $price ) { |
|
|
|
|
792
|
|
|
// this is the default price |
793
|
|
|
if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) { |
|
|
|
|
794
|
|
|
$price = (float) $price['_give_amount']; |
795
|
|
|
}; |
796
|
|
|
} |
797
|
|
|
} else { |
798
|
|
|
|
799
|
|
|
$price = give_get_lowest_price_option( $form_id ); |
800
|
|
|
} |
801
|
|
|
} else { |
802
|
|
|
|
803
|
|
|
$price = give_get_form_price( $form_id ); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
$price = apply_filters( 'give_form_price', give_maybe_sanitize_amount( $price ), $form_id ); |
807
|
|
|
$formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>'; |
808
|
|
|
$formatted_price = apply_filters( 'give_form_price_after_html', $formatted_price, $form_id, $price ); |
809
|
|
|
|
810
|
|
|
if ( $echo ) { |
811
|
|
|
echo $formatted_price; |
|
|
|
|
812
|
|
|
} else { |
813
|
|
|
return $formatted_price; |
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
add_filter( 'give_form_price', 'give_format_amount', 10 ); |
818
|
|
|
add_filter( 'give_form_price', 'give_currency_filter', 20 ); |
819
|
|
|
|
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Retrieves the amount of a variable price option |
823
|
|
|
* |
824
|
|
|
* @since 1.0 |
825
|
|
|
* |
826
|
|
|
* @param int $form_id ID of the form |
827
|
|
|
* @param int $price_id ID of the price option |
828
|
|
|
* |
829
|
|
|
* @return float $amount Amount of the price option |
830
|
|
|
*/ |
831
|
|
|
function give_get_price_option_amount( $form_id = 0, $price_id = 0 ) { |
832
|
|
|
$prices = give_get_variable_prices( $form_id ); |
833
|
|
|
|
834
|
|
|
$amount = 0.00; |
835
|
|
|
|
836
|
|
View Code Duplication |
foreach ( $prices as $price ) { |
|
|
|
|
837
|
|
|
if ( isset( $price['_give_id']['level_id'] ) && $price['_give_id']['level_id'] == $price_id ) { |
838
|
|
|
$amount = isset( $price['_give_amount'] ) ? $price['_give_amount'] : 0.00; |
839
|
|
|
break; |
840
|
|
|
}; |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
return apply_filters( 'give_get_price_option_amount', give_maybe_sanitize_amount( $amount ), $form_id, $price_id ); |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Returns the goal of a form |
848
|
|
|
* |
849
|
|
|
* @since 1.0 |
850
|
|
|
* |
851
|
1 |
|
* @param int $form_id ID number of the form to retrieve a goal for |
852
|
|
|
* |
853
|
1 |
|
* @return mixed string|int Goal of the form |
854
|
|
|
*/ |
855
|
1 |
|
function give_get_form_goal( $form_id = 0 ) { |
856
|
|
|
|
857
|
|
|
if ( empty( $form_id ) ) { |
858
|
|
|
return false; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
$form = new Give_Donate_Form( $form_id ); |
|
|
|
|
862
|
|
|
|
863
|
|
|
return $form->goal; |
864
|
|
|
|
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
/** |
868
|
|
|
* Display/Return a formatted goal for a donation form |
869
|
1 |
|
* |
870
|
|
|
* @since 1.0 |
871
|
1 |
|
* |
872
|
|
|
* @param int $form_id ID of the form price to show |
873
|
|
|
* @param bool $echo Whether to echo or return the results |
874
|
|
|
* |
875
|
|
|
* @return string $formatted_goal |
876
|
|
|
*/ |
877
|
|
|
function give_goal( $form_id = 0, $echo = true ) { |
878
|
|
|
|
879
|
|
|
if ( empty( $form_id ) ) { |
880
|
|
|
$form_id = get_the_ID(); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
$goal = give_get_form_goal( $form_id ); |
884
|
|
|
|
885
|
|
|
$goal = apply_filters( 'give_form_goal', give_maybe_sanitize_amount( $goal ), $form_id ); |
886
|
|
|
$formatted_goal = '<span class="give_price" id="give_price_' . $form_id . '">' . $goal . '</span>'; |
887
|
|
|
$formatted_goal = apply_filters( 'give_form_price_after_html', $formatted_goal, $form_id, $goal ); |
888
|
|
|
|
889
|
|
|
if ( $echo ) { |
890
|
|
|
echo $formatted_goal; |
|
|
|
|
891
|
|
|
} else { |
892
|
|
|
return $formatted_goal; |
893
|
|
|
} |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
add_filter( 'give_form_goal', 'give_format_amount', 10 ); |
897
|
|
|
add_filter( 'give_form_goal', 'give_currency_filter', 20 ); |
898
|
|
|
|
899
|
|
|
|
900
|
|
|
/** |
901
|
|
|
* Checks if users can only donate when logged in |
902
|
|
|
* |
903
|
|
|
* @since 1.0 |
904
|
|
|
* |
905
|
|
|
* @param int $form_id Give form ID |
906
|
|
|
* |
907
|
|
|
* @return bool $ret Whether or not the logged_in_only setting is set |
908
|
|
|
*/ |
909
|
|
|
function give_logged_in_only( $form_id ) { |
910
|
|
|
// If _give_logged_in_only is set to enable then guest can donate from that specific form. |
911
|
|
|
// Otherwise it is member only donation form. |
912
|
|
|
$val = give_get_meta( $form_id, '_give_logged_in_only', true ); |
913
|
|
|
$val = ! empty( $val ) ? $val : 'enabled'; |
914
|
|
|
|
915
|
|
|
$ret = ! give_is_setting_enabled( $val ); |
916
|
|
|
|
917
|
|
|
return (bool) apply_filters( 'give_logged_in_only', $ret, $form_id ); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
|
921
|
|
|
/** |
922
|
|
|
* Checks the option for the "Register / Login Option" |
923
|
|
|
* |
924
|
|
|
* @since 1.4.1 |
925
|
|
|
* |
926
|
|
|
* @param int $form_id |
927
|
|
|
* |
928
|
|
|
* @return string |
929
|
|
|
*/ |
930
|
|
|
function give_show_login_register_option( $form_id ) { |
931
|
|
|
|
932
|
|
|
$show_register_form = give_get_meta( $form_id, '_give_show_register_form', true ); |
933
|
|
|
|
934
|
|
|
return apply_filters( 'give_show_register_form', $show_register_form, $form_id ); |
935
|
|
|
|
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
|
939
|
|
|
/** |
940
|
|
|
* Get pre fill form field values. |
941
|
|
|
* |
942
|
|
|
* Note: this function will extract form field values from give_purchase session data. |
943
|
|
|
* |
944
|
|
|
* @since 1.8 |
945
|
|
|
* |
946
|
|
|
* @param int $form_id Form ID. |
947
|
|
|
* |
948
|
|
|
* @return array |
949
|
|
|
*/ |
950
|
|
|
function _give_get_prefill_form_field_values( $form_id ) { |
951
|
|
|
$logged_in_donor_info = array(); |
952
|
|
|
|
953
|
|
|
if ( is_user_logged_in() ) : |
954
|
|
|
$donor_data = get_userdata( get_current_user_id() ); |
955
|
|
|
$donor_address = get_user_meta( get_current_user_id(), '_give_user_address', true ); |
|
|
|
|
956
|
|
|
|
957
|
|
|
$logged_in_donor_info = array( |
958
|
|
|
// First name. |
959
|
|
|
'give_first' => $donor_data->first_name, |
960
|
|
|
|
961
|
|
|
// Last name. |
962
|
|
|
'give_last' => $donor_data->last_name, |
963
|
|
|
|
964
|
|
|
// Email. |
965
|
|
|
'give_email' => $donor_data->user_email, |
966
|
|
|
|
967
|
|
|
// Street address 1. |
968
|
|
|
'card_address' => ( ! empty( $donor_address['line1'] ) ? $donor_address['line1'] : '' ), |
969
|
|
|
|
970
|
|
|
// Street address 2. |
971
|
|
|
'card_address_2' => ( ! empty( $donor_address['line2'] ) ? $donor_address['line2'] : '' ), |
972
|
|
|
|
973
|
|
|
// Country. |
974
|
|
|
'billing_country' => ( ! empty( $donor_address['country'] ) ? $donor_address['country'] : '' ), |
975
|
|
|
|
976
|
|
|
// State. |
977
|
|
|
'card_state' => ( ! empty( $donor_address['state'] ) ? $donor_address['state'] : '' ), |
978
|
|
|
|
979
|
|
|
// City. |
980
|
|
|
'card_city' => ( ! empty( $donor_address['city'] ) ? $donor_address['city'] : '' ), |
981
|
|
|
|
982
|
|
|
// Zipcode |
983
|
|
|
'card_zip' => ( ! empty( $donor_address['zip'] ) ? $donor_address['zip'] : '' ), |
984
|
|
|
); |
985
|
|
|
endif; |
986
|
|
|
|
987
|
|
|
// Bailout: Auto fill form field values only form form which donor is donating. |
988
|
|
|
if ( |
989
|
|
|
empty( $_GET['form-id'] ) |
|
|
|
|
990
|
|
|
|| ! $form_id |
991
|
|
|
|| ( $form_id !== absint( $_GET['form-id'] ) ) |
|
|
|
|
992
|
|
|
) { |
993
|
|
|
return $logged_in_donor_info; |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
// Get purchase data. |
997
|
|
|
$give_purchase_data = Give()->session->get( 'give_purchase' ); |
998
|
|
|
|
999
|
|
|
// Get donor info from form data. |
1000
|
|
|
$give_donor_info_in_session = empty( $give_purchase_data['post_data'] ) |
1001
|
|
|
? array() |
1002
|
|
|
: $give_purchase_data['post_data']; |
1003
|
|
|
|
1004
|
|
|
// Output. |
1005
|
|
|
return wp_parse_args( $give_donor_info_in_session, $logged_in_donor_info ); |
1006
|
|
|
} |
1007
|
|
|
|