Action uncanny-automator

automator_option_added

Fires after a new option has been successfully added to the Automator's settings.

add_action( 'automator_option_added', $callback, 10, 2 );

Description

Fires after a new option is successfully added or an existing one is updated. Developers can use this hook to react to specific option changes or to perform general post-update tasks. It provides the option key and its new value as parameters.


Usage

add_action( 'automator_option_added', 'your_function_name', 10, 2 );

Parameters

$key (mixed)
This parameter represents the unique identifier or key of the option being added to the automator's settings.
$value (mixed)
The `$key` parameter represents the unique identifier for the option being added.

Examples

/**
 * Log when a new automator option is added.
 *
 * This function demonstrates how to hook into the 'automator_option_added'
 * action to perform a custom action whenever a new option is added through
 * the automator plugin. In this example, we'll log the key and value of
 * the added option to a custom log file for debugging or auditing purposes.
 *
 * @param mixed $key The key of the option that was added.
 * @param mixed $value The value of the option that was added.
 */
add_action( 'automator_option_added', 'my_automator_log_new_option', 10, 2 );

function my_automator_log_new_option( $key, $value ) {
	// Define the log file path. It's good practice to put logs in the uploads directory.
	$upload_dir = wp_upload_dir();
	$log_file_path = trailingslashit( $upload_dir['basedir'] ) . 'automator_option_logs/new_option.log';

	// Ensure the log directory exists.
	if ( ! wp_mkdir_p( dirname( $log_file_path ) ) ) {
		error_log( 'Failed to create log directory for automator options.' );
		return;
	}

	// Format the log message.
	$timestamp = date( 'Y-m-d H:i:s' );
	$log_message = sprintf(
		"[%s] New automator option added. Key: %s, Value: %sn",
		$timestamp,
		print_r( $key, true ), // Use print_r to handle complex data types
		print_r( $value, true )
	);

	// Write the log message to the file.
	// FILE_APPEND flag ensures we append to the file, not overwrite it.
	// LOCK_EX flag prevents concurrent writes.
	if ( false === file_put_contents( $log_file_path, $log_message, FILE_APPEND | LOCK_EX ) ) {
		error_log( 'Failed to write to automator options log file: ' . $log_file_path );
	}
}

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-automator-options.php:345

public function add_option( string $key, $value, bool $autoload = true, bool $run_action_hooks = true ) {

		$key = $this->validate_key( $key );
		if ( false === $key ) {
			return false;
		}

		// Check if option already exists in cache or database.
		$existing = $this->cache->get_from_memory( $key, Automator_Options_Cache::ABSENT );
		if ( Automator_Options_Cache::ABSENT !== $existing && Automator_Options_Cache::MISS !== $existing ) {
			return false; // exists in memory/autoloaded
		}

		// Check database.
		if ( $this->query->option_exists( $key ) ) {
			return false;
		}

		// Fire actions before adding or updating the option.
		if ( $run_action_hooks ) {
			do_action( 'automator_before_add_option', $key, $value, $autoload );
		}

		// Add to database.
		$encoded_value    = Automator_Option_Formatter::encode_value( $value );
		$serialized_value = maybe_serialize( $encoded_value );

		if ( false === $this->query->insert_option( $key, $serialized_value, $autoload ) ) {
			return false;
		}

		// Delete any existing cache entries.
		$this->cache->delete( $key );

		// Update cache with new value.
		$this->cache->set( $key, $value, $serialized_value, $autoload );

		// Fire post-add/update actions.
		if ( $run_action_hooks ) {
			do_action( "automator_add_option_{$key}", $key, $value );
			do_action( 'automator_option_added', $key, $value );
		}

		return true;
	}


Scroll to Top