automator_app_integration_setup_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Add action to allow for integration-specific setup. Fires after an app integration is set up, allowing for integration-specific actions and configurations.
add_action( 'automator_app_integration_setup_{dynamic}', $callback, 10, 1 );
Description
Fires after an app integration's settings ID is set and before it's initialized. Use this dynamic action hook, `automator_app_integration_setup_{integration_slug}`, to perform integration-specific setup tasks. Pass the `$integration` instance as a parameter to access its properties and methods.
Usage
add_action( 'automator_app_integration_setup_{dynamic}', 'your_function_name', 10, 1 );
Parameters
-
$integration(App_Integration) - The app integration instance.
Examples
add_action( 'automator_app_integration_setup_openai', 'my_openai_integration_setup', 10, 1 );
/**
* Sets up custom settings for the OpenAI integration.
*
* This function is hooked into the 'automator_app_integration_setup_openai' action.
* It demonstrates how to add or modify settings specific to the OpenAI integration
* after the core integration has been initialized.
*
* @param App_Integration $integration The instance of the App_Integration for OpenAI.
*/
function my_openai_integration_setup( App_Integration $integration ) {
// Example: Ensure a specific API key field exists and is populated.
// In a real scenario, this might involve fetching from user meta,
// a custom options page, or dynamically generating a placeholder.
$api_key_setting = $integration->get_setting( 'openai_api_key' );
if ( ! $api_key_setting || empty( $api_key_setting['value'] ) ) {
// If the API key is not set, perhaps add a default or prompt the user.
// For this example, we'll just log a notice. In a real plugin,
// you'd likely have a more robust way to handle missing critical settings.
error_log( 'OpenAI API Key is missing for integration: ' . $integration->get_integration() );
// You might also want to set a default or a flag indicating the key is missing.
// $integration->set_setting( 'openai_api_key', [ 'value' => '', 'label' => 'OpenAI API Key', 'type' => 'text', 'required' => true ] );
}
// Example: Register a custom field specific to the OpenAI integration's settings.
// Let's say we want to allow users to select a specific GPT model.
$integration->add_setting(
'openai_gpt_model',
array(
'label' => __( 'GPT Model', 'your-text-domain' ),
'description' => __( 'Select the GPT model to use for text generation.', 'your-text-domain' ),
'type' => 'select',
'options' => array(
'gpt-3.5-turbo' => 'GPT-3.5 Turbo',
'gpt-4' => 'GPT-4',
'gpt-4-turbo' => 'GPT-4 Turbo',
),
'default' => 'gpt-3.5-turbo',
'required' => 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/lib/app-integrations/abstract-app-integration.php:113
protected function setup_app_integration( $config = array() ) {
// Set the integration if not set.
if ( empty( $this->get_integration() ) ) {
if ( ! isset( $config['integration'] ) ) {
throw new Exception( 'Integration must be set' );
}
$this->set_integration( $config['integration'] );
}
// Set the name if not set.
if ( empty( $this->get_name() ) ) {
if ( ! isset( $config['name'] ) ) {
throw new Exception( 'Integration name must be set' );
}
$this->set_name( $config['name'] );
}
// Set the icon ( ID ) if not set.
if ( empty( $this->get_icon() ) ) {
if ( ! isset( $config['icon'] ) ) {
// Use the integration ID as the icon ID.
$config['icon'] = $this->get_integration();
}
$this->set_icon( $config['icon'] );
}
// Set the API endpoint if not set.
if ( empty( $this->get_api_endpoint() ) ) {
if ( ! isset( $config['api_endpoint'] ) ) {
throw new Exception( 'API endpoint is required' );
}
$this->set_api_endpoint( $config['api_endpoint'] );
}
if ( ! isset( $config['settings_id'] ) ) {
$config['settings_id'] = sanitize_title( $this->get_integration() );
}
$this->set_settings_id( sanitize_title( $config['settings_id'] ) );
/**
* Add action to allow for integration-specific setup.
*
* @param App_Integration $integration The app integration instance.
*/
do_action( 'automator_app_integration_setup_' . $this->get_integration(), $this );
// Initialize the app integration.
$this->initialize_app_integration();
}