\nRequire all denied\n\nOptions -Indexes\n" ); } } public static function hosts() { $hosts = array(); foreach ( array( home_url(), site_url() ) as $url ) { $p = wp_parse_url( $url ); if ( empty( $p['host'] ) ) { continue; } $h = strtolower( $p['host'] ) . ( ! empty( $p['port'] ) ? ':' . $p['port'] : '' ); $hosts[] = $h; if ( 0 === strpos( $h, 'www.' ) ) { $hosts[] = substr( $h, 4 ); } else { $hosts[] = 'www.' . $h; } } return array_values( array_unique( $hosts ) ); } public static function build_config() { $edge = (bool) RavenCache_Settings::get( 'edge_headers' ); $cc = $edge ? 'public, max-age=0, s-maxage=' . ( (int) RavenCache_Settings::get( 'edge_ttl' ) * HOUR_IN_SECONDS ) . ', stale-while-revalidate=86400, stale-if-error=86400' : 'max-age=0, must-revalidate'; return array( 'enabled' => (bool) RavenCache_Settings::get( 'page_cache' ), 'ttl' => (int) RavenCache_Settings::get( 'cache_ttl' ) * HOUR_IN_SECONDS, 'gzip' => (bool) RavenCache_Settings::get( 'cache_gzip' ), 'mobile' => (bool) RavenCache_Settings::get( 'cache_mobile' ), 'hosts' => self::hosts(), 'ignore_params' => RavenCache_Settings::lines( 'cache_query_ignore' ), 'allow_params' => RavenCache_Settings::lines( 'cache_query_allow' ), 'exclude_urls' => RavenCache_Settings::lines( 'cache_exclude_urls' ), 'exclude_cookies' => RavenCache_Settings::lines( 'cache_exclude_cookies' ), 'exclude_agents' => RavenCache_Settings::lines( 'cache_exclude_agents' ), 'charset' => get_bloginfo( 'charset' ), 'cache_control' => $cc, ); } public static function write_config() { self::ensure_dirs(); if ( ! is_dir( RAVENCACHE_CACHE_DIR ) ) { return false; } $code = wp_json_encode( self::build_config() ); if ( ! is_string( $code ) ) { return false; } $tmp = RAVENCACHE_CACHE_DIR . '/config.' . wp_generate_password( 6, false ) . '.tmp'; if ( false === @file_put_contents( $tmp, $code ) ) { return false; } $ok = RavenCache_Fs::move( $tmp, RAVENCACHE_CACHE_DIR . '/config.json' ); if ( ! $ok ) { RavenCache_Fs::delete( $tmp ); } if ( $ok && is_file( RAVENCACHE_CACHE_DIR . '/config.php' ) ) { RavenCache_Fs::delete( RAVENCACHE_CACHE_DIR . '/config.php' ); // Leftover from versions that stored the settings as a PHP file. } return $ok; } /* ---------- advanced-cache.php ---------- */ public static function dropin_path() { return WP_CONTENT_DIR . '/advanced-cache.php'; } public static function dropin_is_ours() { $f = self::dropin_path(); if ( ! is_file( $f ) ) { return false; } $head = (string) @file_get_contents( $f, false, null, 0, 600 ); return false !== strpos( $head, 'RAVENCACHE_DROPIN_V1' ); } public static function dropin_foreign() { return is_file( self::dropin_path() ) && ! self::dropin_is_ours(); } public static function install_dropin() { if ( self::dropin_foreign() ) { return false; // Inna wtyczka cache – nie nadpisujemy. } $src = RAVENCACHE_DIR . 'includes/dropin.php'; $dst = self::dropin_path(); if ( is_file( $dst ) && md5_file( $src ) === md5_file( $dst ) ) { return true; } $ok = @copy( $src, $dst ); if ( $ok && function_exists( 'opcache_invalidate' ) ) { @opcache_invalidate( $dst, true ); } return $ok; } public static function remove_dropin() { if ( self::dropin_is_ours() ) { RavenCache_Fs::delete( self::dropin_path() ); } } public static function wp_config_path() { if ( file_exists( ABSPATH . 'wp-config.php' ) ) { return ABSPATH . 'wp-config.php'; } if ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { return dirname( ABSPATH ) . '/wp-config.php'; } return false; } /** Dodaje/usuwa define('WP_CACHE', true) z wp-config.php. */ public static function set_wp_cache( $on ) { $path = self::wp_config_path(); if ( ! $path || ! wp_is_writable( $path ) ) { return false; } $src = (string) file_get_contents( $path ); $tag = '// Added by RavenCache'; if ( ! $on ) { $new = preg_replace( '/^[ \t]*define\(\s*[\'"]WP_CACHE[\'"]\s*,\s*true\s*\);[ \t]*' . preg_quote( $tag, '/' ) . '[ \t]*\r?\n/m', '', $src ); if ( null !== $new && $new !== $src ) { file_put_contents( $path, $new ); } return true; } if ( preg_match( '/define\(\s*[\'"]WP_CACHE[\'"]\s*,\s*true\s*\)/i', $src ) ) { return true; // Już włączone (przez nas lub hosting). } if ( preg_match( '/define\(\s*[\'"]WP_CACHE[\'"]\s*,\s*false\s*\)/i', $src ) ) { $new = preg_replace( '/define\(\s*[\'"]WP_CACHE[\'"]\s*,\s*false\s*\)\s*;/i', "define( 'WP_CACHE', true ); " . $tag, $src, 1 ); } elseif ( preg_match( '/^<\?php\s*/', $src ) ) { $new = preg_replace( '/^<\?php[ \t]*\r?\n?/', "', ' AddOutputFilterByType DEFLATE ' . $types, '', '', ' AddType image/webp .webp', ' AddType image/avif .avif', ' AddType font/woff2 .woff2', '', '', ' ', ' Header set Cache-Control "public, max-age=' . $font . ', immutable"', ' ', ' ', ' Header set Cache-Control "public, max-age=' . $img . '"', ' ', ' ', ' Header set Cache-Control "public, max-age=' . $css_js . '"', ' ', '', '', ' ', ' ExpiresActive On', ' ExpiresByType image/jpeg "access plus 6 months"', ' ExpiresByType image/png "access plus 6 months"', ' ExpiresByType image/webp "access plus 6 months"', ' ExpiresByType image/svg+xml "access plus 6 months"', ' ExpiresByType font/woff2 "access plus 1 year"', ' ExpiresByType text/css "access plus ' . (int) RavenCache_Settings::get( 'static_days' ) . ' days"', ' ExpiresByType application/javascript "access plus ' . (int) RavenCache_Settings::get( 'static_days' ) . ' days"', ' ', '', 'FileETag MTime Size', ); return $lines; } public static function apply_htaccess() { if ( ! RavenCache_Settings::get( 'htaccess' ) ) { self::remove_htaccess(); return; } if ( ! function_exists( 'insert_with_markers' ) ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; } if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $file = self::htaccess_path(); if ( ( file_exists( $file ) && ! wp_is_writable( $file ) ) || ( ! file_exists( $file ) && ! wp_is_writable( dirname( $file ) ) ) ) { set_transient( 'ravencache_htaccess_fail', 'not-writable', DAY_IN_SECONDS ); return; } $backup = file_exists( $file ) ? (string) file_get_contents( $file ) : null; insert_with_markers( $file, self::MARKER, self::htaccess_rules() ); // Test bezpieczeństwa: strona nie może zacząć zwracać 500. $resp = wp_remote_get( home_url( '/' ), array( 'timeout' => 8, 'redirection' => 0, 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), 'headers' => array( 'X-RavenCache-Probe' => '1' ) ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- core WordPress filter. if ( ! is_wp_error( $resp ) && (int) wp_remote_retrieve_response_code( $resp ) >= 500 ) { if ( null === $backup ) { RavenCache_Fs::delete( $file ); } else { file_put_contents( $file, $backup ); } set_transient( 'ravencache_htaccess_fail', 'server-error', DAY_IN_SECONDS ); return; } delete_transient( 'ravencache_htaccess_fail' ); } public static function remove_htaccess() { if ( ! function_exists( 'insert_with_markers' ) ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; } if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $file = self::htaccess_path(); if ( ! file_exists( $file ) || ! wp_is_writable( $file ) ) { return; } $src = (string) file_get_contents( $file ); if ( false === strpos( $src, '# BEGIN ' . self::MARKER ) ) { return; } // Usuwamy cały blok razem ze znacznikami (insert_with_markers zostawiłby puste). $new = preg_replace( '/\R?# BEGIN ' . preg_quote( self::MARKER, '/' ) . '\R.*?# END ' . preg_quote( self::MARKER, '/' ) . '[ \t]*\R?/s', "\n", $src ); if ( null !== $new && $new !== $src ) { file_put_contents( $file, ltrim( $new, "\r\n" ) ); } } public static function nginx_snippet() { $css_js = (int) RavenCache_Settings::get( 'static_days' ); return "# RavenCache: suggested Nginx rules (add to the server {} block)\n" . "gzip on;\ngzip_comp_level 5;\ngzip_vary on;\ngzip_min_length 256;\n" . "gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml image/svg+xml font/ttf font/otf;\n\n" . "location ~* \\.(?:woff2?|ttf|otf|eot)\$ { expires 1y; add_header Cache-Control \"public, max-age=31536000, immutable\"; access_log off; }\n" . "location ~* \\.(?:jpe?g|png|gif|webp|avif|ico|svg|mp4|webm)\$ { expires 6M; add_header Cache-Control \"public, max-age=15552000\"; access_log off; }\n" . "location ~* \\.(?:css|js|mjs)\$ { expires {$css_js}d; add_header Cache-Control \"public, max-age=" . ( $css_js * 86400 ) . "\"; }\n\n" . "# Block access to the cache configuration:\nlocation ~* /wp-content/cache/ravencache/config\\.php\$ { deny all; }\n"; } }