<?php
/**
 * CacheLoop – Browser Cache & Gzip Compression (Free)
 *
 * Writes optimized Expires, Cache-Control, and Gzip directives into the
 * site's .htaccess file (Apache/LiteSpeed compatible). Rules are wrapped
 * in unique marker comments so they can be cleanly removed on deactivation.
 *
 * @package CacheLoop
 */

// Security: Prevent direct file access.
defined( 'ABSPATH' ) || exit;

/**
 * Class CACHELOOP_Browser_Cache
 *
 * Manages .htaccess browser caching and Gzip rules.
 */
class CACHELOOP_Browser_Cache {

	/**
	 * Writes browser cache and Gzip rules to .htaccess.
	 * Called on plugin activation.
	 *
	 * @return bool True on success, false on failure.
	 */
	public static function write_htaccess_rules() {
		$htaccess_file = self::get_htaccess_path();

		// Ensure WordPress admin function file is loaded.
		if ( ! function_exists( 'insert_with_markers' ) ) {
			require_once ABSPATH . 'wp-admin/includes/misc.php';
		}

		// Build the rules block.
		$rules = self::get_htaccess_rules_array();

		return insert_with_markers( $htaccess_file, 'CacheLoop', $rules );
	}

	/**
	 * Removes CacheLoop rules from .htaccess.
	 * Called on plugin deactivation.
	 *
	 * @return bool True on success, false on failure.
	 */
	public static function remove_htaccess_rules() {
		$htaccess_file = self::get_htaccess_path();

		// Ensure WordPress admin function file is loaded.
		if ( ! function_exists( 'insert_with_markers' ) ) {
			require_once ABSPATH . 'wp-admin/includes/misc.php';
		}

		return insert_with_markers( $htaccess_file, 'CacheLoop', array() );
	}

	// ---------------------------------------------------------------------------
	// Helpers
	// ---------------------------------------------------------------------------

	/**
	 * Returns the absolute path to the root .htaccess file.
	 *
	 * @return string
	 */
	private static function get_htaccess_path() {
		return ABSPATH . '.htaccess';
	}

	/**
	 * Builds the htaccess directives array for CacheLoop.
	 *
	 * @return array
	 */
	private static function get_htaccess_rules_array() {
		return array(
			'<IfModule mod_deflate.c>',
			'    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css',
			'    AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript',
			'    AddOutputFilterByType DEFLATE application/json application/xml application/xhtml+xml',
			'    AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/font-woff application/font-woff2',
			'</IfModule>',
			'',
			'<IfModule mod_expires.c>',
			'    ExpiresActive On',
			'    ExpiresDefault                          "access plus 1 month"',
			'    ExpiresByType text/html                 "access plus 0 seconds"',
			'    ExpiresByType text/css                  "access plus 1 year"',
			'    ExpiresByType text/javascript           "access plus 1 year"',
			'    ExpiresByType application/javascript    "access plus 1 year"',
			'    ExpiresByType image/jpeg                "access plus 1 year"',
			'    ExpiresByType image/png                 "access plus 1 year"',
			'    ExpiresByType image/gif                 "access plus 1 year"',
			'    ExpiresByType image/webp                "access plus 1 year"',
			'    ExpiresByType image/svg+xml             "access plus 1 year"',
			'    ExpiresByType image/x-icon              "access plus 1 year"',
			'    ExpiresByType application/pdf           "access plus 1 month"',
			'    ExpiresByType font/ttf                  "access plus 1 year"',
			'    ExpiresByType font/otf                  "access plus 1 year"',
			'    ExpiresByType application/font-woff     "access plus 1 year"',
			'    ExpiresByType application/font-woff2    "access plus 1 year"',
			'</IfModule>',
			'',
			'<IfModule mod_headers.c>',
			'    <FilesMatch "\.(css|js|woff|woff2|ttf|otf|eot|svg)$">',
			'        Header set Cache-Control "max-age=31536000, public, immutable"',
			'    </FilesMatch>',
			'    <FilesMatch "\.(jpg|jpeg|png|gif|webp|ico)$">',
			'        Header set Cache-Control "max-age=31536000, public"',
			'    </FilesMatch>',
			'    <FilesMatch "\.html$">',
			'        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"',
			'    </FilesMatch>',
			'</IfModule>',
		);
	}
}
