-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathfilters.php
More file actions
373 lines (311 loc) · 8.84 KB
/
Copy pathfilters.php
File metadata and controls
373 lines (311 loc) · 8.84 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
use Give\Log\Log;
use Give\Framework\Database\DB;
use Give\Log\ValueObjects\LogType;
/**
* Front-end Filters
*
* @package Give
* @subpackage Functions
* @copyright Copyright (c) 2016, GiveWP
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add backward compatibility for settings who has disable_ as name prefix.
* TODO: Remove this backward compatibility when do not need.
*
* @since 1.8
*
* @param array $old_settings Array of settings.
* @param array $settings Array of settings.
*
* @return void
*/
function give_set_settings_with_disable_prefix( $old_settings, $settings ) {
// Bailout.
if ( ! function_exists( 'give_v18_renamed_core_settings' ) ) {
return;
}
// Get old setting names.
$old_settings = array_flip( give_v18_renamed_core_settings() );
$update_setting = false;
foreach ( $settings as $key => $value ) {
// Check 1. Check if new option is really updated or not.
// Check 2. Continue if key is not renamed.
if ( ! isset( $old_settings[ $key ] ) ) {
continue;
}
// Set old setting.
$settings[ $old_settings[ $key ] ] = 'on';
// Do not need to set old setting if new setting is not set.
if (
( give_is_setting_enabled( $value ) && ( false !== strpos( $old_settings[ $key ], 'disable_' ) ) )
|| ( ! give_is_setting_enabled( $value ) && ( false !== strpos( $old_settings[ $key ], 'enable_' ) ) )
) {
unset( $settings[ $old_settings[ $key ] ] );
}
// Tell bot to update setting.
$update_setting = true;
}
// Update setting if any old setting set.
if ( $update_setting ) {
update_option( 'give_settings', $settings, false );
}
}
add_action( 'update_option_give_settings', 'give_set_settings_with_disable_prefix', 10, 2 );
/**
* Check spam through Akismet.
*
* It will build Akismet query string and call Akismet API.
* Akismet response return 'true' for spam donation.
*
* @since 1.8.14
*
* @param $spam
*
* @return bool|mixed
*/
function give_akismet( $spam ) {
// Build args array.
$args = [];
// Bail out, If spam.
if ( $spam || ! give_is_setting_enabled( give_get_option( 'akismet_spam_protection' ) ) ) {
return $spam;
}
// Bail out, if Akismet key not exist.
if ( ! give_check_akismet_key() ) {
return false;
}
$args['comment_author_email'] = isset( $_POST['give_email'] ) ? sanitize_email( $_POST['give_email'] ) : false;
/**
* Filter list of whitelisted emails
*
* @since 2.5.14
*
* @param array
*/
$whitelist_emails = apply_filters( 'give_akismet_whitelist_emails', give_akismet_get_whitelisted_emails() );
// Whitelist emails.
if ( in_array( $args['comment_author_email'], (array) $whitelist_emails, true ) ) {
return false;
}
$args['comment_author'] = isset( $_POST['give_first'] ) ? give_clean( $_POST['give_first'] ) : '';
$args['blog'] = get_option( 'home' );
$args['blog_lang'] = get_locale();
$args['blog_charset'] = get_option( 'blog_charset' );
$args['user_ip'] = $_SERVER['REMOTE_ADDR'];
$args['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$args['referrer'] = $_SERVER['HTTP_REFERER'];
$args['comment_type'] = 'contact-form';
$form_id = isset( $_POST['give-form-id'] ) ? absint( $_POST['give-form-id'] ) : 0;
$donor_last_name = ! empty( $_POST['give_last'] ) ? ' ' . give_clean( $_POST['give_last'] ) : '';
// Pass Donor comment if enabled.
if ( give_is_donor_comment_field_enabled( $form_id ) ) {
$give_comment = isset( $_POST['give_comment'] ) ? give_clean( $_POST['give_comment'] ) : '';
$args['comment_content'] = $give_comment;
}
$ignore = [ 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ];
foreach ( $_SERVER as $key => $value ) {
if ( ! in_array( $key, $ignore, true ) ) {
$args[ $key ] = $value;
}
}
$response = give_akismet_spam_check_post( $args );
$spam = 'true' === $response[1];
// Log spam information.
if ( $spam && ! give_akismet_is_email_logged( $args['comment_author_email'] ) ) {
$title = sprintf(
'This donor\'s email (%1$s%2$s - %3$s) has been flagged as SPAM',
$args['comment_author'],
$donor_last_name,
$args['comment_author_email']
);
$message = sprintf(
'<p><strong>%1$s</strong><pre>%2$s</pre></p><strong>%3$s</strong><pre>%4$s</pre><p>',
__( 'Request', 'give' ),
print_r( $args, true ),
__( 'Response', 'give' ),
print_r( $response, true )
);
$context = [
'donor_email' => $args['comment_author_email'],
'filter' => 'akismet',
'message' => $message,
];
Log::spam( $title, $context );
}
// It will return Akismet spam detect API response.
return $spam;
}
add_filter( 'give_spam', 'give_akismet' );
/**
* Check Akismet API Key.
*
* @since 1.8.14
*
* @return bool
*/
function give_check_akismet_key() {
if ( is_callable( [ 'Akismet', 'get_api_key' ] ) ) { // Akismet v3.0+
return (bool) Akismet::get_api_key();
}
if ( function_exists( 'akismet_get_key' ) ) {
return (bool) akismet_get_key();
}
return false;
}
/**
* Detect spam through Akismet Comment API.
*
* @param array $args
*
* @return bool|mixed
* @since 1.8.14
* @since 2.3.15 Refactor function to use give_akismet_spam_check_post
*/
function give_akismet_spam_check( $args ) {
$response = give_akismet_spam_check_post( $args );
// It's spam if response status is true.
$spam = 'true' === $response[1];
// Allow developer to modified Akismet spam detection response.
return apply_filters( 'give_akismet_spam_check', $spam, $args );
}
/**
* Detect spam through Akismet Comment API.
*
* @since 2.5.13
*
* @param array $args
*
* @return array
*/
function give_akismet_spam_check_post( $args ) {
global $akismet_api_host, $akismet_api_port;
$query_string = http_build_query( $args );
if ( is_callable( [ 'Akismet', 'http_post' ] ) ) { // Akismet v3.0+
$response = Akismet::http_post( $query_string, 'comment-check' );
} else {
$response = akismet_http_post(
$query_string,
$akismet_api_host,
'/1.1/comment-check',
$akismet_api_port
);
}
return $response;
}
/**
* Check if email already logged or not
*
* @param $email
*
* @return bool
* @since 2.5.13
*/
function give_akismet_is_email_logged( $email ) {
global $wpdb;
return (bool) DB::get_var(
DB::prepare(
"SELECT COUNT(id) FROM {$wpdb->give_log} WHERE log_type = %s AND data LIKE '%s';",
LogType::SPAM,
'%' . esc_sql( $email ) . '%'
)
);
}
/**
* Get list of whitelisted emails.
*
* @return array
* @since 2.5.13
*/
function give_akismet_get_whitelisted_emails() {
return give_get_option(
'akismet_whitelisted_email_addresses',
get_bloginfo( 'admin_email' )
);
}
/**
* Add support of RIAL code for backward compatibility.
* Note: for internal use only
*
* @since 1.8.17
*
* @param array $currencies
*
* @return array
*/
function give_bc_v1817_iranian_currency_code( $currencies ) {
$currencies['RIAL'] = $currencies['IRR'];
return $currencies;
}
if ( ! give_has_upgrade_completed( 'v1817_update_donation_iranian_currency_code' ) ) {
add_filter( 'give_currencies', 'give_bc_v1817_iranian_currency_code', 0 );
}
/**
* Format right to left supported currency amount.
*
* @since 1.8.17
*
* @param $formatted_amount
* @param $currency_args
* @param $price
*
* @return string
*/
function give_format_price_for_right_to_left_supported_currency( $formatted_amount, $currency_args, $price ) {
if ( ! give_is_right_to_left_supported_currency( $currency_args['currency_code'] ) ) {
return $formatted_amount;
}
$formatted_amount = (
'before' === (string) $currency_args['position'] ?
'‫' . $price . $currency_args['symbol'] . '‬' :
'‪' . $price . $currency_args['symbol'] . '‬'
);
$formatted_amount = $currency_args['decode_currency'] ?
html_entity_decode( $formatted_amount, ENT_COMPAT, 'UTF-8' ) :
$formatted_amount;
return $formatted_amount;
}
add_filter( 'give_currency_filter', 'give_format_price_for_right_to_left_supported_currency', 10, 3 );
/**
* Validate active gateway value before returning result.
*
* @since 4.9.0 rename function - PHP 8 compatibility
* @since 2.1.0
*
* @param $value
*
* @return array
*/
function give_validate_active_gateways( $value ) {
$gateways = array_keys( give_get_payment_gateways() );
$active_gateways = is_array( $value ) ? array_keys( $value ) : [];
// Remove deactivated payment gateways.
if ( ! empty( $active_gateways ) ) {
foreach ( $active_gateways as $index => $gateway_id ) {
if ( ! in_array( $gateway_id, $gateways ) ) {
unset( $value[ $gateway_id ] );
}
}
}
if ( empty( $value ) ) {
/**
* Filter the default active gateway
*
* @since 2.1.0
*/
$value = apply_filters(
'give_default_active_gateways',
[
'manual' => 1,
]
);
}
return $value;
}
add_filter( 'give_get_option_gateways', 'give_validate_active_gateways', 10, 1 );