admin
1 year ago
api
3 years ago
database
2 years ago
deprecated
3 years ago
donors
1 year ago
emails
3 years ago
forms
1 year ago
frontend
6 years ago
gateways
1 year ago
libraries
2 years ago
payments
1 year ago
actions.php
5 years ago
ajax-functions.php
2 years ago
class-give-async-process.php
1 year ago
class-give-background-updater.php
2 years ago
class-give-cache-setting.php
2 years ago
class-give-cache.php
3 years ago
class-give-cli-commands.php
3 years ago
class-give-comment.php
6 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
1 year ago
class-give-donor.php
2 years ago
class-give-email-access.php
5 years ago
class-give-license-handler.php
1 year ago
class-give-logging.php
5 years ago
class-give-readme-parser.php
4 years ago
class-give-roles.php
6 years ago
class-give-scripts.php
2 years ago
class-give-session.php
5 years ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
4 years ago
class-notices.php
2 years ago
country-functions.php
5 years ago
currencies-list.php
3 years ago
currency-functions.php
3 years ago
error-tracking.php
6 years ago
filters.php
3 years ago
formatting.php
1 year ago
install.php
2 years ago
login-register.php
2 years ago
misc-functions.php
1 year ago
plugin-compatibility.php
6 years ago
post-types.php
1 year ago
price-functions.php
6 years ago
process-donation.php
1 year ago
setting-functions.php
6 years ago
shortcodes.php
1 year ago
template-functions.php
4 years ago
user-functions.php
3 years ago
filters.php
373 lines
| 1 | <?php |
| 2 | |
| 3 | use Give\Log\Log; |
| 4 | use Give\Framework\Database\DB; |
| 5 | use Give\Log\ValueObjects\LogType; |
| 6 | /** |
| 7 | * Front-end Filters |
| 8 | * |
| 9 | * @package Give |
| 10 | * @subpackage Functions |
| 11 | * @copyright Copyright (c) 2016, GiveWP |
| 12 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 13 | * @since 1.0 |
| 14 | */ |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Add backward compatibility for settings who has disable_ as name prefix. |
| 24 | * TODO: Remove this backward compatibility when do not need. |
| 25 | * |
| 26 | * @since 1.8 |
| 27 | * |
| 28 | * @param array $old_settings Array of settings. |
| 29 | * @param array $settings Array of settings. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | function give_set_settings_with_disable_prefix( $old_settings, $settings ) { |
| 34 | // Bailout. |
| 35 | if ( ! function_exists( 'give_v18_renamed_core_settings' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // Get old setting names. |
| 40 | $old_settings = array_flip( give_v18_renamed_core_settings() ); |
| 41 | $update_setting = false; |
| 42 | |
| 43 | foreach ( $settings as $key => $value ) { |
| 44 | |
| 45 | // Check 1. Check if new option is really updated or not. |
| 46 | // Check 2. Continue if key is not renamed. |
| 47 | if ( ! isset( $old_settings[ $key ] ) ) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | // Set old setting. |
| 52 | $settings[ $old_settings[ $key ] ] = 'on'; |
| 53 | |
| 54 | // Do not need to set old setting if new setting is not set. |
| 55 | if ( |
| 56 | ( give_is_setting_enabled( $value ) && ( false !== strpos( $old_settings[ $key ], 'disable_' ) ) ) |
| 57 | || ( ! give_is_setting_enabled( $value ) && ( false !== strpos( $old_settings[ $key ], 'enable_' ) ) ) |
| 58 | |
| 59 | ) { |
| 60 | unset( $settings[ $old_settings[ $key ] ] ); |
| 61 | } |
| 62 | |
| 63 | // Tell bot to update setting. |
| 64 | $update_setting = true; |
| 65 | } |
| 66 | |
| 67 | // Update setting if any old setting set. |
| 68 | if ( $update_setting ) { |
| 69 | update_option( 'give_settings', $settings, false ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | add_action( 'update_option_give_settings', 'give_set_settings_with_disable_prefix', 10, 2 ); |
| 74 | |
| 75 | /** |
| 76 | * Check spam through Akismet. |
| 77 | * |
| 78 | * It will build Akismet query string and call Akismet API. |
| 79 | * Akismet response return 'true' for spam donation. |
| 80 | * |
| 81 | * @since 1.8.14 |
| 82 | * |
| 83 | * @param $spam |
| 84 | * |
| 85 | * @return bool|mixed |
| 86 | */ |
| 87 | function give_akismet( $spam ) { |
| 88 | // Build args array. |
| 89 | $args = []; |
| 90 | |
| 91 | // Bail out, If spam. |
| 92 | if ( $spam || ! give_is_setting_enabled( give_get_option( 'akismet_spam_protection' ) ) ) { |
| 93 | return $spam; |
| 94 | } |
| 95 | |
| 96 | // Bail out, if Akismet key not exist. |
| 97 | if ( ! give_check_akismet_key() ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | $args['comment_author_email'] = isset( $_POST['give_email'] ) ? sanitize_email( $_POST['give_email'] ) : false; |
| 102 | |
| 103 | /** |
| 104 | * Filter list of whitelisted emails |
| 105 | * |
| 106 | * @since 2.5.14 |
| 107 | * |
| 108 | * @param array |
| 109 | */ |
| 110 | $whitelist_emails = apply_filters( 'give_akismet_whitelist_emails', give_akismet_get_whitelisted_emails() ); |
| 111 | |
| 112 | // Whitelist emails. |
| 113 | if ( in_array( $args['comment_author_email'], (array) $whitelist_emails, true ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | $args['comment_author'] = isset( $_POST['give_first'] ) ? give_clean( $_POST['give_first'] ) : ''; |
| 118 | $args['blog'] = get_option( 'home' ); |
| 119 | $args['blog_lang'] = get_locale(); |
| 120 | $args['blog_charset'] = get_option( 'blog_charset' ); |
| 121 | $args['user_ip'] = $_SERVER['REMOTE_ADDR']; |
| 122 | $args['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
| 123 | $args['referrer'] = $_SERVER['HTTP_REFERER']; |
| 124 | $args['comment_type'] = 'contact-form'; |
| 125 | |
| 126 | $form_id = isset( $_POST['give-form-id'] ) ? absint( $_POST['give-form-id'] ) : 0; |
| 127 | $donor_last_name = ! empty( $_POST['give_last'] ) ? ' ' . give_clean( $_POST['give_last'] ) : ''; |
| 128 | |
| 129 | // Pass Donor comment if enabled. |
| 130 | if ( give_is_donor_comment_field_enabled( $form_id ) ) { |
| 131 | $give_comment = isset( $_POST['give_comment'] ) ? give_clean( $_POST['give_comment'] ) : ''; |
| 132 | |
| 133 | $args['comment_content'] = $give_comment; |
| 134 | } |
| 135 | |
| 136 | $ignore = [ 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ]; |
| 137 | |
| 138 | foreach ( $_SERVER as $key => $value ) { |
| 139 | if ( ! in_array( $key, $ignore, true ) ) { |
| 140 | $args[ $key ] = $value; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | $response = give_akismet_spam_check_post( $args ); |
| 145 | $spam = 'true' === $response[1]; |
| 146 | |
| 147 | // Log spam information. |
| 148 | if ( $spam && ! give_akismet_is_email_logged( $args['comment_author_email'] ) ) { |
| 149 | |
| 150 | $title = sprintf( |
| 151 | 'This donor\'s email (%1$s%2$s - %3$s) has been flagged as SPAM', |
| 152 | $args['comment_author'], |
| 153 | $donor_last_name, |
| 154 | $args['comment_author_email'] |
| 155 | ); |
| 156 | |
| 157 | $message = sprintf( |
| 158 | '<p><strong>%1$s</strong><pre>%2$s</pre></p><strong>%3$s</strong><pre>%4$s</pre><p>', |
| 159 | __( 'Request', 'give' ), |
| 160 | print_r( $args, true ), |
| 161 | __( 'Response', 'give' ), |
| 162 | print_r( $response, true ) |
| 163 | ); |
| 164 | |
| 165 | $context = [ |
| 166 | 'donor_email' => $args['comment_author_email'], |
| 167 | 'filter' => 'akismet', |
| 168 | 'message' => $message, |
| 169 | ]; |
| 170 | |
| 171 | Log::spam( $title, $context ); |
| 172 | } |
| 173 | |
| 174 | // It will return Akismet spam detect API response. |
| 175 | return $spam; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | add_filter( 'give_spam', 'give_akismet' ); |
| 180 | |
| 181 | /** |
| 182 | * Check Akismet API Key. |
| 183 | * |
| 184 | * @since 1.8.14 |
| 185 | * |
| 186 | * @return bool |
| 187 | */ |
| 188 | function give_check_akismet_key() { |
| 189 | if ( is_callable( [ 'Akismet', 'get_api_key' ] ) ) { // Akismet v3.0+ |
| 190 | return (bool) Akismet::get_api_key(); |
| 191 | } |
| 192 | |
| 193 | if ( function_exists( 'akismet_get_key' ) ) { |
| 194 | return (bool) akismet_get_key(); |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Detect spam through Akismet Comment API. |
| 202 | * |
| 203 | * @param array $args |
| 204 | * |
| 205 | * @return bool|mixed |
| 206 | * @since 1.8.14 |
| 207 | * @since 2.3.15 Refactor function to use give_akismet_spam_check_post |
| 208 | */ |
| 209 | function give_akismet_spam_check( $args ) { |
| 210 | $response = give_akismet_spam_check_post( $args ); |
| 211 | |
| 212 | // It's spam if response status is true. |
| 213 | $spam = 'true' === $response[1]; |
| 214 | |
| 215 | // Allow developer to modified Akismet spam detection response. |
| 216 | return apply_filters( 'give_akismet_spam_check', $spam, $args ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Detect spam through Akismet Comment API. |
| 221 | * |
| 222 | * @since 2.5.13 |
| 223 | * |
| 224 | * @param array $args |
| 225 | * |
| 226 | * @return array |
| 227 | */ |
| 228 | function give_akismet_spam_check_post( $args ) { |
| 229 | global $akismet_api_host, $akismet_api_port; |
| 230 | |
| 231 | $query_string = http_build_query( $args ); |
| 232 | |
| 233 | if ( is_callable( [ 'Akismet', 'http_post' ] ) ) { // Akismet v3.0+ |
| 234 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 235 | } else { |
| 236 | $response = akismet_http_post( |
| 237 | $query_string, |
| 238 | $akismet_api_host, |
| 239 | '/1.1/comment-check', |
| 240 | $akismet_api_port |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | return $response; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /** |
| 249 | * Check if email already logged or not |
| 250 | * |
| 251 | * @param $email |
| 252 | * |
| 253 | * @return bool |
| 254 | * @since 2.5.13 |
| 255 | */ |
| 256 | function give_akismet_is_email_logged( $email ) { |
| 257 | global $wpdb; |
| 258 | |
| 259 | return (bool) DB::get_var( |
| 260 | DB::prepare( |
| 261 | "SELECT COUNT(id) FROM {$wpdb->give_log} WHERE log_type = %s AND data LIKE '%s';", |
| 262 | LogType::SPAM, |
| 263 | '%' . esc_sql( $email ) . '%' |
| 264 | ) |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get list of whitelisted emails. |
| 270 | * |
| 271 | * @return array |
| 272 | * @since 2.5.13 |
| 273 | */ |
| 274 | function give_akismet_get_whitelisted_emails() { |
| 275 | return give_get_option( |
| 276 | 'akismet_whitelisted_email_addresses', |
| 277 | get_bloginfo( 'admin_email' ) |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Add support of RIAL code for backward compatibility. |
| 283 | * Note: for internal use only |
| 284 | * |
| 285 | * @since 1.8.17 |
| 286 | * |
| 287 | * @param array $currencies |
| 288 | * |
| 289 | * @return array |
| 290 | */ |
| 291 | function give_bc_v1817_iranian_currency_code( $currencies ) { |
| 292 | $currencies['RIAL'] = $currencies['IRR']; |
| 293 | |
| 294 | return $currencies; |
| 295 | } |
| 296 | |
| 297 | if ( ! give_has_upgrade_completed( 'v1817_update_donation_iranian_currency_code' ) ) { |
| 298 | add_filter( 'give_currencies', 'give_bc_v1817_iranian_currency_code', 0 ); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * Format right to left supported currency amount. |
| 304 | * |
| 305 | * @since 1.8.17 |
| 306 | * |
| 307 | * @param $formatted_amount |
| 308 | * @param $currency_args |
| 309 | * @param $price |
| 310 | * |
| 311 | * @return string |
| 312 | */ |
| 313 | function give_format_price_for_right_to_left_supported_currency( $formatted_amount, $currency_args, $price ) { |
| 314 | if ( ! give_is_right_to_left_supported_currency( $currency_args['currency_code'] ) ) { |
| 315 | return $formatted_amount; |
| 316 | } |
| 317 | |
| 318 | $formatted_amount = ( |
| 319 | 'before' === (string) $currency_args['position'] ? |
| 320 | '‫' . $price . $currency_args['symbol'] . '‬' : |
| 321 | '‪' . $price . $currency_args['symbol'] . '‬' |
| 322 | ); |
| 323 | |
| 324 | $formatted_amount = $currency_args['decode_currency'] ? |
| 325 | html_entity_decode( $formatted_amount, ENT_COMPAT, 'UTF-8' ) : |
| 326 | $formatted_amount; |
| 327 | |
| 328 | return $formatted_amount; |
| 329 | } |
| 330 | |
| 331 | add_filter( 'give_currency_filter', 'give_format_price_for_right_to_left_supported_currency', 10, 3 ); |
| 332 | |
| 333 | /** |
| 334 | * Validate active gateway value before returning result. |
| 335 | * |
| 336 | * @since 2.1.0 |
| 337 | * |
| 338 | * @param $value |
| 339 | * |
| 340 | * @return array |
| 341 | */ |
| 342 | function __give_validate_active_gateways( $value ) { |
| 343 | $gateways = array_keys( give_get_payment_gateways() ); |
| 344 | $active_gateways = is_array( $value ) ? array_keys( $value ) : []; |
| 345 | |
| 346 | // Remove deactivated payment gateways. |
| 347 | if ( ! empty( $active_gateways ) ) { |
| 348 | foreach ( $active_gateways as $index => $gateway_id ) { |
| 349 | if ( ! in_array( $gateway_id, $gateways ) ) { |
| 350 | unset( $value[ $gateway_id ] ); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if ( empty( $value ) ) { |
| 356 | /** |
| 357 | * Filter the default active gateway |
| 358 | * |
| 359 | * @since 2.1.0 |
| 360 | */ |
| 361 | $value = apply_filters( |
| 362 | 'give_default_active_gateways', |
| 363 | [ |
| 364 | 'manual' => 1, |
| 365 | ] |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | return $value; |
| 370 | } |
| 371 | |
| 372 | add_filter( 'give_get_option_gateways', '__give_validate_active_gateways', 10, 1 ); |
| 373 |