Skip to content

Instantly share code, notes, and snippets.

@bryanrsebastian
Created August 11, 2017 02:19
Show Gist options
  • Save bryanrsebastian/d4a37f2edf51c7924a0a1c76cc5439ab to your computer and use it in GitHub Desktop.
Save bryanrsebastian/d4a37f2edf51c7924a0a1c76cc5439ab to your computer and use it in GitHub Desktop.
function wpse27856_set_content_type(){
return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
function myprefix_custom_cron_schedule( $schedules )
{
$schedules['everysecond'] = array(
'interval' => 1, // everysecond
'display' => __( 'Every second' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
wp_schedule_event( time(), 'daily', 'myprefix_cron_hook' );
}
///Hook into that action that'll fire every six hours
add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
//create your function, that runs on cron
function myprefix_cron_function()
{
email_notifications();
}
function email_notifications()
{
$subscriptions = get_posts( array(
'post_type' => 'subscriptions',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'ief_subscription_status',
'value' => 'completed',
)
),
) );
if( $subscriptions && ! empty( $subscriptions ) && ! is_null( $subscriptions ) ) {
foreach( $subscriptions as $subscription ) {
$currrent_date = date( 'Y-m-d' );
$start_subscription = get_field( 'subscription_date', $subscription->ID );
$start_subscription_date = date( 'Y-m-d', strtotime( $start_subscription ) );
$end_subscription = get_field( 'end_subscription_date', $subscription->ID );
$end_subscription_date = date( 'Y-m-d', strtotime( $end_subscription ) );
$remaining_days = date_diff( date_create( $currrent_date ),date_create( $end_subscription_date ) );
$subscriber = get_userdata( get_field( 'subscriber_user_id', $subscription->ID ) );
$course = get_post( get_field( 'course_id', $subscription->ID ) );
/* 2 weeks before the end of subscription */
if( $remaining_days->d == 15 ) {
wp_mail(
$subscriber->user_email,
'Subscrption for '. $course->post_title,
'Hello '. $subscriber->display_name .'<br><br>Please be informed that your subscription for '. $course->post_title .' will expire on '. $end_subscription .' and you have '. $remaining_days->d .' days remaining.<br><br>Sincerely,<br>IEF'
);
}
/* 1 week before the end of subscription */
else if( $remaining_days->d == 8 ) {
wp_mail(
$subscriber->user_email,
'Subscrption for '. $course->post_title,
'Hello '. $subscriber->display_name .'<br><br>Please be informed that your subscription for '. $course->post_title .' will expire on '. $end_subscription .' and you have '. $remaining_days->d .' days remaining.<br><br>Sincerely,<br>IEF'
);
}
/* 3 days before the end of subscription */
else if( $remaining_days->d == 4 ) {
wp_mail(
$subscriber->user_email,
'Subscrption for '. $course->post_title,
'Hello '. $subscriber->display_name .'<br><br>Please be informed that your subscription for '. $course->post_title .' will expire on '. $end_subscription .' and you have '. $remaining_days->d .' days remaining.<br><br>Sincerely,<br>IEF'
);
}
/* 1 day before the end of subscription */
else if( $remaining_days->d == 2 ) {
wp_mail(
$subscriber->user_email,
'Subscrption for '. $course->post_title,
'Hello '. $subscriber->display_name .'<br><br>Please be informed that your subscription for '. $course->post_title .' will expire on '. $end_subscription .' and you have '. $remaining_days->d .' days remaining.<br><br>Sincerely,<br>IEF'
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment