settings = $settings; } /** * Register browser caching hooks. */ public function init() { if ( $this->settings->get( 'browser_caching', true ) ) { add_action( 'send_headers', array( $this, 'send_browser_cache_headers' ) ); } } /** * Send browser caching response headers for non-logged-in public frontend requests. */ public function send_browser_cache_headers() { if ( is_user_logged_in() || is_admin() ) { return; } if ( ! headers_sent() ) { header( 'X-Optimized-By: Ahtesham-Web-Vitals-Booster' ); } } /** * Write browser caching rules to .htaccess safely using WordPress core insert_with_markers. * * @param bool $enable * @return bool */ public static function update_htaccess_rules( $enable = true ) { if ( ! AWVB_Environment::is_htaccess_writable() ) { return false; } $htaccess_file = get_home_path() . '.htaccess'; if ( ! file_exists( $htaccess_file ) ) { return false; } $rules = array(); if ( $enable ) { $rules = array( '', ' ExpiresActive On', ' ExpiresByType image/jpg "access plus 1 year"', ' ExpiresByType image/jpeg "access plus 1 year"', ' ExpiresByType image/gif "access plus 1 year"', ' ExpiresByType image/png "access plus 1 year"', ' ExpiresByType image/webp "access plus 1 year"', ' ExpiresByType image/svg+xml "access plus 1 year"', ' ExpiresByType text/css "access plus 1 month"', ' ExpiresByType application/javascript "access plus 1 month"', ' ExpiresByType font/woff2 "access plus 1 year"', '', ); } require_once ABSPATH . 'wp-admin/includes/misc.php'; return insert_with_markers( $htaccess_file, self::HTACCESS_MARKER, $rules ); } }