Filter uncanny-automator

automator_wp_simpay_skip_field_types

Filters the field types that SimPay will skip in automations, allowing customization of data collection.

add_filter( 'automator_wp_simpay_skip_field_types', $callback, 10, 1 );

Description

Allows developers to filter the default list of WP Simple Pay field types that are skipped when generating tokens. This is useful for excluding specific field types that are not relevant for automation workflows, ensuring only necessary fields are available.


Usage

add_filter( 'automator_wp_simpay_skip_field_types', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Example of using the automator_wp_simpay_skip_field_types filter.
 *
 * This example skips additional field types based on specific conditions.
 * For instance, it skips 'hidden' field types unless a specific setting is enabled,
 * and also skips 'checkbox' fields if the user's role is 'contributor'.
 */
add_filter(
	'automator_wp_simpay_skip_field_types',
	function ( $skip_types ) {
		// Add 'hidden' field type to be skipped by default.
		$skip_types[] = 'hidden';

		// Conditionally skip 'checkbox' field types if the current user role is 'contributor'.
		if ( current_user_can( 'contributor' ) ) {
			$skip_types[] = 'checkbox';
		}

		// Remove duplicates to ensure each type is only listed once.
		return array_unique( $skip_types );
	},
	10,
	1
);

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/wp-simple-pay/tokens/wpsp-tokens.php:167

public function get_custom_field_tokens( $form_id, $tokens, $token_identifier = null ) {
		$fields = array();
		if ( function_exists( 'SimplePayProPost_TypesSimple_PayUtilget_custom_fields' ) && intval( '-1' ) !== intval( $form_id ) ) {
			$form_fields = SimplePayProPost_TypesSimple_PayUtilget_custom_fields( $form_id );
			$skip_types  = apply_filters(
				'automator_wp_simpay_skip_field_types',
				array(
					'email',
					'tax_id',
					'address',
					'telephone',
					'customer_name',
					'plan_select',
					'card',
				)
			);

			if ( ! empty( $form_fields ) ) {
				foreach ( $form_fields as $field ) {
					if ( isset( $field['label'] ) && ! in_array( $field['type'], $skip_types, true ) ) {
						$input_id = $field['id'];
						$token_id = "simpay-form-$form_id-field-$input_id";
						if ( isset( $field['metadata'] ) && ! empty( $field['metadata'] ) ) {
							$token_id = $field['metadata'];
						}
						$existing_tokens = array_column( $tokens, 'tokenId' );
						if ( ! in_array( $token_id, $existing_tokens, true ) ) {
							$fields[] = array(
								'tokenId'         => $token_id,
								'tokenName'       => empty( $field['label'] ) ? sprintf( 'Field ID #%s (no label)', $field['uid'] ) : $field['label'],
								'tokenType'       => 'text',
								'tokenIdentifier' => $token_identifier,
							);
						}
					}
				}
			}
		}

		return $fields;
	}

Scroll to Top