: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Writes temporary files for attachment and uploading
class NF_Exports_TempFileWriter {
* Content to be written to file
* Can send a single string or an array of stringed content
* Directory of final file location
* Temp file name at time of upload, before renaming
protected $basename = [];
* Full file name with path as attached to email
protected $attachmentFilename = [];
protected $fileinfo = [];
* Construct with the content to be written
public function __construct($content) {
if (is_array($content)) {
$this->content = $content;
$this->content = [$content];
// set upload director to /uploads
$this->uploadDir = $dir['path'];
* Write files to temporary location
* @return NfScheduledSubmissionExports\Storage\TempFileWriter
public function writeFiles() {
* Returns array of temp filenames, first file name if single
public function getFileInfo(bool $single = false) {
$this->constructFileInfo();
if ($single && isset($this->fileinfo[0])) {
$return = $this->fileinfo[0];
$return = $this->fileinfo;
public function getAttachmentNames(bool $single = false) {
if ($single && !empty($this->attachmentFilename)) {
$arrayKeys = array_keys($this->attachmentFilename);
$return = $this->attachmentFilename[$arrayKeys[0]];
$return = $this->attachmentFilename;
protected function constructFileInfo() {
foreach ($this->attachmentFilename as $index => $filename) {
$this->fileinfo[$index] = array_merge(pathinfo($filename), wp_upload_dir());
* Generate the FileInfo for a given filename
* @param string $filename
public static function generateFileInfo($filename) {
$array= array_merge(pathinfo($filename), wp_upload_dir());
$array['attachmentName']=$filename;
* Write contents to temporary file location
protected function writeTempFile() {
$path = trailingslashit($this->uploadDir);
foreach (array_keys($this->content) as $index) {
$tempFilename = tempnam($path, 'Sub');
$pathinfo = pathinfo($tempFilename);
$this->dir[$index] = $pathinfo['dirname'];
$this->basename[$index] = $pathinfo['basename'];
$this->tempFile[$index] = fopen($tempFilename, 'r+');
fwrite($this->tempFile[$index], $this->content[$index]);
fclose($this->tempFile[$index]);
* Rename temp file to permanent file name
* @param string $filename
protected function renameFile() {
$filename = apply_filters('ninja_forms_submission_csv_name', 'ninja-forms-submission');
foreach (array_keys($this->content) as $index) {
// remove a file if it already exists
if (file_exists($this->dir[$index] . '/' . $filename . "_$index.csv")) {
unlink($this->dir[$index] . '/' . $filename . "_$index.csv");
$this->attachmentFilename[$index] = $this->dir[$index] . '/' . $filename . "_$index.csv";
rename($this->dir[$index] . '/' . $this->basename[$index], $this->attachmentFilename[$index]);
* Delete file from directory after email with attachment has been sent
public function dropAttachmentFiles() {
foreach (array_keys($this->attachmentFilename) as $index) {
// remove a file if it already exists
self::dropAttachmentFile($this->attachmentFilename[$index]);
* Drop (delete) a given filename
* @param string $filename
public static function dropAttachmentFile($filename) {
if (file_exists($filename)) {