function pmpropbc_reminder_emails() { global $wpdb; //make sure we only run once a day $now = current_time('timestamp'); $today = date("Y-m-d", $now); //have to run for each level, so get levels $levels = pmpro_getAllLevels(true, true); if (empty($levels)) { return; } foreach ($levels as $level) { //get options $options = pmpropbc_getOptions($level->id); if (!empty($options['reminder_days'])) { $date = date("Y-m-d", strtotime("+ " . $options['reminder_days'] . " days", $now)); } else { $date = $today; } //need to get all combos of pay cycle and period $sqlQuery = "SELECT DISTINCT(CONCAT(cycle_number, ' ', cycle_period)) FROM {$wpdb->pmpro_memberships_users} WHERE membership_id = '" . $level->id . "' AND cycle_number > 0 AND status = 'active'"; $combos = $wpdb->get_col($sqlQuery); if (empty($combos)) { continue; } foreach ($combos as $combo) { //get all check orders still pending after X days $sqlQuery = "\r\n\t\t\t\tSELECT id \r\n\t\t\t\tFROM {$wpdb->pmpro_membership_orders} \r\n\t\t\t\tWHERE membership_id = {$level->id} \r\n\t\t\t\t\tAND gateway = 'check' \r\n\t\t\t\t\tAND status = 'pending' \r\n\t\t\t\t\tAND DATE_ADD(timestamp, INTERVAL {$combo}) <= '" . $date . "'\r\n\t\t\t\t\tAND notes NOT LIKE '%Reminder Sent:%' AND notes NOT LIKE '%Reminder Skipped:%'\r\n\t\t\t\tORDER BY id\r\n\t\t\t"; if (defined('PMPRO_CRON_LIMIT')) { $sqlQuery .= " LIMIT " . PMPRO_CRON_LIMIT; } $orders = $wpdb->get_col($sqlQuery); if (empty($orders)) { continue; } foreach ($orders as $order_id) { //get some data $order = new MemberOrder($order_id); $user = get_userdata($order->user_id); $user->membership_level = pmpro_getMembershipLevelForUser($order->user_id); //if they are no longer a member, let's not send them an email if (empty($user->membership_level) || empty($user->membership_level->ID) || $user->membership_level->id != $order->membership_id) { //note when we send the reminder $new_notes = $order->notes . "Reminder Skipped:" . $today . "\n"; $wpdb->query("UPDATE {$wpdb->pmpro_membership_orders} SET notes = '" . esc_sql($new_notes) . "' WHERE id = '" . $order_id . "' LIMIT 1"); continue; } //note when we send the reminder $new_notes = $order->notes . "Reminder Sent:" . $today . "\n"; $wpdb->query("UPDATE {$wpdb->pmpro_membership_orders} SET notes = '" . esc_sql($new_notes) . "' WHERE id = '" . $order_id . "' LIMIT 1"); //setup email to send $email = new PMProEmail(); $email->template = "check_pending_reminder"; $email->email = $user->user_email; $email->subject = sprintf(__("Reminder: New Invoice for %s at %s", "pmpropbc"), $user->membership_level->name, get_option("blogname")); //get body from template $email->body = file_get_contents(PMPRO_PAY_BY_CHECK_DIR . "/email/" . $email->template . ".html"); //setup more data $email->data = array("name" => $user->display_name, "user_login" => $user->user_login, "sitename" => get_option("blogname"), "siteemail" => pmpro_getOption("from_email"), "membership_id" => $user->membership_level->id, "membership_level_name" => $user->membership_level->name, "membership_cost" => pmpro_getLevelCost($user->membership_level), "login_link" => wp_login_url(pmpro_url("account")), "display_name" => $user->display_name, "user_email" => $user->user_email); $email->data["instructions"] = pmpro_getOption('instructions'); $email->data["invoice_id"] = $order->code; $email->data["invoice_total"] = pmpro_formatPrice($order->total); $email->data["invoice_date"] = date(get_option('date_format'), $order->timestamp); $email->data["billing_name"] = $order->billing->name; $email->data["billing_street"] = $order->billing->street; $email->data["billing_city"] = $order->billing->city; $email->data["billing_state"] = $order->billing->state; $email->data["billing_zip"] = $order->billing->zip; $email->data["billing_country"] = $order->billing->country; $email->data["billing_phone"] = $order->billing->phone; $email->data["cardtype"] = $order->cardtype; $email->data["accountnumber"] = hideCardNumber($order->accountnumber); $email->data["expirationmonth"] = $order->expirationmonth; $email->data["expirationyear"] = $order->expirationyear; $email->data["billing_address"] = pmpro_formatAddress($order->billing->name, $order->billing->street, "", $order->billing->city, $order->billing->state, $order->billing->zip, $order->billing->country, $order->billing->phone); if ($order->getDiscountCode()) { $email->data["discount_code"] = "<p>" . __("Discount Code", "pmpro") . ": " . $order->discount_code->code . "</p>\n"; } else { $email->data["discount_code"] = ""; } //send the email $email->sendEmail(); } } } }