Filter uncanny-automator-pro

automator_pro_auto_purge_logs_statuses_to_remove

Filters the statuses for which Automator Pro logs are automatically purged.

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

Description

Filters the list of log statuses to be automatically purged. Developers can use this hook to exclude specific statuses from automatic log deletion, preventing accidental removal of important data. The default includes all finished statuses.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Example of using the 'automator_pro_auto_purge_logs_statuses_to_remove' filter.
 *
 * This example demonstrates how to remove additional log statuses
 * from being automatically purged, in addition to the default ones.
 *
 * For instance, if you have a custom 'Paused' status and want to
 * prevent it from being purged automatically.
 *
 * @param array $statuses An array of log statuses to be removed.
 * @return array The modified array of log statuses to be removed.
 */
add_filter( 'automator_pro_auto_purge_logs_statuses_to_remove', function( $statuses ) {

	// Assume 'Paused' has a status code of 5 (this is hypothetical).
	// We want to ensure that logs with status code 5 are not purged.
	$paused_status_code = 5;

	// Check if the 'Paused' status code is already in the array.
	if ( ! in_array( $paused_status_code, $statuses ) ) {
		// If not, add it to the array of statuses that should NOT be removed.
		// Since the filter is for 'statuses_to_remove', we actually want to remove
		// any status that we *don't* want to purge from this list.
		// Therefore, we add the default statuses we want to keep.

		// For this specific filter, the original function applies statuses to REMOVE.
		// So, if we want to EXCLUDE a status, we need to ensure it's not in the returned array.
		// The existing code removes 'Completed with errors' if it's status code 2.
		// Let's say we also want to exclude 'On Hold' which has a hypothetical status code of 6.
		// This means we need to remove status code 6 from the list of statuses to remove.

		$statuses_to_keep = array( 6 ); // Hypothetical 'On Hold' status

		// Iterate through the statuses that are marked for removal.
		foreach ( $statuses as $key => $status_code ) {
			// If a status code we want to keep is found in the list of statuses to remove,
			// then remove it from the list of statuses to remove.
			if ( in_array( (int) $status_code, $statuses_to_keep ) ) {
				unset( $statuses[ $key ] );
			}
		}
	}

	return $statuses;

}, 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

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

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