Filter uncanny-automator-pro

automator_pro_magic_{$type}_tokens

Filters available magic button tokens for a specific integration type, allowing customization of token data.

add_filter( 'automator_pro_magic_{$type}_tokens', $callback, 10, 3 );

Description

This filter hook allows developers to modify the available tokens for the Magic Button integration. It fires when the Magic Button is being processed, providing access to the token fields, trigger metadata, and arguments. Developers can use this hook to add, remove, or alter tokens displayed within the Magic Button interface for custom integrations or specific token needs.


Usage

add_filter( 'automator_pro_magic_{$type}_tokens', 'your_function_name', 10, 3 );

Parameters

$fields (mixed)
This parameter contains an array of available tokens related to the specified automation type.
$trigger_meta (mixed)
This parameter contains a list of available tokens that can be used in Automator's Magic Button feature.
$args (mixed)
This parameter contains metadata related to the specific trigger that initiated the automator, providing context for token generation.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the 'automator_pro_magic_button_tokens' hook.
 * This function adds a custom token to the list of available tokens when the trigger meta
 * indicates a "WPMAGICBUTTON" type.
 *
 * @param array $fields       The existing token fields.
 * @param string $trigger_meta The meta data of the trigger.
 * @param array $args         Additional arguments passed to the filter.
 *
 * @return array The modified array of token fields.
 */
add_filter( 'automator_pro_magic_button_tokens', 'my_custom_magic_button_tokens', 10, 3 );

function my_custom_magic_button_tokens( $fields, $trigger_meta, $args ) {

	// Check if the trigger meta is specifically for a WordPress Magic Button.
	// This is a hypothetical check, adjust based on actual $trigger_meta values.
	if ( 'WPMAGICBUTTON' === $trigger_meta ) {

		// Define a custom token.
		$custom_token = array(
			'id'    => 'custom_button_text',
			'name'  => __( 'Custom Button Text', 'your-text-domain' ),
			'desc'  => __( 'Inserts custom text specifically for magic buttons.', 'your-text-domain' ),
			'type'  => 'text', // Example type, could be 'textarea', 'select', etc.
			'value' => '', // Default value for the token.
		);

		// Add the custom token to the beginning of the fields array.
		array_unshift( $fields, $custom_token );

		// You could also add more complex logic here, like conditionally adding tokens
		// based on the $args array, or modifying existing tokens.
		// For instance, if $args['button_id'] is present, you might add a token
		// that retrieves the button's title.

	}

	return $fields;
}

/**
 * Example of how to filter the 'automator_pro_magic_link_tokens' hook.
 * This function adds a custom token to the list of available tokens when the trigger meta
 * indicates a "link" type.
 *
 * @param array $fields       The existing token fields.
 * @param string $trigger_meta The meta data of the trigger.
 * @param array $args         Additional arguments passed to the filter.
 *
 * @return array The modified array of token fields.
 */
add_filter( 'automator_pro_magic_link_tokens', 'my_custom_magic_link_tokens', 10, 3 );

function my_custom_magic_link_tokens( $fields, $trigger_meta, $args ) {

	// In this example, we'll add a token that retrieves the URL of a specific link,
	// assuming the $args array might contain information about the link.
	// This is a conceptual example; the actual implementation depends on how Uncanny Automator
	// populates the $args for link triggers.

	// Let's assume $args['link_identifier'] might be passed for a specific link.
	if ( isset( $args['link_identifier'] ) ) {
		$link_identifier = sanitize_text_field( $args['link_identifier'] );

		// Hypothetically, we could try to fetch a URL based on this identifier.
		// In a real scenario, you'd have a function to get the actual URL.
		$hypothetical_url = '#'; // Placeholder

		// If you have a way to get the URL, you can add it as a token.
		// For this example, we'll just add a static placeholder link token.
		$link_url_token = array(
			'id'    => 'magic_link_url',
			'name'  => __( 'Magic Link URL', 'your-text-domain' ),
			'desc'  => __( 'The URL of the magic link.', 'your-text-domain' ),
			'type'  => 'text', // Or 'url' if Uncanny Automator supports that type directly.
			'value' => $hypothetical_url, // This would ideally be dynamic.
		);

		array_push( $fields, $link_url_token );
	}

	return $fields;
}
?>

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

uncanny-automator-pro/src/integrations/magic-button/tokens/magic-button-tokens.php:108
uncanny-automator-pro/src/integrations/magic-button/tokens/magic-button-tokens.php:240
uncanny-automator-pro/src/integrations/uncanny-automator/tokens/uoa-magic-button-tokens.php:85
uncanny-automator-pro/src/integrations/uncanny-automator/tokens/uoa-anon-tokens.php:93

public function magic_button_possible_tokens( $tokens = array(), $args = array() ) {
		if ( ! automator_pro_do_identify_tokens() ) {
			return $tokens;
		}
		$trigger_meta = $args['meta'];

		$fields = array(
			array(
				'tokenId'         => 'automator_button_post_id',
				'tokenName'       => __( 'Post ID', 'uncanny-automator-pro' ),
				'tokenType'       => 'text',
				'tokenIdentifier' => $trigger_meta,
			),
			array(
				'tokenId'         => 'automator_button_post_title',
				'tokenName'       => __( 'Post title', 'uncanny-automator-pro' ),
				'tokenType'       => 'text',
				'tokenIdentifier' => $trigger_meta,
			),
			array(
				'tokenId'         => 'automator_button_id',
				'tokenName'       => __( 'Button ID', 'uncanny-automator-pro' ),
				'tokenType'       => 'int',
				'tokenIdentifier' => $trigger_meta,
			),
			array(
				'tokenId'         => 'automator_button_recipe_id',
				'tokenName'       => __( 'Recipe ID', 'uncanny-automator-pro' ),
				'tokenType'       => 'int',
				'tokenIdentifier' => $trigger_meta,
			),
		);

		/**
		 * Filter automator_pro_magic_{$type}_tokens - for adding custom tokens
		 *
		 * @param array $fields
		 * @param string $trigger_meta
		 * @param array $args
		 *
		 * @return array
		 */
		$type   = 'WPMAGICBUTTON' === $trigger_meta ? 'button' : 'link';
		$fields = apply_filters( "automator_pro_magic_{$type}_tokens", $fields, $trigger_meta, $args );

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

		return $tokens;
	}

Scroll to Top