Filter Dynamic uncanny-automator

automator_set_user_walkthrough_progress_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the user walkthrough progress before it's saved, allowing modification of the current step.

add_filter( 'automator_set_user_walkthrough_progress_{dynamic}', $callback, 10, 2 );

Description

This filter hook allows modification of user walkthrough progress data before it's saved. It fires after progress is parsed and before it's updated. Developers can use this to alter the progress array, for example, to conditionally add or remove steps or change their status. The hook is dynamic and includes the walkthrough ID for more specific filtering.


Usage

add_filter( 'automator_set_user_walkthrough_progress_{dynamic}', 'your_function_name', 10, 2 );

Parameters

$progress (mixed)
This parameter contains the current progress data for a user walkthrough, which can be modified by the filter.
$this (mixed)
This parameter represents the user's current progress in the walkthrough, which can be modified by the filter.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter user walkthrough progress.
 *
 * This function demonstrates how to modify the progress data for a specific user walkthrough.
 * It checks if the walkthrough is related to 'onboarding_tutorial' and adds a new step
 * to the progress if a specific condition is met.
 *
 * @param array $progress The current progress data for the walkthrough.
 * @param int   $user_id  The ID of the user whose progress is being updated.
 * @param int   $walkthrough_id The ID of the walkthrough.
 *
 * @return array The modified progress data.
 */
add_filter( 'automator_set_user_walkthrough_progress_onboarding_tutorial', function( $progress, $user_id, $walkthrough_id ) {

	// Check if the current user has completed the initial setup step.
	// This is a hypothetical check, imagine it's done via another option or meta.
	$has_completed_initial_setup = get_user_meta( $user_id, 'onboarding_initial_setup_complete', true );

	// If initial setup is complete and the 'welcome_screen' step hasn't been completed yet.
	if ( $has_completed_initial_setup && ! isset( $progress['steps']['welcome_screen'] ) ) {
		// Add the 'welcome_screen' step to the progress.
		$progress['steps']['welcome_screen'] = [
			'completed' => true,
			'timestamp' => time(),
		];

		// Update the overall progress status if necessary (e.g., 'in_progress', 'completed').
		// For this example, we'll just mark it as 'in_progress' if it wasn't already.
		if ( empty( $progress['status'] ) || $progress['status'] !== 'completed' ) {
			$progress['status'] = 'in_progress';
		}
	}

	return $progress;

}, 10, 3 );

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/core/lib/utilities/class-automator-user-walkthroughs.php:169

public function set_progress_by_id( string $id, array $update ) {

		$this->validate_walkthrough_id( $id );

		if ( empty( $update ) ) {
			$this->remove_user_walkthrough_progress( $id );
			return;
		}

		$existing = $this->get_progress_by_id( $id );
		$progress = wp_parse_args( $update, $existing );
		$progress = apply_filters( 'automator_set_user_walkthrough_progress', $progress, $this->user_id, $id );
		$progress = apply_filters( 'automator_set_user_walkthrough_progress_' . $id, $progress, $this->user_id );

		if ( empty( $progress ) ) {
			$this->remove_user_walkthrough_progress( $id );
			return;
		}

		$this->user_progress_meta[ $id ] = $progress;
		$this->save_progress();
	}


Scroll to Top