<?php

class CV_Monitor_Activator {

	/**
	 * Fired when the plugin is activated.
	 */
	public static function activate( ) {
		// Use wp_next_scheduled to check if the event is already scheduled
		$timestamp = wp_next_scheduled( 'cv_update_monitor' );
		// Clear out any legacy events
		while ( $timestamp ) {
			wp_unschedule_event( $timestamp, 'cv_update_monitor' );
			$timestamp = wp_next_scheduled( 'cv_update_monitor' );
		}
		// If $timestamp == false schedule daily backups since it hasn't been done previously
		if ( false == $timestamp ) {
			// Schedule the event for right now, then to repeat daily using the hook 'cv_update_monitor'
			wp_schedule_event( time(), 'daily', 'cv_update_monitor' );
		}

		$timestamp2 = wp_next_scheduled( 'cv_optimize_db' );
		while ( $timestamp2 ) {
			wp_unschedule_event( $timestamp2, 'cv_optimize_db' );
			$timestamp2 = wp_next_scheduled( 'cv_optimize_db' );
		}
		if ( false == $timestamp2 ) {
			// Schedule the event for right now, then to repeat weekly using the hook 'cv_optimize_db'
			wp_schedule_event( time(), 'weekly', 'cv_optimize_db' );
		}
	}

	/**
	 * Fired when the plugin is deactivated.
	 */
	public static function deactivate( ) {
		wp_clear_scheduled_hook( 'cv_update_monitor' );
		wp_clear_scheduled_hook( 'cv_optimize_db' );
	}

}