Filter uncanny-automator

automator_wpai_parse_common_tokens_post_related

Filters common tokens for parsing post-related data during WP All Import processing.

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

Description

This filter allows developers to modify the array of common tokens available for post-related automations when using WP All Import. It's applied before WP All Import's tokens are parsed, enabling custom token registration or filtering of existing ones for specific recipes.


Usage

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

Parameters

$pieces (mixed)
This parameter represents an array containing a specific post type, `'WPAI_POSTTYPE_IMPORTED'`, which is used for internal validation or filtering related to imported posts.

Return Value

The filtered value.


Examples

/**
 * Example of how to hook into 'automator_wpai_parse_common_tokens_post_related'
 * to modify the post type validations for WP All Import triggers.
 *
 * This example demonstrates how to add a custom post type validation
 * or remove an existing one based on specific conditions.
 */
add_filter(
	'automator_wpai_parse_common_tokens_post_related',
	function( $trigger_meta_validations, $pieces_data ) {

		// Assuming $pieces_data['pieces'] contains information about the WP All Import import.
		// For demonstration, let's say we want to allow 'my_custom_post_type' only if a specific recipe is used.
		$recipe_id = $pieces_data['recipe_id'] ?? null;
		$pieces = $pieces_data['pieces'] ?? [];

		// Example: If the recipe ID is '123', ensure 'my_custom_post_type' is a valid option.
		if ( '123' === $recipe_id ) {
			// Add our custom post type if it's not already present.
			if ( ! in_array( 'WPAI_POSTTYPE_IMPORTED_MY_CUSTOM_POST_TYPE', $trigger_meta_validations ) ) {
				$trigger_meta_validations[] = 'WPAI_POSTTYPE_IMPORTED_MY_CUSTOM_POST_TYPE';
			}
		}

		// Example: Remove 'WPAI_POSTTYPE_IMPORTED' if the import is for a specific file name (hypothetical).
		// This logic would likely require accessing more data passed through $pieces_data or other context.
		// For simplicity, let's assume we have a hypothetical way to check the import file.
		$import_file_name = 'my_special_import.csv'; // Hypothetical check

		if ( isset( $pieces[0] ) && $import_file_name === $pieces[0] ) {
			// Remove the standard 'WPAI_POSTTYPE_IMPORTED' validation.
			$key_to_remove = array_search( 'WPAI_POSTTYPE_IMPORTED', $trigger_meta_validations );
			if ( $key_to_remove !== false ) {
				unset( $trigger_meta_validations[ $key_to_remove ] );
				// Re-index the array to avoid issues.
				$trigger_meta_validations = array_values( $trigger_meta_validations );
			}
		}

		// Always return the modified array.
		return $trigger_meta_validations;
	},
	10, // Priority
	2  // 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/wp-all-import/tokens/wpai-tokens.php:252

*/
	public function parse_wpai_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

		if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
			return $value;
		}

		$trigger_meta_validations = apply_filters(
			'automator_wpai_parse_common_tokens_post_related',
			array( 'WPAI_POSTTYPE_IMPORTED' ),
			array(
				'pieces'       => $pieces,
				'recipe_id'    => $recipe_id,
				'trigger_data' => $trigger_data,
				'user_id'      => $user_id,


Scroll to Top