<?php
/**
 * XMLSF_MULTISITE_UNINSTALL
 *
 * Set this constant in wp-config.php if you want to allow looping over each site
 * in the network to run XMLSitemapFeed_Uninstall->uninstall() defined in uninstall.php
 *
 * Be careful: There is NO batch-processing so it does not scale on large networks!
 *
 * example:
 * define('XMLSF_MULTISITE_UNINSTALL', true);
 */

// exit if uninstall not called from WordPress
defined('WP_UNINSTALL_PLUGIN') || exit();

/*
 * XML Sitemap Feed uninstallation
 *
 * @since 4.4
 */
class XMLSF_News_Uninstall {

	/*
	 * constructor: manages uninstall for multisite
	 *
	 * @since 4.4
	 */
	function __construct()
	{
		global $wpdb;

		// check if it is a multisite and if XMLSF_MULTISITE_UNINSTALL constant is defined
    	// if so, run the uninstall function for each blog id
		if ( is_multisite() && defined('XMLSF_MULTISITE_UNINSTALL') && XMLSF_MULTISITE_UNINSTALL ) {
			error_log('Clearing Google News Advanced settings from each site brefore uninstall:');
			foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) {
				switch_to_blog($blog_id);
				$this->uninstall($blog_id);
			}
			restore_current_blog();
		}
		else
			$this->uninstall();
	}

	/*
	 * remove plugin data
	 *
	 * @since 4.4
	 */
	function uninstall( $blog_id = false )
	{
		// remove plugin settings
		delete_option('xmlsf_news_advanced');
		delete_option('xmlsf_news_license');
		delete_option('xmlsf_news_log');

		// Kilroy was here
		if ( defined('WP_DEBUG') && WP_DEBUG ) {
			if ($blog_id)
				error_log('Google News Advanced settings cleared from site '.$blog_id.' on uninstall.');
			else
				error_log('Google News Advanced settings cleared on uninstall.');
		}
	}
}

new XMLSF_News_Uninstall();
