Filter uncanny-automator

automator_wpai_save_common_triggers_tokens_for_posttype

Filters common trigger tokens for post types when saving them with WP All Import.

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

Description

This filter allows developers to modify the list of common trigger token validation codes for WP All Import post type triggers. You can add or remove validation codes from the default `WPAI_POSTTYPE_IMPORTED` array to customize how these tokens are processed when saving trigger data.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of valid trigger codes that are allowed to be saved for a specific post type.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_wpai_save_common_triggers_tokens_for_posttype' filter.
 *
 * This filter allows you to modify the list of post types that trigger the 'WPAI_POSTTYPE_IMPORTED'
 * token for automator. In this example, we're adding a custom post type to the list if it exists.
 *
 * @param array $trigger_meta_validations The array of post type slugs that trigger the token.
 * @param array $args The arguments passed to the save_token_data function.
 *
 * @return array The modified array of trigger post type slugs.
 */
add_filter( 'automator_wpai_save_common_triggers_tokens_for_posttype', function( $trigger_meta_validations, $args ) {
    // Check if a custom post type should be added based on the import arguments.
    // This is a hypothetical example, the actual logic would depend on how you
    // identify a specific post type within the $args.
    if ( isset( $args['trigger_args']['post_type'] ) && 'my_custom_post_type' === $args['trigger_args']['post_type'] ) {
        // Add 'my_custom_post_type' to the list of post types that trigger the token.
        $trigger_meta_validations[] = 'MY_CUSTOM_POST_TYPE_IMPORTED'; // Assuming a constant for the token slug
    }

    // In a real-world scenario, you might also want to remove certain post types
    // from the default list based on conditions within $args.

    return $trigger_meta_validations;
}, 10, 2 ); // Priority 10, accepts 2 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/wp-all-import/tokens/wpai-tokens.php:37

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