<?php
namespace Smart_Media_Cleanup;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Settings {

	const OPTION_KEY = 'smc_settings';

	private $options;

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menus' ) );
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function register_menus() {
		add_menu_page(
			esc_html__( 'Media Cleanup', 'smart-media-cleanup' ),
			esc_html__( 'Media Cleanup', 'smart-media-cleanup' ),
			'manage_options',
			'smart-media-cleanup',
			array( $this, 'render_settings_page' ),
			'dashicons-images-alt2',
			76
		);

		add_submenu_page(
			'smart-media-cleanup',
			esc_html__( 'Settings', 'smart-media-cleanup' ),
			esc_html__( 'Settings', 'smart-media-cleanup' ),
			'manage_options',
			'smart-media-cleanup',
			array( $this, 'render_settings_page' )
		);

		add_submenu_page(
			'smart-media-cleanup',
			esc_html__( 'Orphan Cleanup', 'smart-media-cleanup' ),
			esc_html__( 'Orphan Cleanup', 'smart-media-cleanup' ),
			'manage_options',
			'smc-orphan',
			array( Plugin::instance()->orphan_admin, 'render_page' )
		);

		add_submenu_page(
			'smart-media-cleanup',
			esc_html__( 'Log', 'smart-media-cleanup' ),
			esc_html__( 'Log', 'smart-media-cleanup' ),
			'manage_options',
			'smc-log',
			array( $this, 'render_log_page' )
		);
	}

	public function register_settings() {
		register_setting(
			'smc_settings_group',
			self::OPTION_KEY,
			array(
				'sanitize_callback' => array( $this, 'sanitize' ),
			)
		);

		// Section 1: General.
		add_settings_section(
			'smc_section_general',
			esc_html__( 'General', 'smart-media-cleanup' ),
			'__return_false',
			'smart-media-cleanup'
		);

		add_settings_field(
			'smc_enabled',
			esc_html__( 'Enable plugin', 'smart-media-cleanup' ),
			array( $this, 'field_enabled' ),
			'smart-media-cleanup',
			'smc_section_general'
		);

		add_settings_field(
			'smc_post_types',
			esc_html__( 'Post types to monitor', 'smart-media-cleanup' ),
			array( $this, 'field_post_types' ),
			'smart-media-cleanup',
			'smc_section_general'
		);

		// Section 2: What to delete.
		add_settings_section(
			'smc_section_delete',
			esc_html__( 'What to Delete', 'smart-media-cleanup' ),
			'__return_false',
			'smart-media-cleanup'
		);

		add_settings_field(
			'smc_delete_featured',
			esc_html__( 'Delete featured image', 'smart-media-cleanup' ),
			array( $this, 'field_delete_featured' ),
			'smart-media-cleanup',
			'smc_section_delete'
		);

		add_settings_field(
			'smc_delete_attached',
			esc_html__( 'Delete attached media', 'smart-media-cleanup' ),
			array( $this, 'field_delete_attached' ),
			'smart-media-cleanup',
			'smc_section_delete'
		);

		add_settings_field(
			'smc_delete_meta_galleries',
			esc_html__( 'Delete media in meta fields', 'smart-media-cleanup' ),
			array( $this, 'field_delete_meta_galleries' ),
			'smart-media-cleanup',
			'smc_section_delete'
		);

		add_settings_field(
			'smc_meta_keys',
			esc_html__( 'Meta field keys', 'smart-media-cleanup' ),
			array( $this, 'field_meta_keys' ),
			'smart-media-cleanup',
			'smc_section_delete'
		);

		// Section 3: Safety.
		add_settings_section(
			'smc_section_safety',
			esc_html__( 'Safety', 'smart-media-cleanup' ),
			'__return_false',
			'smart-media-cleanup'
		);

		add_settings_field(
			'smc_trigger',
			esc_html__( 'Trigger', 'smart-media-cleanup' ),
			array( $this, 'field_trigger' ),
			'smart-media-cleanup',
			'smc_section_safety'
		);

		add_settings_field(
			'smc_check_usage',
			esc_html__( 'Check usage before deleting', 'smart-media-cleanup' ),
			array( $this, 'field_check_usage' ),
			'smart-media-cleanup',
			'smc_section_safety'
		);

		// Section 4: Logging.
		add_settings_section(
			'smc_section_logging',
			esc_html__( 'Logging', 'smart-media-cleanup' ),
			'__return_false',
			'smart-media-cleanup'
		);

		add_settings_field(
			'smc_enable_log',
			esc_html__( 'Enable log', 'smart-media-cleanup' ),
			array( $this, 'field_enable_log' ),
			'smart-media-cleanup',
			'smc_section_logging'
		);

		add_settings_field(
			'smc_log_retention_days',
			esc_html__( 'Log retention (days)', 'smart-media-cleanup' ),
			array( $this, 'field_log_retention_days' ),
			'smart-media-cleanup',
			'smc_section_logging'
		);

		// Section 5: Virtual Folders.
		add_settings_section(
			'smc_section_folders',
			esc_html__( 'Virtual Folders', 'smart-media-cleanup' ),
			array( $this, 'section_folders_intro' ),
			'smart-media-cleanup'
		);

		add_settings_field(
			'smc_enable_virtual_folders',
			esc_html__( 'Enable virtual folders', 'smart-media-cleanup' ),
			array( $this, 'field_enable_virtual_folders' ),
			'smart-media-cleanup',
			'smc_section_folders'
		);

		add_settings_field(
			'smc_folder_names',
			esc_html__( 'CPT folder names', 'smart-media-cleanup' ),
			array( $this, 'field_folder_names' ),
			'smart-media-cleanup',
			'smc_section_folders'
		);
	}

