Filter uncanny-automator

automator_action_log_view_query

Filters the query arguments used for retrieving automator action logs before they are executed.

add_filter( 'automator_action_log_view_query', $callback, 10, 1 );

Description

This filter allows modification of the SQL query used to retrieve data for the Automator Action Log view. Developers can alter the `$qry` parameter to customize which log entries are fetched, add custom joins, or modify filtering conditions before the data is returned to the view.


Usage

add_filter( 'automator_action_log_view_query', 'your_function_name', 10, 1 );

Parameters

$qry (mixed)
This parameter is a boolean that determines whether the query results should be grouped by default.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'automator_action_log_view_query' hook to modify the SQL query.
 *
 * This example demonstrates how to add a condition to the SQL query to only retrieve
 * action logs where the 'user_id' is a specific value.
 *
 * @param string $qry The original SQL query string.
 * @return string The modified SQL query string.
 */
add_filter( 'automator_action_log_view_query', function( $qry ) {

    // In a real-world scenario, you might get the user ID from a GET parameter,
    // a user option, or a specific context in your plugin.
    // For demonstration purposes, we'll hardcode a user ID.
    $target_user_id = 5; // Replace with an actual user ID from your WordPress installation.

    global $wpdb;

    // Append a WHERE clause to filter by user_id.
    // We need to carefully append this to the existing query structure.
    // Assuming the query structure has a WHERE clause already or can accommodate one.
    // A more robust solution would parse the existing query.
    // For simplicity, we'll assume we can directly append or modify.

    // Let's assume the existing query might not have a WHERE clause, or we want to add to it.
    // We'll look for 'FROM' and insert our condition after the join clauses.
    // This is a simplistic approach and might need adjustment based on the exact $qry structure.

    $from_position = strpos( $qry, 'FROM' );

    if ( $from_position !== false ) {
        // Find the end of the JOIN clauses to append the WHERE clause correctly.
        $where_position = strripos( $qry, 'WHERE' ); // Look for an existing WHERE clause.

        if ( $where_position !== false ) {
            // If a WHERE clause exists, append the new condition with AND.
            $qry = substr_replace( $qry, " AND a.user_id = {$target_user_id}", $where_position + strlen( 'WHERE' ), 0 );
        } else {
            // If no WHERE clause exists, add a new one.
            // We need to find the end of the last JOIN clause to append the WHERE.
            $last_join_pos = strrpos( $qry, 'JOIN' );
            if ( $last_join_pos !== false ) {
                $qry .= " WHERE a.user_id = {$target_user_id}";
            } else {
                // This is a fallback, assuming the query structure is very basic
                // and we can directly append after FROM. This is less safe.
                $qry = str_replace( 'FROM', "FROM {$wpdb->users} u ON a.user_id = u.ID WHERE a.user_id = {$target_user_id}", $qry );
            }
        }
    }

    return $qry;
}, 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/class-automator-db.php:547

public static function action_log_view_query( $group_by = true ) {
		global $wpdb;
		$qry = "SELECT a.automator_action_id,
					a.date_time AS action_date,
					a.completed AS action_completed,
					a.error_message,
					a.automator_recipe_id,
					a.ID AS action_log_id,
					a.automator_recipe_log_id AS recipe_log_id,
					r.date_time AS recipe_date_time,
					r.completed AS recipe_completed,
					r.run_number AS recipe_run_number,
					pa.post_title AS action_title,
					am.meta_value AS action_sentence,
					p.post_title AS recipe_title,
					u.ID AS user_id,
					u.user_email,
					u.display_name
			FROM {$wpdb->prefix}uap_action_log a
			LEFT JOIN {$wpdb->prefix}uap_recipe_log r
			ON a.automator_recipe_log_id = r.ID
			LEFT JOIN {$wpdb->posts} p
			ON p.ID = a.automator_recipe_id
			JOIN {$wpdb->posts} pa
			ON pa.ID = a.automator_action_id
			LEFT JOIN {$wpdb->prefix}uap_action_log_meta am
			ON a.automator_action_id = am.automator_action_id AND am.automator_action_log_id = a.ID AND am.user_id = a.user_id AND am.meta_key = 'sentence_human_readable_html'
			LEFT JOIN {$wpdb->users} u
			ON a.user_id = u.ID";
		if ( $group_by ) {
			$qry .= ' GROUP BY a.ID';
		}

		return apply_filters(
			'automator_action_log_view_query',
			$qry
		);
	}

Scroll to Top