Filter uncanny-automator

automator_pro_gravity_forms_user_id

Filters the user ID used by Automator Pro for Gravity Forms entries before saving.

add_filter( 'automator_pro_gravity_forms_user_id', $callback, 10, 2 );

Description

Allows developers to modify the user ID used when a Gravity Forms entry is updated or created. This filter is applied after the default user is determined but before it's assigned to the entry. You can use it to dynamically set the user based on form data or other logic, overriding the default current user.


Usage

add_filter( 'automator_pro_gravity_forms_user_id', 'your_function_name', 10, 2 );

Parameters

$user_id (mixed)
This parameter holds the user ID that will be used for the action, defaulting to the currently logged-in user.
$entry (mixed)
This parameter holds the user ID to be used for the automation, allowing for overrides or modifications.

Return Value

The filtered value.


Examples

<?php
/**
 * Filters the user ID associated with a Gravity Forms entry update for Uncanny Automator Pro.
 *
 * This function allows modifying the user ID that Uncanny Automator Pro uses when a Gravity Forms
 * entry is updated or created. It's particularly useful for scenarios where you want to attribute
 * actions to a specific user based on form data or other conditions, rather than just the user
 * who is currently logged in or the initial entry creator.
 *
 * @param int|null|WP_User $user_id The current user ID, which could be an integer ID, null, or a WP_User object.
 * @param array            $entry   The Gravity Forms entry array.
 * @param WP_User          $current_user The currently logged-in WordPress user object.
 *
 * @return int|null The modified user ID to be used by Uncanny Automator Pro.
 */
add_filter( 'automator_pro_gravity_forms_user_id', function( $user_id, $entry, $current_user ) {

    // Example: If a specific field in the Gravity Form contains a user ID (e.g., email or username),
    // try to find and use that user's ID.
    // This assumes you have a way to map form field values to user IDs.
    // For simplicity, let's assume a hidden field with ID '10' contains the user's email.

    $gravity_forms_api = class_exists( 'GFAPI' ) ? GFAPI::get_instance() : null;

    if ( ! $gravity_forms_api ) {
        return $user_id; // Cannot proceed without Gravity Forms API
    }

    // Let's assume a hidden field (field ID 10) in the form contains the user's email address
    // when the entry is submitted or updated.
    $user_email_from_form = null;
    if ( isset( $entry['fields'] ) ) {
        foreach ( $entry['fields'] as $field ) {
            if ( $field['id'] == 10 && ! empty( $field['value'] ) ) {
                $user_email_from_form = sanitize_email( $field['value'] );
                break;
            }
        }
    }

    if ( $user_email_from_form ) {
        $user = get_user_by( 'email', $user_email_from_form );
        if ( $user instanceof WP_User && ! empty( $user->ID ) ) {
            // Found a user by email from the form, use their ID.
            return absint( $user->ID );
        }
    }

    // If no specific user ID was found from the form, fall back to the original user ID.
    // This could be the entry creator or the currently logged-in user.
    return $user_id;

}, 10, 3 ); // Priority 10, accepts 3 arguments: $user_id, $entry, $current_user
?>

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/gravity-forms/triggers/anon-gf-form-entry-updated.php:187
src/integrations/gravity-forms/triggers/anon-gf-subform.php:128
uncanny-automator-pro/src/integrations/gravity-forms/triggers/gf-subfield.php:208
uncanny-automator-pro/src/integrations/gravity-forms/triggers/anon-gf-form-field-matchable.php:165
uncanny-automator-pro/src/integrations/gravity-forms/triggers/anon-gf-sublist.php:255
uncanny-automator-pro/src/integrations/gravity-forms/triggers/gf-subform-payment.php:114
uncanny-automator-pro/src/integrations/gravity-forms/triggers/anon-gf-subfield.php:184
uncanny-automator-pro/src/integrations/gravity-forms/triggers/anon-gf-subform-payment.php:141

public function validate( $trigger, $hook_args ) {

		// If any form is selected
		if ( '-1' === $trigger['meta'][ $this->get_trigger_meta() ] ) {
			return true;
		}

		$hook_args = $this->process_data_from_two_hooks( $hook_args );

		list( $form, $entry_id, $original_entry ) = $hook_args;

		if ( absint( $form['id'] ) === absint( $trigger['meta'][ $this->get_trigger_meta() ] ) ) {
			// Get entry to check created_by
			$entry = GFAPI::get_entry( $entry_id );

			// Set user ID dynamically
			$user_id = isset( $entry['created_by'] ) && 0 !== absint( $entry['created_by'] ) ? absint( $entry['created_by'] ) : wp_get_current_user()->ID;
			$user_id = apply_filters( 'automator_pro_gravity_forms_user_id', $user_id, $entry, wp_get_current_user() );

			$this->set_user_id( $user_id );

			return true;
		}

		return false;
	}

Scroll to Top