Filter uncanny-automator

automator_cf7_data_type_of_array

Filters the data type for Contact Form 7 checkbox fields, allowing customization of how this data is processed.

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

Description

Filters the data types that are considered arrays for Contact Form 7 fields. Developers can add or modify array data types to customize how form data is handled, particularly for multi-value fields like checkboxes or select menus. This hook fires when processing form tags to determine if a field should be treated as an array.


Usage

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

Parameters

$tag (mixed)
This parameter defines the data types that will be considered for arrays, specifically including 'checkbox' fields.
$contact_form (mixed)
This parameter holds the specific tag name from the Contact Form 7 form that is currently being processed.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the automator_cf7_data_type_of_array filter.
 *
 * This filter allows you to define additional Contact Form 7 field types
 * that should be treated as arrays by the Automator plugin.
 *
 * In this example, we're adding the 'radio' field type to be considered
 * as an array data type. This might be useful if you have a specific use case
 * where you want to handle radio button selections as an array.
 *
 * @param array $array_data_types An array of Contact Form 7 field types that are treated as arrays.
 * @param object $tag The current Contact Form 7 tag object being processed.
 * @param WP_Post $contact_form The Contact Form 7 post object.
 * @return array The modified array of array data types.
 */
add_filter(
	'automator_cf7_data_type_of_array',
	function ( $array_data_types, $tag, $contact_form ) {
		// Add 'radio' as a field type that should be treated as an array.
		// This is useful if you have a specific workflow that processes radio button
		// selections as an array.
		if ( ! in_array( 'radio', $array_data_types, true ) ) {
			$array_data_types[] = 'radio';
		}

		return $array_data_types;
	},
	10, // Priority
	3  // Number of accepted arguments
);

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/contact-form7/tokens/cf7-tokens.php:103

public function get_data_from_contact_form( WPCF7_ContactForm $contact_form ) {
		$data        = array();
		$posted_data = array(
			'POST'  => wp_unslash( $_POST ),  // phpcs:ignore 
			'FILES' => wp_unslash( $_FILES ), // phpcs:ignore
		);
		if ( $contact_form instanceof WPCF7_ContactForm ) {
			$tags = $contact_form->scan_form_tags();
			foreach ( $tags as $tag ) {
				if ( empty( $tag->name ) ) {
					continue;
				}

				$is_multiple      = in_array( 'multiple', $tag->options, true ) ? true : false;
				$array_data_types = apply_filters( 'automator_cf7_data_type_of_array', array( 'checkbox' ), $tag, $contact_form );
				if ( $is_multiple || in_array( $tag->type, $array_data_types, true ) ) {
					$request_tag_name = automator_filter_input_array( $tag->name, INPUT_POST );
				} elseif ( 'file' === $tag->type ) {
					$request_tag_name = '';
					$file             = isset( $_FILES[ $tag->name ] ) && isset( $_FILES[ $tag->name ]['name'] ) ? filter_var( wp_unslash( $_FILES[ $tag->name ]['name'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing
					$request_tag_name = $file ? basename( $file ) : '';

				} elseif ( 'textarea' === $tag->type ) {
					// Use textarea sanitization for textarea field.
					$request_tag_name = sanitize_textarea_field( filter_input( INPUT_POST, $tag->name, FILTER_DEFAULT ) );

				} else {

					$request_tag_name = automator_filter_input( $tag->name, INPUT_POST );

					// Try and catch array data.
					if ( empty( $request_tag_name ) ) {
						$request_tag_name = automator_filter_input_array( $tag->name, INPUT_POST );
					}
				}

				$pipes = $tag->pipes;
				$value = ! empty( $request_tag_name ) ? $request_tag_name : '';

				if ( WPCF7_USE_PIPE && $pipes instanceof WPCF7_Pipes && ! $pipes->zero() ) {
					if ( is_array( $value ) ) {
						$new_value = array();

						foreach ( $value as $v ) {
							$new_value[] = $pipes->do_pipe( wp_unslash( $v ) );
						}

						$value = $new_value;
					} else {
						$value = $pipes->do_pipe( wp_unslash( $value ) );
					}
				}

				$data[ $tag->name ] = apply_filters( 'automator_cf7_submitted_field_value', $value, $tag, $posted_data );
			}//end foreach
			return $data;
		}//end if
	}

Scroll to Top