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