Filter uncanny-automator

automator_api_register_integration_tokens

Allow additional integration tokens to be registered. Filters to register additional integration tokens for the API integration.

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

Description

Fires when integration tokens are registered for the API. Developers can add custom integration tokens by returning an array of token definitions. Each token definition should include 'code', 'name', 'data_type', and optionally 'requires_user_data'.


Usage

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

Parameters

$tokens (array)
Existing tokens array.

Return Value

array Additional tokens array.


Examples

<?php
/**
 * Register a custom token for a hypothetical "Mailchimp" integration.
 *
 * This function adds a new token that can be used within the Automator plugin
 * to represent a specific piece of data from a Mailchimp integration, such as
 * the subscriber's email address.
 *
 * @param array $tokens The existing array of registered tokens.
 * @return array The modified array of tokens, including the new custom token.
 */
add_filter( 'automator_api_register_integration_tokens', 'my_custom_mailchimp_token', 10, 1 );

function my_custom_mailchimp_token( $tokens ) {
	// Define the details for our custom Mailchimp subscriber email token.
	$mailchimp_subscriber_email_token = array(
		'code'               => 'MAILCHIMP_SUBSCRIBER_EMAIL',
		'name'               => __( 'Mailchimp Subscriber Email', 'your-text-domain' ),
		'data_type'          => 'email', // Specifies that this token represents an email address.
		'requires_user_data' => false,   // This token doesn't necessarily require user-specific data context.
	);

	// Add the new token to the existing array, using its code as the key.
	$tokens['MAILCHIMP_SUBSCRIBER_EMAIL'] = $mailchimp_subscriber_email_token;

	// Return the updated tokens array to be registered.
	return $tokens;
}

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/api/components/token/integration/registry/class-wp-integration-token-registry.php:235

private function load_tokens_from_wordpress(): void {
		// Build minimal structure for integrations
		$items = $this->build_items_structure();

		// Create minimal Structure-like object for filter compatibility
		// Token classes call $structure->get_recipe_id(), so we provide that method
		$structure_mock = $this->create_structure_mock();

		// Apply the automator_integration_items filter to get tokens
		$items = apply_filters( 'automator_integration_items', $items, $structure_mock );

		// Get active integrations to filter tokens
		$active_integrations = Automator()->get_integrations();

		// Only process tokens for active integrations
		foreach ( $items as $integration_code => $integration_data ) {
			// Skip inactive integrations.
			if ( ! isset( $active_integrations[ $integration_code ] ) ) {
				continue;
			}

			// Confirm if app and if it's connected.
			if ( $integration_data['is_app'] ?? false ) {
				// Skip if app is not connected.
				$is_app_connected = $integration_data['miscellaneous']['is_app_connected'] ?? null;
				if ( null === $is_app_connected ) {
					continue;
				}
			}

			$tokens = $integration_data['tokens'] ?? array();

			foreach ( $tokens as $token ) {

				// error_log( print_r( array( 'token' => $token, 'integration_code' => $integration_code ), true ) );

				$token_id = $token['id'] ?? '';

				// Skip if no token ID
				if ( empty( $token_id ) ) {
					continue;
				}

				// Store original properties.
				$this->tokens[ $token_id ] = array(
					'id'             => $token_id,
					'integration'    => $integration_code,
					'name'           => $token['name'] ?? '',
					'cacheable'      => $token['cacheable'] ?? false,
					'requiresUser'   => $token['requiresUser'] ?? false,
					'type'           => $token['type'] ?? 'text',
					'supportedItems' => $token['supportedItems'] ?? array(),
					'fields'         => $token['fields'] ?? array(),
					'idTemplate'     => $token['idTemplate'] ?? '',
					'nameTemplate'   => $token['nameTemplate'] ?? '',
				);
			}
		}

		/**
		 * Allow additional integration tokens to be registered.
		 *
		 * @param array $tokens Existing tokens array.
		 *
		 * @return array Additional tokens array.
		 * @example
		 * [
		 *  'TOKEN_ID' => array(
		 *      'code'               => 'TOKEN_ID',
		 *      'name'               => 'TOKEN_NAME',
		 *      'data_type'          => 'TOKEN_DATA_TYPE',
		 *      'requires_user_data' => true,
		 *  ),
		 * ]
		 */
		$additional_tokens = apply_filters( 'automator_api_register_integration_tokens', array() );
		foreach ( $additional_tokens as $code => $token ) {
			$this->register_token( $code, $token );
		}
	}

Scroll to Top