<?php

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

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

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

	/**
	 * Called when the plugin is activated.
	 *
	 * @since 1.0.0
	 */
	public static function activate() {	
		// Insert the plugin settings and default values for the first time
		$defaults = aiopod_get_default_settings();

		foreach ( $defaults as $option_name => $values ) {
			if ( false === get_option( $option_name ) ) {	
        		add_option( $option_name, $values );						
    		}
		}
		
		// Add custom AIOPOD capabilities
		if ( false === get_option( 'aiopod_version' ) ) {
			$roles = new AIOPOD_Roles;
			$roles->add_caps();
		}
		
		// Remove the unfiltered_html capability from editors
		aiopod_remove_unfiltered_html_capability_from_editors();

		// Create custom database tables
		AIOPOD_Statistics_Helper::get_instance()->create_custom_tables();

		// Insert the plugin version
		add_option( 'aiopod_version', AIOPOD_PLUGIN_VERSION );

		// Take back the core term counts released on deactivation
		self::sync_core_term_counts();
	}

	/**
	 * Restore the core term counts this plugin contributes to.
	 *
	 * Mirrors AIOPOD_Deactivator::sync_core_term_counts(). Does nothing unless the WordPress
	 * taxonomies setting is on, which on a first activation it never is.
	 *
	 * @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;
		}

		aiopod_update_core_term_counts( array( 'wp_taxonomies' => array() ), $settings );
	}

}
