file. * * @param string $w3tc_key An MD5 of the DB query. * @param string $w3tc_group Cache group. * @param string $mode File mode. For example: 'wb' for write binary. * * @return resource|false File pointer on success, false on failure. */ private function fopen_write( $w3tc_key, $w3tc_group, $mode ) { // Get the storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_$w3tc_key"). $storage_key = $this->get_item_key( $w3tc_key ); // Get the subpath for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}). $sub_path = $this->_get_path( $storage_key, $w3tc_group ); // Ge the entire path of the cache file. $path = $this->_cache_dir . DIRECTORY_SEPARATOR . $sub_path; // Create the directory if it does not exist. $dir = dirname( $path ); if ( ! @is_dir( $dir ) ) { if ( ! Util_File::mkdir_from( $dir, dirname( W3TC_CACHE_DIR ) ) ) { return false; } } // Open the cache file for writing. return @fopen( $path, $mode ); } /** * Flushes cache files matching a specific regex pattern. * * This method scans a directory and removes cache files that match the provided regular expression. Supports multisite setups. * * @since 2.7.1 * * @param string $regex The regular expression pattern to match file names. * * @return void */ private function _flush_based_on_regex( $regex ) { if ( Util_Environment::is_wpmu() && ! Util_Environment::is_wpmu_subdomain() ) { $domain = get_home_url(); $parsed = parse_url( $domain ); $host = $parsed['host']; $path = isset( $parsed['path'] ) ? '/' . trim( $parsed['path'], '/' ) : ''; $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . DIRECTORY_SEPARATOR . $host . $path; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . DIRECTORY_SEPARATOR . Util_Environment::host(); } $dir = @opendir( $flush_dir ); if ( $dir ) { $w3tc_entry = @readdir( $dir ); while ( false !== $w3tc_entry ) { if ( '.' === $w3tc_entry || '..' === $w3tc_entry ) { $w3tc_entry = @readdir( $dir ); continue; } if ( preg_match( '~' . $regex . '~', basename( $w3tc_entry ) ) ) { Util_File::rmdir( $flush_dir . DIRECTORY_SEPARATOR . $w3tc_entry ); } $w3tc_entry = @readdir( $dir ); } @closedir( $dir ); } } } file. * * @param string $w3tc_key An MD5 of the DB query. * @param string $w3tc_group Cache group. * @param string $mode File mode. For example: 'wb' for write binary. * * @return resource|false File pointer on success, false on failure. */ private function fopen_write( $w3tc_key, $w3tc_group, $mode ) { // Get the storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_$w3tc_key"). $storage_key = $this->get_item_key( $w3tc_key ); // Get the subpath for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}). $sub_path = $this->_get_path( $storage_key, $w3tc_group ); // Ge the entire path of the cache file. $path = $this->_cache_dir . DIRECTORY_SEPARATOR . $sub_path; // Create the directory if it does not exist. $dir = dirname( $path ); if ( ! @is_dir( $dir ) ) { if ( ! Util_File::mkdir_from( $dir, dirname( W3TC_CACHE_DIR ) ) ) { return false; } } // Open the cache file for writing. return @fopen( $path, $mode ); } /** * Flushes cache files matching a specific regex pattern. * * This method scans a directory and removes cache files that match the provided regular expression. Supports multisite setups. * * @since 2.7.1 * * @param string $regex The regular expression pattern to match file names. * * @return void */ private function _flush_based_on_regex( $regex ) { if ( Util_Environment::is_wpmu() && ! Util_Environment::is_wpmu_subdomain() ) { $domain = get_home_url(); $parsed = parse_url( $domain ); $host = $parsed['host']; $path = isset( $parsed['path'] ) ? '/' . trim( $parsed['path'], '/' ) : ''; $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . DIRECTORY_SEPARATOR . $host . $path; } else { $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . DIRECTORY_SEPARATOR . Util_Environment::host(); } $dir = @opendir( $flush_dir ); if ( $dir ) { $w3tc_entry = @readdir( $dir ); while ( false !== $w3tc_entry ) { if ( '.' === $w3tc_entry || '..' === $w3tc_entry ) { $w3tc_entry = @readdir( $dir ); continue; } if ( preg_match( '~' . $regex . '~', basename( $w3tc_entry ) ) ) { Util_File::rmdir( $flush_dir . DIRECTORY_SEPARATOR . $w3tc_entry ); } $w3tc_entry = @readdir( $dir ); } @closedir( $dir ); } } }