Action uncanny-automator

automator_add_integration_recipe_parts

Fires when adding recipe parts to a new integration within the core Automator plugin.

add_action( 'automator_add_integration_recipe_parts', $callback, 10, 1 );

Description

Fired after core automator integrations are loaded, this action hook allows developers to programmatically register custom triggers, actions, or tokens for any integration, including custom ones. It's the primary method for extending Uncanny Automator's functionality with new recipe components.


Usage

add_action( 'automator_add_integration_recipe_parts', 'your_function_name', 10, 1 );

Examples

// Example of adding a custom trigger to Uncanny Automator via the automator_add_integration_recipe_parts hook.
// This hook is intended for developers to register their own triggers, actions, and tokens for integrations.

add_action( 'automator_add_integration_recipe_parts', 'my_custom_automator_integration_parts', 20, 0 );

/**
 * Registers custom triggers, actions, and tokens for a hypothetical "My Plugin" integration.
 *
 * @since 1.0.0
 * @return void
 */
function my_custom_automator_integration_parts() {
    // Assuming you have a class to manage your integration's components.
    if ( class_exists( 'My_Plugin_Automator_Integration' ) ) {
        $integration_manager = new My_Plugin_Automator_Integration();

        // Register a new trigger.
        $integration_manager->register_trigger(
            'my_plugin_new_user_registered', // Unique ID for the trigger
            array(
                'name' => __( 'New User Registered in My Plugin', 'my-plugin-textdomain' ),
                'template' => 'user-registered-template.php', // Path to a template file for the trigger's UI
                'description' => __( 'Triggers when a new user registers specifically through My Plugin's custom registration form.', 'my-plugin-textdomain' ),
                'icon' => 'dashicons-admin-users', // WordPress Dashicon
                'custom_callback' => array( $integration_manager, 'check_my_plugin_user_registration' ), // Function to check if the trigger condition is met
                'editable' => true, // Whether the trigger can be edited by the user
                'save_meta' => array( 'user_role' ), // Meta fields to save with the trigger (e.g., specific user role)
            )
        );

        // Register a new action.
        $integration_manager->register_action(
            'my_plugin_send_custom_notification', // Unique ID for the action
            array(
                'name' => __( 'Send Custom Notification via My Plugin', 'my-plugin-textdomain' ),
                'template' => 'send-notification-template.php', // Path to a template file for the action's UI
                'description' => __( 'Sends a custom notification to a user using My Plugin's notification system.', 'my-plugin-textdomain' ),
                'icon' => 'dashicons-email-alt',
                'run_callback' => array( $integration_manager, 'execute_my_plugin_notification' ), // Function to execute the action
                'save_meta' => array( 'notification_subject', 'notification_body' ), // Meta fields to save with the action
            )
        );

        // Register a new token.
        $integration_manager->register_token(
            'my_plugin_user_display_name', // Unique ID for the token
            array(
                'name' => __( 'My Plugin User Display Name', 'my-plugin-textdomain' ),
                'description' => __( 'The display name of the user from My Plugin.', 'my-plugin-textdomain' ),
                'type' => 'text', // Data type of the token
                'get_callback' => array( $integration_manager, 'get_my_plugin_user_display_name' ), // Function to retrieve the token's value
            )
        );

        // You would likely have more complex logic here to load these registrations
        // from separate files or classes for better organization.
    }
}

// Example of a hypothetical class that would handle the integration's logic.
// In a real scenario, this class would be part of your plugin.
class My_Plugin_Automator_Integration {

    /**
     * Dummy method to simulate checking for a new user registration in My Plugin.
     * In a real scenario, this would hook into My Plugin's user registration process.
     *
     * @param array $recipe An array containing information about the recipe.
     * @param array $trigger_data Data related to the trigger.
     * @return bool True if the condition is met, false otherwise.
     */
    public function check_my_plugin_user_registration( $recipe, $trigger_data ) {
        // Replace with actual logic to check if a new user registered via My Plugin.
        // This might involve checking custom tables or user meta.
        $new_user_registered_via_my_plugin = true; // Placeholder

        if ( $new_user_registered_via_my_plugin ) {
            return true;
        }
        return false;
    }

    /**
     * Dummy method to simulate sending a custom notification via My Plugin.
     *
     * @param array $recipe An array containing information about the recipe.
     * @param array $action_data Data related to the action.
     * @return bool True on success, false on failure.
     */
    public function execute_my_plugin_notification( $recipe, $action_data ) {
        // Replace with actual logic to send a notification using My Plugin's API.
        $notification_subject = $action_data['meta']['notification_subject'];
        $notification_body = $action_data['meta']['notification_body'];
        $user_id = $recipe['user_id']; // Example: get the user ID associated with the recipe

        // Simulate sending notification
        error_log( "Sending notification to user {$user_id} with subject: {$notification_subject} and body: {$notification_body}" );

        return true;
    }

    /**
     * Dummy method to retrieve the display name of a user from My Plugin.
     *
     * @param array $recipe An array containing information about the recipe.
     * @param array $token_data Data related to the token.
     * @return string The user's display name.
     */
    public function get_my_plugin_user_display_name( $recipe, $token_data ) {
        // Replace with actual logic to get the user's display name from My Plugin.
        // This might involve fetching it from user meta or a custom table.
        $user_id = $recipe['user_id'];
        $user = get_user_by( 'id', $user_id ); // Example: get the WordPress user object

        if ( $user ) {
            return $user->display_name;
        }
        return __( 'Unknown User', 'my-plugin-textdomain' );
    }

    /**
     * Dummy method to register a trigger.
     * In a real scenario, this would interact with Uncanny Automator's API.
     */
    public function register_trigger( $id, $args ) {
        // This is a placeholder. In a real integration, you would call an Uncanny Automator
        // API function to register the trigger.
        error_log( "Registering trigger: {$id} with args: " . print_r( $args, true ) );
    }

    /**
     * Dummy method to register an action.
     * In a real scenario, this would interact with Uncanny Automator's API.
     */
    public function register_action( $id, $args ) {
        // This is a placeholder. In a real integration, you would call an Uncanny Automator
        // API function to register the action.
        error_log( "Registering action: {$id} with args: " . print_r( $args, true ) );
    }

    /**
     * Dummy method to register a token.
     * In a real scenario, this would interact with Uncanny Automator's API.
     */
    public function register_token( $id, $args ) {
        // This is a placeholder. In a real integration, you would call an Uncanny Automator
        // API function to register the token.
        error_log( "Registering token: {$id} with args: " . print_r( $args, true ) );
    }
}

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/core/classes/class-initialize-automator.php:317

public function load_recipe_parts() {
		try {
			$this->loader->load_recipe_parts( $this->active_directories, $this->directories_to_include );
		} catch ( Error $e ) {
			throw new Automator_Error( esc_html( $e->getMessage() ) );
		} catch ( Exception $e ) {
			throw new Automator_Exception( esc_html( $e->getMessage() ) );
		}

		// Let others hook in and add triggers actions or tokens
		do_action_deprecated( 'uncanny_automator_add_integration_triggers_actions_tokens', array(), '3.0', 'automator_add_integration_recipe_parts' );
		do_action( 'automator_add_integration_recipe_parts' );
	}


Scroll to Top