Viewing 5 replies - 1 through 5 (of 5 total)
  • s2 doesn’t include this feature, so I wrote a helper plugin that sends an automated email one week before the EOT, and then again one day before EOT:

    https://s2renewalreminders.com/

    This looks like a very convenient tool, Johnathon!

    I decided to write s2hacks_EOT_Email_Notify.php after a lot of reading on the subject of EOT notifications and not being able to find an existing implementation that suits my needs. I hope I haven’t duplicated anything that already exists…

    I prefer a scheduled ‘job’ to run each day to check all users instead of code that runs for each user at some point during their logged in session. I run a table tennis club site with mainly public content, so it is rare for the members to actually log in. In my case, s2Member is mainly used to track memberships not control content access, so notifications are extremely important.

    Below is the approach I’m using as of tonight. The comments explain the entire approach for 3 pre-expiration notifications. A wave of 1st Notification e-mail messages went out from my site upon landing the .php file in the mu-plugins folder.

    <?php
    //
    //     s2hacks_EOT_Email_Notify.php
    //
    //     Create user_meta s2hacks_eot_notify_state to track upcoming
    //     EOT expirations.  Send e-mail to user and admin as EOT crosses
    //     specific thresholds of remaining days.
    //
    //     s2hacks_eot_notify_state values
    //         0: EOT is far into the future
    //         1: EOT crossed 1st threshold and 1st Notification has been sent
    //         2: EOT crossed 2nd threshold and 2nd Notification has been sent
    //         3: EOT crossed 3rd threshold and 3rd Notification has been sent
    //
    //     NOTES
    //        1) Place this .php file in your mu-plugins folder to schedule
    //           the initial (immediate) run of s2hacks_eot_notify_event and
    //           the subsequent daily recurring s2hacks_eot_notify_event.
    //           Suggestion: Use plugin "WP Crontrol" or similar to see the
    //                       WP-cron events.
    //        2) If this mu-plugin is removed...
    //           A) You must manually
    //              clean up the WP-cron schedule residue.  The "WP Crontrol"
    //              plugin can be used to remove the scheduled
    //              s2hacks_eot_notify_event.
    //           B) Also, any instances of the meta_key named
    //              s2hacks_eot_notify_state will be your responsibility
    //              to remove from the wp_usermeta table.
    //              Suggestion: Use phpMyAdmin or equivalent to locate and
    //                          delete the residue rows.
    //        3) Put the word 'Development' into your Site Title (Dashboard,
    //           Settings, General, Site Title) to suppress the user e-mail
    //           while you are testing.
    //        4) Set a user's first name to 'Test' to receive user e-mail even
    //           when running with a 'Development' Site Title.
    //
    //
    // Change History
    // =========================================================================
    // 2014/05/11 - 1) TPK: Initial hack - v1.0.0
    // 2014/05/12 - 1) TPK: v1.0.1
    //                 A) Fixed detection of existing s2hacks_eot_notify_state
    //                    to prevent multiple instances (rows) for the same user
    //                    ID.
    //                 B) Added additional documentation.
    //
    
    //
    // Part 1 - Schedule Processing
    //
    add_action('wp', 's2hacks_eot_notify_schedule');
    /**/
    function s2hacks_eot_notify_schedule() {
        if ( ! wp_next_scheduled(                   's2hacks_eot_notify_event' ) ) {
             wp_schedule_single_event( time() + 15, 's2hacks_eot_notify_event' );  // First One ~now
             wp_schedule_event( time(), 'daily',    's2hacks_eot_notify_event' );  // Two through n on daily basis
        }
    }
    //
    // Part 2 - Create user meta when it doesn't already exist
    //          Act on user meta
    //
    add_action('s2hacks_eot_notify_event', 's2hacks_eot_notify_state');
    /**/
    function s2hacks_eot_notify_state() {
    		/**/
    		if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['auto_eot_system_enabled']) # If the Automatic EOT system is disabled...
    			return; # ...we don't need to do anything
    		/* Otherwise... */
    		$users_query = new WP_User_Query( array(
                                                    'fields' => 'all_with_meta',
                                                    'orderby' => 'display_name',
                                                   )
    									     );
            if ( !empty( $users_query->results)) {
                foreach ( $users_query->results as $user ) {
                    $this_ID = $user->ID;
                    $this_role = $user->roles[0];
                    switch ( substr(strtolower($this_role),-1) ) {
                         case "1":  // s2member_level1
                         case "2":  // s2member_level2
                         case "3":  // s2member_level3
                         case "4":  // s2member_level4
    					            unset($this_eot_notify_state);
    					            $this_eot_notify_state     = get_user_field('s2hacks_eot_notify_state', $this_ID);
                                    if ( false === $this_eot_notify_state ) {
    								    // Insert initial value
    								    add_user_meta($this_ID, 's2hacks_eot_notify_state', '0', false);
    								    $this_eot_notify_state = get_user_field('s2hacks_eot_notify_state', $this_ID);
    							    }
                                    $this_EOT                  = get_user_field('s2member_auto_eot_time', $this_ID);
                                    if (!empty($this_EOT)) {
     				                    $this_EOT_readable = date('m-d-y', $this_EOT);
                                        $this_EOT_sortfriendly = date('Y-m-d', $this_EOT);
                                        $dateNow = date_create(date('Y-m-d'));
    	                                $dateEOT = date_create(date('Y-m-d', $this_EOT));
                                        $idaysremaining = (int) date_diff($dateNow, $dateEOT)->format("%a");
    									if ( ( $idaysremaining > 31 ) && ( $this_eot_notify_state != "0" ) ) {
    									    // Renewal has taken place after a previous notification
    										update_user_meta($this_ID, 's2hacks_eot_notify_state', '0');
    									    $this_eot_notify_state = get_user_field('s2hacks_eot_notify_state', $this_ID);
    									}
    									$bSendEmail = false;
    									switch ( $this_eot_notify_state ) {
    									    case "0":  // 1st Notification
                                                       if ( $idaysremaining < 31 ) {
                                                           $bSendEmail = true;
    													   update_user_meta($this_ID, 's2hacks_eot_notify_state', '1');
    													   $subject = '1st Notification';
    												   }
    										           break;
    									    case "1":  // 2nd Notification
                                                       if ( $idaysremaining < 8 ) {
                                                           $bSendEmail = true;
    													   update_user_meta($this_ID, 's2hacks_eot_notify_state', '2');
    													   $subject = '2nd Notification';
    												   }
    										           break;
    									    case "2":  // 3rd Notification
                                                       if ( $idaysremaining < 2 ) {
                                                           $bSendEmail = true;
    													   update_user_meta($this_ID, 's2hacks_eot_notify_state', '3');
    													   $subject = '3rd Notification';
    												   }
    										           break;
    									    default:   //
    										           break;
    									}
    									if ( $bSendEmail ) {
    									    // Send email
    										$this_email = $user->user_email;
    										$admin_email = get_bloginfo('admin_email');
    										$subject = $subject . ' of Impending Membership Expiration';
    										$usermessage = 'Reminder: Your membership will expire in ' . $idaysremaining . ' days.  Please renew now.';
    										$adminmessage = 'Notice: ' . $this_email . ' will expire in ' . $idaysremaining . ' days.';
    										$bSendUserEmail = true;
    										if ( strpos(get_bloginfo('name'), 'Development') !== false ) {
                                                 // On Development Site
    											 $bSendUserEmail = false;
    											 if ( 'Test' === $user->first_name ) {
    											     // Test account - OK to send
    											     $bSendUserEmail = true;
    											 }
                                            }
    										if ( $bSendUserEmail ) {
             									wp_mail( $this_email,  $subject, $usermessage );
    										}
    										// Always send to admin
    										wp_mail( $admin_email, $subject, $adminmessage );
    									}
                                    }
                                    break;
                         default:   // Every other role
                                    break;
    				}
                }
            } else {
                  //echo "$users is empty";
            }
    }
    ?>

    Thread Starter simonac

    (@simonac)

    Great! Thank you guys for the info!

    Hi

    Does this still work? I’ve been futzing with it and can’t seem to get it to do anything.

    I see the jobs in wp croncontrol but no email is ever sent out

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Email Notification Before Expiration’ is closed to new replies.