Action Since 5.7 uncanny-automator

automator_recipe_type_updated

Fires when a recipe type is updated. Fires when a recipe type is updated, passing its post ID, the recipe type object, and a return value.

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

Description

Fires after a recipe type has been successfully updated in the Automator plugin. Developers can use this hook to perform custom actions, such as logging the update, triggering additional automations based on recipe type changes, or modifying the returned data. It fires within the REST API update process for recipe types.


Usage

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

Parameters

$post_id (mixed)
- **$recipe_type** `mixed`
$return (mixed)

Examples

add_action( 'automator_recipe_type_updated', 'my_automator_recipe_updated_handler', 10, 3 );

/**
 * Example handler for the 'automator_recipe_type_updated' hook.
 * This function demonstrates how to react when a recipe type is updated
 * by logging the recipe ID and the updated recipe type.
 *
 * @param int    $post_id      The ID of the updated recipe post.
 * @param string $recipe_type  The updated recipe type slug.
 * @param array  $return_data  The array of data returned by the update process.
 */
function my_automator_recipe_updated_handler( $post_id, $recipe_type, $return_data ) {
    // Log the update event for debugging or monitoring purposes.
    // In a real-world scenario, you might perform more complex actions here,
    // such as clearing caches, sending notifications, or updating related data.
    if ( WP_DEBUG ) {
        error_log( sprintf(
            'Automator: Recipe type updated. Post ID: %d, Recipe Type: %s',
            $post_id,
            $recipe_type
        ) );
    }

    // Example: If the recipe type is 'new_user_welcome', send an internal notification.
    if ( 'new_user_welcome' === $recipe_type ) {
        // This is a placeholder. In a real application, you would use a WordPress
        // notification system or a custom method to notify administrators.
        // For demonstration, we'll just log it again.
        if ( WP_DEBUG ) {
            error_log( sprintf(
                'Automator Notification: A "%s" recipe type was updated for post ID %d.',
                $recipe_type,
                $post_id
            ) );
        }
    }

    // You can also inspect the $return_data if needed.
    // For example, check if a specific field within '_recipe' was changed.
    if ( isset( $return_data['_recipe'] ) && is_object( $return_data['_recipe'] ) ) {
        // $recipe_object = $return_data['_recipe'];
        // Access properties of $recipe_object if necessary.
    }
}

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:968

public function change_post_recipe_type( WP_REST_Request $request ) {

		// Make sure we have a post ID and a post status
		if ( $request->has_param( 'post_ID' ) && $request->has_param( 'recipe_type' ) ) {

			$recipe_types = apply_filters_deprecated( 'uap_recipe_types', array( Automator()->get_recipe_types() ), '3.0', 'automator_recipe_types' );
			$recipe_types = apply_filters( 'automator_recipe_types', $recipe_types );

			$recipe_type = sanitize_text_field( $request->get_param( 'recipe_type' ) );
			$post_id     = absint( $request->get_param( 'post_ID' ) );

			if ( in_array( $recipe_type, $recipe_types, true ) && $post_id ) {

				$updated = Automator()->utilities->set_recipe_type( $post_id, $recipe_type );

				if ( false !== $updated ) {
					$return['message']        = 'Updated!';
					$return['success']        = true;
					$return['action']         = 'updated_post';
					$return['recipes_object'] = Automator()->get_recipes_data( true, $post_id );
					$return['_recipe']        = Automator()->get_recipe_object( $post_id );

					/**
					 * Fires when a recipe type is updated.
					 *
					 * @since 5.7
					 */
					do_action( 'automator_recipe_type_updated', $post_id, $recipe_type, $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 );
	}


Scroll to Top