Filter uncanny-automator

automator_wsformlite_parse_common_trigger_tokens

Filters WS Form Lite submitted data to include custom trigger tokens in automations.

add_filter( 'automator_wsformlite_parse_common_trigger_tokens', $callback, 10, 1 );

Description

Filters the array of WS Form Lite common trigger tokens before they are parsed. Developers can use this hook to add, remove, or modify token identifiers, allowing for custom data replacement within WS Form Lite submissions in AutomatorWP recipes. This hook fires during the token parsing process for WS Form Lite triggers.


Usage

add_filter( 'automator_wsformlite_parse_common_trigger_tokens', 'your_function_name', 10, 1 );

Parameters

$pieces (mixed)
This parameter contains an array of token prefixes that identify specific WS Form Lite trigger types.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_wsformlite_parse_common_trigger_tokens' filter.
 * This example demonstrates how to add a custom token for WS Form Lite submissions
 * to be used within the Automator plugin.
 *
 * @param array $tokens An array of tokens that will be processed.
 * @param array $args   An array containing contextual data for the trigger.
 * @return array The modified array of tokens.
 */
function my_custom_wsformlite_tokens( $tokens, $args ) {

    // Ensure we have the necessary arguments to proceed.
    if ( ! isset( $args['pieces'] ) || ! isset( $args['trigger_data'] ) ) {
        return $tokens;
    }

    $pieces       = $args['pieces'];
    $trigger_data = $args['trigger_data'];

    // Check if the current trigger is specifically for WS Form Lite submissions.
    // This assumes 'WSFORM_FROM_SUBMITTED' is the token representing a WS Form submission.
    if ( in_array( 'WSFORM_FROM_SUBMITTED', $pieces, true ) ) {

        // Get the actual form submission data from the trigger meta.
        // The structure of $trigger_data will depend on how WS Form Lite and Automator integrate.
        // For this example, let's assume the submission data is available at $trigger_data['meta']['wsform_submission_data'].
        $submission_data = isset( $trigger_data['meta']['wsform_submission_data'] ) ? $trigger_data['meta']['wsform_submission_data'] : null;

        if ( $submission_data && is_array( $submission_data ) ) {
            // Add a custom token for the email address of the submitter.
            // We're assuming the 'email' field exists in the submission data.
            if ( isset( $submission_data['email'] ) ) {
                $tokens['WSFORM_SUBMITTER_EMAIL'] = $submission_data['email'];
            }

            // Add a custom token for a specific field value, e.g., 'product_name'.
            // Again, assuming this field exists in the submission data.
            if ( isset( $submission_data['product_name'] ) ) {
                $tokens['WSFORM_PRODUCT_NAME'] = $submission_data['product_name'];
            }
        }
    }

    return $tokens;
}
add_filter( 'automator_wsformlite_parse_common_trigger_tokens', 'my_custom_wsformlite_tokens', 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/ws-form-lite/tokens/ws-form-lite-tokens.php:138

public function parse_wsformlite_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

		if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
			return $value;
		}

		$trigger_meta_validations = apply_filters(
			'automator_wsformlite_parse_common_trigger_tokens',
			array( 'WSFORM_FROM_SUBMITTED', 'WSFORM_ANON_FROM_SUBMITTED' ),
			array(
				'pieces'       => $pieces,
				'recipe_id'    => $recipe_id,
				'trigger_data' => $trigger_data,
				'user_id'      => $user_id,
				'replace_args' => $replace_args,
			)
		);

		if ( ! array_intersect( $trigger_meta_validations, $pieces ) ) {
			return $value;
		}

		$entry_id   = false;
		$to_replace = $pieces[2];
		$token      = explode( '|', $to_replace );
		if ( is_array( $token ) && isset( $token[1] ) ) {
			$entry_id = Automator()->db->token->get( 'form_entry_id', $replace_args );
		} else {
			$form_id = Automator()->db->token->get( 'form_id', $replace_args );
		}

		switch ( $to_replace ) {
			case 'FORM_ID':
				$value = $form_id;
				break;
			case 'FORM_TITLE':
				global $wpdb;
				$value = $wpdb->get_var( $wpdb->prepare( "SELECT label FROM {$wpdb->prefix}wsf_form WHERE id=%d", $form_id ) );
				break;
			default:
				if ( $entry_id ) {
					global $wpdb;
					$field_value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->prefix}wsf_submit_meta WHERE parent_id=%d AND meta_key LIKE %s", $entry_id, $token[1] ) );
					$value       = maybe_unserialize( $field_value );
					// Check for File Uploads.
					if ( is_array( $value ) && ! empty( $value[0] ) && is_array( $value[0] ) && isset( $value[0]['size'] ) && isset( $value[0]['type'] ) ) {
						$value = $this->get_file_upload_value( $value );
					}
				}
				break;
		}

		return $value;
	}

Scroll to Top