Filter
uncanny-automator
automator_completed_runs
Filters the results of completed Automator runs, allowing modification before they are returned.
add_filter( 'automator_completed_runs', $callback, 10, 1 );
Description
Filters the number of completed recipe runs. Developers can use this to modify the count before it's returned, for example, to apply custom logic or adjust the results based on specific conditions. The hook fires after the database query for completed runs has been executed.
Usage
add_filter( 'automator_completed_runs', 'your_function_name', 10, 1 );
Parameters
-
$results(mixed) - This parameter contains the results of a database query counting completed recipe runs.
Return Value
The filtered value.
Examples
<?php
/**
* Filter the automator_completed_runs to also include runs from a specific user ID if provided.
*
* This example demonstrates how to modify the result of the 'automator_completed_runs' filter
* to further refine the count based on additional criteria, such as a specific user.
*
* @param int|string|null $results The original number of completed runs. It's usually an integer, but can be null or other types depending on the original query.
* @return int The modified number of completed runs.
*/
add_filter( 'automator_completed_runs', function ( $results ) {
// Assume we have a global or passed in variable for the current user ID we want to filter by.
// In a real scenario, this might come from another filter, a global variable, or a function argument
// that was passed implicitly through a chain of filters.
// For this example, let's simulate fetching a specific user ID.
$specific_user_id = get_current_user_id(); // Or some other mechanism to get a user ID.
// If we don't have a specific user ID to filter by, return the original results.
if ( ! $specific_user_id || ! is_numeric( $specific_user_id ) ) {
return $results;
}
// If the original results are already a numeric string or integer, ensure it's an integer.
$current_count = absint( $results );
// In a real plugin, you'd likely re-query the database with the additional user ID condition.
// For demonstration, let's simulate adding a few more runs to the count if a specific user is found.
// THIS IS A SIMPLIFICATION. A real implementation would involve a new database query.
global $wpdb;
$automator_runs_table = $wpdb->prefix . 'automator_runs'; // Assuming a table name.
// Example of how you might re-query (simplified):
// You would need to know the original query's conditions to add to them correctly.
// For instance, if the original query was for a specific recipe_id, you'd add that.
// Let's assume we know the recipe ID from context, or it's passed differently.
// $recipe_id = ...; // Get the recipe ID from somewhere.
// Let's imagine the original query was something like:
// SELECT COUNT(*) FROM {$automator_runs_table} WHERE recipe_id = {$recipe_id} AND status = 'completed' AND date_time >= '{$date}'
// To add a user filter, you might modify it to:
// SELECT COUNT(*) FROM {$automator_runs_table} WHERE recipe_id = {$recipe_id} AND status = 'completed' AND date_time >= '{$date}' AND user_id = {$specific_user_id}
// For this example, we'll just arbitrarily add to the count if the user is logged in.
// A more robust solution would be to fetch the actual count for the specific user.
// Let's pretend we found 5 additional runs for this user.
$additional_runs_for_user = 5; // Replace with actual database query result.
// Update the total count.
$total_completed_runs = $current_count + $additional_runs_for_user;
// Return the modified count.
return $total_completed_runs;
}, 10, 1 ); // Priority 10, 1 accepted argument.
?>
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/utilities/class-automator-get-data.php:1404
public function completed_runs( $seconds_to_include = null ) {
global $wpdb;
$tbl = Automator()->db->tables->recipe;
$query = "SELECT COUNT(*) FROM {$wpdb->prefix}{$tbl} WHERE completed=1";
if ( null !== $seconds_to_include ) {
$timestamp = current_time( 'timestamp' );
$time_ago = strtotime( "-$seconds_to_include Seconds", $timestamp );
$date = date_i18n( 'Y-m-d H:i:s', $time_ago );
$query .= " AND date_time >= '$date'";
}
$results = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return apply_filters( 'automator_completed_runs', absint( $results ) );
}