Filter uncanny-automator

actionified_triggers

Filters the list of triggers available for Actionified, allowing modification before they are displayed.

add_filter( 'actionified_triggers', $callback, 10, 1 );

Description

Allows modification of the global `$actionified_triggers` object before it's registered. Developers can dynamically add, remove, or alter trigger definitions, their associated actions, validation functions, priorities, and accepted arguments. This filter is crucial for custom trigger management.


Usage

add_filter( 'actionified_triggers', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to modify the Actionified triggers before they are processed.
 *
 * This function demonstrates how to add, modify, or remove triggers based on
 * certain conditions. In this example, we'll conditionally add a new trigger
 * if a specific plugin is active.
 */
add_filter(
	'actionified_triggers',
	function( $actionified_triggers, $actionify_instance ) {
		// Check if a specific plugin is active. For demonstration, let's assume
		// we are looking for a hypothetical "Advanced Features Plugin".
		if ( is_plugin_active( 'advanced-features-plugin/advanced-features-plugin.php' ) ) {

			// Define a new trigger. This would typically involve setting up
			// its properties like validation function, priority, and accepted arguments.
			$new_trigger_code = 'advanced_feature_enabled';
			$new_trigger_actions = array( 'advanced_feature_callback_function' ); // Array of callback function slugs
			$new_trigger_validation_function = 'advanced_feature_validation_check'; // Name of a validation function
			$new_trigger_priority = 10;
			$new_trigger_accepted_args = 1; // Assuming the validation function accepts 1 argument

			// Dynamically create a new trigger object. The actual structure might
			// depend on how Actionified expects triggers to be structured.
			// This assumes $actionified_triggers is an object or an array of objects.
			if ( is_object( $actionified_triggers ) ) {
				// If it's an object, we might be able to add properties directly.
				// This is a simplified assumption. In a real scenario, you might
				// need to create a new object for the trigger.
				$actionified_triggers->$new_trigger_code = new stdClass(); // Assuming stdClass for trigger representation
				$actionified_triggers->$new_trigger_code->trigger_actions = $new_trigger_actions;
				$actionified_triggers->$new_trigger_code->trigger_validation_function = $new_trigger_validation_function;
				$actionified_triggers->$new_trigger_code->trigger_priority = $new_trigger_priority;
				$actionified_triggers->$new_trigger_code->trigger_accepted_args = $new_trigger_accepted_args;
			} elseif ( is_array( $actionified_triggers ) ) {
				// If it's an array, we'd add a new element.
				$actionified_triggers[ $new_trigger_code ] = array(
					'trigger_actions'             => $new_trigger_actions,
					'trigger_validation_function' => $new_trigger_validation_function,
					'trigger_priority'            => $new_trigger_priority,
					'trigger_accepted_args'       => $new_trigger_accepted_args,
				);
			}
		}

		// Always return the modified (or unmodified) triggers.
		return $actionified_triggers;
	},
	10, // Priority for this filter
	2   // Number of arguments this filter callback accepts ($actionified_triggers, $this)
);

// Ensure you have the corresponding callback and validation functions defined elsewhere.
// For example:
/*
function advanced_feature_callback_function() {
    // Your logic for the advanced feature trigger
    error_log('Advanced feature trigger executed!');
}

function advanced_feature_validation_check( $arg1 ) {
    // Your validation logic. Return true if validation passes, false otherwise.
    return true; // Always pass for this example
}
*/

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-actionify-triggers.php:200
src/core/classes/class-actionify-triggers.php:242

public function get_active_triggers_from_recipes() {

		$this->recipes = Automator()->get_recipes_data( true );

		if ( empty( $this->recipes ) ) {
			return array();
		}
		// Collect all trigger codes that have been actionified, so we don't double register
		$actionified_triggers = new stdClass();

		foreach ( $this->recipes as $recipe ) {

			// Only actionify published recipes
			if ( 'publish' !== $recipe['post_status'] ) {
				continue;
			}

			// Only actionify uncompleted recipes
			if ( true === $recipe['completed_by_current_user'] ) {
				continue;
			}
			// Loop through each trigger and add our trigger event to the hook
			foreach ( $recipe['triggers'] as $trigger ) {

				// Map action to specific recipeID/TriggerID combination
				if ( ! array_key_exists( 'code', $trigger['meta'] ) ) {
					continue;
				}

				$trigger_code = $trigger['meta']['code'];

				// We only want to add one action for each trigger
				if ( isset( $actionified_triggers->$trigger_code ) ) {
					continue;
				}

				// The trigger may exist in the DB but the plugin integration may not be active, if it is not
				$trigger_actions             = Automator()->get->trigger_actions_from_trigger_code( $trigger_code );
				$trigger_validation_function = Automator()->get->trigger_validation_function_from_trigger_code( $trigger_code );
				$trigger_priority            = Automator()->get->trigger_priority_from_trigger_code( $trigger_code );
				$trigger_accepted_args       = Automator()->get->trigger_accepted_args_from_trigger_code( $trigger_code );

				// Initialize trigger
				if ( empty( $trigger_validation_function ) ) {
					continue;
				}

				$actionified_triggers->$trigger_code = new stdClass();

				$actionified_triggers->$trigger_code->trigger_actions             = $trigger_actions;
				$actionified_triggers->$trigger_code->trigger_validation_function = $trigger_validation_function;
				$actionified_triggers->$trigger_code->trigger_priority            = $trigger_priority;
				$actionified_triggers->$trigger_code->trigger_accepted_args       = $trigger_accepted_args;
			}
		}

		return apply_filters( 'actionified_triggers', $actionified_triggers, $this );
	}

Scroll to Top