1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Front-end Actions |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Functions |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Hooks Give actions, when present in the $_GET superglobal. Every give_action |
19
|
|
|
* present in $_GET is called using WordPress's do_action function. These |
20
|
|
|
* functions are called on init. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
function give_get_actions() { |
|
|
|
|
27
|
|
|
|
28
|
|
|
$_get_action = ! empty( $_GET['give_action'] ) ? $_GET['give_action'] : null; |
|
|
|
|
29
|
|
|
|
30
|
|
|
// Add backward compatibility to give-action param ( $_GET ) |
31
|
|
|
if ( empty( $_get_action ) ) { |
32
|
|
|
$_get_action = ! empty( $_GET['give-action'] ) ? $_GET['give-action'] : null; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( isset( $_get_action ) ) { |
36
|
|
|
/** |
37
|
|
|
* Fires in WordPress init or admin init, when give_action is present in $_GET. |
38
|
|
|
* |
39
|
|
|
* @since 1.0 |
40
|
|
|
* |
41
|
|
|
* @param array $_GET Array of HTTP GET variables. |
42
|
|
|
*/ |
43
|
|
|
do_action( "give_{$_get_action}", $_GET ); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
add_action( 'init', 'give_get_actions' ); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Hooks Give actions, when present in the $_POST super global. Every give_action |
52
|
|
|
* present in $_POST is called using WordPress's do_action function. These |
53
|
|
|
* functions are called on init. |
54
|
|
|
* |
55
|
|
|
* @since 1.0 |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
function give_post_actions() { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$_post_action = ! empty( $_POST['give_action'] ) ? $_POST['give_action'] : null; |
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
64
|
|
|
// Add backward compatibility to give-action param ( $_POST ). |
65
|
|
|
if ( empty( $_post_action ) ) { |
66
|
|
|
$_post_action = ! empty( $_POST['give-action'] ) ? $_POST['give-action'] : null; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( isset( $_post_action ) ) { |
70
|
|
|
/** |
71
|
|
|
* Fires in WordPress init or admin init, when give_action is present in $_POST. |
72
|
|
|
* |
73
|
|
|
* @since 1.0 |
74
|
|
|
* |
75
|
|
|
* @param array $_POST Array of HTTP POST variables. |
76
|
|
|
*/ |
77
|
|
|
do_action( "give_{$_post_action}", $_POST ); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
add_action( 'init', 'give_post_actions' ); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Connect WordPress user with Donor. |
86
|
|
|
* |
87
|
|
|
* @since 1.7 |
88
|
|
|
* |
89
|
|
|
* @param int $user_id User ID |
90
|
|
|
* @param array $user_data User Data |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
function give_connect_donor_to_wpuser( $user_id, $user_data ) { |
95
|
|
|
/* @var Give_Donor $donor */ |
96
|
|
|
$donor = new Give_Donor( $user_data['user_email'] ); |
97
|
|
|
|
98
|
|
|
// Validate donor id and check if do nor is already connect to wp user or not. |
99
|
|
|
if ( $donor->id && ! $donor->user_id ) { |
100
|
|
|
|
101
|
|
|
// Update donor user_id. |
102
|
|
|
if ( $donor->update( array( 'user_id' => $user_id ) ) ) { |
103
|
|
|
$donor_note = sprintf( esc_html__( 'WordPress user #%d is connected to #%d', 'give' ), $user_id, $donor->id ); |
104
|
|
|
$donor->add_note( $donor_note ); |
105
|
|
|
|
106
|
|
|
// Update user_id meta in payments. |
107
|
|
|
if ( ! empty( $donor->payment_ids ) && ( $donations = explode( ',', $donor->payment_ids ) ) ) { |
108
|
|
|
foreach ( $donations as $donation ) { |
109
|
|
|
give_update_meta( $donation, '_give_payment_user_id', $user_id ); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
add_action( 'give_insert_user', 'give_connect_donor_to_wpuser', 10, 2 ); |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Setup site home url check |
121
|
|
|
* |
122
|
|
|
* Note: if location of site changes then run cron to validate licenses |
123
|
|
|
* |
124
|
|
|
* @since 1.7 |
125
|
|
|
* @updated 1.8.15 - Resolved issue with endless looping because of URL mismatches. |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
function give_validate_license_when_site_migrated() { |
129
|
|
|
// Store current site address if not already stored. |
130
|
|
|
$home_url_parts = parse_url( home_url() ); |
131
|
|
|
$home_url = isset( $home_url_parts['host'] ) ? $home_url_parts['host'] : false; |
132
|
|
|
$home_url .= isset( $home_url_parts['path'] ) ? $home_url_parts['path'] : ''; |
133
|
|
|
$site_address_before_migrate = get_option( 'give_site_address_before_migrate' ); |
134
|
|
|
|
135
|
|
|
// Need $home_url to proceed |
136
|
|
|
if ( ! $home_url ) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Save site address |
141
|
|
|
if ( ! $site_address_before_migrate ) { |
142
|
|
|
// Update site address. |
143
|
|
|
update_option( 'give_site_address_before_migrate', $home_url ); |
144
|
|
|
|
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Backwards compat. for before when we were storing URL scheme. |
149
|
|
|
if ( strpos( $site_address_before_migrate, 'http' ) ) { |
150
|
|
|
$site_address_before_migrate = parse_url( $site_address_before_migrate ); |
151
|
|
|
$site_address_before_migrate = isset( $site_address_before_migrate['host'] ) ? $site_address_before_migrate['host'] : false; |
152
|
|
|
// Add path for multisite installs. |
153
|
|
|
$site_address_before_migrate .= isset( $site_address_before_migrate['path'] ) ? $site_address_before_migrate['path'] : ''; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// If the two URLs don't match run CRON. |
157
|
|
|
if ( $home_url !== $site_address_before_migrate ) { |
158
|
|
|
// Immediately run cron. |
159
|
|
|
wp_schedule_single_event( time(), 'give_validate_license_when_site_migrated' ); |
160
|
|
|
|
161
|
|
|
// Update site address. |
162
|
|
|
update_option( 'give_site_address_before_migrate', $home_url ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
add_action( 'admin_init', 'give_validate_license_when_site_migrated' ); |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Processing after donor batch export complete |
172
|
|
|
* |
173
|
|
|
* @since 1.8 |
174
|
|
|
* |
175
|
|
|
* @param $data |
176
|
|
|
*/ |
177
|
|
|
function give_donor_batch_export_complete( $data ) { |
178
|
|
|
// Remove donor ids cache. |
179
|
|
|
if ( |
180
|
|
|
isset( $data['class'] ) |
181
|
|
|
&& 'Give_Batch_Donors_Export' === $data['class'] |
182
|
|
|
&& ! empty( $data['forms'] ) |
183
|
|
|
&& isset( $data['give_export_option']['query_id'] ) |
184
|
|
|
) { |
185
|
|
|
Give_Cache::delete( Give_Cache::get_key( $data['give_export_option']['query_id'] ) ); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
add_action( 'give_file_export_complete', 'give_donor_batch_export_complete' ); |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Print css for wordpress setting pages. |
193
|
|
|
* |
194
|
|
|
* @since 1.8.7 |
195
|
|
|
*/ |
196
|
|
|
function give_admin_quick_css() { |
197
|
|
|
/* @var WP_Screen $screen */ |
198
|
|
|
$screen = get_current_screen(); |
199
|
|
|
|
200
|
|
|
if ( ! ( $screen instanceof WP_Screen ) ) { |
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
switch ( true ) { |
205
|
|
|
case ( 'plugins' === $screen->base || 'plugins-network' === $screen->base ): |
206
|
|
|
?> |
207
|
|
|
<style> |
208
|
|
|
tr.active.update + tr.give-addon-notice-tr td { |
209
|
|
|
box-shadow: none; |
210
|
|
|
-webkit-box-shadow: none; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
tr.active + tr.give-addon-notice-tr td { |
214
|
|
|
position: relative; |
215
|
|
|
top: -1px; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
tr.active + tr.give-addon-notice-tr .notice { |
219
|
|
|
margin: 5px 20px 15px 40px; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
tr.give-addon-notice-tr .dashicons { |
223
|
|
|
color: #f56e28; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
tr.give-addon-notice-tr td { |
227
|
|
|
border-left: 4px solid #00a0d2; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
tr.give-addon-notice-tr td { |
231
|
|
|
padding: 0 !important; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
tr.active.update + tr.give-addon-notice-tr .notice { |
235
|
|
|
margin: 5px 20px 5px 40px; |
236
|
|
|
} |
237
|
|
|
</style> |
238
|
|
|
<?php |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
add_action( 'admin_head', 'give_admin_quick_css' ); |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set Donation Amount for Multi Level Donation Forms |
247
|
|
|
* |
248
|
|
|
* @param int $form_id |
249
|
|
|
* @param object $form |
|
|
|
|
250
|
|
|
* |
251
|
|
|
* @since 1.8.9 |
252
|
|
|
* |
253
|
|
|
* @return void |
254
|
|
|
*/ |
255
|
|
|
function give_set_donation_levels_max_min_amount( $form_id ) { |
256
|
|
|
if ( |
257
|
|
|
( 'set' === $_POST['_give_price_option'] ) || |
258
|
|
|
( in_array( '_give_donation_levels', $_POST ) && count( $_POST['_give_donation_levels'] ) <= 0 ) || |
259
|
|
|
! ( $donation_levels_amounts = wp_list_pluck( $_POST['_give_donation_levels'], '_give_amount' ) ) |
|
|
|
|
260
|
|
|
) { |
261
|
|
|
// Delete old meta. |
262
|
|
|
give_delete_meta( $form_id, '_give_levels_minimum_amount' ); |
263
|
|
|
give_delete_meta( $form_id, '_give_levels_maximum_amount' ); |
264
|
|
|
|
265
|
|
|
return; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Sanitize donation level amounts. |
269
|
|
|
$donation_levels_amounts = array_map( 'give_maybe_sanitize_amount', $donation_levels_amounts ); |
270
|
|
|
|
271
|
|
|
$min_amount = min( $donation_levels_amounts ); |
272
|
|
|
$max_amount = max( $donation_levels_amounts ); |
273
|
|
|
|
274
|
|
|
// Set Minimum and Maximum amount for Multi Level Donation Forms |
275
|
|
|
give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount_for_db( $min_amount ) : 0 ); |
276
|
|
|
give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount_for_db( $max_amount ) : 0 ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
add_action( 'give_pre_process_give_forms_meta', 'give_set_donation_levels_max_min_amount', 30 ); |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.