admin-actions.php
| 1 | <?php |
| 2 | |
| 3 | use Give\Framework\Database\DB; |
| 4 | use Give\Log\ValueObjects\LogType; |
| 5 | |
| 6 | /** |
| 7 | * Admin Actions |
| 8 | * |
| 9 | * @package Give |
| 10 | * @since 1.0 |
| 11 | * @copyright Copyright (c) 2016, GiveWP |
| 12 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 13 | * @subpackage Admin/Actions |
| 14 | */ |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Load wp editor by ajax. |
| 23 | * |
| 24 | * @since 1.8 |
| 25 | */ |
| 26 | function give_load_wp_editor() { |
| 27 | if ( ! isset( $_POST['wp_editor'] ) || ! current_user_can( 'edit_give_forms' ) ) { |
| 28 | die(); |
| 29 | } |
| 30 | |
| 31 | $wp_editor = json_decode( base64_decode( $_POST['wp_editor'] ), true ); |
| 32 | $wp_editor[2]['textarea_name'] = give_clean( $_POST['textarea_name'] ); |
| 33 | |
| 34 | wp_editor( wp_kses_post( $wp_editor[0] ), give_clean( $_POST['wp_editor_id'] ), $wp_editor[2] ); |
| 35 | |
| 36 | die(); |
| 37 | } |
| 38 | |
| 39 | add_action( 'wp_ajax_give_load_wp_editor', 'give_load_wp_editor' ); |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Redirect admin to clean url give admin pages. |
| 44 | * |
| 45 | * @since 1.8 |
| 46 | * @return bool |
| 47 | */ |
| 48 | function give_redirect_to_clean_url_admin_pages() { |
| 49 | // Give admin pages. |
| 50 | $give_pages = [ |
| 51 | 'give-payment-history', |
| 52 | 'give-donors', |
| 53 | 'give-reports', |
| 54 | 'give-tools', |
| 55 | ]; |
| 56 | |
| 57 | // Get current page. |
| 58 | $current_page = isset( $_GET['page'] ) ? esc_attr( $_GET['page'] ) : ''; |
| 59 | |
| 60 | // Bailout. |
| 61 | if ( |
| 62 | empty( $current_page ) |
| 63 | || empty( $_GET['_wp_http_referer'] ) |
| 64 | || ! in_array( $current_page, $give_pages ) |
| 65 | ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Verify current page request. |
| 71 | * |
| 72 | * @since 1.8 |
| 73 | */ |
| 74 | $redirect = apply_filters( "give_validate_{$current_page}", true ); |
| 75 | |
| 76 | if ( $redirect ) { |
| 77 | // Redirect. |
| 78 | wp_redirect( |
| 79 | remove_query_arg( |
| 80 | [ '_wp_http_referer', '_wpnonce' ], |
| 81 | wp_unslash( $_SERVER['REQUEST_URI'] ) |
| 82 | ) |
| 83 | ); |
| 84 | exit; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | add_action( 'admin_init', 'give_redirect_to_clean_url_admin_pages' ); |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Hide Outdated PHP Notice Shortly. |
| 93 | * |
| 94 | * This code is used with AJAX call to hide outdated PHP notice for a short period of time |
| 95 | * |
| 96 | * @since 1.8.9 |
| 97 | * @return void |
| 98 | */ |
| 99 | function give_hide_outdated_php_notice() { |
| 100 | |
| 101 | if ( ! isset( $_POST['_give_hide_outdated_php_notices_shortly'] ) || ! current_user_can( 'manage_give_settings' ) ) { |
| 102 | give_die(); |
| 103 | } |
| 104 | |
| 105 | // Transient key name. |
| 106 | $transient_key = '_give_hide_outdated_php_notices_shortly'; |
| 107 | |
| 108 | if ( Give_Cache::get( $transient_key, true ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | // Hide notice for 24 hours. |
| 113 | Give_Cache::set( $transient_key, true, DAY_IN_SECONDS, true ); |
| 114 | |
| 115 | give_die(); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | add_action( 'wp_ajax_give_hide_outdated_php_notice', 'give_hide_outdated_php_notice' ); |
| 120 | |
| 121 | /** |
| 122 | * Register admin notices. |
| 123 | * |
| 124 | * @since 1.8.9 |
| 125 | */ |
| 126 | function _give_register_admin_notices() { |
| 127 | // Bailout. |
| 128 | if ( ! is_admin() ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Bulk action notices. |
| 133 | if ( |
| 134 | isset( $_GET['action'] ) && |
| 135 | ! empty( $_GET['action'] ) |
| 136 | ) { |
| 137 | |
| 138 | // Add payment bulk notice. |
| 139 | if ( |
| 140 | current_user_can( 'edit_give_payments' ) && |
| 141 | isset( $_GET['payment'] ) && |
| 142 | ! empty( $_GET['payment'] ) |
| 143 | ) { |
| 144 | $payment_count = isset( $_GET['payment'] ) ? count( $_GET['payment'] ) : 0; |
| 145 | |
| 146 | switch ( $_GET['action'] ) { |
| 147 | case 'delete': |
| 148 | Give()->notices->register_notice( |
| 149 | [ |
| 150 | 'id' => 'bulk_action_delete', |
| 151 | 'type' => 'updated', |
| 152 | 'description' => sprintf( |
| 153 | _n( |
| 154 | 'Successfully deleted one donation.', |
| 155 | 'Successfully deleted %d donations.', |
| 156 | $payment_count, |
| 157 | 'give' |
| 158 | ), |
| 159 | $payment_count |
| 160 | ), |
| 161 | 'show' => true, |
| 162 | ] |
| 163 | ); |
| 164 | |
| 165 | break; |
| 166 | |
| 167 | case 'resend-receipt': |
| 168 | Give()->notices->register_notice( |
| 169 | [ |
| 170 | 'id' => 'bulk_action_resend_receipt', |
| 171 | 'type' => 'updated', |
| 172 | 'description' => sprintf( |
| 173 | _n( |
| 174 | 'Successfully sent email receipt to one recipient.', |
| 175 | 'Successfully sent email receipts to %d recipients.', |
| 176 | $payment_count, |
| 177 | 'give' |
| 178 | ), |
| 179 | $payment_count |
| 180 | ), |
| 181 | 'show' => true, |
| 182 | ] |
| 183 | ); |
| 184 | break; |
| 185 | |
| 186 | case 'set-status-publish': |
| 187 | case 'set-status-pending': |
| 188 | case 'set-status-processing': |
| 189 | case 'set-status-refunded': |
| 190 | case 'set-status-revoked': |
| 191 | case 'set-status-failed': |
| 192 | case 'set-status-cancelled': |
| 193 | case 'set-status-abandoned': |
| 194 | case 'set-status-preapproval': |
| 195 | Give()->notices->register_notice( |
| 196 | [ |
| 197 | 'id' => 'bulk_action_status_change', |
| 198 | 'type' => 'updated', |
| 199 | 'description' => _n( |
| 200 | 'Donation status updated successfully.', |
| 201 | 'Donation statuses updated successfully.', |
| 202 | $payment_count, |
| 203 | 'give' |
| 204 | ), |
| 205 | 'show' => true, |
| 206 | ] |
| 207 | ); |
| 208 | break; |
| 209 | }// End switch(). |
| 210 | }// End if(). |
| 211 | }// End if(). |
| 212 | |
| 213 | // Add give message notices. |
| 214 | $message_notices = give_get_admin_messages_key(); |
| 215 | if ( ! empty( $message_notices ) ) { |
| 216 | foreach ( $message_notices as $message_notice ) { |
| 217 | // Donation reports errors. |
| 218 | if ( current_user_can( 'view_give_reports' ) ) { |
| 219 | switch ( $message_notice ) { |
| 220 | case 'donation-deleted': |
| 221 | Give()->notices->register_notice( |
| 222 | [ |
| 223 | 'id' => 'give-donation-deleted', |
| 224 | 'type' => 'updated', |
| 225 | 'description' => __( 'The donation has been deleted.', 'give' ), |
| 226 | 'show' => true, |
| 227 | ] |
| 228 | ); |
| 229 | break; |
| 230 | case 'email-sent': |
| 231 | Give()->notices->register_notice( |
| 232 | [ |
| 233 | 'id' => 'give-email-sent', |
| 234 | 'type' => 'updated', |
| 235 | 'description' => __( 'The donation receipt has been resent.', 'give' ), |
| 236 | 'show' => true, |
| 237 | ] |
| 238 | ); |
| 239 | break; |
| 240 | case 'refreshed-reports': |
| 241 | Give()->notices->register_notice( |
| 242 | [ |
| 243 | 'id' => 'give-refreshed-reports', |
| 244 | 'type' => 'updated', |
| 245 | 'description' => __( 'The reports cache has been cleared.', 'give' ), |
| 246 | 'show' => true, |
| 247 | ] |
| 248 | ); |
| 249 | break; |
| 250 | case 'donation-note-deleted': |
| 251 | Give()->notices->register_notice( |
| 252 | [ |
| 253 | 'id' => 'give-donation-note-deleted', |
| 254 | 'type' => 'updated', |
| 255 | 'description' => __( 'The donation note has been deleted.', 'give' ), |
| 256 | 'show' => true, |
| 257 | ] |
| 258 | ); |
| 259 | break; |
| 260 | }// End switch(). |
| 261 | }// End if(). |
| 262 | |
| 263 | // Give settings notices and errors. |
| 264 | if ( current_user_can( 'manage_give_settings' ) ) { |
| 265 | switch ( $message_notice ) { |
| 266 | case 'settings-imported': |
| 267 | Give()->notices->register_notice( |
| 268 | [ |
| 269 | 'id' => 'give-settings-imported', |
| 270 | 'type' => 'updated', |
| 271 | 'description' => __( 'The settings have been imported.', 'give' ), |
| 272 | 'show' => true, |
| 273 | ] |
| 274 | ); |
| 275 | break; |
| 276 | case 'api-key-generated': |
| 277 | Give()->notices->register_notice( |
| 278 | [ |
| 279 | 'id' => 'give-api-key-generated', |
| 280 | 'type' => 'updated', |
| 281 | 'description' => __( 'API keys have been generated.', 'give' ), |
| 282 | 'show' => true, |
| 283 | ] |
| 284 | ); |
| 285 | break; |
| 286 | case 'api-key-exists': |
| 287 | Give()->notices->register_notice( |
| 288 | [ |
| 289 | 'id' => 'give-api-key-exists', |
| 290 | 'type' => 'updated', |
| 291 | 'description' => __( 'The specified user already has API keys.', 'give' ), |
| 292 | 'show' => true, |
| 293 | ] |
| 294 | ); |
| 295 | break; |
| 296 | case 'api-key-regenerated': |
| 297 | Give()->notices->register_notice( |
| 298 | [ |
| 299 | 'id' => 'give-api-key-regenerated', |
| 300 | 'type' => 'updated', |
| 301 | 'description' => __( 'API keys have been regenerated.', 'give' ), |
| 302 | 'show' => true, |
| 303 | ] |
| 304 | ); |
| 305 | break; |
| 306 | case 'api-key-revoked': |
| 307 | Give()->notices->register_notice( |
| 308 | [ |
| 309 | 'id' => 'give-api-key-revoked', |
| 310 | 'type' => 'updated', |
| 311 | 'description' => __( 'API keys have been revoked.', 'give' ), |
| 312 | 'show' => true, |
| 313 | ] |
| 314 | ); |
| 315 | break; |
| 316 | case 'sent-test-email': |
| 317 | Give()->notices->register_notice( |
| 318 | [ |
| 319 | 'id' => 'give-sent-test-email', |
| 320 | 'type' => 'updated', |
| 321 | 'description' => sprintf( __( 'The test email has been sent to %s.', 'give' ), wp_get_current_user()->user_email ), |
| 322 | 'show' => true, |
| 323 | ] |
| 324 | ); |
| 325 | break; |
| 326 | case 'matched-success-failure-page': |
| 327 | Give()->notices->register_notice( |
| 328 | [ |
| 329 | 'id' => 'give-matched-success-failure-page', |
| 330 | 'type' => 'updated', |
| 331 | 'description' => __( 'You cannot set the success and failed pages to the same page', 'give' ), |
| 332 | 'show' => true, |
| 333 | ] |
| 334 | ); |
| 335 | break; |
| 336 | case 'akismet-deblacklisted-email': |
| 337 | Give()->notices->register_notice( |
| 338 | [ |
| 339 | 'id' => 'give-akismet-deblacklisted-email', |
| 340 | 'type' => 'updated', |
| 341 | 'description' => __( 'Email de-blacklisted successfully. Now Donor will able to process donation with email flagged as spam', 'give' ), |
| 342 | 'show' => true, |
| 343 | 'dismissible' => 'auto', |
| 344 | ] |
| 345 | ); |
| 346 | break; |
| 347 | }// End switch(). |
| 348 | }// End if(). |
| 349 | |
| 350 | // Payments errors. |
| 351 | if ( current_user_can( 'edit_give_payments' ) ) { |
| 352 | switch ( $message_notice ) { |
| 353 | case 'note-added': |
| 354 | Give()->notices->register_notice( |
| 355 | [ |
| 356 | 'id' => 'give-note-added', |
| 357 | 'type' => 'updated', |
| 358 | 'description' => __( 'The donation note has been added.', 'give' ), |
| 359 | 'show' => true, |
| 360 | ] |
| 361 | ); |
| 362 | break; |
| 363 | case 'payment-updated': |
| 364 | Give()->notices->register_notice( |
| 365 | [ |
| 366 | 'id' => 'give-payment-updated', |
| 367 | 'type' => 'updated', |
| 368 | 'description' => __( 'The donation has been updated.', 'give' ), |
| 369 | 'show' => true, |
| 370 | ] |
| 371 | ); |
| 372 | break; |
| 373 | }// End switch(). |
| 374 | }// End if(). |
| 375 | |
| 376 | // Donor Notices. |
| 377 | if ( current_user_can( 'edit_give_payments' ) ) { |
| 378 | switch ( $message_notice ) { |
| 379 | case 'donor-deleted': |
| 380 | Give()->notices->register_notice( |
| 381 | [ |
| 382 | 'id' => 'give-donor-deleted', |
| 383 | 'type' => 'updated', |
| 384 | 'description' => __( 'The selected donor(s) has been deleted.', 'give' ), |
| 385 | 'show' => true, |
| 386 | ] |
| 387 | ); |
| 388 | break; |
| 389 | |
| 390 | case 'donor-donations-deleted': |
| 391 | Give()->notices->register_notice( |
| 392 | [ |
| 393 | 'id' => 'give-donor-donations-deleted', |
| 394 | 'type' => 'updated', |
| 395 | 'description' => __( 'The selected donor(s) and the associated donation(s) has been deleted.', 'give' ), |
| 396 | 'show' => true, |
| 397 | ] |
| 398 | ); |
| 399 | break; |
| 400 | |
| 401 | case 'confirm-delete-donor': |
| 402 | Give()->notices->register_notice( |
| 403 | [ |
| 404 | 'id' => 'give-confirm-delete-donor', |
| 405 | 'type' => 'updated', |
| 406 | 'description' => __( 'You must confirm to delete the selected donor(s).', 'give' ), |
| 407 | 'show' => true, |
| 408 | ] |
| 409 | ); |
| 410 | break; |
| 411 | |
| 412 | case 'invalid-donor-id': |
| 413 | Give()->notices->register_notice( |
| 414 | [ |
| 415 | 'id' => 'give-invalid-donor-id', |
| 416 | 'type' => 'updated', |
| 417 | 'description' => __( 'Invalid Donor ID.', 'give' ), |
| 418 | 'show' => true, |
| 419 | ] |
| 420 | ); |
| 421 | break; |
| 422 | |
| 423 | case 'donor-delete-failed': |
| 424 | Give()->notices->register_notice( |
| 425 | [ |
| 426 | 'id' => 'give-donor-delete-failed', |
| 427 | 'type' => 'error', |
| 428 | 'description' => __( 'Unable to delete selected donor(s).', 'give' ), |
| 429 | 'show' => true, |
| 430 | ] |
| 431 | ); |
| 432 | break; |
| 433 | |
| 434 | case 'email-added': |
| 435 | Give()->notices->register_notice( |
| 436 | [ |
| 437 | 'id' => 'give-email-added', |
| 438 | 'type' => 'updated', |
| 439 | 'description' => __( 'Donor email added.', 'give' ), |
| 440 | 'show' => true, |
| 441 | ] |
| 442 | ); |
| 443 | break; |
| 444 | |
| 445 | case 'email-removed': |
| 446 | Give()->notices->register_notice( |
| 447 | [ |
| 448 | 'id' => 'give-email-removed', |
| 449 | 'type' => 'updated', |
| 450 | 'description' => __( 'Donor email removed.', 'give' ), |
| 451 | 'show' => true, |
| 452 | ] |
| 453 | ); |
| 454 | break; |
| 455 | |
| 456 | case 'email-remove-failed': |
| 457 | Give()->notices->register_notice( |
| 458 | [ |
| 459 | 'id' => 'give-email-remove-failed', |
| 460 | 'type' => 'updated', |
| 461 | 'description' => __( 'Failed to remove donor email.', 'give' ), |
| 462 | 'show' => true, |
| 463 | ] |
| 464 | ); |
| 465 | break; |
| 466 | |
| 467 | case 'primary-email-updated': |
| 468 | Give()->notices->register_notice( |
| 469 | [ |
| 470 | 'id' => 'give-primary-email-updated', |
| 471 | 'type' => 'updated', |
| 472 | 'description' => __( 'Primary email updated for donor.', 'give' ), |
| 473 | 'show' => true, |
| 474 | ] |
| 475 | ); |
| 476 | break; |
| 477 | |
| 478 | case 'primary-email-failed': |
| 479 | Give()->notices->register_notice( |
| 480 | [ |
| 481 | 'id' => 'give-primary-email-failed', |
| 482 | 'type' => 'updated', |
| 483 | 'description' => __( 'Failed to set primary email.', 'give' ), |
| 484 | 'show' => true, |
| 485 | ] |
| 486 | ); |
| 487 | break; |
| 488 | |
| 489 | case 'reconnect-user': |
| 490 | Give()->notices->register_notice( |
| 491 | [ |
| 492 | 'id' => 'give-reconnect-user', |
| 493 | 'type' => 'updated', |
| 494 | 'description' => __( 'User has been successfully connected with Donor.', 'give' ), |
| 495 | 'show' => true, |
| 496 | ] |
| 497 | ); |
| 498 | break; |
| 499 | |
| 500 | case 'disconnect-user': |
| 501 | Give()->notices->register_notice( |
| 502 | [ |
| 503 | 'id' => 'give-disconnect-user', |
| 504 | 'type' => 'updated', |
| 505 | 'description' => __( 'User has been successfully disconnected from donor.', 'give' ), |
| 506 | 'show' => true, |
| 507 | ] |
| 508 | ); |
| 509 | break; |
| 510 | |
| 511 | case 'profile-updated': |
| 512 | Give()->notices->register_notice( |
| 513 | [ |
| 514 | 'id' => 'give-profile-updated', |
| 515 | 'type' => 'updated', |
| 516 | 'description' => __( 'Donor information updated successfully.', 'give' ), |
| 517 | 'show' => true, |
| 518 | ] |
| 519 | ); |
| 520 | break; |
| 521 | }// End switch(). |
| 522 | }// End if(). |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Spam log admin notice |
| 528 | */ |
| 529 | if ( |
| 530 | current_user_can( 'manage_give_settings' ) && |
| 531 | give_is_setting_enabled( give_get_option( 'akismet_spam_protection' ) ) |
| 532 | ) { |
| 533 | global $wpdb; |
| 534 | |
| 535 | $current_time = current_time( 'timestamp' ); |
| 536 | $end_of_current_time_in_gmt = get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( 'tomorrow', $current_time ) ), 'U' ); |
| 537 | $current_time_gmt = get_gmt_from_date( date( 'Y-m-d H:i:s', $current_time ), 'U' ); |
| 538 | |
| 539 | $spam_count = DB::get_var( |
| 540 | DB::prepare( "SELECT COUNT(id) FROM {$wpdb->give_log} WHERE log_type = %s AND date >= CURDATE();", LogType::SPAM ) |
| 541 | ); |
| 542 | |
| 543 | if ( $spam_count && ! Give_Admin_Settings::is_setting_page( 'logs', 'spam' ) ) { |
| 544 | Give()->notices->register_notice( |
| 545 | [ |
| 546 | 'id' => 'give-new-akismet-spam-found', |
| 547 | 'type' => 'warning', |
| 548 | 'description' => sprintf( |
| 549 | __( 'Akismet flagged %1$s %2$s as spam. If you believe %7$s %5$s actual %6$s, you can whitelist %7$s to allow the %6$s to process donations. <a href="%3$s" title="%4$s">Click here</a> to review spam logs.', 'give' ), |
| 550 | $spam_count, |
| 551 | _n( 'donor email', 'donor emails', $spam_count, 'give' ), |
| 552 | esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-tools&tab=logs§ion=spam' ) ), |
| 553 | __( 'Go to spam log list page', 'give' ), |
| 554 | _n( 'was', 'were', $spam_count, 'give' ), |
| 555 | _n( 'donor', 'donors', $spam_count, 'give' ), |
| 556 | _n( 'this', 'these', $spam_count, 'give' ) |
| 557 | ), |
| 558 | 'dismissible_type' => 'user', |
| 559 | 'dismiss_interval' => 'custom', |
| 560 | 'dismiss_interval_time' => $end_of_current_time_in_gmt - $current_time_gmt, |
| 561 | ] |
| 562 | ); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | add_action( 'admin_notices', '_give_register_admin_notices', - 1 ); |
| 568 | |
| 569 | |
| 570 | /** |
| 571 | * Display admin bar when active. |
| 572 | * |
| 573 | * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference. |
| 574 | * |
| 575 | * @return bool |
| 576 | */ |
| 577 | function _give_show_test_mode_notice_in_admin_bar( $wp_admin_bar ) { |
| 578 | $is_test_mode = ! empty( $_POST['test_mode'] ) ? |
| 579 | give_is_setting_enabled( $_POST['test_mode'] ) : |
| 580 | give_is_test_mode(); |
| 581 | |
| 582 | if ( |
| 583 | ! current_user_can( 'view_give_reports' ) || |
| 584 | ! $is_test_mode |
| 585 | ) { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | // Add the main site admin menu item. |
| 590 | $wp_admin_bar->add_menu( |
| 591 | [ |
| 592 | 'id' => 'give-test-notice', |
| 593 | 'href' => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ), |
| 594 | 'parent' => 'top-secondary', |
| 595 | 'title' => __( 'GiveWP Test Mode Active', 'give' ), |
| 596 | 'meta' => [ |
| 597 | 'class' => 'give-test-mode-active', |
| 598 | ], |
| 599 | ] |
| 600 | ); |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | add_action( 'admin_bar_menu', '_give_show_test_mode_notice_in_admin_bar', 1000, 1 ); |
| 606 | |
| 607 | /** |
| 608 | * Outputs the Give admin bar CSS. |
| 609 | */ |
| 610 | function _give_test_mode_notice_admin_bar_css() { |
| 611 | if ( ! give_is_test_mode() ) { |
| 612 | return; |
| 613 | } |
| 614 | ?> |
| 615 | <style> |
| 616 | #wpadminbar .give-test-mode-active > .ab-item { |
| 617 | color: #fff; |
| 618 | background-color: #ffba00; |
| 619 | } |
| 620 | |
| 621 | #wpadminbar .give-test-mode-active:hover > .ab-item, #wpadminbar .give-test-mode-active:hover > .ab-item { |
| 622 | background-color: rgba(203, 144, 0, 1) !important; |
| 623 | color: #fff !important; |
| 624 | } |
| 625 | </style> |
| 626 | <?php |
| 627 | } |
| 628 | |
| 629 | add_action( 'admin_head', '_give_test_mode_notice_admin_bar_css' ); |
| 630 | |
| 631 | |
| 632 | /** |
| 633 | * Add Link to Import page in from donation archive and donation single page |
| 634 | * |
| 635 | * @since 1.8.13 |
| 636 | */ |
| 637 | function give_import_page_link_callback() { |
| 638 | ?> |
| 639 | <a href="<?php echo esc_url( give_import_page_url() ); ?>" |
| 640 | class="page-import-action page-title-action"><?php _e( 'Import Donations', 'give' ); ?></a> |
| 641 | |
| 642 | <?php |
| 643 | // Check if view donation single page only. |
| 644 | if ( ! empty( $_REQUEST['view'] ) && 'view-payment-details' === (string) give_clean( $_REQUEST['view'] ) && 'give-payment-history' === give_clean( $_REQUEST['page'] ) ) { |
| 645 | ?> |
| 646 | <style type="text/css"> |
| 647 | .wrap #transaction-details-heading { |
| 648 | display: inline-block; |
| 649 | } |
| 650 | </style> |
| 651 | <?php |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | add_action( 'give_payments_page_top', 'give_import_page_link_callback', 11 ); |
| 656 | |
| 657 | /** |
| 658 | * Load donation import ajax callback |
| 659 | * Fire when importing from CSV start |
| 660 | * |
| 661 | * @since 1.8.13 |
| 662 | */ |
| 663 | function give_donation_import_callback() { |
| 664 | // Bailout. |
| 665 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 666 | give_die(); |
| 667 | } |
| 668 | |
| 669 | // Disable Give cache |
| 670 | Give_Cache::get_instance()->disable(); |
| 671 | |
| 672 | $import_setting = []; |
| 673 | $fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null; |
| 674 | |
| 675 | parse_str( $fields, $output ); |
| 676 | |
| 677 | $import_setting['create_user'] = $output['create_user']; |
| 678 | $import_setting['mode'] = $output['mode']; |
| 679 | $import_setting['delimiter'] = $output['delimiter']; |
| 680 | $import_setting['csv'] = $output['csv']; |
| 681 | $import_setting['delete_csv'] = $output['delete_csv']; |
| 682 | $import_setting['dry_run'] = $output['dry_run']; |
| 683 | |
| 684 | // Parent key id. |
| 685 | $main_key = maybe_unserialize( $output['main_key'] ); |
| 686 | |
| 687 | $current = absint( $_REQUEST['current'] ); |
| 688 | $total_ajax = absint( $_REQUEST['total_ajax'] ); |
| 689 | $start = absint( $_REQUEST['start'] ); |
| 690 | $end = absint( $_REQUEST['end'] ); |
| 691 | $next = absint( $_REQUEST['next'] ); |
| 692 | $total = absint( $_REQUEST['total'] ); |
| 693 | $per_page = absint( $_REQUEST['per_page'] ); |
| 694 | if ( empty( $output['delimiter'] ) ) { |
| 695 | $delimiter = ','; |
| 696 | } else { |
| 697 | $delimiter = $output['delimiter']; |
| 698 | } |
| 699 | |
| 700 | // Processing done here. |
| 701 | $raw_data = give_get_donation_data_from_csv( $output['csv'], $start, $end, $delimiter ); |
| 702 | $raw_key = maybe_unserialize( $output['mapto'] ); |
| 703 | $import_setting['raw_key'] = $raw_key; |
| 704 | |
| 705 | if ( ! empty( $output['dry_run'] ) ) { |
| 706 | $import_setting['csv_raw_data'] = give_get_donation_data_from_csv( $output['csv'], 1, $end, $delimiter ); |
| 707 | |
| 708 | $import_setting['donors_list'] = Give()->donors->get_donors( |
| 709 | [ |
| 710 | 'number' => - 1, |
| 711 | 'fields' => [ 'id', 'user_id', 'email' ], |
| 712 | ] |
| 713 | ); |
| 714 | } |
| 715 | |
| 716 | // Prevent normal emails. |
| 717 | remove_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 ); |
| 718 | remove_action( 'give_insert_user', 'give_new_user_notification', 10 ); |
| 719 | remove_action( 'give_insert_payment', 'give_payment_save_page_data' ); |
| 720 | |
| 721 | $current_key = $start; |
| 722 | foreach ( $raw_data as $row_data ) { |
| 723 | $import_setting['donation_key'] = $current_key; |
| 724 | give_save_import_donation_to_db( $raw_key, $row_data, $main_key, $import_setting ); |
| 725 | $current_key ++; |
| 726 | } |
| 727 | |
| 728 | // Check if function exists or not. |
| 729 | if ( function_exists( 'give_payment_save_page_data' ) ) { |
| 730 | add_action( 'give_insert_payment', 'give_payment_save_page_data' ); |
| 731 | } |
| 732 | add_action( 'give_insert_user', 'give_new_user_notification', 10, 2 ); |
| 733 | add_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 ); |
| 734 | |
| 735 | if ( $next == false ) { |
| 736 | $json_data = [ |
| 737 | 'success' => true, |
| 738 | 'message' => __( 'All donation uploaded successfully!', 'give' ), |
| 739 | ]; |
| 740 | } else { |
| 741 | $index_start = $start; |
| 742 | $index_end = $end; |
| 743 | $last = false; |
| 744 | $next = true; |
| 745 | if ( $next ) { |
| 746 | $index_start = $index_start + $per_page; |
| 747 | $index_end = $per_page + ( $index_start - 1 ); |
| 748 | } |
| 749 | if ( $index_end >= $total ) { |
| 750 | $index_end = $total; |
| 751 | $last = true; |
| 752 | } |
| 753 | $json_data = [ |
| 754 | 'raw_data' => $raw_data, |
| 755 | 'raw_key' => $raw_key, |
| 756 | 'next' => $next, |
| 757 | 'start' => $index_start, |
| 758 | 'end' => $index_end, |
| 759 | 'last' => $last, |
| 760 | ]; |
| 761 | } |
| 762 | |
| 763 | $url = give_import_page_url( |
| 764 | [ |
| 765 | 'step' => '4', |
| 766 | 'importer-type' => 'import_donations', |
| 767 | 'csv' => $output['csv'], |
| 768 | 'total' => $total, |
| 769 | 'delete_csv' => $import_setting['delete_csv'], |
| 770 | 'success' => ( isset( $json_data['success'] ) ? $json_data['success'] : '' ), |
| 771 | 'dry_run' => $output['dry_run'], |
| 772 | ] |
| 773 | ); |
| 774 | $json_data['url'] = $url; |
| 775 | |
| 776 | $current ++; |
| 777 | $json_data['current'] = $current; |
| 778 | |
| 779 | $percentage = ( 100 / ( $total_ajax + 1 ) ) * $current; |
| 780 | $json_data['percentage'] = $percentage; |
| 781 | |
| 782 | // Enable Give cache |
| 783 | Give_Cache::get_instance()->enable(); |
| 784 | |
| 785 | $json_data = apply_filters( 'give_import_ajax_responces', $json_data, $fields ); |
| 786 | wp_die( json_encode( $json_data ) ); |
| 787 | } |
| 788 | |
| 789 | add_action( 'wp_ajax_give_donation_import', 'give_donation_import_callback' ); |
| 790 | |
| 791 | /** |
| 792 | * Load core settings import ajax callback |
| 793 | * Fire when importing from JSON start |
| 794 | * |
| 795 | * @since 1.8.17 |
| 796 | */ |
| 797 | |
| 798 | function give_core_settings_import_callback() { |
| 799 | // Bailout. |
| 800 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 801 | give_die(); |
| 802 | } |
| 803 | |
| 804 | $fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null; |
| 805 | parse_str( $fields, $fields ); |
| 806 | |
| 807 | $json_data['success'] = false; |
| 808 | |
| 809 | /** |
| 810 | * Filter to Modify fields that are being pass by the ajax before importing of the core setting start. |
| 811 | * |
| 812 | * @access public |
| 813 | * |
| 814 | * @since 1.8.17 |
| 815 | * |
| 816 | * @param array $fields |
| 817 | * |
| 818 | * @return array $fields |
| 819 | */ |
| 820 | $fields = (array) apply_filters( 'give_import_core_settings_fields', $fields ); |
| 821 | |
| 822 | $file_name = ( ! empty( $fields['file_name'] ) ? give_clean( $fields['file_name'] ) : false ); |
| 823 | |
| 824 | if ( ! empty( $file_name ) ) { |
| 825 | $type = ( ! empty( $fields['type'] ) ? give_clean( $fields['type'] ) : 'merge' ); |
| 826 | |
| 827 | // Get the json data from the file and then alter it in array format |
| 828 | $json_string = give_get_core_settings_json( $file_name ); |
| 829 | $json_to_array = json_decode( $json_string, true ); |
| 830 | |
| 831 | // get the current setting from the options table. |
| 832 | $host_give_options = Give_Cache_Setting::get_settings(); |
| 833 | |
| 834 | // Save old settins for backup. |
| 835 | update_option( 'give_settings_old', $host_give_options, false ); |
| 836 | |
| 837 | /** |
| 838 | * Filter to Modify Core Settings that are being going to get import in options table as give settings. |
| 839 | * |
| 840 | * @access public |
| 841 | * |
| 842 | * @since 1.8.17 |
| 843 | * |
| 844 | * @param array $type Type of Import |
| 845 | * @param array $host_give_options Setting old setting that used to be in the options table. |
| 846 | * @param array $fields Data that is being send from the ajax |
| 847 | * |
| 848 | * @param array $json_to_array Setting that are being going to get imported |
| 849 | * |
| 850 | * @return array $json_to_array Setting that are being going to get imported |
| 851 | */ |
| 852 | $json_to_array = (array) apply_filters( 'give_import_core_settings_data', $json_to_array, $type, $host_give_options, $fields ); |
| 853 | |
| 854 | update_option( 'give_settings', $json_to_array, false ); |
| 855 | |
| 856 | $json_data['success'] = true; |
| 857 | } |
| 858 | |
| 859 | $json_data['percentage'] = 100; |
| 860 | |
| 861 | /** |
| 862 | * Filter to Modify core import setting page url |
| 863 | * |
| 864 | * @access public |
| 865 | * |
| 866 | * @since 1.8.17 |
| 867 | * @return array $url |
| 868 | */ |
| 869 | $json_data['url'] = give_import_page_url( |
| 870 | (array) apply_filters( |
| 871 | 'give_import_core_settings_success_url', |
| 872 | [ |
| 873 | 'step' => ( empty( $json_data['success'] ) ? '1' : '3' ), |
| 874 | 'importer-type' => 'import_core_setting', |
| 875 | 'success' => ( empty( $json_data['success'] ) ? '0' : '1' ), |
| 876 | ] |
| 877 | ) |
| 878 | ); |
| 879 | |
| 880 | wp_send_json( $json_data ); |
| 881 | } |
| 882 | |
| 883 | add_action( 'wp_ajax_give_core_settings_import', 'give_core_settings_import_callback' ); |
| 884 | |
| 885 | /** |
| 886 | * Initializes blank slate content if a list table is empty. |
| 887 | * |
| 888 | * @since 1.8.13 |
| 889 | */ |
| 890 | function give_blank_slate() { |
| 891 | $blank_slate = new Give_Blank_Slate(); |
| 892 | $blank_slate->init(); |
| 893 | } |
| 894 | |
| 895 | add_action( 'current_screen', 'give_blank_slate' ); |
| 896 | |
| 897 | /** |
| 898 | * Validate Fields of User Profile |
| 899 | * |
| 900 | * @since 2.0 |
| 901 | * |
| 902 | * @param int|bool $update True or False. |
| 903 | * @param object $user WP User Data. |
| 904 | * |
| 905 | * @param object $errors Object of WP Errors. |
| 906 | * |
| 907 | * @return mixed |
| 908 | */ |
| 909 | function give_validate_user_profile( $errors, $update, $user ) { |
| 910 | |
| 911 | if ( ! empty( $_POST['action'] ) && ( 'adduser' === $_POST['action'] || 'createuser' === $_POST['action'] ) ) { |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | if ( ! empty( $user->ID ) ) { |
| 916 | $donor = Give()->donors->get_donor_by( 'user_id', $user->ID ); |
| 917 | |
| 918 | if ( $donor ) { |
| 919 | // If Donor is attached with User, then validate first name. |
| 920 | if ( empty( $_POST['first_name'] ) ) { |
| 921 | $errors->add( |
| 922 | 'empty_first_name', |
| 923 | sprintf( |
| 924 | '<strong>%1$s:</strong> %2$s', |
| 925 | __( 'ERROR', 'give' ), |
| 926 | __( 'Please enter your first name.', 'give' ) |
| 927 | ) |
| 928 | ); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | } |
| 934 | |
| 935 | add_action( 'user_profile_update_errors', 'give_validate_user_profile', 10, 3 ); |
| 936 | |
| 937 | /** |
| 938 | * Show Donor Information on User Profile Page. |
| 939 | * |
| 940 | * @since 2.0 |
| 941 | * |
| 942 | * @param object $user User Object. |
| 943 | * |
| 944 | */ |
| 945 | function give_donor_information_profile_fields( $user ) { |
| 946 | $donor = Give()->donors->get_donor_by( 'user_id', $user->ID ); |
| 947 | |
| 948 | // Display Donor Information, only if donor is attached with User. |
| 949 | if ( ! empty( $donor->user_id ) ) : |
| 950 | ?> |
| 951 | <tr> |
| 952 | <th scope="row"><?php _e( 'Donor', 'give' ); ?></th> |
| 953 | <td> |
| 954 | <a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ); ?>"> |
| 955 | <?php _e( 'View Donor Information', 'give' ); ?> |
| 956 | </a> |
| 957 | </td> |
| 958 | </tr> |
| 959 | <?php |
| 960 | endif; |
| 961 | } |
| 962 | |
| 963 | add_action( 'personal_options', 'give_donor_information_profile_fields' ); |
| 964 | /** |
| 965 | * Get Array of WP User Roles. |
| 966 | * |
| 967 | * @since 1.8.13 |
| 968 | * @return array |
| 969 | */ |
| 970 | function give_get_user_roles() { |
| 971 | $user_roles = []; |
| 972 | |
| 973 | // Loop through User Roles. |
| 974 | foreach ( get_editable_roles() as $role_name => $role_info ) : |
| 975 | $user_roles[ $role_name ] = $role_info['name']; |
| 976 | endforeach; |
| 977 | |
| 978 | return $user_roles; |
| 979 | } |
| 980 | |
| 981 | |
| 982 | /** |
| 983 | * Ajax handle for donor address. |
| 984 | * |
| 985 | * @since 2.0 |
| 986 | * @since 2.11.0 decode url before parsing and sanitizing url when set $post. |
| 987 | * @return void |
| 988 | */ |
| 989 | function __give_ajax_donor_manage_addresses() { |
| 990 | // Bailout. |
| 991 | if ( |
| 992 | empty( $_POST['form'] ) || |
| 993 | empty( $_POST['donorID'] ) |
| 994 | ) { |
| 995 | wp_send_json_error( |
| 996 | [ |
| 997 | 'error' => 1, |
| 998 | ] |
| 999 | ); |
| 1000 | } |
| 1001 | |
| 1002 | $post = give_clean( wp_parse_args( urldecode_deep( $_POST ) ) ); |
| 1003 | $donorID = absint( $post['donorID'] ); |
| 1004 | $form_data = give_clean( wp_parse_args( $post['form'] ) ); |
| 1005 | $is_multi_address_type = ( 'billing' === $form_data['address-id'] || false !== strpos( $form_data['address-id'], '_' ) ); |
| 1006 | $exploded_address_id = explode( '_', $form_data['address-id'] ); |
| 1007 | $address_type = false !== strpos( $form_data['address-id'], '_' ) ? |
| 1008 | array_shift( $exploded_address_id ) : |
| 1009 | $form_data['address-id']; |
| 1010 | $address_id = false !== strpos( $form_data['address-id'], '_' ) ? |
| 1011 | array_pop( $exploded_address_id ) : |
| 1012 | null; |
| 1013 | $response_data = [ |
| 1014 | 'action' => $form_data['address-action'], |
| 1015 | 'id' => $form_data['address-id'], |
| 1016 | ]; |
| 1017 | |
| 1018 | // Security check. |
| 1019 | if ( ! wp_verify_nonce( $form_data['_wpnonce'], 'give-manage-donor-addresses' ) ) { |
| 1020 | wp_send_json_error( |
| 1021 | [ |
| 1022 | 'error' => 1, |
| 1023 | 'error_msg' => wp_sprintf( |
| 1024 | '<div class="notice notice-error"><p>%s</p></div>', |
| 1025 | __( 'Error: Security issue.', 'give' ) |
| 1026 | ), |
| 1027 | ] |
| 1028 | ); |
| 1029 | } |
| 1030 | |
| 1031 | $donor = new Give_Donor( $donorID ); |
| 1032 | |
| 1033 | // Verify donor. |
| 1034 | if ( ! $donor->id ) { |
| 1035 | wp_send_json_error( |
| 1036 | [ |
| 1037 | 'error' => 3, |
| 1038 | ] |
| 1039 | ); |
| 1040 | } |
| 1041 | |
| 1042 | // Unset all data except address. |
| 1043 | unset( |
| 1044 | $form_data['_wpnonce'], |
| 1045 | $form_data['address-action'], |
| 1046 | $form_data['address-id'] |
| 1047 | ); |
| 1048 | |
| 1049 | // Process action. |
| 1050 | switch ( $response_data['action'] ) { |
| 1051 | |
| 1052 | case 'add': |
| 1053 | if ( ! $donor->add_address( "{$address_type}[]", $form_data ) ) { |
| 1054 | wp_send_json_error( |
| 1055 | [ |
| 1056 | 'error' => 1, |
| 1057 | 'error_msg' => wp_sprintf( |
| 1058 | '<div class="notice notice-error"><p>%s</p></div>', |
| 1059 | __( 'Error: Unable to save the address. Please check if address already exist.', 'give' ) |
| 1060 | ), |
| 1061 | ] |
| 1062 | ); |
| 1063 | } |
| 1064 | |
| 1065 | $total_addresses = count( $donor->address[ $address_type ] ); |
| 1066 | |
| 1067 | $address_index = $is_multi_address_type ? |
| 1068 | $total_addresses - 1 : |
| 1069 | $address_type; |
| 1070 | |
| 1071 | $array_keys = array_keys( $donor->address[ $address_type ] ); |
| 1072 | |
| 1073 | $address_id = $is_multi_address_type ? |
| 1074 | end( $array_keys ) : |
| 1075 | $address_type; |
| 1076 | |
| 1077 | $response_data['address_html'] = __give_get_format_address( |
| 1078 | end( $donor->address['billing'] ), |
| 1079 | [ |
| 1080 | // We can add only billing address from donor screen. |
| 1081 | 'type' => 'billing', |
| 1082 | 'id' => $address_id, |
| 1083 | 'index' => ++ $address_index, |
| 1084 | ] |
| 1085 | ); |
| 1086 | $response_data['success_msg'] = wp_sprintf( |
| 1087 | '<div class="notice updated"><p>%s</p></div>', |
| 1088 | __( 'Successfully added a new address to the donor.', 'give' ) |
| 1089 | ); |
| 1090 | |
| 1091 | if ( $is_multi_address_type ) { |
| 1092 | $response_data['id'] = "{$response_data['id']}_{$address_index}"; |
| 1093 | } |
| 1094 | |
| 1095 | break; |
| 1096 | |
| 1097 | case 'remove': |
| 1098 | if ( ! $donor->remove_address( $response_data['id'] ) ) { |
| 1099 | wp_send_json_error( |
| 1100 | [ |
| 1101 | 'error' => 2, |
| 1102 | 'error_msg' => wp_sprintf( |
| 1103 | '<div class="notice notice-error"><p>%s</p></div>', |
| 1104 | __( 'Error: Unable to delete address.', 'give' ) |
| 1105 | ), |
| 1106 | ] |
| 1107 | ); |
| 1108 | } |
| 1109 | |
| 1110 | $response_data['success_msg'] = wp_sprintf( |
| 1111 | '<div class="notice updated"><p>%s</p></div>', |
| 1112 | __( 'Successfully removed a address of donor.', 'give' ) |
| 1113 | ); |
| 1114 | |
| 1115 | break; |
| 1116 | |
| 1117 | case 'update': |
| 1118 | if ( ! $donor->update_address( $response_data['id'], $form_data ) ) { |
| 1119 | wp_send_json_error( |
| 1120 | [ |
| 1121 | 'error' => 3, |
| 1122 | 'error_msg' => wp_sprintf( |
| 1123 | '<div class="notice notice-error"><p>%s</p></div>', |
| 1124 | __( 'Error: Unable to update address. Please check if address already exist.', 'give' ) |
| 1125 | ), |
| 1126 | ] |
| 1127 | ); |
| 1128 | } |
| 1129 | |
| 1130 | $response_data['address_html'] = __give_get_format_address( |
| 1131 | $is_multi_address_type ? |
| 1132 | $donor->address[ $address_type ][ $address_id ] : |
| 1133 | $donor->address[ $address_type ], |
| 1134 | [ |
| 1135 | 'type' => $address_type, |
| 1136 | 'id' => $address_id, |
| 1137 | 'index' => $address_id, |
| 1138 | ] |
| 1139 | ); |
| 1140 | $response_data['success_msg'] = wp_sprintf( |
| 1141 | '<div class="notice updated"><p>%s</p></div>', |
| 1142 | __( 'Successfully updated a address of donor', 'give' ) |
| 1143 | ); |
| 1144 | |
| 1145 | break; |
| 1146 | }// End switch(). |
| 1147 | |
| 1148 | wp_send_json_success( $response_data ); |
| 1149 | } |
| 1150 | |
| 1151 | add_action( 'wp_ajax_donor_manage_addresses', '__give_ajax_donor_manage_addresses' ); |
| 1152 | |
| 1153 | /** |
| 1154 | * Admin donor billing address label |
| 1155 | * |
| 1156 | * @since 2.0 |
| 1157 | * |
| 1158 | * @param string $address_label |
| 1159 | * |
| 1160 | * @return string |
| 1161 | */ |
| 1162 | function __give_donor_billing_address_label( $address_label ) { |
| 1163 | $address_label = __( 'Billing Address', 'give' ); |
| 1164 | |
| 1165 | return $address_label; |
| 1166 | } |
| 1167 | |
| 1168 | add_action( 'give_donor_billing_address_label', '__give_donor_billing_address_label' ); |
| 1169 | |
| 1170 | /** |
| 1171 | * Admin donor personal address label |
| 1172 | * |
| 1173 | * @since 2.0 |
| 1174 | * |
| 1175 | * @param string $address_label |
| 1176 | * |
| 1177 | * @return string |
| 1178 | */ |
| 1179 | function __give_donor_personal_address_label( $address_label ) { |
| 1180 | $address_label = __( 'Personal Address', 'give' ); |
| 1181 | |
| 1182 | return $address_label; |
| 1183 | } |
| 1184 | |
| 1185 | add_action( 'give_donor_personal_address_label', '__give_donor_personal_address_label' ); |
| 1186 | |
| 1187 | /** |
| 1188 | * Update Donor Information when User Profile is updated from admin. |
| 1189 | * Note: for internal use only. |
| 1190 | * |
| 1191 | * @since 2.0 |
| 1192 | * |
| 1193 | * @param int $user_id |
| 1194 | * |
| 1195 | * @access public |
| 1196 | * @return bool |
| 1197 | */ |
| 1198 | function give_update_donor_name_on_user_update( $user_id = 0 ) { |
| 1199 | |
| 1200 | if ( current_user_can( 'edit_user', $user_id ) ) { |
| 1201 | |
| 1202 | $donor = new Give_Donor( $user_id, true ); |
| 1203 | |
| 1204 | // Bailout, if donor doesn't exists. |
| 1205 | if ( ! $donor ) { |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | // Get User First name and Last name. |
| 1210 | $first_name = ( $_POST['first_name'] ) ? give_clean( $_POST['first_name'] ) : get_user_meta( $user_id, 'first_name', true ); |
| 1211 | $last_name = ( $_POST['last_name'] ) ? give_clean( $_POST['last_name'] ) : get_user_meta( $user_id, 'last_name', true ); |
| 1212 | $full_name = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) ); |
| 1213 | |
| 1214 | // Assign User First name and Last name to Donor. |
| 1215 | Give()->donors->update( |
| 1216 | $donor->id, |
| 1217 | [ |
| 1218 | 'name' => $full_name, |
| 1219 | ] |
| 1220 | ); |
| 1221 | Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name ); |
| 1222 | Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name ); |
| 1223 | |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | add_action( 'edit_user_profile_update', 'give_update_donor_name_on_user_update', 10 ); |
| 1228 | add_action( 'personal_options_update', 'give_update_donor_name_on_user_update', 10 ); |
| 1229 | |
| 1230 | |
| 1231 | /** |
| 1232 | * Updates the email address of a donor record when the email on a user is updated |
| 1233 | * Note: for internal use only. |
| 1234 | * |
| 1235 | * @since 1.4.3 |
| 1236 | * @access public |
| 1237 | * |
| 1238 | * @param WP_User|bool $old_user_data User data. |
| 1239 | * |
| 1240 | * @param int $user_id User ID. |
| 1241 | * |
| 1242 | * @return bool |
| 1243 | */ |
| 1244 | function give_update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) { |
| 1245 | |
| 1246 | $donor = new Give_Donor( $user_id, true ); |
| 1247 | |
| 1248 | if ( ! $donor ) { |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | $user = get_userdata( $user_id ); |
| 1253 | |
| 1254 | if ( ! empty( $user ) && $user->user_email !== $donor->email ) { |
| 1255 | |
| 1256 | $success = Give()->donors->update( |
| 1257 | $donor->id, |
| 1258 | [ |
| 1259 | 'email' => $user->user_email, |
| 1260 | ] |
| 1261 | ); |
| 1262 | |
| 1263 | if ( $success ) { |
| 1264 | // Update some payment meta if we need to |
| 1265 | $payments_array = explode( ',', $donor->payment_ids ); |
| 1266 | |
| 1267 | if ( ! empty( $payments_array ) ) { |
| 1268 | |
| 1269 | foreach ( $payments_array as $payment_id ) { |
| 1270 | |
| 1271 | give_update_payment_meta( $payment_id, 'email', $user->user_email ); |
| 1272 | |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * Fires after updating donor email on user update. |
| 1278 | * |
| 1279 | * @since 1.4.3 |
| 1280 | * |
| 1281 | * @param Give_Donor $donor Give donor object. |
| 1282 | * |
| 1283 | * @param WP_User $user WordPress User object. |
| 1284 | */ |
| 1285 | do_action( 'give_update_donor_email_on_user_update', $user, $donor ); |
| 1286 | |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | } |
| 1291 | |
| 1292 | add_action( 'profile_update', 'give_update_donor_email_on_user_update', 10, 2 ); |
| 1293 | |
| 1294 | |
| 1295 | /** |
| 1296 | * Flushes Give's cache. |
| 1297 | */ |
| 1298 | function give_cache_flush() { |
| 1299 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 1300 | wp_die(); |
| 1301 | } |
| 1302 | |
| 1303 | $result = Give_Cache::flush_cache(); |
| 1304 | |
| 1305 | if ( $result ) { |
| 1306 | wp_send_json_success( |
| 1307 | [ |
| 1308 | 'message' => __( 'Cache flushed successfully.', 'give' ), |
| 1309 | ] |
| 1310 | ); |
| 1311 | } else { |
| 1312 | wp_send_json_error( |
| 1313 | [ |
| 1314 | 'message' => __( 'An error occurred while flushing the cache.', 'give' ), |
| 1315 | ] |
| 1316 | ); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | add_action( 'wp_ajax_give_cache_flush', 'give_cache_flush', 10, 0 ); |
| 1321 | |
| 1322 | /** |
| 1323 | * Admin notices for errors |
| 1324 | * note: only for internal use |
| 1325 | * |
| 1326 | * @access public |
| 1327 | * @since 2.5.0 |
| 1328 | * @return void |
| 1329 | */ |
| 1330 | function give_license_notices() { |
| 1331 | |
| 1332 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | // Do not show licenses notices on license tab. |
| 1337 | if ( Give_Admin_Settings::is_setting_page( 'licenses' ) ) { |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | $give_plugins = give_get_plugins( [ 'only_premium_add_ons' => true ] ); |
| 1342 | $give_licenses = get_option( 'give_licenses', [] ); |
| 1343 | $notice_data = []; |
| 1344 | $license_data = []; |
| 1345 | $invalid_license_count = 0; |
| 1346 | $addons_with_license = []; |
| 1347 | |
| 1348 | // Loop through Give licenses to find license status. |
| 1349 | foreach ( $give_licenses as $key => $give_license ) { |
| 1350 | if ( empty( $license_data[ $give_license['license'] ] ) ) { |
| 1351 | $license_data[ $give_license['license'] ] = [ |
| 1352 | 'count' => 0, |
| 1353 | 'add-ons' => [], |
| 1354 | ]; |
| 1355 | } |
| 1356 | |
| 1357 | // Setup data for all access pass. |
| 1358 | if ( $give_license['is_all_access_pass'] ) { |
| 1359 | $addons_list = wp_list_pluck( $give_license['download'], 'plugin_slug' ); |
| 1360 | foreach ( $addons_list as $item ) { |
| 1361 | $license_data[ $give_license['license'] ]['add-ons'][] = $addons_with_license[] = $item; |
| 1362 | } |
| 1363 | } else { |
| 1364 | $license_data[ $give_license['license'] ]['add-ons'][] = $addons_with_license[] = $give_license['plugin_slug']; |
| 1365 | } |
| 1366 | |
| 1367 | $license_data[ $give_license['license'] ]['count'] += 1; |
| 1368 | } |
| 1369 | |
| 1370 | // Set data for inactive add-ons. |
| 1371 | $inactive_addons = array_diff( wp_list_pluck( $give_plugins, 'Dir' ), $addons_with_license ); |
| 1372 | |
| 1373 | $license_data['inactive'] = [ |
| 1374 | 'count' => count( $inactive_addons ), |
| 1375 | 'add-ons' => array_values( $inactive_addons ), |
| 1376 | ]; |
| 1377 | |
| 1378 | // Unset active license add-ons as not required. |
| 1379 | unset( $license_data['valid'] ); |
| 1380 | |
| 1381 | // Combine site inactive with inactive and unset site_inactive because already merged information with inactive |
| 1382 | if ( ! empty( $license_data['site_inactive'] ) ) { |
| 1383 | $license_data['inactive']['count'] += $license_data['site_inactive']['count']; |
| 1384 | $license_data['inactive']['add-ons'] += $license_data['site_inactive']['add-ons']; |
| 1385 | |
| 1386 | unset( $license_data['site_inactive'] ); |
| 1387 | } |
| 1388 | |
| 1389 | // Loop through license data. |
| 1390 | foreach ( $license_data as $key => $license ) { |
| 1391 | if ( ! $license['count'] ) { |
| 1392 | continue; |
| 1393 | } |
| 1394 | |
| 1395 | $notice_data[ $key ] = sprintf( |
| 1396 | '%1$s %2$s', |
| 1397 | $license['count'], |
| 1398 | $key |
| 1399 | ); |
| 1400 | |
| 1401 | // This will contain sum of count expect license with valid status. |
| 1402 | $invalid_license_count += $license['count']; |
| 1403 | } |
| 1404 | |
| 1405 | // Prepare license notice description. |
| 1406 | $prepared_notice_status = implode( ' , ', $notice_data ); |
| 1407 | $prepared_notice_status = 2 <= count( $notice_data ) |
| 1408 | ? substr_replace( $prepared_notice_status, 'and', strrpos( $prepared_notice_status, ',' ), 1 ) |
| 1409 | : $prepared_notice_status; |
| 1410 | |
| 1411 | $notice_description = sprintf( |
| 1412 | _n( |
| 1413 | 'Your GiveWP add-on is not receiving critical updates and new features because you have %1$s license key. Please <a href="%2$s" title="%3$s">activate your license</a> to receive updates and <a href="%4$s" target="_blank" title="%5$s">priority support</a>', |
| 1414 | 'Your GiveWP add-ons are not receiving critical updates and new features because you have %1$s license keys. Please <a href="%2$s" title="%3$s">activate your license</a> to receive updates and <a href="%4$s" target="_blank" title="%5$s">priority support</a>', |
| 1415 | $invalid_license_count, |
| 1416 | 'give' |
| 1417 | ), |
| 1418 | $prepared_notice_status, |
| 1419 | admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ), |
| 1420 | __( 'Activate License', 'give' ), |
| 1421 | esc_url( 'http://docs.givewp.com/pb-priority-support' ), |
| 1422 | __( 'Priority Support', 'give' ) |
| 1423 | ); |
| 1424 | |
| 1425 | // Check by add-on if any give add-on activated without license. |
| 1426 | // Do not show this notice if add-on activated with in 3 days. |
| 1427 | $is_required_days_past = current_time( 'timestamp' ) > ( Give_Cache_Setting::get_option( 'give_addon_last_activated' ) + ( 3 * DAY_IN_SECONDS ) ); |
| 1428 | |
| 1429 | // Default license notice arguments. |
| 1430 | $license_notice_args = [ |
| 1431 | 'id' => 'give-invalid-expired-license', |
| 1432 | 'type' => 'error', |
| 1433 | 'description' => $notice_description, |
| 1434 | 'dismissible_type' => 'user', |
| 1435 | 'dismiss_interval' => 'shortly', |
| 1436 | ]; |
| 1437 | |
| 1438 | // Register Notices. |
| 1439 | if ( $invalid_license_count && $is_required_days_past ) { |
| 1440 | Give()->notices->register_notice( $license_notice_args ); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | add_action( 'admin_notices', 'give_license_notices' ); |
| 1445 | |
| 1446 | |
| 1447 | /** |
| 1448 | * Log give addon activation time |
| 1449 | * |
| 1450 | * @since 2.5.0 |
| 1451 | * |
| 1452 | * @param $network_wide |
| 1453 | * |
| 1454 | * @param $plugin |
| 1455 | */ |
| 1456 | function give_log_addon_activation_time( $plugin, $network_wide ) { |
| 1457 | if ( $network_wide ) { |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | $plugin_data = give_get_plugins( [ 'only_premium_add_ons' => true ] ); |
| 1462 | $plugin_data = ! empty( $plugin_data[ $plugin ] ) ? $plugin_data[ $plugin ] : []; |
| 1463 | |
| 1464 | if ( $plugin_data ) { |
| 1465 | update_option( 'give_addon_last_activated', current_time( 'timestamp' ), 'no' ); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | add_action( 'activate_plugin', 'give_log_addon_activation_time', 10, 2 ); |
| 1470 | |
| 1471 | |
| 1472 | /** |
| 1473 | * Hide all admin notice from add-ons page |
| 1474 | * |
| 1475 | * Note: only for internal use |
| 1476 | * |
| 1477 | * @since 2.5.0 |
| 1478 | */ |
| 1479 | function give_hide_notices_on_add_ons_page() { |
| 1480 | $page = ! empty( $_GET['page'] ) ? give_clean( $_GET['page'] ) : ''; |
| 1481 | |
| 1482 | // Bailout. |
| 1483 | if ( 'give-addons' !== $page ) { |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | remove_all_actions( 'admin_notices' ); |
| 1488 | } |
| 1489 | |
| 1490 | add_action( 'in_admin_header', 'give_hide_notices_on_add_ons_page', 999 ); |
| 1491 | |
| 1492 | |
| 1493 | /** |
| 1494 | * Admin JS |
| 1495 | * |
| 1496 | * @since 2.5.0 |
| 1497 | */ |
| 1498 | function give_admin_quick_js() { |
| 1499 | if ( is_multisite() && is_blog_admin() ) { |
| 1500 | ?> |
| 1501 | <script> |
| 1502 | jQuery(document).ready(function ($) { |
| 1503 | var $updateNotices = $('[id$="-update"] ', '.wp-list-table'); |
| 1504 | |
| 1505 | if ($updateNotices.length) { |
| 1506 | $.each($updateNotices, function (index, $updateNotice) { |
| 1507 | $updateNotice = $($updateNotice); |
| 1508 | $updateNotice.prev().addClass('update'); |
| 1509 | }); |
| 1510 | } |
| 1511 | }); |
| 1512 | </script> |
| 1513 | <?php |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | add_action( 'admin_head', 'give_admin_quick_js' ); |
| 1518 | |
| 1519 | /** |
| 1520 | * Add Admin addon menu related scripts |
| 1521 | * |
| 1522 | * @since 2.6.0 |
| 1523 | */ |
| 1524 | function give_admin_addon_menu_inline_scripts() { |
| 1525 | ?> |
| 1526 | <script> |
| 1527 | (function ($) { |
| 1528 | const $addonLink = $('#menu-posts-give_forms a[href^="edit.php?post_type=give_forms&page=give-add-ons"]'); |
| 1529 | <?php if ( empty( give_get_plugins( [ 'only_premium_add_ons' => true ] ) ) ) : ?> |
| 1530 | $addonLink.addClass('give-highlight'); |
| 1531 | $addonLink.prepend('<span class="dashicons dashicons-star-filled"></span>'); |
| 1532 | <?php endif; ?> |
| 1533 | })(jQuery) |
| 1534 | </script> |
| 1535 | <style> |
| 1536 | #menu-posts-give_forms a[href^="edit.php?post_type=give_forms&page=give-add-ons"].give-highlight { |
| 1537 | color: rgb(43, 194, 83); |
| 1538 | font-weight: 700; |
| 1539 | vertical-align: top; |
| 1540 | text-shadow: 0 1px 2px #00000080; |
| 1541 | } |
| 1542 | |
| 1543 | #menu-posts-give_forms a[href^="edit.php?post_type=give_forms&page=give-add-ons"].give-highlight span.dashicons { |
| 1544 | font-size: 14px !important; |
| 1545 | width: auto; |
| 1546 | height: 18px; |
| 1547 | padding-right: 3px; |
| 1548 | vertical-align: middle; |
| 1549 | } |
| 1550 | </style> |
| 1551 | <?php |
| 1552 | } |
| 1553 | |
| 1554 | add_action( 'admin_footer', 'give_admin_addon_menu_inline_scripts' ); |
| 1555 | |
| 1556 | /** |
| 1557 | * Handle akismet_deblacklist_spammed_email_handler give-action |
| 1558 | * |
| 1559 | * @since 2.5.14 |
| 1560 | * |
| 1561 | * @param array $get |
| 1562 | * |
| 1563 | */ |
| 1564 | function give_akismet_deblacklist_spammed_email_handler( $get ) { |
| 1565 | $email = ! empty( $get['email'] ) && is_email( $get['email'] ) ? give_clean( $get['email'] ) : ''; |
| 1566 | $log = ! empty( $get['log'] ) ? absint( $get['log'] ) : ''; |
| 1567 | $action = "give_akismet_deblacklist_spammed_email_{$email}"; |
| 1568 | |
| 1569 | check_admin_referer( $action ); |
| 1570 | $emails = give_akismet_get_whitelisted_emails(); |
| 1571 | |
| 1572 | if ( ! in_array( $email, $emails, true ) ) { |
| 1573 | array_unshift( $emails, $email ); |
| 1574 | |
| 1575 | give_update_option( 'akismet_whitelisted_email_addresses', $emails ); |
| 1576 | |
| 1577 | // Redirect to Akismet setting page. |
| 1578 | wp_safe_redirect( 'wp-admin/edit.php?post_type=give_forms&page=give-settings&tab=advanced§ion=akismet-spam-protection&give-message=akismet-deblacklisted-email' ); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | add_action( 'give_akismet_deblacklist_spammed_email', 'give_akismet_deblacklist_spammed_email_handler' ); |
| 1583 | |
| 1584 | /** |
| 1585 | * Add Custom setting view for form them setting panel |
| 1586 | * |
| 1587 | * @since 2.7.0 |
| 1588 | */ |
| 1589 | function give_render_form_theme_setting_panel() { |
| 1590 | require_once GIVE_PLUGIN_DIR . 'src/Views/Admin/Form/Metabox-Settings.php'; |
| 1591 | } |
| 1592 | |
| 1593 | add_action( 'give_post_form_template_options_settings', 'give_render_form_theme_setting_panel' ); |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 |