', 'RewriteEngine On', 'RewriteCond %{HTTP_ACCEPT} image/avif [NC]', 'RewriteCond %{REQUEST_FILENAME}.avif -f', 'RewriteRule ^(.+)\.(jpe?g|png|gif)$ $1.avif [L]', '# Serve WebP when the browser accepts it and a variant exists.', 'RewriteCond %{HTTP_ACCEPT} image/webp [NC]', 'RewriteCond %{REQUEST_FILENAME}.webp -f', 'RewriteRule ^(.+)\.(jpe?g|png|gif)$ $1.webp [L]', '', '# Let caches know the response depends on the Accept header.', '', '', 'Header append Vary Accept', '', '', ); } /** * Write rules into .htaccess. * * @return bool */ public static function add() { if ( ! self::writable() ) { return false; } self::remove_legacy(); return insert_with_markers( self::path(), self::MARKER, self::rules() ); } /** * Remove our rules from .htaccess. * * @return bool */ public static function remove() { $fs = self::fs(); if ( ! $fs || ! $fs->exists( self::path() ) ) { return true; } if ( ! $fs->is_writable( self::path() ) ) { return false; } $ok = insert_with_markers( self::path(), self::MARKER, array() ); return self::remove_legacy() && $ok; } /** * Remove rules written by older plugin versions. * * @return bool */ private static function remove_legacy() { $fs = self::fs(); if ( ! $fs || ! $fs->exists( self::path() ) || ! $fs->is_writable( self::path() ) ) { return true; } $ok = true; foreach ( self::LEGACY_MARKERS as $marker ) { $ok = insert_with_markers( self::path(), $marker, array() ) && $ok; } return $ok; } /** * Whether the .htaccess file (or its folder) is writable. * * @return bool */ private static function writable() { $fs = self::fs(); if ( ! $fs ) { return false; } $file = self::path(); if ( $fs->exists( $file ) ) { return $fs->is_writable( $file ); } return $fs->is_writable( ABSPATH ); } /** * Initialize and return the direct WP_Filesystem API. * * Only the "direct" transport is trusted. On servers where direct * writes are not possible, WordPress may pick the FTP/SSH transports * which can abort the request with a credentials prompt (wp_die) in * the middle of admin operations such as plugin deactivation. To keep * activation, deactivation and settings saves safe on every server we * degrade gracefully instead: callers treat null as "nothing to do". * * @return WP_Filesystem_Base|null */ private static function fs() { if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } global $wp_filesystem; if ( ! isset( $wp_filesystem ) || ! $wp_filesystem instanceof WP_Filesystem_Base ) { try { WP_Filesystem(); } catch ( \Throwable $e ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition -- Deliberate safety net. $wp_filesystem = null; } } // FTP/SSH transports can wp_die() on any method call when // credentials are missing — never touch the filesystem there. if ( ! $wp_filesystem instanceof WP_Filesystem_Base || 'WP_Filesystem_Direct' !== get_class( $wp_filesystem ) ) { return null; } return $wp_filesystem; } /** * .htaccess path (WordPress root). * * @return string */ private static function path() { return ABSPATH . '.htaccess'; } }