Filter uncanny-automator

automator_sanitized_data

Filters sanitized data before it's used in the Automator plugin, allowing modification of data based on type, meta key, and options.

add_filter( 'automator_sanitized_data', $callback, 10, 4 );

Description

Fires after data is potentially sanitized for Automator inputs. Developers can use this filter to further modify or validate the sanitized data before it's used in automations. It's applied to various data types passed through the sanitization process.


Usage

add_filter( 'automator_sanitized_data', 'your_function_name', 10, 4 );

Parameters

$data (mixed)
This parameter contains the data that needs to be sanitized.
$type (mixed)
This parameter contains the raw data that needs to be sanitized.
$meta_key (mixed)
This parameter specifies the data type for sanitization, defaulting to 'text'.
$options (mixed)
This parameter represents the meta key associated with the data being sanitized, often used for post meta or other custom field storage.

Return Value

The filtered value.


Examples

<?php
/**
 * Example callback for the 'automator_sanitized_data' filter hook.
 *
 * This callback checks if the data is a specific type and performs custom sanitization
 * or modification if it meets certain criteria. In this example, we'll assume we want
 * to ensure that if the $type is 'email', the data is always lowercase.
 *
 * @param mixed  $data      The data being sanitized.
 * @param mixed  $type      The type of data being processed.
 * @param mixed  $meta_key  The meta key associated with the data.
 * @param mixed  $options   Additional options for sanitization.
 *
 * @return mixed The potentially modified sanitized data.
 */
function my_custom_automator_sanitization( $data, $type, $meta_key, $options ) {
    // Only perform custom sanitization if the data type is 'email'
    if ( 'email' === $type && is_string( $data ) ) {
        // Ensure the email address is always in lowercase
        $data = strtolower( $data );
    }

    // You could add other conditions here for different data types or meta keys.
    // For example, if you wanted to strip slashes from a specific field:
    // if ( 'user_bio' === $meta_key && is_string( $data ) ) {
    //     $data = stripslashes( $data );
    // }

    // Always return the data, whether modified or not.
    return $data;
}
add_filter( 'automator_sanitized_data', 'my_custom_automator_sanitization', 10, 4 );

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/lib/utilities/class-automator-utilities.php:518

public function automator_sanitize( $data, $type = 'text', $meta_key = '', $options = array() ) {

		// If it's an array, handle it early and return data.
		if ( is_array( $data ) ) {
			return $this->automator_sanitize_array( $data, $meta_key, $options );
		}

		if ( empty( $type ) || 'mixed' === $type ) {
			$type = $this->maybe_get_field_type( $meta_key, $options );
		}

		switch ( $type ) {
			case 'html':
			case 'repeater':
			case 'markdown':
			case 'select':
				// Do nothing for these types.
				break;
			case 'textarea':
				$data = sanitize_textarea_field( $data );
				break;
			case 'url':
				// Only escape the data if there are no tokens.
				preg_match_all( '/{{s*(.*?)s*}}/', $data, $tokens );
				if ( ! isset( $tokens[0] ) || empty( $tokens[0] ) ) {
					$data = esc_url_raw( $data ); // Use esc_url_raw so ampersand won't be encoded.
				}
				break;
			case 'text':
				$data = sanitize_text_field( $data );
				break;
			case 'mixed':
				// Apply default sanitization for 'mixed' type.
			default:
				if ( wp_strip_all_tags( $data ) === $data ) {
					$data = sanitize_text_field( $data );
				}

				break;
		}

		return apply_filters( 'automator_sanitized_data', $data, $type, $meta_key, $options );
	}

Scroll to Top