Filter uncanny-automator

automator_field_values_before_save

Filters field values right before they are saved, allowing modification of the meta value and item data.

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

Description

Fires before a recipe's meta field values are saved, allowing you to modify them. Developers can alter `$meta_value` (an array of field key/value pairs) and `$item` (the recipe object) to customize data before it's persisted. This is crucial for data validation, transformation, or custom processing.


Usage

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

Parameters

$meta_value (mixed)
This parameter contains the value of the meta field before it is saved to the database, allowing it to be modified by filters.
$item (mixed)
This parameter contains the raw value of the meta field that is about to be saved.

Return Value

The filtered value.


Examples

add_filter( 'automator_field_values_before_save', 'my_automator_clean_api_keys', 10, 2 );

/**
 * Cleans and sanitizes API keys before they are saved for an Automator recipe.
 * This prevents accidentally saving sensitive API keys in plain text if they are malformed or contain unwanted characters.
 *
 * @param array $meta_value The array of meta values being prepared for saving. This often contains keys and their associated values.
 * @param mixed $item       The item being processed (e.g., a recipe object).
 *
 * @return array The potentially modified $meta_value array after cleaning API keys.
 */
function my_automator_clean_api_keys( $meta_value, $item ) {
    // Define a list of keys that are likely API keys or sensitive credentials.
    $api_key_fields = array(
        'api_key',
        'secret_key',
        'password', // In some contexts, password might be a sensitive key.
        'auth_token',
        'client_secret',
    );

    // Check if $meta_value is an array, as expected for multiple fields.
    if ( is_array( $meta_value ) ) {
        foreach ( $meta_value as $key => $value ) {
            // Check if the current key is in our list of sensitive fields.
            if ( in_array( $key, $api_key_fields, true ) ) {
                // Sanitize the value by removing any whitespace and potentially unwanted characters.
                // esc_url() is a good general-purpose sanitizer for URL-like strings,
                // and sanitize_text_field() is good for general text.
                // We'll use sanitize_text_field here for broader applicability.
                $sanitized_value = sanitize_text_field( $value );

                // Optionally, add further validation or specific sanitization if needed.
                // For example, if you expect a specific format for an API key.

                // Update the meta value with the sanitized version.
                $meta_value[ $key ] = $sanitized_value;
            }
        }
    }

    // Always return the $meta_value, whether modified or not.
    return $meta_value;
}

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/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:712

// @since 6.0
				do_action( 'automator_recipe_before_update', $item, $request );

				$before_update_value = get_post_meta( $item_id, $meta_key, true );

				if ( is_array( $meta_value ) ) {
					// Allow integrations to hook into the filter.
					$meta_value = apply_filters( 'automator_field_values_before_save', $meta_value, $item );

					foreach ( $meta_value as $meta_key => $meta_val ) {
						$meta_val = Automator()->utilities->maybe_slash_json_value( $meta_val, true );
						update_post_meta( $item_id, $meta_key, $meta_val );

						/**
						 * Added in case the action uses another action's token,


Internal Usage

Found in src/integrations/campaign-monitor/campaign-monitor-integration.php:91:

add_filter( 'automator_field_values_before_save', array( $this->helpers, 'maybe_save_action_client_meta' ), 20, 2 );

Found in src/core/lib/webhooks/class-automator-send-webhook.php:61:

add_filter( 'automator_field_values_before_save', array( $this, 'encrypt_authorization_values' ), 10, 2 );

Found in uncanny-automator-pro/src/integrations/wp/actions/wp-setpostthumbnail.php:19:

add_filter( 'automator_field_values_before_save', array( $this, 'handle_mixed_media_sanitation' ), 10, 2 );
Scroll to Top