Prune_Logs::maybe_purge_logs()


Source Source

File: src/core/admin/class-prune-logs.php

	public function maybe_purge_logs() {
		if ( ! automator_filter_has_var( 'automator_manual_purge_days_nonce', INPUT_POST ) ) {
			return;
		}

		if ( ! wp_verify_nonce( automator_filter_input( 'automator_manual_purge_days_nonce', INPUT_POST ), 'automator_manual_purge_days_nonce' ) ) {
			return;
		}

		$prune_days_limit = automator_filter_input( 'automator_manual_purge_days', INPUT_POST );

		if ( empty( $prune_days_limit ) ) {
			return;
		}
		if ( intval( $prune_days_limit ) < 1 ) {
			return;
		}

		global $wpdb;

		$previous_time = gmdate( 'Y-m-d', strtotime( '-' . $prune_days_limit . ' days' ) );
		$recipes       = $wpdb->get_results( $wpdb->prepare( "SELECT `ID`, `automator_recipe_id` FROM {$wpdb->prefix}uap_recipe_log WHERE `date_time` < %s AND ( `completed` = %d OR `completed` = %d  OR `completed` = %d )", $previous_time, 1, 2, 9 ) );

		if ( empty( $recipes ) ) {
			update_option( 'automator_last_manual_prune_date', time() );
			$referrer = wp_get_referer();
			wp_safe_redirect( $referrer . '&pruned=1' );
			exit;
		}

		foreach ( $recipes as $recipe ) {
			$recipe_id               = absint( $recipe->automator_recipe_id );
			$automator_recipe_log_id = absint( $recipe->ID );

			// Prune recipe logs.
			automator_purge_recipe_logs( $recipe_id, $automator_recipe_log_id );

			// Prune trigger logs.
			automator_purge_trigger_logs( $recipe_id, $automator_recipe_log_id );

			// Prune action logs.
			automator_purge_action_logs( $recipe_id, $automator_recipe_log_id );

			// Prune closure logs.
			automator_purge_closure_logs( $recipe_id, $automator_recipe_log_id );
		}
		update_option( 'automator_last_manual_prune_date', time() );

		$referrer = wp_get_referer();
		wp_safe_redirect( $referrer . '&pruned=1' );
		exit;
	}