<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[8194] sites/trunk/wordcamp.org/public_html/wp-content/plugins: WordCamp Payment: Send reminder mails to organizer about pending invoices.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/8194">8194</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/8194","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>vedjain</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-02-04 17:39:20 +0000 (Mon, 04 Feb 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp Payment: Send reminder mails to organizer about pending invoices.

Sometimes a sponsor invoice can be in pending state for too long, in which case either organizers need to cancel the invoice or they need to nudge the sponsors to pay them. This patch sends a reminder mail to organizer about an invoice that has been pending for too long. After 60 days of being sent, or after 60 days of WordCamp start date, we will send a mail to WordCamp support notfying them about the invoice.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsbootstrapphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/bootstrap.php</a></li>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsincludessponsorinvoicephp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-invoice.php</a></li>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkincludessponsorinvoicesdashboardphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsbootstrapphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/bootstrap.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/bootstrap.php   2019-02-04 09:51:48 UTC (rev 8193)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/bootstrap.php     2019-02-04 17:39:20 UTC (rev 8194)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -15,7 +15,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_admin() ) {
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( __DIR__ . '/includes/wordcamp-budgets.php' );
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( __DIR__ . '/includes/payment-request.php' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        require_once( __DIR__ . '/includes/sponsor-invoice.php' );
</del><span class="cx" style="display: block; padding: 0 10px">         require_once( __DIR__ . '/includes/reimbursement-request.php' );
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( __DIR__ . '/includes/encryption.php' );
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( __DIR__ . '/includes/budget-tool.php' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -26,4 +25,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_admin() || wp_doing_cron() || wp_doing_ajax() ) {
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( __DIR__ . '/includes/privacy.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        require_once( __DIR__ . '/includes/sponsor-invoice.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsincludessponsorinvoicephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-invoice.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-invoice.php    2019-02-04 09:51:48 UTC (rev 8193)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-invoice.php      2019-02-04 17:39:20 UTC (rev 8194)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -427,6 +427,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        update_post_meta( $post_id, $meta_key, $value );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       if ( 'wcbsi_approved' === $post->post_status ) {
+               $invoice_sent_at = get_post_meta( $post_id, 'Sent at', true );
+               if ( empty( $invoice_sent_at ) ) {
+                       update_post_meta( $post_id, 'Sent at', time() );
+               }
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkincludessponsorinvoicesdashboardphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php 2019-02-04 09:51:48 UTC (rev 8193)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php   2019-02-04 17:39:20 UTC (rev 8194)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,12 +14,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( defined( 'DOING_CRON' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'wcbdsi_check_for_paid_invoices', __NAMESPACE__ . '\check_for_paid_invoices'       );
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'save_post',                      __NAMESPACE__ . '\update_index_row',       10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        add_action( 'plugins_loaded',                 __NAMESPACE__ . '\schedule_sent_invoice_reminder' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( is_network_admin() ) {
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'plugins_loaded',        __NAMESPACE__ . '\schedule_cron_events'  );
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'network_admin_menu',    __NAMESPACE__ . '\register_submenu_page' );
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'init',                  __NAMESPACE__ . '\upgrade_database'      );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px"> } elseif ( is_admin() ) {
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'save_post',    __NAMESPACE__ . '\update_index_row', 11, 2 );   // See note in callback about priority
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'trashed_post', __NAMESPACE__ . '\delete_index_row'        );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -26,6 +26,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        add_action( 'delete_post',  __NAMESPACE__ . '\delete_index_row'        );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'send_invoice_pending_reminder', __NAMESPACE__ . '\send_invoice_pending_reminder' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Schedule cron job when plugin is activated
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -461,3 +463,177 @@
</span><span class="cx" style="display: block; padding: 0 10px">                )
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Schedule cron to send reminder mails to organizers for unpaid invoices.
+ */
+function schedule_sent_invoice_reminder() {
+       if ( wp_next_scheduled( 'send_invoice_pending_reminder' ) ) {
+               return;
+       }
+
+       wp_schedule_event( time(), 'daily', 'send_invoice_pending_reminder' );
+}
+
+/**
+ * Send reminder to organizer about the unpaid invoice.
+ */
+function send_invoice_pending_reminder() {
+       global $wpdb;
+
+       $table_name    = get_index_table_name();
+       $sent_invoices = $wpdb->get_results(
+               $wpdb->prepare(
+                       "
+                               SELECT blog_id, invoice_id
+                               FROM $table_name
+                               WHERE status = 'wcbsi_approved'
+                               LIMIT 1000
+                       ",
+                       array()
+               )
+       );
+
+       foreach ( $sent_invoices as $invoice_data ) {
+               $blog_id    = $invoice_data->blog_id;
+               $invoice_id = $invoice_data->invoice_id;
+
+               switch_to_blog( $blog_id );
+
+               $invoice_sent_at = get_post_meta( $invoice_id, 'Sent at', true );
+
+               if ( empty( $invoice_sent_at ) ) {
+                       // Backfill for older invoices.
+                       update_post_meta( $invoice_id, 'Sent at', time() );
+                       update_post_meta( $invoice_id, 'Backfilled Sent at', true );
+                       restore_current_blog();
+                       continue;
+               }
+
+               $last_reminder       = get_post_meta( $invoice_id, 'last_reminder_details', true );
+               $invoice_defaulted   = get_post_meta( $invoice_id, 'invoice_defaulted', true );
+               $reminder_step       = 1;
+               $last_step_time      = $invoice_sent_at;
+               $wordcamp_post       = get_wordcamp_post();
+               $wordcamp_start_date = ( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'] ?? array() )[0];
+               $wordcamp_lead_email = ( $wordcamp_post->meta['Email Address'] ?? array() )[0];
+
+               if ( empty( $wordcamp_post ) ) {
+                       // Maybe this is a central.wordcamp.org test sponsor invoice.
+                       restore_current_blog();
+                       continue;
+               }
+
+               if ( ! empty ( $invoice_defaulted ) ) {
+                       restore_current_blog();
+                       continue;
+               }
+
+               if ( ! empty( $last_reminder ) ) {
+                       $reminder_step  = $last_reminder['step'] + 1;
+                       $last_step_time = $last_reminder['sent_at'];
+               }
+
+               // We will send reminders after 30, 45, and 60 days.
+               $reminder_schedule = array(
+                       1 => 30,
+                       2 => 15,
+                       3 => 15,
+               );
+
+               if ( $reminder_step > count( $reminder_schedule ) || ( $wordcamp_start_date && time() > ( (int) $wordcamp_start_date + 2 * MONTH_IN_SECONDS ) ) ) {
+                       send_invoice_defaulted_notification( $invoice_id );
+                       update_post_meta( $invoice_id, 'invoice_defaulted', true );
+                       restore_current_blog();
+                       continue;
+               }
+
+               $next_reminder_in = $last_step_time + $reminder_schedule[ $reminder_step ] * DAY_IN_SECONDS;
+
+               if ( time() < (int) $next_reminder_in ) {
+                       restore_current_blog();
+                       continue;
+               }
+
+               $current_reminder_details = array(
+                       'sent_at' => time(),
+                       'step'    => $reminder_step,
+               );
+
+               send_invoice_pending_reminder_mail( $invoice_id, $wordcamp_lead_email );
+
+               update_post_meta( $invoice_id, 'last_reminder_details', $current_reminder_details );
+
+               restore_current_blog();
+       }
+}
+
+/**
+ * Send mail to organizer about a pending sponsor invoice.
+ *
+ * @param int    $invoice_id
+ * @param string $organizer_mail
+ */
+function send_invoice_pending_reminder_mail( $invoice_id, $organizer_mail ) {
+       $invoice   = get_post( $invoice_id );
+       $edit_link = get_site_url() . "/wp-admin/post.php?post=$invoice_id&action=edit";
+
+       $reminder_body = str_replace(
+               "\t",
+               '',
+               sprintf(
+                       __(
+                               "Howdy organizers,
+                               <br>
+                               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.
+                               <br>
+                               Thanks for all your hard work on WordCamp.",
+                               'wordcamporg'
+                       ),
+                       $edit_link,
+                       $invoice->post_title
+               )
+       );
+
+       $author = get_user_by( 'ID', $invoice->post_author );
+       wp_mail(
+               array( $author->user_email ),
+               sprintf( __( "Pending invoice: %s" ,'wordcamporg' ), $invoice->post_title ),
+               $reminder_body,
+               array(
+                       'From: WordCamp Central <support@wordcamp.org>',
+                       'Content-Type: text/html; charset=UTF-8',
+                       'Sender: wordpress@' . strtolower( $_SERVER['SERVER_NAME'] ),
+                       sprintf( "CC: %s", $organizer_mail ), // CC to lead organizer in case post author is not active anymore.
+               )
+       );
+}
+
+/**
+ * Send email to support@wordcamp.org about Sponsor invoice that has been pending for too long.
+ *
+ * @param int $invoice_id
+ */
+function send_invoice_defaulted_notification( $invoice_id ) {
+       $edit_link = get_site_url() . "/wp-admin/post.php?post=$invoice_id&action=edit";
+
+       $notification_body = str_replace(
+               "\t",
+               '',
+               "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.
+               <br>
+               Invoice link: $edit_link
+               <br>"
+       );
+
+       wp_mail(
+               array( "support@wordcamp.org" ),
+               "Sponsor Invoice pending for too long",
+               $notification_body,
+               array(
+                       'From: WordCamp Central <support@wordcamp.org>',
+                       'Content-Type: text/html; charset=UTF-8',
+                       'Sender: wordpress@' . strtolower( $_SERVER['SERVER_NAME'] ),
+               )
+       );
+}
</ins></span></pre>
</div>
</div>

</body>
</html>