Site icon Hip-Hop Website Design and Development

WordPress cron hooks – same callback for completely different action?

I am trying to better understand how WordPress deals with cron jobs so that I can better figure out how to create some custom jobs that send emails based on single occurrence events, like lesson started or lesson webinar started.

In my case, I want to have emails sent to users when I have certain content that becomes available. The availability is based on a date field that I have as custom meta for a custom post type.

So imagine a bunch of courses and lessons. When a lesson opens up, I want that event to trigger sending emails to users with access to the lesson. Likewise, when a lesson’s webinar starts, I also want to trigger emails.

Right now, I react to a save_post hook for my custom post type and do the this, where $date is the future start date or webinar date for the given lesson, and other variables are passed as needed

    $gmt = new DateTime();
    $gmt->setTimezone( new DateTimeZone( 'UTC' ) );
    $current_time = $gmt->getTimestamp();

    if ( $date > $current_time ) {
        $sent_on  = $date;
        if ( wp_next_scheduled( 'my_custom_cron', [ $notif_id,  $notif_name, $membership_id, $lesson_id ] ) ) {
            wp_clear_scheduled_hook( 'my_custom_cron', [ $notif_id, $notif_name, $membership_id, $lesson_id ] );
        }
        wp_schedule_single_event( $sent_on, 'my_custom_cron', [ $notif_id, $notif_name, $membership_id, $lesson_id ] );
    }

So this sets up single event cron is WP. In above, notif_name is lesosn_started or lesson_webinar_started.

To react to this hook, I have

add_action( 'my_custom_crons','my_custom_crons', 99, 4 );
function my_custom_crons( $notif, $notification_type, $membership_id, $lesson_id) {

    global $wpdb;
    $wpdb->query("SET time_zone = 'UTC'");
    $activeusers = $wpdb->get_results("SELECT DISTINCT user_id FROM wp_mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00') AND product_id = '$membership_id'");
    if ( empty( $activeusers ) ) {
        return;
    }

    // ok, we have users that are active
    foreach ( $activeusers as $activeuser ) {
        $user_id = absint( $activeuser->user_id );
        if ($user_id != 0) {
            // bunch of checking
            // get notification message for notification type (lessons tarted, or webinar started)
            // then send the emails

            wp_clear_scheduled_hook('my_custom_crons', array(
                $notif->ID,
                $notification_type, 
                $membership_id,
                $lesson_id,
            ));
        }
    }
}

All seems to be working in terms of cron being set up. If I run wp cron event list I see the my_custom_crons job scheduled for a given lesson that I just set up with a release date.

But what happens if I have multiple lessons and multiple events I want to use for email triggers, all with various dates? As mentioned, I might have lesson_started and lesson_webinar_started event for various lessons. So will my current approach work, or will various events for various lessons all triggered at different times, all using my one cron hook’s logic, interfere with one another?

Just not seeing bigger picture of how I should be doing this…

Thanks