Filter uncanny-automator

automator_sanitized_json

Filters the sanitized JSON data before it is saved, allowing modification based on type, meta key, and options.

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

Description

Filters sanitized JSON data, allowing developers to modify the filtered output. This hook is applied after JSON data is decoded and individual values are filtered. Developers can use it to alter the sanitized JSON string, manipulate the type, meta key, or options used during filtering, or perform custom validation on the resulting data before it's returned.


Usage

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

Parameters

$filtered (mixed)
The `$filtered` parameter contains the data that has been processed and potentially sanitized based on the specified filter type.
$type (mixed)
The `$filtered` parameter contains the sanitized data after applying the specified filters.
$meta_key (mixed)
This parameter indicates the type of data being sanitized, influencing the specific filtering method applied.
$options (mixed)
This parameter specifies the meta key associated with the data being sanitized.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_sanitized_json' filter hook.
 * This example will ensure that any email addresses within the JSON data
 * are validated and that any non-email values are sanitized as strings.
 */
add_filter( 'automator_sanitized_json', function( $filtered_json, $type, $meta_key, $options ) {

	// Decode the JSON string back into a PHP array/object for processing.
	$sanitized_data = json_decode( $filtered_json, true );

	// Check if decoding was successful and if it's an array.
	if ( ! is_array( $sanitized_data ) ) {
		return $filtered_json; // Return original if not a valid array.
	}

	// Iterate through the data to apply specific sanitization based on keys.
	foreach ( $sanitized_data as $key => &$value ) {
		// Specifically validate email addresses.
		if ( strpos( $key, 'email' ) !== false && is_string( $value ) ) {
			if ( filter_var( $value, FILTER_VALIDATE_EMAIL ) === false ) {
				// If it's not a valid email, revert to a general string sanitization.
				$value = sanitize_text_field( $value );
			}
			// If it's a valid email, we assume it's okay as is after initial sanitization.
		} else {
			// For any other keys, ensure they are sanitized as general text fields.
			$value = sanitize_text_field( $value );
		}
	}
	unset( $value ); // Unset the reference to avoid unintended modifications.

	// Re-encode the processed data back into a JSON string and return it.
	return wp_json_encode( $sanitized_data );

}, 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:552

public function automator_sanitize_json( $data, $slash_only = false ) {
		if ( $slash_only ) {
			return wp_slash( $data );
		}
		$filters = array(
			'email'   => FILTER_VALIDATE_EMAIL,
			'url'     => FILTER_VALIDATE_URL,
			'name'    => FILTER_UNSAFE_RAW,
			'address' => FILTER_UNSAFE_RAW,
		);
		$options = array(
			'email' => array(
				'flags' => FILTER_NULL_ON_FAILURE,
			),
			'url'   => array(
				'flags' => FILTER_NULL_ON_FAILURE,
			),
			//... and so on
		);
		$inputs   = json_decode( $data );
		$filtered = array();
		foreach ( $inputs as $key => $value ) {
			$filtered[ $key ] = filter_var( $value, $filters[ $key ], $options[ $key ] );
		}

		return apply_filters( 'automator_sanitized_json', wp_slash( wp_json_encode( $filtered ) ), $type, $meta_key, $options );
	}

Scroll to Top