Action Since 7.0 uncanny-automator

automator_recipe_item_delete_before

Fires before any recipe item is deleted. Fires before a recipe item is deleted, providing the REST request and item type.

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

Description

Fires before any recipe item is deleted via the API. Developers can use this hook to perform custom logic or validations before an item is permanently removed. It provides the REST request object and the item type being deleted.


Usage

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

Parameters

$request (WP_REST_Request)
The REST request object.
$type (string)
The item type.

Examples

add_action(
	'automator_recipe_item_delete_before',
	'my_automator_prevent_critical_item_deletion',
	10,
	2
);

/**
 * Prevents the deletion of critical recipe items.
 *
 * This function checks if the item being deleted is of a type that should not
 * be removed by regular users, such as core automation triggers or actions.
 * If it's a critical item, it throws a WP_Error to halt the deletion process.
 *
 * @param WP_REST_Request $request The REST request object.
 * @param string          $type    The item type being deleted.
 */
function my_automator_prevent_critical_item_deletion( WP_REST_Request $request, string $type ) {
	// Define a list of item types that are considered critical and should not be deleted.
	$critical_item_types = array(
		'core_trigger',
		'core_action',
		// Add any other critical item types here as needed.
	);

	// Check if the current item type is in the critical list.
	if ( in_array( $type, $critical_item_types, true ) ) {
		// Get the ID of the item being deleted from the request parameters.
		$item_id = $request->get_param( 'id' );

		// Throw a WordPress error to prevent the deletion.
		// This will typically halt the API request and return an error response to the client.
		throw new WP_Error(
			'automator_critical_item_delete_prohibited',
			sprintf(
				__( 'Deletion of critical item type "%s" (ID: %s) is not allowed.', 'my-automator-plugin' ),
				esc_html( $type ),
				esc_html( $item_id )
			),
			array( 'status' => 400 ) // HTTP status code for bad request
		);
	}

	// If the item is not critical, do nothing and allow the deletion process to continue.
}

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/api/transports/restful/recipe/items/traits/hooks/trait-hook-delete-operations.php:31

protected function dispatch_before_delete_hooks(): void {
		$type = $this->get_item_type();

		/**
		 * Fires before any recipe item is deleted.
		 *
		 * @since 7.0
		 *
		 * @param WP_REST_Request $request The REST request object.
		 * @param string          $type    The item type.
		 */
		do_action(
			'automator_recipe_item_delete_before',
			$this->get_request(),
			$type
		);

		/**
		 * Fires before a specific item type is deleted.
		 *
		 * @since 7.0
		 *
		 * @param WP_REST_Request $request The REST request object.
		 */
		do_action(
			"automator_recipe_{$type}_delete_before",
			$this->get_request()
		);
	}


Scroll to Top