gravityflow_step_assignees

The gravityflow_step_assignees filter allows for the assignees of a step to be filtered.

Example #1: Adding a role-based assignee based on a field value

    add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
    function sh_step_assignees_role_field_value( $assignees, $step ) {
       if ( gravity_flow()->is_workflow_detail_page() && $step->get_id() == 63 ) {
            $entry = $step->get_entry();
            //Add logic to confirm the entry value actually matches to a role within your site
            $new_role = str_replace( '-', '_', $entry['10'] );
            $args = array(
                'id'  => $new_role,
                'type' => 'role',
                'key' => 'role|' . $new_role,
                'editable_fields' => array( '1000' ),
            );
            $new_assignee = new Gravity_Flow_Assignee( $args, $step );
            $assignees[] = $new_assignee;
        }
        return $assignees;
    }

Apart from checking on the step id, we can also check on the step type ('approval' for instance).

add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
function sh_step_assignees_role_field_value( $assignees, $step ) {
   if ( $step->step_type ) {
        $entry = $step->get_entry();
        //Add logic to confirm the entry value actually matches to a role within your site
        $new_role = str_replace( '-', '_', $entry['10'] );
        $args = array(
            'id'  => $new_role,
            'type' => 'role',
            'key' => 'role|' . $new_role,
            'editable_fields' => array( '1000' ),
        );
        $new_assignee = new Gravity_Flow_Assignee( $args, $step );
        $assignees[] = $new_assignee;
    }
    return $assignees;
}

Example #2: Remove current user from assignees if they created the entry.

The following snippet can be used to remove the current user from the list of assignees if the current user was the one who created the entry. The delisting of the current user is checked from all 3 possibilities - user_id, role, or email assignment.

add_filter('gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2);
function sh_step_assignees_role_field_value($assignees, $step) {
    if ( gravity_flow()->is_workflow_detail_page() && $step->get_id() == 350 ) {
        $entry = $step->get_entry();
        $created_by_user_id = $entry['created_by'];
        $current_user = wp_get_current_user();

        if ( $created_by_user_id != $current_user->ID) {
            return $assignees;
        }

        $new_assignees = array();

        foreach ( $assignees as $assignee ) {
            switch ($assignee->get_type()) {
                case 'user_id':
                    $assignee_id = $assignee->get_id();

                    if ( $assignee_id != $current_user->ID ) {
                        $new_assignees[] = $assignee;
                    }
                    break;

                case 'role':
                    $assignee_role = $assignee->get_id();

                    if ( ! in_array($assignee_role, $current_user->roles ) ) {
                        $new_assignees[] = $assignee;
                    }
                    break;

                case 'email':
                    $assignee_email = $assignee->get_id();

                    if ( $assignee_email != $current_user->user_email ) {
                        $new_assignees[] = $assignee;
                    }
                    break;
            }
        }
        $assignees = $new_assignees;
    }
        return $assignees;
}

Example #3: Convert role-based assignee into users.

This filter ensures that the step is always assigned to every user of a given role - over ridding whatever assignee(s) are defined in the step settings.

add_filter( 'gravityflow_step_assignees', 'jo_step_assignees_by_user_role', 10, 2 );
function jo_step_assignees_by_user_role( $assignees, $step ) {

	//Modify this to match the step ID of your approval step that needs customization.
	$step_id_approval = 570;
	//Modify this to match the role which you want to have assignes derived from.
	$step_role        = 'administrator';

	if ( $step->get_id() == $step_id_approval ) {

		//Add logic to confirm the entry value actually matches to a role within your site
		$assignees = array();
		$users     = get_users( array( 'fields' => array( 'ID' ), 'role' => $step_role ) );

		foreach ( $users as $user ) {
			$args         = array(
				'name'            => 'generic',
				'id'              => $user->ID,
				'type'            => 'user_id',
				'key'             => 'user_id|' . $user->ID,
				'editable_fields' => array(),
			);
			$new_assignee = new Gravity_Flow_Assignee( $args, $step );
			$assignees[]  = $new_assignee;
		}
	}
	return $assignees;
}

Example #4: Add an assignee for a Workflow Step, if none is assigned (or removed)

This filter ensures that a step is always assigned to a user. See the FAQ about 'What happens to workflow when a step has no assignees / an assignee user account no longer exists?' for why this approach may be beneficial in specific use cases.

add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
function sh_step_assignees_role_field_value( $assignees, $step ) {
    if ( $step->get_id() == 63 ) {
        if ( empty( $assignees ) ) {
            $args = array(
                'id'  => 'administrator',
                'type' => 'role',
                'key' => 'role|administrator',
            );
            $new_assignee = new Gravity_Flow_Assignee( $args, $step );
            $assignees[] = $new_assignee;
        }
    }
    return $assignees;
}

Example #5: Let a user input step be considered complete after X assignees have responded.

The default assignee policy offers any (1 assignee) or all (every assignee) options. With this filter you could let the step be considered complete when a specific # have completed their task (as the 5 in this version shows) or based on a % of total assignees, etc. Ensure that this filter is in place before any entries on the selected user input step have completed their task (or resstart them) to avoid any edge cases where the "complete" tally differs from the overall step status.

add_filter( 'gravityflow_step_assignees', 'jo_user_input_assignee_count_complete', 10, 2 );
function jo_user_input_assignee_count_complete( $assignees, $step ) {
 	if ( $step->get_id() != 63 ) {
		return $assignees;
	}

	$complete_assignees = array();
	foreach( $assignees as $assignee ) {
		if ( $assignee->get_status() == 'complete' ) {
			$complete_assignees[] = $assignee;
		}
	}

	if ( count( $complete_assignees ) >= 5 ) {
		$assignees = $complete_assignees;
	}

	return $assignees;
}

Placement

This code should be placed in the functions.php file of your active theme.