<?php

/**
 * Fired during plugin deactivation.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Deactivator class.
 *
 * @since 1.0.0
 */
class AIOPOD_Deactivator {

	/**
	 * Called when the plugin is deactivated.
	 *
	 * @since 1.0.0
	 */
	public static function deactivate() {	
		if ( wp_next_scheduled( 'aiopod_schedule_every_five_minutes' ) ) {
			wp_clear_scheduled_hook( 'aiopod_schedule_every_five_minutes' );
		}

		self::sync_core_term_counts();
	}

	/**
	 * Correct the core term counts this plugin was contributing to.
	 *
	 * Core recounts a term only when one of its relationships changes, so without this a term
	 * keeps counting podcasts for as long as the plugin stays off. The setting is left alone —
	 * reactivating has to restore it. Mirrored by AIOPOD_Activator::sync_core_term_counts().
	 *
	 * @since  1.0.4
	 * @access private
	 */
	private static function sync_core_term_counts() {
		if ( ! function_exists( 'aiopod_update_core_term_counts' ) ) {
			return;
		}

		$settings = aiopod_get_option( 'aiopod_taxonomies_settings' );

		if ( empty( $settings['wp_taxonomies'] ) ) {
			return;
		}

		// Detaches first: `_update_post_term_count()` reads the live registration
		aiopod_update_core_term_counts( $settings, array( 'wp_taxonomies' => array() ) );
	}

}
