function pmpro_cron_expiration_warnings() { global $wpdb; //make sure we only run once a day $today = date_i18n("Y-m-d 00:00:00", current_time("timestamp")); $pmpro_email_days_before_expiration = apply_filters("pmpro_email_days_before_expiration", 7); // Configure the interval to select records from $interval_start = $today; $interval_end = date_i18n('Y-m-d 00:00:00', strtotime("{$today} +{$pmpro_email_days_before_expiration} days", current_time('timestamp'))); //look for memberships that are going to expire within one week (but we haven't emailed them within a week) $sqlQuery = $wpdb->prepare("SELECT DISTINCT\n \t\t\t\tmu.user_id,\n \t\t\t\tmu.membership_id,\n \t\t\t\tmu.startdate,\n \t\t\t\tmu.enddate,\n \t\t\t\tum.meta_value AS notice \t\t\t \n \t\t\tFROM {$wpdb->pmpro_memberships_users} AS mu\n \t\t\t LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = mu.user_id\n \tAND um.meta_key = %s\n\t\t\tWHERE ( um.meta_value IS NULL OR DATE_ADD(um.meta_value, INTERVAL %d DAY) < %s ) \n\t\t\t\tAND ( mu.status = 'active' )\t\t \n \t\t\t AND ( mu.enddate IS NOT NULL )\n \t\t\t AND ( mu.enddate <> '0000-00-00 00:00:00' )\n \t\t\t AND ( mu.enddate BETWEEN %s AND %s )\t\t \n \t\t\t AND ( mu.membership_id <> 0 OR mu.membership_id <> NULL )\n\t\t\tORDER BY mu.enddate\n\t\t\t", "pmpro_expiration_notice", $pmpro_email_days_before_expiration, $today, $interval_start, $interval_end); if (defined('PMPRO_CRON_LIMIT')) { $sqlQuery .= " LIMIT " . PMPRO_CRON_LIMIT; } $expiring_soon = $wpdb->get_results($sqlQuery); foreach ($expiring_soon as $e) { $send_email = apply_filters("pmpro_send_expiration_warning_email", true, $e->user_id); if ($send_email) { //send an email $pmproemail = new PMProEmail(); $euser = get_userdata($e->user_id); $pmproemail->sendMembershipExpiringEmail($euser); if (current_user_can('manage_options')) { printf(__("Membership expiring email sent to %s. ", "pmpro"), $euser->user_email); } else { echo ". "; } } //delete all user meta for this key to prevent duplicate user meta rows delete_user_meta($e->user_id, "pmpro_expiration_notice"); //update user meta so we don't email them again update_user_meta($e->user_id, "pmpro_expiration_notice", $today); } }
function pmpro_cron_expiration_warnings() { global $wpdb; //make sure we only run once a day $today = date("Y-m-d 00:00:00", current_time("timestamp")); $pmpro_email_days_before_expiration = apply_filters("pmpro_email_days_before_expiration", 7); //look for memberships that are going to expire within one week (but we haven't emailed them within a week) $sqlQuery = $wpdb->prepare("SELECT mu.user_id, mu.membership_id, mu.startdate, mu.enddate\n\t\t FROM {$wpdb->pmpro_memberships_users} AS mu\n \tLEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = mu.user_id AND um.meta_key = %s\n\t\t\tINNER JOIN {$wpdb->users} AS u ON u.ID = mu.user_id AND (\n\t\t\t\tmu.membership_id <> 0 OR\n\t\t\t\tmu.membership_id <> NULL OR\n\t\t\t\tmu.membership_id <> 'NULL'\n\t\t\t)\n\t\t WHERE mu.status = 'active'\n \t \tAND mu.enddate IS NOT NULL\n\t\t\tAND mu.enddate <> ''\n\t\t\tAND mu.enddate <> '0000-00-00 00:00:00'\n\t\t\tAND DATE_SUB(mu.enddate, INTERVAL %d Day) <= %s\n\t\t\tAND (um.meta_value IS NULL OR DATE_ADD(um.meta_value, INTERVAL %d Day) <= %s)\n\t\tORDER BY mu.enddate", "pmpro_expiration_notice", $pmpro_email_days_before_expiration, $today, $pmpro_email_days_before_expiration, $today); if (defined('PMPRO_CRON_LIMIT')) { $sqlQuery .= " LIMIT " . PMPRO_CRON_LIMIT; } $expiring_soon = $wpdb->get_results($sqlQuery); foreach ($expiring_soon as $e) { $send_email = apply_filters("pmpro_send_expiration_warning_email", true, $e->user_id); if ($send_email) { //send an email $pmproemail = new PMProEmail(); $euser = get_userdata($e->user_id); $pmproemail->sendMembershipExpiringEmail($euser); if (current_user_can('manage_options')) { printf(__("Membership expiring email sent to %s. ", "pmpro"), $euser->user_email); } else { echo ". "; } } //update user meta so we don't email them again update_user_meta($e->user_id, "pmpro_expiration_notice", $today); } }
function pmpro_cron_expiration_warnings() { global $wpdb; //make sure we only run once a day $today = date("Y-m-d 00:00:00"); $pmpro_email_days_before_expiration = apply_filters("pmpro_email_days_before_expiration", 7); //look for memberships that are going to expire within one week (but we haven't emailed them within a week) $sqlQuery = "SELECT mu.user_id, mu.membership_id, mu.startdate, mu.enddate FROM $wpdb->pmpro_memberships_users mu LEFT JOIN $wpdb->usermeta um ON um.user_id = mu.user_id AND um.meta_key = 'pmpro_expiration_notice' WHERE mu.status = 'active' AND mu.enddate IS NOT NULL AND mu.enddate <> '' AND mu.enddate <> '0000-00-00 00:00:00' AND DATE_SUB(mu.enddate, INTERVAL " . $pmpro_email_days_before_expiration . " Day) <= '" . $today . "' AND (um.meta_value IS NULL OR DATE_ADD(um.meta_value, INTERVAL " . $pmpro_email_days_before_expiration . " Day) <= '" . $today . "') ORDER BY mu.enddate"; $expiring_soon = $wpdb->get_results($sqlQuery); foreach($expiring_soon as $e) { $send_email = apply_filters("pmpro_send_expiration_warning_email", true, $e->user_id); if($send_email) { //send an email $pmproemail = new PMProEmail(); $euser = get_userdata($e->user_id); $pmproemail->sendMembershipExpiringEmail($euser); echo "Membership expiring email sent to " . $euser->user_email . ". "; } //update user meta so we don't email them again update_user_meta($euser->ID, "pmpro_expiration_notice", $today); } }