LLMS_Engagements::maybe_trigger_engagement()
Handles all actions that could potentially trigger an engagement
Description Description
It will fire or schedule the actions after gathering all necessary data
Return Return
(void)
Source Source
File: includes/class.llms.engagements.php
public function maybe_trigger_engagement() { $action = current_filter(); $args = func_get_args(); $this->log( '======= start maybe_trigger_engagement ========' ); $this->log( '$action: ' . $action ); $this->log( '$args: ' . json_encode( $args ) ); // setup variables used in queries and triggers based on the action switch ( $action ) { case 'lifterlms_created_person' : $user_id = intval( $args[0] ); $trigger_type = 'user_registration'; $related_post_id = ''; break; case 'lifterlms_course_completed' : case 'lifterlms_course_track_completed' : case 'lifterlms_lesson_completed' : case 'lifterlms_section_completed' : $user_id = intval( $args[0] ); $related_post_id = intval( $args[1] ); $trigger_type = str_replace( 'lifterlms_', '', $action ); break; case 'lifterlms_quiz_completed': case 'lifterlms_quiz_passed': case 'lifterlms_quiz_failed': $user_id = absint( $args[0] ); $related_post_id = absint( $args[1] ); $trigger_type = str_replace( 'lifterlms_', '', $action ); break; case 'llms_user_added_to_membership_level': case 'llms_user_enrolled_in_course': $user_id = intval( $args[0] ); $related_post_id = intval( $args[1] ); $trigger_type = str_replace( 'llms_', '', get_post_type( $related_post_id ) ) . '_enrollment'; break; case 'lifterlms_access_plan_purchased' : case 'lifterlms_product_purchased' : $user_id = intval( $args[0] ); $related_post_id = intval( $args[1] ); $trigger_type = str_replace( 'llms_', '', get_post_type( $related_post_id ) ) . '_purchased'; break; // allow extensions to hook into our engagements default : extract( apply_filters( 'lifterlms_external_engagement_query_arguments' , array( 'related_post_id' => null, 'trigger_type' => null, 'user_id' => null, ), $action, $args ) ); }// End switch(). // we need a user and a trigger to proceed, related_post is optional though if ( ! $user_id || ! $trigger_type ) { return; } // gather triggerable engagements matching the supplied criteria $engagements = apply_filters( 'lifterlms_get_engagements' , $this->get_engagements( $trigger_type, $related_post_id ), $trigger_type, $related_post_id ); $this->log( '$engagements: ' . json_encode( $engagements ) ); // only trigger engagements if there are engagements if ( $engagements ) { // loop through the engagements foreach ( $engagements as $e ) { $handler_action = null; $handler_args = null; // do actions based on the event type switch ( $e->event_type ) { case 'achievement' : $handler_action = 'lifterlms_engagement_award_achievement'; $handler_args = array( $user_id, $e->engagement_id, $related_post_id ); break; case 'certificate' : /** * @todo fix this * if there's no related post id we have to send one anyway for certs to work * this would only be for registration events @ version 2.3.0 * we'll just send the engagement_id twice until we find a better solution */ $related_post_id = ( ! $related_post_id ) ? $e->engagement_id : $related_post_id; $handler_action = 'lifterlms_engagement_award_certificate'; $handler_args = array( $user_id, $e->engagement_id, $related_post_id ); break; case 'email' : $handler_action = 'lifterlms_engagement_send_email'; $handler_args = array( $user_id, $e->engagement_id, $related_post_id ); break; // allow extensions to hook into our engagements default : extract( apply_filters( 'lifterlms_external_engagement_handler_arguments' , array( 'handler_action' => $handler_action, 'handler_args' => $handler_args, ), $e, $user_id, $related_post_id, $trigger_type ) ); }// End switch(). // can't proceed without an action and a handler if ( ! $handler_action && ! $handler_args ) { continue; } // if we have a delay, schedule the engagement handler $delay = intval( $e->delay ); $this->log( '$delay: ' . $delay ); $this->log( '$handler_action: ' . $handler_action ); $this->log( '$handler_args: ' . json_encode( $handler_args ) ); if ( $delay ) { wp_schedule_single_event( time() + ( DAY_IN_SECONDS * $delay ), $handler_action, array( $handler_args ) ); } // End if(). else { do_action( $handler_action, $handler_args ); } }// End foreach(). }// End if(). $this->log( '======= end maybe_trigger_engagement ========' ); }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |