/
actions.php
335 lines (273 loc) · 8.63 KB
/
actions.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
<?php
/**
* Front-end Actions
*
* @package Give
* @subpackage Functions
* @copyright Copyright (c) 2016, WordImpress
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Hooks Give actions, when present in the $_GET superglobal. Every give_action
* present in $_GET is called using WordPress's do_action function. These
* functions are called on init.
*
* @since 1.0
*
* @return void
*/
function give_get_actions() {
$_get_action = ! empty( $_GET['give_action'] ) ? $_GET['give_action'] : null;
// Add backward compatibility to give-action param ( $_GET )
if ( empty( $_get_action ) ) {
$_get_action = ! empty( $_GET['give-action'] ) ? $_GET['give-action'] : null;
}
if ( isset( $_get_action ) ) {
/**
* Fires in WordPress init or admin init, when give_action is present in $_GET.
*
* @since 1.0
*
* @param array $_GET Array of HTTP GET variables.
*/
do_action( "give_{$_get_action}", $_GET );
}
}
add_action( 'init', 'give_get_actions' );
/**
* Hooks Give actions, when present in the $_POST super global. Every give_action
* present in $_POST is called using WordPress's do_action function. These
* functions are called on init.
*
* @since 1.0
*
* @return void
*/
function give_post_actions() {
$_post_action = ! empty( $_POST['give_action'] ) ? $_POST['give_action'] : null;
// Add backward compatibility to give-action param ( $_POST ).
if ( empty( $_post_action ) ) {
$_post_action = ! empty( $_POST['give-action'] ) ? $_POST['give-action'] : null;
}
if ( isset( $_post_action ) ) {
/**
* Fires in WordPress init or admin init, when give_action is present in $_POST.
*
* @since 1.0
*
* @param array $_POST Array of HTTP POST variables.
*/
do_action( "give_{$_post_action}", $_POST );
}
}
add_action( 'init', 'give_post_actions' );
/**
* Connect WordPress user with Donor.
*
* @param int $user_id User ID.
* @param array $user_data User Data.
*
* @since 1.7
*
* @return void
*/
function give_connect_donor_to_wpuser( $user_id, $user_data ) {
/* @var Give_Donor $donor */
$donor = new Give_Donor( $user_data['user_email'] );
// Validate donor id and check if do nor is already connect to wp user or not.
if ( $donor->id && ! $donor->user_id ) {
// Update donor user_id.
if ( $donor->update( array( 'user_id' => $user_id ) ) ) {
$donor_note = sprintf( esc_html__( 'WordPress user #%d is connected to #%d', 'give' ), $user_id, $donor->id );
$donor->add_note( $donor_note );
// Update user_id meta in payments.
// if( ! empty( $donor->payment_ids ) && ( $donations = explode( ',', $donor->payment_ids ) ) ) {
// foreach ( $donations as $donation ) {
// give_update_meta( $donation, '_give_payment_user_id', $user_id );
// }
// }
// Do not need to update user_id in payment because we will get user id from donor id now.
}
}
}
add_action( 'give_insert_user', 'give_connect_donor_to_wpuser', 10, 2 );
/**
* Setup site home url check
*
* Note: if location of site changes then run cron to validate licenses
*
* @since 1.7
* @updated 1.8.15 - Resolved issue with endless looping because of URL mismatches.
* @return void
*/
function give_validate_license_when_site_migrated() {
// Store current site address if not already stored.
$home_url_parts = parse_url( home_url() );
$home_url = isset( $home_url_parts['host'] ) ? $home_url_parts['host'] : false;
$home_url .= isset( $home_url_parts['path'] ) ? $home_url_parts['path'] : '';
$site_address_before_migrate = get_option( 'give_site_address_before_migrate' );
// Need $home_url to proceed.
if ( ! $home_url ) {
return;
}
// Save site address.
if ( ! $site_address_before_migrate ) {
// Update site address.
update_option( 'give_site_address_before_migrate', $home_url );
return;
}
// Backwards compat. for before when we were storing URL scheme.
if ( strpos( $site_address_before_migrate, 'http' ) ) {
$site_address_before_migrate = parse_url( $site_address_before_migrate );
$site_address_before_migrate = isset( $site_address_before_migrate['host'] ) ? $site_address_before_migrate['host'] : false;
// Add path for multisite installs.
$site_address_before_migrate .= isset( $site_address_before_migrate['path'] ) ? $site_address_before_migrate['path'] : '';
}
// If the two URLs don't match run CRON.
if ( $home_url !== $site_address_before_migrate ) {
// Immediately run cron.
wp_schedule_single_event( time(), 'give_validate_license_when_site_migrated' );
// Update site address.
update_option( 'give_site_address_before_migrate', $home_url );
}
}
add_action( 'admin_init', 'give_validate_license_when_site_migrated' );
/**
* Processing after donor batch export complete
*
* @since 1.8
*
* @param $data
*/
function give_donor_batch_export_complete( $data ) {
// Remove donor ids cache.
if (
isset( $data['class'] )
&& 'Give_Batch_Donors_Export' === $data['class']
&& ! empty( $data['forms'] )
&& isset( $data['give_export_option']['query_id'] )
) {
Give_Cache::delete( Give_Cache::get_key( $data['give_export_option']['query_id'] ) );
}
}
add_action( 'give_file_export_complete', 'give_donor_batch_export_complete' );
/**
* Print css for wordpress setting pages.
*
* @since 1.8.7
*/
function give_admin_quick_css() {
/* @var WP_Screen $screen */
$screen = get_current_screen();
if ( ! ( $screen instanceof WP_Screen ) ) {
return false;
}
switch ( true ) {
case ( 'plugins' === $screen->base || 'plugins-network' === $screen->base ):
?>
<style>
tr.active.update + tr.give-addon-notice-tr td {
box-shadow: none;
-webkit-box-shadow: none;
}
tr.active + tr.give-addon-notice-tr td {
position: relative;
top: -1px;
}
tr.active + tr.give-addon-notice-tr .notice {
margin: 5px 20px 15px 40px;
}
tr.give-addon-notice-tr .dashicons {
color: #f56e28;
}
tr.give-addon-notice-tr td {
border-left: 4px solid #00a0d2;
}
tr.give-addon-notice-tr td {
padding: 0 !important;
}
tr.active.update + tr.give-addon-notice-tr .notice {
margin: 5px 20px 5px 40px;
}
</style>
<?php
}
}
add_action( 'admin_head', 'give_admin_quick_css' );
/**
* Set Donation Amount for Multi Level Donation Forms
*
* @param int $form_id Donation Form ID.
*
* @since 1.8.9
*
* @return void
*/
function give_set_donation_levels_max_min_amount( $form_id ) {
if (
( 'set' === $_POST['_give_price_option'] ) ||
( in_array( '_give_donation_levels', $_POST ) && count( $_POST['_give_donation_levels'] ) <= 0 ) ||
! ( $donation_levels_amounts = wp_list_pluck( $_POST['_give_donation_levels'], '_give_amount' ) )
) {
// Delete old meta.
give_delete_meta( $form_id, '_give_levels_minimum_amount' );
give_delete_meta( $form_id, '_give_levels_maximum_amount' );
return;
}
// Sanitize donation level amounts.
$donation_levels_amounts = array_map( 'give_maybe_sanitize_amount', $donation_levels_amounts );
$min_amount = min( $donation_levels_amounts );
$max_amount = max( $donation_levels_amounts );
// Set Minimum and Maximum amount for Multi Level Donation Forms.
give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount_for_db( $min_amount ) : 0 );
give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount_for_db( $max_amount ) : 0 );
}
add_action( 'give_pre_process_give_forms_meta', 'give_set_donation_levels_max_min_amount', 30 );
/**
* Save donor address when donation complete
*
* @since 2.0
*
* @param int $payment_id
*/
function _give_save_donor_billing_address( $payment_id ) {
/* @var Give_Payment $donation */
$donation = new Give_Payment( $payment_id );
// Bailout
if ( ! $donation->customer_id ) {
return;
}
/* @var Give_Donor $donor */
$donor = new Give_Donor( $donation->customer_id );
// Save address.
$donor->add_address( 'billing[]', $donation->address );
}
add_action( 'give_complete_donation', '_give_save_donor_billing_address', 9999 );
/**
* Update form id in payment logs
*
* @since 2.0
*
* @param array $args
*/
function give_update_log_form_id( $args ) {
$new_form_id = absint( $args[0] );
$payment_id = absint( $args[1] );
$logs = Give()->logs->get_logs( $payment_id );
// Bailout.
if ( empty( $logs ) ) {
return;
}
/* @var object $log */
foreach ( $logs as $log ) {
Give()->logs->logmeta_db->update_meta( $log->ID, '_give_log_form_id', $new_form_id );
}
// Delete cache.
Give()->logs->delete_cache();
}
add_action( 'give_update_log_form_id', 'give_update_log_form_id' );