Filter uncanny-automator

automator_wpai_common_possible_tokens_for_import

Filters the possible tokens available for import actions within WP All Import to customize token options.

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

Description

This filter hook allows you to customize the available tokens for WP All Import integrations. It fires when defining import tokens, enabling developers to add or remove specific tokens based on import completion statuses or other arguments. Use it to control which tokens are presented to users during import setup.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of trigger codes that the filter will process.

Return Value

The filtered value.


Examples

/**
 * Add custom tokens for specific WP All Import trigger events.
 *
 * This function hooks into the 'automator_wpai_common_possible_tokens_for_import' filter
 * to conditionally add custom tokens when the 'WPAI_IMPORT_COMPLETED' trigger code is present.
 *
 * @param array $trigger_meta_validations The array of trigger codes that are validated.
 * @param array $args The arguments passed to the filter.
 * @return array The modified array of trigger codes.
 */
add_filter(
	'automator_wpai_common_possible_tokens_for_import',
	function ( $trigger_meta_validations, $args ) {
		// Check if the import completed trigger is part of the arguments
		if ( isset( $args['triggers_meta']['code'] ) && 'WPAI_IMPORT_COMPLETED' === $args['triggers_meta']['code'] ) {
			// In a real-world scenario, you might want to check other conditions from $args
			// For example, if a specific import ID or template name is used.
			// Let's assume for this example that we also want to add tokens if the import
			// involves a specific template name.
			$specific_template_name = 'My Custom Product Import';

			if ( isset( $args['import_template_name'] ) && $specific_template_name === $args['import_template_name'] ) {
				// Add a new custom token identifier for this specific scenario.
				// This identifier would then be used by other parts of the automation plugin
				// to map to actual data fields.
				$trigger_meta_validations[] = 'WPAI_IMPORT_COMPLETED_CUSTOM_PRODUCTS';
			}

			// You could also add general tokens for any import completed event
			// if that's desired.
			// $trigger_meta_validations[] = 'WPAI_IMPORT_COMPLETED_GENERAL';
		}

		return $trigger_meta_validations;
	},
	10, // Priority
	2  // Accepted args
);

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-all-import/tokens/wpai-tokens.php:183

public function wpai_possible_tokens( $tokens = array(), $args = array() ) {
		$trigger_code = $args['triggers_meta']['code'];

		$trigger_meta_validations = apply_filters(
			'automator_wpai_common_possible_tokens_for_import',
			array( 'WPAI_IMPORT_COMPLETED' ),
			$args
		);

		if ( in_array( $trigger_code, $trigger_meta_validations, true ) ) {

			$fields = array(
				array(
					'tokenId'         => 'IMPORT_ID',
					'tokenName'       => esc_html__( 'Import ID', 'uncanny-automator' ),
					'tokenType'       => 'int',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'IMPORT_FILE_NAME',
					'tokenName'       => esc_html__( 'Import file name', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'IMPORT_DATE',
					'tokenName'       => esc_html__( 'Import date', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'IMPORT_TYPE',
					'tokenName'       => esc_html__( 'Import type', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'IMPORT_RUN_TIME',
					'tokenName'       => esc_html__( 'Import run time', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'IMPORT_SUMMARY',
					'tokenName'       => esc_html__( 'Import summary', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
			);

			$tokens = array_merge( $tokens, $fields );
		}

		return $tokens;
	}

Scroll to Top