<?php

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

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

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

	/**
	 * Called when the plugin is uninstalled.
	 *
	 * @since 1.0.0
	 */
	public static function uninstall() {
		if ( ! defined( 'AIOPOD_UNINSTALL_PLUGIN' ) ) {
			define( 'AIOPOD_UNINSTALL_PLUGIN', true );
		}

		$cleanup_settings = get_option( 'aiopod_cleanup_settings' );

		if ( ! is_array( $cleanup_settings ) || empty( $cleanup_settings['delete_plugin_data'] ) ) {
			return;
		}

		global $wpdb;

		// Delete all the custom post types
		$aiopod_post_types = array( 'aiopod_podcasts', 'aiopod_episodes' );

		foreach ( $aiopod_post_types as $post_type ) {
			$items = get_posts( array( 'post_type' => $post_type, 'post_status' => 'any', 'numberposts' => -1, 'fields' => 'ids' ) );
			
			if ( count( $items ) ) {
				foreach ( $items as $item ) {
					wp_delete_post( $item, true );
				}
			}			
		}

		// Delete all the terms & taxonomies
		$aiopod_taxonomies = array( 'aiopod_categories', 'aiopod_tags', 'aiopod_participants' );

		foreach ( $aiopod_taxonomies as $taxonomy ) {
			$terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s') ORDER BY t.name ASC", $taxonomy ) );
			
			// Delete terms
			if ( count( $terms ) ) {
				foreach ( $terms as $term ) {					
					$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
					$wpdb->delete( $wpdb->terms, array( 'term_id' => $term->term_id ) );
				}
			}
			
			// Delete taxonomies
			$wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
		}

		// Delete the plugin pages
		$pages_settings = get_option( 'aiopod_pages_settings' );

		if ( is_array( $pages_settings ) ) {
			foreach ( $pages_settings as $page => $id ) {
				if ( $id > 0 ) {
					wp_delete_post( $id, true );
				}	
			}
		}

		// Delete all the plugin options
		$aiopod_settings = array(
			'aiopod_appearance_settings',
			'aiopod_pages_settings',
			'aiopod_taxonomies_settings',
			'aiopod_performance_settings',
			'aiopod_formatting_settings',
			'aiopod_pagination_settings',
			'aiopod_cleanup_settings',
			'aiopod_player_settings',
			'aiopod_podcasts_settings',	
			'aiopod_podcast_settings',
			'aiopod_episodes_settings',
			'aiopod_filters_settings',
			'aiopod_episode_settings',
			'aiopod_related_episodes_settings',
			'aiopod_categories_settings',
			'aiopod_category_settings',
			'aiopod_imported_categories',
			'aiopod_tags_settings',
			'aiopod_tag_settings',
			'aiopod_participants_settings',	
			'aiopod_participant_settings',
			'aiopod_seo_settings',
			'aiopod_share_buttons_settings',					
			'aiopod_restrictions_settings',	
			'aiopod_privacy_settings',
			'aiopod_feed_cache_versions',
			'aiopod_version'
		);

		foreach ( $aiopod_settings as $settings ) {
			delete_option( $settings );
		}

		// Delete capabilities
		$roles = new AIOPOD_Roles;
		$roles->remove_caps();

		// Drop custom statistics tables
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}aiopod_event_logs" );
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}aiopod_daily_stats" );
	}

}
