_cache_dir = ( isset( $config['cache_dir'] ) ? trim( $config['cache_dir'] ) : 'cache' ); $this->_clean_timelimit = ( isset( $config['clean_timelimit'] ) ? (int) $config['clean_timelimit'] : 180 ); $this->_exclude = ( isset( $config['exclude'] ) ? (array) $config['exclude'] : array() ); } /** * Run clean operation * * @return void */ public function clean() { @set_time_limit( $this->_clean_timelimit ); $this->_clean( $this->_cache_dir, false ); } /** * Run clean operation * * @return void */ public function clean_before() { @set_time_limit( $this->_clean_timelimit ); $this->_clean( $this->_cache_dir, false ); } /** * Clean * * @param string $path Path. * @param bool $remove Remove flag. * * @return void */ public function _clean( $path, $remove = true ) { $dir = @opendir( $path ); if ( $dir ) { while ( ( $entry = @readdir( $dir ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition if ( '.' === $entry || '..' === $entry ) { continue; } foreach ( $this->_exclude as $mask ) { if ( fnmatch( $mask, basename( $entry ) ) ) { continue 2; } } $full_path = $path . DIRECTORY_SEPARATOR . $entry; if ( @is_dir( $full_path ) ) { $this->_clean( $full_path ); } elseif ( ! $this->is_valid( $full_path ) ) { @unlink( $full_path ); } } @closedir( $dir ); if ( $remove ) { @rmdir( $path ); } } } /** * Check if file is valid * * @param string $file File. * * @return bool */ public function is_valid( $file ) { $valid = false; if ( file_exists( $file ) ) { $fp = @fopen( $file, 'rb' ); if ( $fp ) { $expires = @fread( $fp, 4 ); if ( false !== $expires ) { list( , $expires_at ) = @unpack( 'L', $expires ); $valid = ( time() < $expires_at ); } @fclose( $fp ); } } return $valid; } }