Filter uncanny-automator-pro

automator_pro_auto_purge_logs_previous_time

Filters the base datetime used for purging old logs. Modifies the time before which logs are removed.

add_filter( 'automator_pro_auto_purge_logs_previous_time', $callback, 10, 2 );

Description

Filters the calculated base datetime for automatic log purging. Developers can modify the `$datetime_base` to customize the cutoff time for logs to be purged, allowing for advanced scheduling or specific log retention policies. The `$purge_value` is also provided for context.


Usage

add_filter( 'automator_pro_auto_purge_logs_previous_time', 'your_function_name', 10, 2 );

Parameters

$datetime_base (mixed)
This parameter represents the base date and time against which the log purging will be calculated.
$purge_value (mixed)
This parameter represents the base datetime from which log entries will be purged.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the automator_pro_auto_purge_logs_previous_time hook.
 * This example modifies the $datetime_base to be 7 days ago instead of the default,
 * effectively purging logs older than 7 days.
 *
 * @param mixed $datetime_base The base datetime object to compare against for purging.
 * @param mixed $purge_value   The user-defined purge value (e.g., number of days, weeks, etc.).
 * @return DateTime The modified base datetime object.
 */
add_filter(
	'automator_pro_auto_purge_logs_previous_time',
	function ( $datetime_base, $purge_value ) {
		// Assuming $datetime_base is a DateTime object or can be cast to one.
		// If the purge value is not specifically set to something else, default to 7 days.
		// In a real-world scenario, you might inspect $purge_value to make more nuanced decisions.
		if ( ! is_a( $datetime_base, 'DateTime' ) ) {
			// Attempt to create a DateTime object if it's not already one.
			try {
				$datetime_base = new DateTime( $datetime_base );
			} catch ( Exception $e ) {
				// Fallback or log error if conversion fails.
				// For this example, we'll assume it's a DateTime object for simplicity.
				// In production, robust error handling is crucial.
				return $datetime_base;
			}
		}

		// We want to purge logs older than 7 days.
		// If the original $datetime_base already represents a time further in the past
		// than 7 days ago, we don't need to change it.
		// Otherwise, set it to 7 days ago.
		$seven_days_ago = new DateTime();
		$seven_days_ago->modify( '-7 days' );

		if ( $datetime_base < $seven_days_ago ) {
			// The existing datetime_base is already older than 7 days, no change needed.
			return $datetime_base;
		} else {
			// Set the base time to 7 days ago.
			return $seven_days_ago;
		}
	},
	10, // Priority
	2   // Accepted arguments
);

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

uncanny-automator-pro/src/core/extensions/activity-log-settings.php:189

public function delete_records( $purge_value = 1, $unit = 'days' ) {

		global $wpdb;

		$finished_statuses = apply_filters( 'automator_pro_auto_purge_logs_statuses_to_remove', Automator_Status::get_finished_statuses() );

		// Optionally not remove "Completed with errors".
		if ( true === apply_filters( 'automator_pro_auto_purge_logs_exclude_completed_with_errors', true ) ) {
			foreach ( $finished_statuses as $k => $v ) {
				if ( 2 === (int) $v ) {
					unset( $finished_statuses[ $k ] );
				}
			}
		}

		$datetime_base = $this->get_datetime_base( $purge_value, $unit );
		$previous_time = apply_filters( 'automator_pro_auto_purge_logs_previous_time', $datetime_base, $purge_value );
		$chunk_size    = apply_filters( 'automator_pro_auto_purge_chunk_size', self::PURGE_CHUNK_SIZE );
		$memory_limit  = $this->get_memory_limit_bytes() * self::MEMORY_THRESHOLD;

		$placeholders = implode( ',', array_fill( 0, count( $finished_statuses ), '%d' ) );

		do {
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			$recipes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT `ID`, `automator_recipe_id`, `run_number`
					FROM {$wpdb->prefix}uap_recipe_log
					WHERE `date_time` < %s
						AND `completed` IN ({$placeholders})
					LIMIT %d",
					array_merge( array( $previous_time ), $finished_statuses, array( $chunk_size ) )
				)
			);

			if ( empty( $recipes ) ) {
				break;
			}

			foreach ( $recipes as $recipe ) {

				$recipe_id               = absint( $recipe->automator_recipe_id );
				$automator_recipe_log_id = absint( $recipe->ID );
				$run_number              = absint( $recipe->run_number );

				// Purge recipe logs.
				automator_purge_recipe_logs( $recipe_id, $automator_recipe_log_id );
				// Purge trigger logs.
				automator_purge_trigger_logs( $recipe_id, $automator_recipe_log_id );
				// Purge action logs.
				automator_purge_action_logs( $recipe_id, $automator_recipe_log_id );
				// Purge closure logs.
				automator_purge_closure_logs( $recipe_id, $automator_recipe_log_id );

				do_action( 'automator_recipe_log_deleted', $recipe_id, $automator_recipe_log_id, $run_number );
			}

			// If approaching memory limit, reschedule remaining work and bail.
			// Guard against duplicate scheduling — only queue if no pending run exists.
			if ( memory_get_usage( true ) > $memory_limit ) {
				if ( false === as_next_scheduled_action( self::$cron_schedule, array(), 'uncanny-automator' ) ) {
					as_schedule_single_action( time() + 60, self::$cron_schedule, array(), 'uncanny-automator' );
				}
				return;
			}
		} while ( count( $recipes ) >= $chunk_size );
	}


Scroll to Top