Filter uncanny-automator

automator_wpai_save_common_triggers_tokens_for_import

Filters common trigger tokens for import completion and arguments, allowing modification before saving.

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

Description

Fires after WP All Import completes a file import, allowing developers to filter the available trigger tokens for import completion actions. Modify the array of trigger codes to customize which import completion events can trigger automations.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of trigger codes that are valid for saving common trigger tokens during WP All Import actions.

Return Value

The filtered value.


Examples

/**
 * Modifies the list of trigger codes that are considered valid for the 'import completed' event.
 * This example demonstrates how to add a custom trigger code or remove an existing one.
 *
 * @param array $trigger_codes The original array of trigger codes.
 * @param array $args          The arguments passed to the filter. Expected to contain 'entry_args' and 'trigger_args'.
 *
 * @return array The modified array of trigger codes.
 */
add_filter( 'automator_wpai_save_common_triggers_tokens_for_import', function ( $trigger_codes, $args ) {

	// Check if the current import context is relevant for this filter.
	// For example, if we only want to modify trigger codes for specific import types.
	// In this realistic example, we'll assume we don't need specific context.

	// Example: Add a custom trigger code if it's not already present.
	$custom_trigger_code = 'WPAI_IMPORT_FINISHED_SUCCESSFULLY';
	if ( ! in_array( $custom_trigger_code, $trigger_codes ) ) {
		$trigger_codes[] = $custom_trigger_code;
	}

	// Example: Remove a specific trigger code if it exists.
	$code_to_remove = 'WPAI_IMPORT_COMPLETED';
	$key = array_search( $code_to_remove, $trigger_codes );
	if ( $key !== false ) {
		unset( $trigger_codes[ $key ] );
		// Re-index array to ensure clean iteration later if needed.
		$trigger_codes = array_values( $trigger_codes );
	}

	// If you wanted to completely replace the trigger codes:
	// $trigger_codes = array( 'WPAI_MY_CUSTOM_IMPORT_COMPLETE' );

	return $trigger_codes;

}, 10, 2 );

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:42

public function save_token_data( $args, $trigger ) {
		if ( ! isset( $args['trigger_args'] ) || ! isset( $args['entry_args']['code'] ) ) {
			return;
		}

		$trigger_meta_validations = apply_filters(
			'automator_wpai_save_common_triggers_tokens_for_posttype',
			array( 'WPAI_POSTTYPE_IMPORTED' ),
			$args
		);
		$trigger_code_validations = apply_filters(
			'automator_wpai_save_common_triggers_tokens_for_import',
			array( 'WPAI_IMPORT_COMPLETED' ),
			$args
		);

		if ( in_array( $args['entry_args']['code'], $trigger_meta_validations ) ) {
			$post_id           = $args['trigger_args'][0];
			$trigger_log_entry = $args['trigger_entry'];
			if ( ! empty( $post_id ) ) {
				Automator()->db->token->save( 'post_id', $post_id, $trigger_log_entry );
			}
		}
		if ( in_array( $args['entry_args']['code'], $trigger_code_validations ) ) {
			$import_id         = $args['trigger_args'][0];
			$trigger_log_entry = $args['trigger_entry'];
			if ( ! empty( $import_id ) ) {
				Automator()->db->token->save( 'import_id', $import_id, $trigger_log_entry );
			}
		}
	}


Scroll to Top