http_response_code(), 'headers' => $headers, 'created' => time(), 'expires' => time() + $ttl, 'flags' => array_unique( flag() ), 'path' => $key['path'], ]; $meta = json_encode( $meta ); $cache_key = md5( json_encode( $key ) ); $level = substr( $cache_key, -2 ); if ( ! wp_mkdir_p( CACHE_DIR . "/{$level}/" ) ) { return $contents; } // Open a new cache file. $hash = wp_generate_password( 6, false ); $f = fopen( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php", 'xb' ); // Could not create file. if ( false === $f ) { return $contents; } fwrite( $f, '' ); fwrite( $f, pack( 'L', strlen( $meta ) ) ); fwrite( $f, $meta ); fwrite( $f, $contents ); // Close the file. fclose( $f ); // Atomic (hopefully) rename. if ( ! rename( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php", CACHE_DIR . "/{$level}/{$cache_key}.php" ) ) { unlink( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php" ); } return $contents; }; // Attach to main output buffer. ob_start( $ob_callback );