Filter uncanny-automator

uncanny_automator_log_serve_json

Filters the JSON data served for recipe logs, allowing modification before it's sent to the client.

add_filter( 'uncanny_automator_log_serve_json', $callback, 10, 4 );

Description

Fires before the Uncanny Automator log data is served as JSON. Developers can modify the log data, including recipe details, triggers, and flow items, before it's sent to the client. This hook allows for custom manipulation or filtering of log information for display or further processing.


Usage

add_filter( 'uncanny_automator_log_serve_json', 'your_function_name', 10, 4 );

Parameters

$json (mixed)
This parameter contains the JSON data that will be served by the log endpoint.
$recipe (mixed)
This parameter contains the JSON data to be served, which is typically an array of log entries.
$triggers_items (mixed)
This parameter holds the recipe object related to the log entry.
$flow_items (mixed)
This parameter contains an array of the trigger items associated with the recipe.

Return Value

The filtered value.


Examples

<?php
/**
 * Example function to modify the JSON data served for an Uncanny Automator log entry.
 * This example adds a custom field to the log data, indicating whether a specific trigger has completed.
 *
 * @param mixed $json The original JSON data being prepared.
 * @param Uncanny_AutomatorRecipe $recipe The recipe object associated with the log.
 * @param array $triggers_items An array of trigger items for the recipe.
 * @param array $flow_items An array of flow items for the recipe.
 * @return mixed The modified JSON data.
 */
add_filter( 'uncanny_automator_log_serve_json', function( $json, $recipe, $triggers_items, $flow_items ) {

    // Check if the JSON is already an array and if we have trigger information.
    if ( is_array( $json ) && ! empty( $triggers_items ) ) {

        // Let's assume we want to add a field indicating if the first trigger has completed.
        // This is a hypothetical example, real logic would depend on the structure of $triggers_items.
        $first_trigger_completed = false;

        // Iterate through the trigger items to find a status, adjust this logic as needed
        // based on the actual structure of $triggers_items provided by the hook.
        if ( ! empty( $triggers_items[0] ) && isset( $triggers_items[0]['status'] ) ) {
            if ( 'completed' === $triggers_items[0]['status'] ) {
                $first_trigger_completed = true;
            }
        }

        // Add our custom field to the JSON data.
        $json['custom_first_trigger_status'] = $first_trigger_completed ? 'Completed' : 'Pending';
    }

    // Always return the modified (or original if no modification was possible) data.
    return $json;

}, 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/services/rest/endpoint/log-endpoint.php:233

protected function serve_json( $recipe = array(), $triggers_items = array(), $flow_items = array() ) {

		if ( ! is_array( $recipe ) || ! is_array( $triggers_items ) ) {
			return array();
		}

		$formatter = $this->get_formatter();

		$flow_items = $this->flow_items_rearrange_by_date( $flow_items );
		usort( $flow_items, array( $this, 'sort_action_items_by_date' ) );

		$triggered_by_user = absint( $recipe['user_id'] );
		$run_number        = absint( $recipe['run_number'] );
		$start_date        = isset( $triggers_items[0]['start_date'] ) ? $triggers_items[0]['start_date'] : null;
		$start_date_full   = isset( $triggers_items[0]['start_date_full'] ) ? $triggers_items[0]['start_date_full'] : null;
		$raw_start_date    = isset( $triggers_items[0]['_raw_start_date'] ) ? $triggers_items[0]['_raw_start_date'] : null;
		$status_id         = $this->determine_status_id( $this->automator_factory->status(), $recipe['completed'], $flow_items );
		$recipe_id         = absint( $recipe['automator_recipe_id'] );
		$end_date          = $this->resolve_end_date( $flow_items );
		$end_date_full     = $this->resolve_end_date( $flow_items, false );
		$raw_end_date      = $this->resolve_end_date_raw( $flow_items );
		$title             = trim( $recipe['recipe_title'] );
		$logic             = strtoupper( $this->logs_factory->trigger()->get_logic( $recipe ) );

		$triggers_statuses = array_column( $triggers_items, 'status_id' );

		$has_triggers_not_completed       = in_array( 'not-completed', $triggers_statuses, true );
		$triggers_num_times_not_completed = in_array( 'in-progress', $triggers_statuses, true );

		if ( $has_triggers_not_completed || $triggers_num_times_not_completed ) {
			$end_date      = null;
			$end_date_full = null;
			$raw_end_date  = null;
		}

		$json = array(
			'recipe_id'         => $recipe_id,
			'run_number'        => $run_number,
			'title'             => ! empty( $title ) ? $title : sprintf( 'ID: %d (no title)', $recipe_id ),
			'status_id'         => $status_id,
			'start_date'        => $start_date,
			'start_date_full'   => $start_date_full,
			'end_date'          => $end_date,
			'end_date_full'     => $end_date_full,
			'date_elapsed'      => $formatter::get_date_elapsed( $raw_start_date, $raw_end_date ),
			'triggered_by_user' => $triggered_by_user,
			'triggers'          => array(
				'logic' => $logic,
				'items' => $triggers_items,
			),
			'actions'           => array(
				'items' => $flow_items,
			),
			'recipe_edit_url'   => get_edit_post_link( $recipe_id, '&' ),
			'log_delete_url'    => $this->get_delete_url(),
		);

		return apply_filters( 'uncanny_automator_log_serve_json', $json, $recipe, $triggers_items, $flow_items );
	}


Scroll to Top