r_path Path to the current cache directory. * @return void */ private function maybe_create_nginx_mobile_file( $cache_dir_path ) { global $is_nginx; if ( ! $this->config->get_config( 'do_caching_mobile_files' ) ) { return; } if ( ! $is_nginx ) { return; } $nginx_mobile_detect = $cache_dir_path . '/.mobile-active'; if ( rocket_direct_filesystem()->exists( $nginx_mobile_detect ) ) { return; } rocket_direct_filesystem()->touch( $nginx_mobile_detect ); } /** * Create a hidden empty file when webp is enabled but the buffer doesn’t contain webp files. * * @since 3.4 * * @param string $cache_dir_path Path to the current cache directory (without trailing slah). */ private function maybe_create_nowebp_file( $cache_dir_path ) { $nowebp_filepath = $cache_dir_path . DIRECTORY_SEPARATOR . '.no-webp'; if ( rocket_direct_filesystem()->exists( $nowebp_filepath ) ) { return; } rocket_direct_filesystem()->touch( $nowebp_filepath ); } /** * Tell if generating cache files is allowed. * * @since 3.3 * * @return bool */ public static function can_generate_caching_files() { /** * Allow to the generate the caching file. * * @since 2.5 * * @param bool True will force the cache file generation. */ return (bool) apply_filters( 'do_rocket_generate_caching_files', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } /** * Gets the base cache path for the current request * * @since 3.3 * * @param array $cookies Cookies for the current request. * @return string */ private function get_request_cache_path( $cookies ) { $host = $this->config->get_host(); if ( $this->config->get_config( 'url_no_dots' ) ) { $host = str_replace( '.', '_', $host ); } $request_uri = $this->tests->get_clean_request_uri(); $cookie_hash = $this->config->get_config( 'cookie_hash' ); $logged_in_cookie = $this->config->get_config( 'logged_in_cookie' ); $logged_in_cookie_no_hash = str_replace( $cookie_hash, '', $logged_in_cookie ); // Get cache folder of host name. if ( $logged_in_cookie && isset( $cookies[ $logged_in_cookie ] ) && ! $this->tests->has_rejected_cookie( $logged_in_cookie_no_hash ) ) { if ( $this->config->get_config( 'common_cache_logged_users' ) ) { return $this->cache_dir_path . $host . '-loggedin-' . $this->config->get_config( 'secret_cache_key' ) . rtrim( $request_uri, '/' ); } $user_key = explode( '|', $cookies[ $logged_in_cookie ] ); $user_key = reset( $user_key ); $user_key = $user_key . '-' . $this->config->get_config( 'secret_cache_key' ); // Get cache folder of host name. return $this->cache_dir_path . $host . '-' . $user_key . rtrim( $request_uri, '/' ); } return $this->cache_dir_path . $host . rtrim( $request_uri, '/' ); } /** * Modifies the filename if the request is from a mobile device. * * @since 3.3 * * @param string $filename Cache filename. * @return string */ private function maybe_mobile_filename( $filename ) { $cache_mobile_files_tablet = $this->config->get_config( 'cache_mobile_files_tablet' ); if ( ! ( $this->config->get_config( 'cache_mobile' ) && $this->config->get_config( 'do_caching_mobile_files' ) ) ) { return $filename; } if ( ! $cache_mobile_files_tablet ) { return $filename; } if ( ! class_exists( 'WP_Rocket_Mobile_Detect' ) ) { return $filename; } $detect = new \WP_Rocket_Mobile_Detect(); if ( $detect->isMobile() && ! $detect->isTablet() && 'desktop' === $cache_mobile_files_tablet || ( $detect->isMobile() || $detect->isTablet() ) && 'mobile' === $cache_mobile_files_tablet ) { return $filename .= '-mobile'; } return $filename; } /** * Modifies the filename if the request is WebP compatible * * @since 3.4 * * @param string $filename Cache filename. * @return string */ private function maybe_webp_filename( $filename ) { if ( ! $this->config->get_config( 'cache_webp' ) ) { return $filename; } /** * Force WP Rocket to disable its webp cache. * * @since 3.4 * * @param bool $disable_webp_cache Set to true to disable the webp cache. */ $disable_webp_cache = apply_filters( 'rocket_disable_webp_cache', false ); if ( $disable_webp_cache ) { return $filename; } $http_accept = $this->config->get_server_input( 'HTTP_ACCEPT', '' ); if ( ! $http_accept && function_exists( 'apache_request_headers' ) ) { $headers = apache_request_headers(); $http_accept = isset( $headers['Accept'] ) ? $headers['Accept'] : ''; } if ( ! $http_accept || false === strpos( $http_accept, 'webp' ) ) { if ( preg_match( '#Firefox/(?[0-9]{2})#i', $this->config->get_server_input( 'HTTP_USER_AGENT' ), $matches ) ) { if ( 66 <= (int) $matches['version'] ) { return $filename . '-webp'; } } return $filename; } return $filename . '-webp'; } /** * Modifies the filename if dynamic cookies are set * * @param string $filename Cache filename. * @param array $cookies Cookies for the request. * @return string */ private function maybe_dynamic_cookies_filename( $filename, $cookies ) { $cache_dynamic_cookies = $this->config->get_config( 'cache_dynamic_cookies' ); if ( ! $cache_dynamic_cookies ) { return $filename; } foreach ( $cache_dynamic_cookies as $key => $cookie_name ) { if ( is_array( $cookie_name ) ) { if ( isset( $_COOKIE[ $key ] ) ) { foreach ( $cookie_name as $cookie_key ) { if ( '' !== $cookies[ $key ][ $cookie_key ] ) { $cache_key = $cookies[ $key ][ $cookie_key ]; $cache_key = preg_replace( '/[^a-z0-9_\-]/i', '-', $cache_key ); $filename .= '-' . $cache_key; } } } continue; } if ( isset( $cookies[ $cookie_name ] ) && '' !== $cookies[ $cookie_name ] ) { $cache_key = $cookies[ $cookie_name ]; $cache_key = preg_replace( '/[^a-z0-9_\-]/i', '-', $cache_key ); $filename .= '-' . $cache_key; } } return $filename; } /** * Force lowercase on encoded url strings from different alphabets to prevent issues on some hostings. * * @since 3.3 * * @param array $matches Cache path. * @return string Cache path in lowercase. */ protected function reset_lowercase( $matches ) { return strtolower( $matches[0] ); } }