	public function enqueue_assets( $hook ) {
		$smc_pages = array( 'toplevel_page_smart-media-cleanup', 'media-cleanup_page_smc-orphan', 'media-cleanup_page_smc-log' );
		if ( ! in_array( $hook, $smc_pages, true ) ) {
			return;
		}
		wp_enqueue_style( 'smc-admin', SMC_URL . 'admin/css/admin.css', array(), SMC_VERSION );
		wp_enqueue_script( 'smc-admin', SMC_URL . 'admin/js/admin.js', array( 'jquery' ), SMC_VERSION, true );
		wp_localize_script( 'smc-admin', 'smcData', array(
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'smc_orphan' ),
			'i18n'    => array(
				'scanning'       => __( 'Scanning…', 'smart-media-cleanup' ),
				'scanComplete'   => __( 'Scan complete.', 'smart-media-cleanup' ),
				'scanAgain'      => __( 'Scan Again', 'smart-media-cleanup' ),
				'deleting'       => __( 'Deleting…', 'smart-media-cleanup' ),
				/* translators: %d: number of deleted files */
				'deleteComplete' => __( 'Done. %d file(s) deleted.', 'smart-media-cleanup' ),
				'confirmDelete'  => __( 'Are you sure you want to permanently delete the selected files? This cannot be undone.', 'smart-media-cleanup' ),
				'noSelection'    => __( 'Please select at least one file to delete.', 'smart-media-cleanup' ),
				'error'          => __( 'An error occurred. Please try again.', 'smart-media-cleanup' ),
				'orphansFound'   => __( 'orphan file(s) found', 'smart-media-cleanup' ),
				'total'          => __( 'total', 'smart-media-cleanup' ),
				'view'           => __( 'View', 'smart-media-cleanup' ),
				'skipped'        => __( 'skipped — still in use', 'smart-media-cleanup' ),
			),
		) );
	}

	// -------------------------------------------------------------------------
	// Field callbacks
	// -------------------------------------------------------------------------

	public function field_enabled() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[enabled]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['enabled'], true, false ),
			esc_html__( 'Enable automatic media cleanup on post deletion', 'smart-media-cleanup' )
		);
	}

	public function field_post_types() {
		$options    = $this->get();
		$selected   = (array) $options['post_types'];
		$post_types = get_post_types( array( 'public' => true ), 'objects' );

		echo '<fieldset>';
		foreach ( $post_types as $pt ) {
			if ( 'attachment' === $pt->name ) {
				continue;
			}
			printf(
				'<label style="display:block;margin-bottom:4px"><input type="checkbox" name="%1$s[post_types][]" value="%2$s" %3$s> %4$s <code>(%5$s)</code></label>',
				esc_attr( self::OPTION_KEY ),
				esc_attr( $pt->name ),
				checked( in_array( $pt->name, $selected, true ), true, false ),
				esc_html( $pt->label ),
				esc_html( $pt->name )
			);
		}
		echo '</fieldset>';
	}

	public function field_delete_featured() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[delete_featured]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['delete_featured'], true, false ),
			esc_html__( 'Delete the featured image when a monitored post is deleted', 'smart-media-cleanup' )
		);
	}

	public function field_delete_attached() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[delete_attached]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['delete_attached'], true, false ),
			esc_html__( 'Delete media attached via post_parent (uploaded directly to this post)', 'smart-media-cleanup' )
		);
	}

	public function field_delete_meta_galleries() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[delete_meta_galleries]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['delete_meta_galleries'], true, false ),
			esc_html__( 'Delete media referenced in the meta fields listed below', 'smart-media-cleanup' )
		);
	}

	public function field_meta_keys() {
		$options   = $this->get();
		$meta_keys = implode( "\n", (array) $options['meta_keys'] );

		printf(
			'<textarea name="%1$s[meta_keys]" rows="6" cols="40" class="large-text code">%2$s</textarea>',
			esc_attr( self::OPTION_KEY ),
			esc_textarea( $meta_keys )
		);

		echo '<p class="description" style="margin-top:8px;">';
		esc_html_e( 'One meta key per line. Add the internal field name (not the label) for every image or gallery custom field whose images should be protected and cleaned up.', 'smart-media-cleanup' );
		echo '</p>';

		echo '<div class="smc-help-box" style="margin-top:10px;padding:10px 14px;background:#f6f7f7;border-left:3px solid #72aee6;font-size:13px;">';
		echo '<p style="margin:0 0 6px;font-weight:600;">' . esc_html__( 'Where to find your field names:', 'smart-media-cleanup' ) . '</p>';
		echo '<ul style="margin:0;padding-left:18px;list-style:disc;">';

		printf(
			'<li style="margin-bottom:4px;"><strong>JetEngine</strong> — %s</li>',
			esc_html__( 'go to JetEngine → Meta Boxes, open the meta box for your post type or taxonomy, and copy the Field Name (not the label).', 'smart-media-cleanup' )
		);

		printf(
			'<li style="margin-bottom:4px;"><strong>Advanced Custom Fields</strong> — %s</li>',
			esc_html__( 'go to Custom Fields → Field Groups, open a group, and copy the Field Name shown below each image, gallery, or file field.', 'smart-media-cleanup' )
		);

		printf(
			'<li><strong>%s</strong> — %s</li>',
			esc_html__( 'Other plugins', 'smart-media-cleanup' ),
			esc_html__( 'check the plugin documentation or inspect the postmeta table in your database to find the correct key name.', 'smart-media-cleanup' )
		);

		echo '</ul>';
		echo '<p style="margin:8px 0 0;color:#646970;">';
		esc_html_e( 'WooCommerce product galleries and category thumbnails are protected automatically — no need to add them here.', 'smart-media-cleanup' );
		echo '</p>';
		echo '<p style="margin:4px 0 0;color:#646970;">';
		esc_html_e( 'Each key you add is checked in post meta, taxonomy term meta, and site-wide options (e.g. ACF Options pages) — you only need to add it once.', 'smart-media-cleanup' );
		echo '</p>';
		echo '</div>';

		echo '<div class="smc-help-box" style="margin-top:8px;padding:10px 14px;background:#fff8e5;border-left:3px solid #dba617;font-size:13px;">';
		echo '<span class="dashicons dashicons-info" style="float:left;margin-right:6px;color:#dba617;"></span>';
		echo '<p style="margin:0;">';
		printf(
			/* translators: "Orphan Cleanup" is the name of a plugin tool page. */
			esc_html__( 'Images attached to taxonomy terms (e.g. brand logos, category thumbnails) are not deleted automatically when a term is deleted. Use the %s tool to find and remove them after deleting a term.', 'smart-media-cleanup' ),
			'<strong>' . esc_html__( 'Orphan Cleanup', 'smart-media-cleanup' ) . '</strong>'
		);
		echo '</p>';
		echo '</div>';
	}

	public function field_trigger() {
		$options = $this->get();
		$current = $options['trigger'];

		echo '<fieldset>';

		printf(
			'<label style="display:block;margin-bottom:6px"><input type="radio" name="%1$s[trigger]" value="permanent" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $current, 'permanent', false ),
			esc_html__( 'On permanent delete only (recommended)', 'smart-media-cleanup' )
		);

		printf(
			'<label style="display:block;margin-bottom:4px"><input type="radio" name="%1$s[trigger]" value="both" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $current, 'both', false ),
			esc_html__( 'On trash and permanent delete', 'smart-media-cleanup' )
		);

		echo '<p class="smc-caution"><span class="dashicons dashicons-warning"></span> ';
		esc_html_e( 'Caution: "On trash and permanent delete" means media files will be deleted as soon as a post is trashed — before you have a chance to restore it. Use only if you are sure you never need to recover trashed posts.', 'smart-media-cleanup' );
		echo '</p>';

		echo '</fieldset>';
	}

	public function field_check_usage() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[check_usage]" value="1" %2$s> %3$s</label>
			<p class="description">%4$s</p>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['check_usage'], true, false ),
			esc_html__( 'Check if attachment is used elsewhere before deleting (recommended)', 'smart-media-cleanup' ),
			esc_html__( 'Prevents deleting images that are shared across multiple posts or pages.', 'smart-media-cleanup' )
		);

		if ( empty( $options['check_usage'] ) ) {
			echo '<p class="smc-caution"><span class="dashicons dashicons-warning"></span> ';
			esc_html_e( 'Caution: with this option off, the plugin will delete attachments even if they are used on other posts. This can permanently break images on your site.', 'smart-media-cleanup' );
			echo '</p>';
		}
	}

	public function field_enable_log() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[enable_log]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['enable_log'], true, false ),
			esc_html__( 'Record deletions in the activity log', 'smart-media-cleanup' )
		);
	}

	public function field_log_retention_days() {
		$options = $this->get();
		printf(
			'<input type="number" name="%1$s[log_retention_days]" value="%2$s" min="1" max="365" class="small-text">
			<p class="description">%3$s</p>',
			esc_attr( self::OPTION_KEY ),
			esc_attr( $options['log_retention_days'] ),
			esc_html__( 'Log entries older than this many days will be automatically removed.', 'smart-media-cleanup' )
		);
	}

	public function section_folders_intro() {
		echo '<p class="description" style="margin-bottom:4px;">';
		esc_html_e( 'Organize your media library into folders automatically. Images uploaded while editing a post or product land inside a subfolder named after that post, grouped under a top-level folder you name.', 'smart-media-cleanup' );
		echo '</p>';
	}

	public function field_enable_virtual_folders() {
		$options = $this->get();
		printf(
			'<label><input type="checkbox" name="%1$s[enable_virtual_folders]" value="1" %2$s> %3$s</label>',
			esc_attr( self::OPTION_KEY ),
			checked( $options['enable_virtual_folders'], true, false ),
			esc_html__( 'Show folders panel in the Media Library', 'smart-media-cleanup' )
		);
	}

	public function field_folder_names() {
		$options      = $this->get();
		$folder_names = isset( $options['folder_names'] ) ? (array) $options['folder_names'] : array();
		$post_types   = get_post_types( array( 'public' => true ), 'objects' );

		echo '<table class="widefat" style="max-width:440px;border-collapse:collapse;">';
		echo '<thead><tr>';
		echo '<th style="padding:10px 12px;width:50%;">' . esc_html__( 'Post Type', 'smart-media-cleanup' ) . '</th>';
		echo '<th style="padding:10px 12px;">' . esc_html__( 'Folder Name', 'smart-media-cleanup' ) . '</th>';
		echo '</tr></thead><tbody>';

		foreach ( $post_types as $pt ) {
			if ( 'attachment' === $pt->name ) {
				continue;
			}
			$current = isset( $folder_names[ $pt->name ] ) ? $folder_names[ $pt->name ] : '';
			printf(
				'<tr>'
				. '<td style="padding:10px 12px;"><strong>%1$s</strong><br><code style="font-size:11px;color:#646970;">%2$s</code></td>'
				. '<td style="padding:10px 12px;"><input type="text" name="%3$s[folder_names][%2$s]" value="%4$s" class="regular-text" placeholder="%1$s" style="width:100%%;"></td>'
				. '</tr>',
				esc_html( $pt->label ),
				esc_attr( $pt->name ),
				esc_attr( self::OPTION_KEY ),
				esc_attr( $current )
			);
		}

		echo '</tbody></table>';

		echo '<p class="description" style="margin-top:8px;">';
		esc_html_e( 'Enter a name for each post type you want auto-organized into folders. Leave a row empty to skip that type.', 'smart-media-cleanup' );
		echo '</p>';

		echo '<div style="margin-top:12px;padding:12px 14px;background:#f0f6fc;border-left:3px solid #2271b1;font-size:13px;">';
		echo '<p style="margin:0 0 8px;font-weight:600;">' . esc_html__( 'Sync existing posts to folders', 'smart-media-cleanup' ) . '</p>';
		echo '<p style="margin:0 0 10px;color:#3c434a;">';
		esc_html_e( 'Images are auto-assigned to folders when a post is saved. Click the button below to assign folders to all existing posts that already have images.', 'smart-media-cleanup' );
		echo '</p>';
		printf(
			'<button type="button" id="smc-sync-folders" class="button" data-nonce="%1$s">%2$s</button>'
			. ' <span id="smc-sync-result" style="margin-left:10px;font-style:italic;color:#646970;"></span>',
			esc_attr( wp_create_nonce( 'smc_folders' ) ),
			esc_html__( 'Sync All Posts to Folders', 'smart-media-cleanup' )
		);
		echo '</div>';

		echo '<div class="smc-help-box" style="margin-top:10px;padding:10px 14px;background:#f6f7f7;border-left:3px solid #72aee6;font-size:13px;">';
		echo '<span class="dashicons dashicons-info" style="float:left;margin-right:6px;color:#72aee6;"></span>';
		echo '<p style="margin:0;">';
		esc_html_e( 'If you use a duplicate plugin (e.g. Duplicate Post, YITH Duplicate), duplicated posts reuse the same image files — no new attachments are created, so images will not be auto-assigned to the duplicate\'s subfolder. You can assign them manually from the Media Library.', 'smart-media-cleanup' );
		echo '</p>';
		echo '</div>';
	}

	// -------------------------------------------------------------------------
	// Sanitize
	// -------------------------------------------------------------------------

	public function sanitize( $input ) {
		$clean = array();

		$clean['enabled']               = ! empty( $input['enabled'] );
		$clean['delete_featured']       = ! empty( $input['delete_featured'] );
		$clean['delete_attached']       = ! empty( $input['delete_attached'] );
		$clean['delete_meta_galleries'] = ! empty( $input['delete_meta_galleries'] );
		$clean['check_usage']           = ! empty( $input['check_usage'] );
		$clean['enable_log']            = ! empty( $input['enable_log'] );

		$clean['trigger'] = in_array( $input['trigger'] ?? '', array( 'permanent', 'both' ), true )
			? $input['trigger']
			: 'permanent';

		$clean['log_retention_days'] = max( 1, min( 365, absint( $input['log_retention_days'] ?? 30 ) ) );

		$raw_post_types     = isset( $input['post_types'] ) ? (array) $input['post_types'] : array();
		$valid_post_types   = array_keys( get_post_types( array( 'public' => true ) ) );
		$clean['post_types'] = array_values( array_intersect( $raw_post_types, $valid_post_types ) );

		$raw_meta_keys      = isset( $input['meta_keys'] ) ? wp_unslash( $input['meta_keys'] ) : '';
		$meta_lines         = preg_split( '/[\r\n]+/', $raw_meta_keys );
		$clean['meta_keys'] = array_values( array_filter( array_map( 'sanitize_key', array_map( 'trim', $meta_lines ) ) ) );

		$clean['enable_virtual_folders'] = ! empty( $input['enable_virtual_folders'] );

		$raw_folder_names      = isset( $input['folder_names'] ) ? (array) $input['folder_names'] : array();
		$clean['folder_names'] = array();
		foreach ( $raw_folder_names as $pt => $name ) {
			$pt   = sanitize_key( $pt );
			$name = sanitize_text_field( $name );
			if ( $pt && $name ) {
				$clean['folder_names'][ $pt ] = $name;
			}
		}

		return $clean;
	}

	// -------------------------------------------------------------------------
	// Page renderers
	// -------------------------------------------------------------------------

	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		$auto_detected = $this->get_auto_detected_plugins();
		require SMC_PATH . 'admin/views/settings-page.php';
	}

	public function get_auto_detected_plugins() {
		$detected = array();

		if ( defined( 'WPSEO_VERSION' ) || class_exists( 'WPSEO_Options', false ) ) {
			$detected[] = array(
				'name' => 'Yoast SEO',
				'keys' => array( '_yoast_wpseo_opengraph-image-id', '_yoast_wpseo_twitter-image-id' ),
			);
		}

		if ( defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath', false ) ) {
			$detected[] = array(
				'name' => 'RankMath SEO',
				'keys' => array( 'rank_math_facebook_image_id', 'rank_math_twitter_image_id' ),
			);
		}

		if ( defined( 'MINORANK_VERSION' ) || class_exists( 'Minorank_Meta', false ) ) {
			$detected[] = array(
				'name' => 'Minorank SEO',
				'keys' => array( '_minorank_og_image (postmeta)', 'minorank_local_business → logo_url (options)' ),
			);
		}

		// WooCommerce — product galleries and category thumbnails are auto-checked.
		if ( class_exists( 'WooCommerce', false ) || defined( 'WC_VERSION' ) ) {
			$detected[] = array(
				'name' => 'WooCommerce',
				'keys' => array( '_product_image_gallery', 'thumbnail_id (product category/tag)' ),
			);
		}

		// Advanced Custom Fields — enumerate image/gallery/file fields from all field groups.
		if ( function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_get_fields' ) ) {
			$acf_keys = array();
			$groups   = acf_get_field_groups();
			foreach ( $groups as $group ) {
				$fields = acf_get_fields( $group['key'] );
				if ( ! is_array( $fields ) ) {
					continue;
				}
				foreach ( $fields as $field ) {
					if ( in_array( $field['type'], array( 'image', 'gallery', 'file' ), true ) ) {
						$acf_keys[] = $field['name'];
					}
				}
			}
			if ( ! empty( $acf_keys ) ) {
				$detected[] = array(
					'name' => 'Advanced Custom Fields',
					'keys' => array_unique( $acf_keys ),
				);
			}
		}

		return $detected;
	}

	public function render_log_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		require SMC_PATH . 'admin/views/log-page.php';
	}

	// -------------------------------------------------------------------------
	// Getter
	// -------------------------------------------------------------------------

	public function get() {
		if ( null === $this->options ) {
			$defaults = array(
				'enabled'               => true,
				'post_types'            => array( 'post' ),
				'delete_featured'       => true,
				'delete_attached'       => true,
				'delete_meta_galleries' => true,
				'meta_keys'             => array(),
				'trigger'               => 'permanent',
				'check_usage'           => true,
				'enable_log'             => true,
				'log_retention_days'     => 30,
				'enable_virtual_folders' => false,
				'folder_names'           => array(),
			);

			$saved         = get_option( self::OPTION_KEY, array() );
			$this->options = wp_parse_args( $saved, $defaults );
		}

		return $this->options;
	}
}
