$exists,
'is_ours' => $is_ours,
'foreign_name' => $foreign_name,
'wp_cache_const' => $wp_cache_on,
'wp_config_write' => $wp_config_write,
'wp_content_write' => $wp_content_write,
'active' => $exists && $is_ours && $wp_cache_on,
);
}
/**
* Write static runtime cache configuration & MIME .htaccess inside wp-content/uploads/aio-cache/.
*/
public static function write_runtime_config( $options = null ) {
if ( ! is_array( $options ) ) {
$options = get_option( 'aio_options', array() );
}
$cache_dir = self::get_cache_dir();
if ( ! file_exists( $cache_dir ) ) {
wp_mkdir_p( $cache_dir );
}
if ( ! is_dir( $cache_dir ) || ! is_writable( $cache_dir ) ) {
return false;
}
// Protect .php files in aio-cache and configure Apache/LiteSpeed .gz HTML serving
$cache_htaccess = $cache_dir . '.htaccess';
$ht_rules = implode(
"\n",
array(
'# AIO Tools Cache Directory Rules',
'',
' ',
' Require all denied',
' ',
' ',
' Order allow,deny',
' Deny from all',
' ',
'',
'',
' AddType text/html .gz',
' AddEncoding gzip .gz',
' AddDefaultCharset UTF-8',
'',
'',
' Header set Vary "Accept-Encoding, Cookie"',
' Header set Cache-Control "no-cache, no-store, must-revalidate"',
'',
'',
)
);
@file_put_contents( $cache_htaccess, $ht_rules ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@file_put_contents( $cache_dir . 'index.php', " 0 ? ( $ttl_hours * HOUR_IN_SECONDS ) : 0;
$exclude_urls = array();
if ( ! empty( $options['optCacheExcludeUrls'] ) ) {
foreach ( preg_split( '/[\r\n]+/', (string) $options['optCacheExcludeUrls'] ) as $u ) {
$u = trim( $u );
if ( $u !== '' ) {
$exclude_urls[] = $u;
}
}
}
$exclude_cookies = array();
if ( ! empty( $options['optCacheExcludeCookies'] ) ) {
foreach ( preg_split( '/[\r\n]+/', (string) $options['optCacheExcludeCookies'] ) as $c ) {
$c = trim( $c );
if ( $c !== '' ) {
$exclude_cookies[] = $c;
}
}
}
$runtime = array(
'enabled' => (bool) $cache_enabled,
'cache_dir' => $cache_dir,
'expected_host' => strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ),
'ttl' => (int) $ttl_secs,
'separate_mobile' => ! empty( $options['optCacheSeparateMobile'] ),
'ignore_tracking' => ! isset( $options['optCacheIgnoreTracking'] ) || ! empty( $options['optCacheIgnoreTracking'] ),
'exclude_urls' => $exclude_urls,
'exclude_cookies' => $exclude_cookies,
);
$cfg_file = $cache_dir . 'cache-config.php';
$export = " 100 ) {
header( 'Content-Type: image/webp' );
header( 'Content-Length: ' . filesize( \$webp_cand ) );
header( 'Cache-Control: public, max-age=31536000, immutable' );
header( 'X-AIO-WebP: HIT-DROPIN' );
readfile( \$webp_cand );
exit;
}
}
if ( ! is_file( \$abs_file ) ) {
http_response_code( 404 );
header( 'Content-Type: text/plain; charset=UTF-8' );
header( 'Cache-Control: public, max-age=60' );
header( 'X-AIO-Static-404: 1' );
echo '404 Not Found';
exit;
}
}
return;
}
// Inspect query parameters (optionally ignore marketing/analytics tracking params)
if ( ! empty( \$_GET ) ) {
if ( empty( \$cfg['ignore_tracking'] ) ) {
return;
}
\$ignored = array(
'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id',
'fbclid', 'gclid', 'dclid', 'msclkid', 'ttclid', 'twclid', 'yclid', 'zarsrc',
'_ga', '_gl', 'mc_cid', 'mc_eid',
);
foreach ( array_keys( \$_GET ) as \$qkey ) {
if ( ! in_array( strtolower( (string) \$qkey ), \$ignored, true ) ) {
return;
}
}
}
// Check login, WooCommerce cart, password-protected post, and custom excluded cookies
if ( ! empty( \$_COOKIE ) ) {
\$cookie_prefixes = array(
'wordpress_logged_in_',
'wp-postpass_',
'woocommerce_items_in_cart',
'wp_woocommerce_session_',
'aio_maint_bypass',
'comment_author_',
);
if ( ! empty( \$cfg['exclude_cookies'] ) && is_array( \$cfg['exclude_cookies'] ) ) {
\$cookie_prefixes = array_merge( \$cookie_prefixes, \$cfg['exclude_cookies'] );
}
foreach ( array_keys( \$_COOKIE ) as \$ckey ) {
foreach ( \$cookie_prefixes as \$pref ) {
if ( \$pref !== '' && stripos( (string) \$ckey, \$pref ) !== false ) {
return;
}
}
}
}
if ( preg_match( '/\.(?:xml|txt|json|php|ico|css|js|jpe?g|png|gif|webp|avif|svg|woff2?|ttf|otf|eot)$/i', \$uri_path ) ) {
return;
}
if ( preg_match( '#/wp-(?:admin|login|json|cron)#i', \$uri_path ) ) {
return;
}
if ( ! empty( \$cfg['exclude_urls'] ) && is_array( \$cfg['exclude_urls'] ) ) {
foreach ( \$cfg['exclude_urls'] as \$ex_pat ) {
if ( \$ex_pat !== '' && stripos( \$uri_path, \$ex_pat ) !== false ) {
return;
}
}
}
\$segments = array_filter( explode( '/', trim( \$uri_path, '/' ) ), 'strlen' );
\$safe_segments = array();
foreach ( \$segments as \$seg ) {
\$clean = preg_replace( '/[^A-Za-z0-9._~%-]/', '_', \$seg );
if ( \$clean === '' || \$clean === '.' || \$clean === '..' ) {
return;
}
\$safe_segments[] = substr( \$clean, 0, 120 );
}
\$is_https = ( ! empty( \$_SERVER['HTTPS'] ) && strtolower( (string) \$_SERVER['HTTPS'] ) !== 'off' )
|| ( isset( \$_SERVER['SERVER_PORT'] ) && (string) \$_SERVER['SERVER_PORT'] === '443' )
|| ( isset( \$_SERVER['HTTP_X_FORWARDED_PROTO'] ) && strtolower( (string) \$_SERVER['HTTP_X_FORWARDED_PROTO'] ) === 'https' );
\$is_mobile = false;
if ( ! empty( \$cfg['separate_mobile'] ) && ! empty( \$_SERVER['HTTP_USER_AGENT'] ) ) {
\$ua = (string) \$_SERVER['HTTP_USER_AGENT'];
\$is_mobile = (bool) preg_match( '/Mobile|Android|Silk\/|Kindle|BlackBerry|Opera (?:Mini|Mobi)|iPhone|iPod/i', \$ua );
}
\$scheme_prefix = \$is_https ? 'https-' : 'http-';
\$sub_path = ! empty( \$safe_segments ) ? implode( '/', \$safe_segments ) . '/' : '';
\$file_name = \$is_mobile ? 'index-mobile.html' : 'index.html';
\$cache_dir = str_replace( '\\\\', '/', \$cfg['cache_dir'] );
\$cache_file = \$cache_dir . \$scheme_prefix . \$cfg['expected_host'] . '/' . \$sub_path . \$file_name;
if ( strpos( \$cache_file, \$cache_dir ) !== 0 || ! is_file( \$cache_file ) ) {
return;
}
\$ttl = isset( \$cfg['ttl'] ) ? (int) \$cfg['ttl'] : 86400;
if ( \$ttl > 0 && ( time() - (int) filemtime( \$cache_file ) ) > \$ttl ) {
return;
}
\$accept_enc = isset( \$_SERVER['HTTP_ACCEPT_ENCODING'] ) ? strtolower( (string) \$_SERVER['HTTP_ACCEPT_ENCODING'] ) : '';
\$gz_file = \$cache_file . '.gz';
\$can_gzip = ( strpos( \$accept_enc, 'gzip' ) !== false ) && ! ini_get( 'zlib.output_compression' ) && is_file( \$gz_file );
header( 'Content-Type: text/html; charset=UTF-8' );
header( 'Vary: Accept-Encoding' );
if ( \$can_gzip ) {
header( 'Content-Encoding: gzip' );
header( 'Content-Length: ' . filesize( \$gz_file ) );
header( 'X-AIO-Cache: HIT-DROPIN-GZIP' );
readfile( \$gz_file );
exit;
}
header( 'X-AIO-Cache: HIT-DROPIN' );
readfile( \$cache_file );
exit;
} )();
PHP;
}
}