Action
uncanny-automator
automator_recipe_imported
Fires after a recipe is successfully imported into the Automator system.
add_action( 'automator_recipe_imported', $callback, 10, 1 );
Description
Fires after a recipe has been successfully imported into the system. Developers can use this hook to perform actions on the newly imported recipe, such as updating its metadata, associating it with specific users, or triggering custom notifications. The `$new_recipe_id` parameter provides the ID of the imported recipe.
Usage
add_action( 'automator_recipe_imported', 'your_function_name', 10, 1 );
Parameters
-
$new_recipe_id(mixed) - The `$new_recipe_id` parameter contains the ID of the newly imported recipe.
Examples
/**
* Handle actions after a recipe has been successfully imported.
*
* @param int $new_recipe_id The ID of the newly imported recipe.
*/
function my_automator_handle_recipe_imported( $new_recipe_id ) {
// Ensure the recipe ID is valid.
if ( ! is_numeric( $new_recipe_id ) || $new_recipe_id <= 0 ) {
// Log an error or handle it appropriately if the recipe ID is invalid.
error_log( 'automator_recipe_imported hook received an invalid recipe ID: ' . print_r( $new_recipe_id, true ) );
return;
}
// Get the recipe object using its ID.
$recipe = get_post( $new_recipe_id );
// Check if the recipe post exists.
if ( ! $recipe || 'mt-recipe' !== $recipe->post_type ) {
error_log( 'automator_recipe_imported hook: Could not retrieve valid recipe post for ID: ' . $new_recipe_id );
return;
}
// Example: Update a custom field on the imported recipe.
// Let's say we want to mark it as "imported" with the current date.
$current_user = wp_get_current_user();
$imported_by_user_id = $current_user ? $current_user->ID : 0;
update_post_meta( $new_recipe_id, '_my_automator_imported_status', 'imported' );
update_post_meta( $new_recipe_id, '_my_automator_imported_timestamp', current_time( 'mysql' ) );
update_post_meta( $new_recipe_id, '_my_automator_imported_by_user', $imported_by_user_id );
// Example: Send an admin notification.
$admin_email = get_option( 'admin_email' );
$subject = sprintf( __( 'New Automator Recipe Imported: %s', 'your-text-domain' ), $recipe->post_title );
$message = sprintf(
__( 'A new Automator recipe "%s" (ID: %d) has been imported by user ID %d.', 'your-text-domain' ),
$recipe->post_title,
$new_recipe_id,
$imported_by_user_id
);
wp_mail( $admin_email, $subject, $message );
// You could also trigger other actions, like creating a custom log entry,
// adding to a specific category, or performing further data manipulation.
}
// Hook into the 'automator_recipe_imported' action.
// The second parameter '1' indicates that this function accepts one argument ($new_recipe_id).
add_action( 'automator_recipe_imported', 'my_automator_handle_recipe_imported', 10, 1 );
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/class-recipe-template-library.php:404
src/core/admin/class-import-recipe.php:214
src/core/admin/class-import-recipe.php:249
private function import_template( $request ) {
$params = $request->get_params();
try {
// Import template.
$template_id = isset( $params['template'] ) ? absint( $params['template'] ) : 0;
if ( empty( $template_id ) ) {
throw new Exception( 'Invalid template ID', 400 );
}
// Fetch the template export.
$response = wp_remote_get( $this->api_base . $this->library_directory . $template_id . '.json' );
if ( is_wp_error( $response ) ) {
throw new Exception( esc_html( $response->get_error_message() ), 400 );
}
if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
throw new Exception( 'Template not found', 400 );
}
$recipe_json = json_decode( wp_remote_retrieve_body( $response ) );
// Check for decode error.
if ( json_last_error() !== JSON_ERROR_NONE || empty( $recipe_json ) ) {
throw new Exception( 'Invalid template data', 400 );
}
// Pre-import filters.
$this->importer()->pre_import_filters();
// Import the recipe.
$new_recipe_id = $this->importer()->import_recipe_json( $recipe_json );
if ( is_wp_error( $new_recipe_id ) ) {
throw new Exception( esc_html( $new_recipe_id->get_error_message() ), 400 );
}
do_action( 'automator_recipe_imported', $new_recipe_id );
// Set the title as the value property for the log event.
$request->set_param( 'value', $recipe_json->recipe->post->post_title );
// Log the import event.
$this->reports()->log_event( $request );
// Success - return redirect url to new recipe.
return new WP_REST_Response(
array(
'success' => true,
'redirect' => get_edit_post_link( $new_recipe_id, 'url' ),
),
200
);
} catch ( Exception $e ) {
return new WP_REST_Response(
array(
'success' => false,
'error' => $e->getMessage(),
),
200
);
}
}