automator_wplms_save_tokens
Fires when saving tokens for WPLMS, allowing custom actions with token arguments and results.
add_action( 'automator_wplms_save_tokens', $callback, 10, 2 );
Description
Fired after WPLMS course, quiz, or unit completion, this action hook allows developers to hook into the token saving process. It provides the token arguments and the completion result, enabling custom actions or modifications before tokens are persisted.
Usage
add_action( 'automator_wplms_save_tokens', 'your_function_name', 10, 2 );
Parameters
-
$token_args(mixed) - This parameter contains an array of arguments used to store authentication tokens for the WPLMS integration.
-
$result(mixed) - This parameter contains an array of arguments related to the WPLMS event that triggered the hook, used for further processing or logic.
Examples
/**
* Example callback function for the automator_wplms_save_tokens action hook.
* This function logs the token arguments and result when a WPLMS event occurs.
*
* @param array $token_args An array containing details about the WPLMS event (e.g., course_id, user_id, action).
* @param array $result An array containing specific arguments related to the event's result.
*/
function my_custom_wplms_token_handler( $token_args, $result ) {
// Log the token arguments and the result for debugging or further processing.
error_log( 'WPLMS Automator Token Saved:' );
error_log( 'Token Args: ' . print_r( $token_args, true ) );
error_log( 'Result Args: ' . print_r( $result, true ) );
// In a real-world scenario, you might perform additional actions here,
// such as updating a custom meta field for the user, triggering another
// internal process, or interacting with an external service.
// For example:
/*
if ( isset( $token_args['user_id'] ) && isset( $token_args['course_id'] ) ) {
$user_id = $token_args['user_id'];
$course_id = $token_args['course_id'];
// Example: Update a user meta field to track completed courses.
$completed_courses = get_user_meta( $user_id, 'wplms_completed_courses', true );
if ( empty( $completed_courses ) ) {
$completed_courses = array();
}
if ( ! in_array( $course_id, $completed_courses ) ) {
$completed_courses[] = $course_id;
update_user_meta( $user_id, 'wplms_completed_courses', $completed_courses );
}
}
*/
}
// Hook the custom function to the 'automator_wplms_save_tokens' action.
// The '10' is the priority, and '2' indicates that the callback function accepts 2 arguments.
add_action( 'automator_wplms_save_tokens', 'my_custom_wplms_token_handler', 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wplms/triggers/wplms-coursecompleted.php:142
src/integrations/wplms/triggers/wplms-quizcompleted.php:144
src/integrations/wplms/triggers/wplms-coursestarted.php:142
src/integrations/wplms/triggers/wplms-unitcompleted.php:145
public function wplms_course_completed( $course_id, $user_id ) {
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
if ( empty( $user_id ) ) {
return;
}
$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
$required_course = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
$matched_recipe_ids = array();
if ( empty( $recipes ) ) {
return;
}
if ( empty( $required_course ) ) {
return;
}
foreach ( $recipes as $recipe_id => $recipe ) {
foreach ( $recipe['triggers'] as $trigger ) {
$recipe_id = absint( $recipe_id );
$trigger_id = absint( $trigger['ID'] );
if ( ! isset( $required_course[ $recipe_id ] ) ) {
continue;
}
if ( ! isset( $required_course[ $recipe_id ][ $trigger_id ] ) ) {
continue;
}
if (
( intval( '-1' ) === intval( $required_course[ $recipe_id ][ $trigger_id ] ) )
||
( absint( $course_id ) === absint( $required_course[ $recipe_id ][ $trigger_id ] ) )
) {
$matched_recipe_ids[ $recipe_id ] = array(
'recipe_id' => $recipe_id,
'trigger_id' => $trigger_id,
);
}
}
}
if ( empty( $matched_recipe_ids ) ) {
return;
}
foreach ( $matched_recipe_ids as $matched_recipe_id ) {
$pass_args = array(
'code' => $this->trigger_code,
'meta' => $this->trigger_meta,
'user_id' => $user_id,
'recipe_to_match' => $matched_recipe_id['recipe_id'],
'trigger_to_match' => $matched_recipe_id['trigger_id'],
'ignore_post_id' => true,
'is_signed_in' => true,
);
$arr = Automator()->maybe_add_trigger_entry( $pass_args, false );
if ( $arr ) {
foreach ( $arr as $result ) {
if ( true === $result['result'] ) {
$token_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'action' => 'course_completed',
);
do_action( 'automator_wplms_save_tokens', $token_args, $result['args'] );
Automator()->maybe_trigger_complete( $result['args'] );
}
}
}
}
}
Internal Usage
Found in src/integrations/wplms/tokens/wplms-tokens.php:23:
add_action( 'automator_wplms_save_tokens', array( $this, 'automator_wplms_save_tokens_func' ), 10, 2 );