Filter uncanny-automator

automator_register_integration

Filters the integration object and its code before it's registered.

add_filter( 'automator_register_integration', $callback, 10, 2 );

Description

Allows developers to modify or replace an integration object and its code before it's registered. This hook is useful for dynamically altering integration configurations, adding custom logic, or performing validation. It fires after initial checks but before final registration.


Usage

add_filter( 'automator_register_integration', 'your_function_name', 10, 2 );

Parameters

$integration (mixed)
This parameter holds the integration object itself, containing all the necessary details and methods for a specific integration.
$integration_code (mixed)
This parameter contains the integration object itself, which holds all the information and functionality of the integration.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the 'automator_register_integration' hook.
 *
 * This example demonstrates how to modify an integration's data before it's registered.
 * In this case, we're ensuring that a 'name' key exists in the integration data
 * and defaulting it if it's missing.
 *
 * @param array  $integration      The integration data array.
 * @param string $integration_code The unique code for the integration.
 *
 * @return array The modified integration data.
 */
add_filter( 'automator_register_integration', function( $integration, $integration_code ) {

	// Ensure the integration is an array before proceeding.
	if ( ! is_array( $integration ) ) {
		return $integration; // Return original if not an array to avoid errors.
	}

	// If the 'name' key is missing, add it with a default value based on the integration code.
	if ( ! isset( $integration['name'] ) ) {
		// A more realistic scenario might involve translating a key or using a more descriptive name.
		$integration['name'] = ucwords( str_replace( '_', ' ', $integration_code ) );
	}

	// You could also add other default values or perform other checks/modifications here.
	// For example:
	// if ( ! isset( $integration['description'] ) ) {
	//     $integration['description'] = 'A description for ' . $integration['name'];
	// }

	// Return the modified integration data.
	return $integration;

}, 10, 2 ); // 10 is the priority, 2 is the 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/core/lib/utilities/class-automator-registration.php:273

public function integration( $integration_code = null, $integration = null ) {

		// Only registers the integration if its not yet registered.
		if ( ! isset( Automator()->all_integrations[ $integration_code ] ) ) {
			Automator()->set_all_integrations( $integration_code, $integration );
		}

		if ( null === $integration_code || ! is_string( $integration_code ) ) {
			throw new Automator_Exception( 'You are trying to register an integration without passing an integration code.', 1002 );
		}

		if ( null === $integration || ! is_array( $integration ) ) {
			throw new Automator_Exception( 'You are trying to register an integration without passing an integration object.', 1002 );
		}

		$integration = apply_filters( 'automator_register_integration', $integration, $integration_code );

		// Register integration if it doesn't already exist
		if ( ! array_key_exists( $integration_code, Automator()->integrations ) ) {
			Automator()->set_integrations( $integration_code, $integration );
		} elseif ( array_key_exists( 'icon_svg', $integration ) ) {
			Automator()->set_integrations( $integration_code, $integration );
		}

		// Fire up the loopable tokens.
		if ( isset( $integration['loopable_tokens'] ) ) {
			foreach ( (array) $integration['loopable_tokens'] as $id => $loopable_token_class ) {
				( new $loopable_token_class( $integration_code ) )->register_hooks();
			}
		}

		// Order integrations alphabetically
		Automator()->utilities->sort_integrations_alphabetically();

		return true;

	}

Scroll to Top