Changeset 8194 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php
- Timestamp:
- 02/04/2019 05:39:20 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php
r6052 r8194 15 15 add_action( 'wcbdsi_check_for_paid_invoices', __NAMESPACE__ . '\check_for_paid_invoices' ); 16 16 add_action( 'save_post', __NAMESPACE__ . '\update_index_row', 10, 2 ); 17 add_action( 'plugins_loaded', __NAMESPACE__ . '\schedule_sent_invoice_reminder' ); 17 18 18 19 } elseif ( is_network_admin() ) { … … 20 21 add_action( 'network_admin_menu', __NAMESPACE__ . '\register_submenu_page' ); 21 22 add_action( 'init', __NAMESPACE__ . '\upgrade_database' ); 22 23 23 } elseif ( is_admin() ) { 24 24 add_action( 'save_post', __NAMESPACE__ . '\update_index_row', 11, 2 ); // See note in callback about priority … … 26 26 add_action( 'delete_post', __NAMESPACE__ . '\delete_index_row' ); 27 27 } 28 29 add_action( 'send_invoice_pending_reminder', __NAMESPACE__ . '\send_invoice_pending_reminder' ); 28 30 29 31 /** … … 462 464 ); 463 465 } 466 467 /** 468 * Schedule cron to send reminder mails to organizers for unpaid invoices. 469 */ 470 function schedule_sent_invoice_reminder() { 471 if ( wp_next_scheduled( 'send_invoice_pending_reminder' ) ) { 472 return; 473 } 474 475 wp_schedule_event( time(), 'daily', 'send_invoice_pending_reminder' ); 476 } 477 478 /** 479 * Send reminder to organizer about the unpaid invoice. 480 */ 481 function send_invoice_pending_reminder() { 482 global $wpdb; 483 484 $table_name = get_index_table_name(); 485 $sent_invoices = $wpdb->get_results( 486 $wpdb->prepare( 487 " 488 SELECT blog_id, invoice_id 489 FROM $table_name 490 WHERE status = 'wcbsi_approved' 491 LIMIT 1000 492 ", 493 array() 494 ) 495 ); 496 497 foreach ( $sent_invoices as $invoice_data ) { 498 $blog_id = $invoice_data->blog_id; 499 $invoice_id = $invoice_data->invoice_id; 500 501 switch_to_blog( $blog_id ); 502 503 $invoice_sent_at = get_post_meta( $invoice_id, 'Sent at', true ); 504 505 if ( empty( $invoice_sent_at ) ) { 506 // Backfill for older invoices. 507 update_post_meta( $invoice_id, 'Sent at', time() ); 508 update_post_meta( $invoice_id, 'Backfilled Sent at', true ); 509 restore_current_blog(); 510 continue; 511 } 512 513 $last_reminder = get_post_meta( $invoice_id, 'last_reminder_details', true ); 514 $invoice_defaulted = get_post_meta( $invoice_id, 'invoice_defaulted', true ); 515 $reminder_step = 1; 516 $last_step_time = $invoice_sent_at; 517 $wordcamp_post = get_wordcamp_post(); 518 $wordcamp_start_date = ( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'] ?? array() )[0]; 519 $wordcamp_lead_email = ( $wordcamp_post->meta['Email Address'] ?? array() )[0]; 520 521 if ( empty( $wordcamp_post ) ) { 522 // Maybe this is a central.wordcamp.org test sponsor invoice. 523 restore_current_blog(); 524 continue; 525 } 526 527 if ( ! empty ( $invoice_defaulted ) ) { 528 restore_current_blog(); 529 continue; 530 } 531 532 if ( ! empty( $last_reminder ) ) { 533 $reminder_step = $last_reminder['step'] + 1; 534 $last_step_time = $last_reminder['sent_at']; 535 } 536 537 // We will send reminders after 30, 45, and 60 days. 538 $reminder_schedule = array( 539 1 => 30, 540 2 => 15, 541 3 => 15, 542 ); 543 544 if ( $reminder_step > count( $reminder_schedule ) || ( $wordcamp_start_date && time() > ( (int) $wordcamp_start_date + 2 * MONTH_IN_SECONDS ) ) ) { 545 send_invoice_defaulted_notification( $invoice_id ); 546 update_post_meta( $invoice_id, 'invoice_defaulted', true ); 547 restore_current_blog(); 548 continue; 549 } 550 551 $next_reminder_in = $last_step_time + $reminder_schedule[ $reminder_step ] * DAY_IN_SECONDS; 552 553 if ( time() < (int) $next_reminder_in ) { 554 restore_current_blog(); 555 continue; 556 } 557 558 $current_reminder_details = array( 559 'sent_at' => time(), 560 'step' => $reminder_step, 561 ); 562 563 send_invoice_pending_reminder_mail( $invoice_id, $wordcamp_lead_email ); 564 565 update_post_meta( $invoice_id, 'last_reminder_details', $current_reminder_details ); 566 567 restore_current_blog(); 568 } 569 } 570 571 /** 572 * Send mail to organizer about a pending sponsor invoice. 573 * 574 * @param int $invoice_id 575 * @param string $organizer_mail 576 */ 577 function send_invoice_pending_reminder_mail( $invoice_id, $organizer_mail ) { 578 $invoice = get_post( $invoice_id ); 579 $edit_link = get_site_url() . "/wp-admin/post.php?post=$invoice_id&action=edit"; 580 581 $reminder_body = str_replace( 582 "\t", 583 '', 584 sprintf( 585 __( 586 "Howdy organizers, 587 <br> 588 It looks like the invoice <a href='%s'>%s</a> is still unpaid. If you still expect the sponsor to pay this invoice, please contact them to find out when we should expect payment. If this invoice needs to be cancelled, please email support@wordcamp.org. 589 <br> 590 Thanks for all your hard work on WordCamp.", 591 'wordcamporg' 592 ), 593 $edit_link, 594 $invoice->post_title 595 ) 596 ); 597 598 $author = get_user_by( 'ID', $invoice->post_author ); 599 wp_mail( 600 array( $author->user_email ), 601 sprintf( __( "Pending invoice: %s" ,'wordcamporg' ), $invoice->post_title ), 602 $reminder_body, 603 array( 604 'From: WordCamp Central <support@wordcamp.org>', 605 'Content-Type: text/html; charset=UTF-8', 606 'Sender: wordpress@' . strtolower( $_SERVER['SERVER_NAME'] ), 607 sprintf( "CC: %s", $organizer_mail ), // CC to lead organizer in case post author is not active anymore. 608 ) 609 ); 610 } 611 612 /** 613 * Send email to support@wordcamp.org about Sponsor invoice that has been pending for too long. 614 * 615 * @param int $invoice_id 616 */ 617 function send_invoice_defaulted_notification( $invoice_id ) { 618 $edit_link = get_site_url() . "/wp-admin/post.php?post=$invoice_id&action=edit"; 619 620 $notification_body = str_replace( 621 "\t", 622 '', 623 "A Sponsor Invoice has been in pending state for too long now. Please check to see if we want to cancel this. No further reminders will be sent to the organizers. 624 <br> 625 Invoice link: $edit_link 626 <br>" 627 ); 628 629 wp_mail( 630 array( "support@wordcamp.org" ), 631 "Sponsor Invoice pending for too long", 632 $notification_body, 633 array( 634 'From: WordCamp Central <support@wordcamp.org>', 635 'Content-Type: text/html; charset=UTF-8', 636 'Sender: wordpress@' . strtolower( $_SERVER['SERVER_NAME'] ), 637 ) 638 ); 639 }
Note: See TracChangeset
for help on using the changeset viewer.