Skip to content

Instantly share code, notes, and snippets.

@thirdender
Created January 4, 2012 00:09
Show Gist options
  • Save thirdender/1557701 to your computer and use it in GitHub Desktop.
Save thirdender/1557701 to your computer and use it in GitHub Desktop.
WPInvoice notification code (unfinished)
<?php
function reminder_invoice_notification($wpi_settings) {
global $wpi_settings;
// Output the invoice selection field in the settings
$rinotify .= '<table><tr><th>Select Invoice Template to use:</th><td>';
$rinotify .= '<select id="wpi_change_notification">';
foreach ($wpi_settings['notification'] as $key => $item) {
$rinotify .= "<option value=\"$key\">{$item['name']}</option>";
}
$rinotify .= '</select>';
$rinotify .= '</td></tr></table>';
echo $rinotify;
}
// If the event function is not being called, schedule it to be called
function post_date_activation() {
if (!wp_next_scheduled( 'post_date_hook' )) {
wp_schedule_event( time(), 'twicedaily', 'post_date_hook' );
}
}
add_action('wp', 'post_date_activation');
// Send e-mails to unfulfilled invoices (should be called twice daily)
function post_date_function() {
global $wpdb, $wpi_settings;
// This is a timestamp from one week ago... anything that has a last
// notified timestamp lower than this needs a new e-mail sent
$needsNotification = strtotime('-1 week');
$dbResults = $wpdb->get_results($wpdb->prepare("SELECT post_date FROM {$wpdb->posts} WHERE post_type = 'wpi_object' AND post_status = 'active'"));
// Check the timestamp on each invoice to see if they need a new
// notification e-mail
foreach ($dbResults as $result) {
if (strtotime($result->post_modified) < $needsNotification) {
// Send e-mail to the customer
// TODO: Look at wpi_ajax.php for code to load the notification template from the $wpi_settings variable
$invoiceID = wpi_post_id_to_invoice_id($result->ID);
$invoice = new WPI_Invoice();
$invoice->load_invoice("id=$invoiceID");
$name = stripslashes($wpi_settings['business_name']);
$from = stripslashes($wpi_settings['email_address']);
$permalink = get_invoice_permalink($invoice['invoice_id']);
$headers = "From: {$name} <{$from}>\r\n";
$message = "Dear {$invoice['user_data']['display_name']},
{$wpi_settings['business_name']} has received your payment for the invoice.
You can overview invoice status and payment history by clicking this link:
{$permalink}
Thank you very much for your patronage.
Best regards,
$name ($from)";
$subject = "Invoice #{$invoice['invoice_id']} has been paid";
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
$subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8');
if (wp_mail($invoice['user_data']['user_email'], $subject, $message, $headers)) {
WPI_Functions::log_event($result->ID, 'invoice', 'emailed', '', 'Reminder eMailed');
}
// And update the database notification timestamp with
// the current time
// TODO: add code to update the database with last time sent
}
}
}
// Call 'post_date_function' when the 'post_date_hook' hook is called
add_action('post_date_hook', 'post_date_function');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment