Action
Since 5.7
uncanny-automator
automator_recipe_user_selector_updated
Fires when a recipe user selector is updated. Fires when a user selector within a recipe is updated, providing recipe ID, source, and field data.
add_action( 'automator_recipe_user_selector_updated', $callback, 10, 2 );
Description
Fires after a user selector within a recipe has been updated via the REST API. Developers can use this hook to perform custom actions, such as logging changes, triggering additional automations, or validating the updated user selection. It provides the recipe ID, source of the update, and updated field data for granular control.
Usage
add_action( 'automator_recipe_user_selector_updated', 'your_function_name', 10, 2 );
Parameters
-
$recipe_id(mixed) - - **$source** `mixed`
-
$fields(mixed) - - **$return** `mixed`
Examples
<?php
/**
* Example function to handle the 'automator_recipe_user_selector_updated' action.
* This function logs the update and potentially performs other actions based on the data.
*
* @param int|string $recipe_id The ID of the recipe that was updated.
* @param string $source The source of the update (e.g., 'rest_api', 'admin_ui').
* @param array $fields An array of fields that were updated.
* @param array $return The response data being returned by the API.
*/
function my_automator_recipe_user_selector_updated_handler( $recipe_id, $source, $fields, $return ) {
// Log the update for debugging or auditing purposes.
error_log( sprintf(
'Automator recipe user selector updated for recipe ID: %1$s. Source: %2$s. Fields: %3$s.',
$recipe_id,
$source,
print_r( $fields, true ) // Print array contents for debugging
) );
// Example: If the 'user_id' field was updated, you might want to trigger a notification
// or update a meta field on the recipe itself.
if ( isset( $fields['user_id'] ) && ! empty( $fields['user_id'] ) ) {
$new_user_id = sanitize_text_field( $fields['user_id'] );
$recipe_object = isset( $return['_recipe'] ) ? $return['_recipe'] : Automator()->get_recipe_object( $recipe_id );
if ( $recipe_object ) {
// Potentially update a custom meta field on the recipe post.
// This is a hypothetical example; adjust based on your needs.
update_post_meta( $recipe_id, '_automator_last_assigned_user_id', $new_user_id );
// You could also trigger an internal event or send an email notification.
// For example, to send an internal notice:
// do_action( 'my_automator_user_reassigned', $recipe_id, $new_user_id );
}
}
// This is an action hook, so no return value is necessary for the hook itself.
// The $return array is handled by the core function calling the hook.
}
// Hook the function into the 'automator_recipe_user_selector_updated' action.
// We specify 4 arguments because the hook passes $recipe_id, $source, $fields, and $return.
add_action( 'automator_recipe_user_selector_updated', 'my_automator_recipe_user_selector_updated_handler', 10, 4 );
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/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:1356
public function user_selector( WP_REST_Request $request ) {
// Make sure we have a post ID and a post status
if ( $request->has_param( 'source' ) && $request->has_param( 'source' ) ) {
$source = Automator()->utilities->automator_sanitize( $request->get_param( 'source' ) );
$fields = Automator()->utilities->automator_sanitize( $request->get_param( 'data' ), 'mixed' );
$recipe_id = (int) $request->get_param( 'recipeId' );
//get recipe post id or action post id
update_post_meta( $recipe_id, 'source', $source );
update_post_meta( $recipe_id, 'fields', $fields );
Automator()->cache->clear_automator_recipe_part_cache( $recipe_id );
$return['message'] = 'Updated!';
$return['success'] = true;
$return['action'] = 'user_selector';
$return['recipes_object'] = Automator()->get_recipes_data( true, $recipe_id );
$return['_recipe'] = Automator()->get_recipe_object( $recipe_id );
/**
* Fires when a recipe user selector is updated.
*
* @since 5.7
*/
do_action( 'automator_recipe_user_selector_updated', $recipe_id, $source, $fields, $return );
return new WP_REST_Response( $return, 200 );
}
$return['message'] = 'Failed to update';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 200 );
}