Filter Dynamic uncanny-automator

automator_sanitize_input_fields_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters user input for specific automator fields, allowing modification before processing.

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

Description

This dynamic filter hook allows sanitization of recipe input fields. It fires after default meta values are retrieved for a recipe and before they are saved. Developers can use it to modify or validate meta keys and values to ensure data integrity. The dynamic part `{dynamic}` refers to the recipe ID, allowing for recipe-specific sanitization logic.


Usage

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

Parameters

$meta_key (mixed)
This parameter is a boolean that indicates whether the sanitization process should proceed.
$meta_value (mixed)
This parameter contains the meta key associated with the input field being sanitized.

Return Value

The filtered value.


Examples

/**
 * Example filter for 'automator_sanitize_input_fields_{dynamic}'.
 *
 * This filter allows you to further sanitize specific meta values based on their meta key
 * or the recipe ID they belong to. In this example, we'll prevent certain sensitive
 * meta keys from being sanitized if they are associated with a specific recipe ID.
 *
 * @param mixed  $value      The initial sanitized value (default is true).
 * @param string $meta_key   The meta key of the input field.
 * @param mixed  $meta_value The meta value of the input field.
 * @param int    $recipe_id  The ID of the current recipe.
 *
 * @return mixed The potentially modified sanitized value.
 */
add_filter( 'automator_sanitize_input_fields_123', function ( $value, $meta_key, $meta_value, $recipe_id ) {
	// Assume recipe ID 123 is a special case where we don't want to overly sanitize certain fields.
	if ( $recipe_id === 123 ) {
		// Example: Do not sanitize 'api_key' or 'secret_token' for this specific recipe.
		if ( in_array( $meta_key, array( 'api_key', 'secret_token' ) ) ) {
			// Returning false here will bypass the default sanitization for this field.
			// Note: The original hook returns `true` by default to proceed with sanitization.
			// By returning `false`, we're essentially saying "don't sanitize this particular field
			// with the default method if it's this meta_key and this recipe_id."
			// However, the source code shows `true === apply_filters(...)`.
			// So to *prevent* further sanitization, we should return `false`.
			return false;
		}
	}

	// For all other cases, let the default sanitization proceed by returning true.
	return $value;
}, 10, 4 );

/**
 * Example filter for 'automator_sanitize_input_fields' (general hook).
 *
 * This is a more general example. It demonstrates how to ensure that meta values
 * intended to be URLs are properly sanitized as URLs, and that numeric values
 * are treated as integers.
 *
 * @param mixed  $value      The initial sanitized value (default is true).
 * @param string $meta_key   The meta key of the input field.
 * @param mixed  $meta_value The meta value of the input field.
 *
 * @return mixed The potentially modified sanitized value.
 */
add_filter( 'automator_sanitize_input_fields', function ( $value, $meta_key, $meta_value ) {
	// If the meta value is intended to be a URL, sanitize it as such.
	if ( strpos( $meta_key, '_url' ) !== false && is_string( $meta_value ) ) {
		// You would typically use a more robust URL sanitization function here.
		// For demonstration, we'll use sanitize_url which is a WordPress function.
		// Note: This example assumes the $meta_value is meant to be a URL.
		// The actual logic would depend on your specific application's needs.
		return esc_url_raw( $meta_value );
	}

	// If the meta value looks like a number, and the meta key suggests it, try to cast to integer.
	// Be cautious with this; ensure it's truly meant to be an integer.
	if ( is_numeric( $meta_value ) && ! is_int( $meta_value ) && ! is_float( $meta_value ) ) {
		// The source code uses Automator()->utilities->automator_sanitize($meta_value)
		// which likely handles various sanitization types.
		// For demonstration, we'll show a specific integer sanitization.
		return intval( $meta_value );
	}

	// Return the original value if no specific sanitization is applied.
	return $value;
}, 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/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:572

do_action( 'automator_recipe_closure_created', $post_id, $item_code, $request );
		}

		if ( ! empty( $default_meta ) && is_array( $default_meta ) ) {
			foreach ( $default_meta as $meta_key => $meta_value ) {
				if (
					true === apply_filters( 'automator_sanitize_input_fields', true, $meta_key, $meta_value, $recipe->ID ) &&
					true === apply_filters( 'automator_sanitize_input_fields_' . $recipe->ID, true, $meta_key, $meta_value )
				) {
					$meta_value = Automator()->utilities->automator_sanitize( $meta_value );
					$meta_key   = Automator()->utilities->automator_sanitize( $meta_key );
				}
				update_post_meta( $post_id, $meta_key, $meta_value );
			}
		}


Scroll to Top