<?php
/**
 * ZenBlocks Uninstall
 *
 * Fired when the plugin is uninstalled (deleted from Plugins page).
 * Removes all plugin data from the database and filesystem.
 *
 * @package ZenBlocks
 */

// If uninstall not called from WordPress, abort.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

/**
 * Remove plugin options from the database.
 */
delete_option( 'zenblocks_breakpoints' );
delete_option( 'zenblocks_responsive_css_version' );

/**
 * Remove generated CSS files.
 *
 * ZenBlocks stores per-page CSS and responsive CSS in the uploads directory.
 */
$zenblocks_upload_dir = wp_upload_dir();
$zenblocks_css_dir    = $zenblocks_upload_dir['basedir'] . '/zenblocks';

if ( is_dir( $zenblocks_css_dir ) ) {
	$zenblocks_files = glob( $zenblocks_css_dir . '/*.css' );
	if ( is_array( $zenblocks_files ) ) {
		foreach ( $zenblocks_files as $zenblocks_file ) {
			wp_delete_file( $zenblocks_file );
		}
	}
	// Remove the directory itself.
	global $wp_filesystem;
	if ( ! function_exists( 'WP_Filesystem' ) ) {
		require_once ABSPATH . 'wp-admin/includes/file.php';
	}
	WP_Filesystem();
	if ( $wp_filesystem ) {
		$wp_filesystem->rmdir( $zenblocks_css_dir );
	}
}
