Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/ninja-fo.../includes/Admin/Processe...
File: ExportSubmissions.php
<?php if (!defined('ABSPATH')) exit;
[0] Fix | Delete
[1] Fix | Delete
[2] Fix | Delete
use NinjaForms\Includes\Factories\SubmissionAggregateFactory;
[3] Fix | Delete
use NinjaForms\Includes\Factories\SubmissionFilterFactory;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Class NF_Abstracts_Batch_Process
[7] Fix | Delete
*/
[8] Fix | Delete
class NF_Admin_Processes_ExportSubmissions extends NF_Abstracts_BatchProcess
[9] Fix | Delete
{
[10] Fix | Delete
protected $_slug = 'export_submissions';
[11] Fix | Delete
protected $form = '';
[12] Fix | Delete
protected $subs_per_step = 25;
[13] Fix | Delete
protected $sub_count = 0;
[14] Fix | Delete
protected $format = 'csv';
[15] Fix | Delete
protected $offset = 0;
[16] Fix | Delete
protected $delimiter = ',';
[17] Fix | Delete
protected $enclosure = '"';
[18] Fix | Delete
protected $terminator = "\n";
[19] Fix | Delete
[20] Fix | Delete
protected $currentPosition = 0;
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Filepath of downloaded file in default directory
[24] Fix | Delete
*
[25] Fix | Delete
* @param array $forms
[26] Fix | Delete
*/
[27] Fix | Delete
protected $file_path = '';
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* File URL of downloaded file in default directory
[31] Fix | Delete
*
[32] Fix | Delete
* @param array $forms
[33] Fix | Delete
*/
[34] Fix | Delete
protected $fileUrl = '';
[35] Fix | Delete
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* @var NF_Exports_SubmissionCsvExport
[39] Fix | Delete
*/
[40] Fix | Delete
protected $csvObject;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Aggregated submission keys in output order
[44] Fix | Delete
*
[45] Fix | Delete
* @var array
[46] Fix | Delete
*/
[47] Fix | Delete
protected $indexedLookup;
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Override parent construct to pass form Ids
[51] Fix | Delete
*
[52] Fix | Delete
* @param array $forms
[53] Fix | Delete
*/
[54] Fix | Delete
public function __construct($form = '')
[55] Fix | Delete
{
[56] Fix | Delete
global $wpdb;
[57] Fix | Delete
[58] Fix | Delete
$this->form = $form;
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Set $_db to $wpdb.
[62] Fix | Delete
* This helps us by not requiring us to declare global $wpdb in every class method.
[63] Fix | Delete
*/
[64] Fix | Delete
$this->_db = $wpdb;
[65] Fix | Delete
[66] Fix | Delete
// Run init.
[67] Fix | Delete
$this->init();
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Function to run any setup steps necessary to begin processing.
[72] Fix | Delete
*/
[73] Fix | Delete
public function startup()
[74] Fix | Delete
{
[75] Fix | Delete
[76] Fix | Delete
//TODO: Verify what type of export this is (filter) later
[77] Fix | Delete
//TODO: Read in submission IDs later
[78] Fix | Delete
//TODO: Read in start/end date params later
[79] Fix | Delete
// Right now, we assume that we have a single form ID and that the export is always a csv file.
[80] Fix | Delete
[81] Fix | Delete
// Verify filterable values.
[82] Fix | Delete
$this->subs_per_step = apply_filters('ninja_forms_export_subs_per_step', $this->subs_per_step);
[83] Fix | Delete
$this->delimiter = apply_filters('nf_sub_csv_delimiter', $this->delimiter);
[84] Fix | Delete
$this->enclosure = apply_filters('nf_sub_csv_enclosure', $this->enclosure);
[85] Fix | Delete
$this->terminator = apply_filters('nf_sub_csv_terminator', $this->terminator);
[86] Fix | Delete
[87] Fix | Delete
// Construct a new submission aggregate.
[88] Fix | Delete
$params = (new SubmissionFilterFactory())->maybeLimitByLoggedInUser()->setNfFormIds([$this->form]);
[89] Fix | Delete
$params->setEndDate(time());
[90] Fix | Delete
$params->setStartDate(0);
[91] Fix | Delete
$params->setStatus(["active", "publish"]);
[92] Fix | Delete
[93] Fix | Delete
$submissionAggregateCsvAdapter = (new SubmissionAggregateFactory())->SubmissionAggregateCsvExportAdapter();
[94] Fix | Delete
$submissionAggregateCsvAdapter->submissionAggregate->filterSubmissions($params);
[95] Fix | Delete
[96] Fix | Delete
$this->csvObject = (new NF_Exports_SubmissionCsvExport())->setUseAdminLabels(true)->setSubmissionAggregateCsvExportAdapter($submissionAggregateCsvAdapter);
[97] Fix | Delete
$this->indexedLookup = $this->csvObject->reverseSubmissionOrder();
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
[101] Fix | Delete
//Get a count of how many submissions we're dealing with.
[102] Fix | Delete
$this->sub_count = $submissionAggregateCsvAdapter->submissionAggregate->getSubmissionCount();
[103] Fix | Delete
[104] Fix | Delete
// If there are no subs, bail.
[105] Fix | Delete
if (0 === $this->sub_count) {
[106] Fix | Delete
$this->add_error('no_submissions', esc_html__('No Submissions to export.', 'ninja-forms'), 'fatal');
[107] Fix | Delete
$this->batch_complete();
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
// Initialize our file.
[111] Fix | Delete
$this->file_path = $this->constructFilepath();
[112] Fix | Delete
// Use 'w' to delete the original file if one exists and replace it with a new one.
[113] Fix | Delete
if (!$file = fopen($this->file_path, 'w')) {
[114] Fix | Delete
$this->add_error('write_failure', esc_html__('Unable to write file.', 'ninja-forms'), 'fatal');
[115] Fix | Delete
$this->batch_complete();
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
// Add headers to the file.
[119] Fix | Delete
// We can only do this outside of the process method under the assumption that a single form ID is provided.
[120] Fix | Delete
$labels = $this->csvObject->getLabels();
[121] Fix | Delete
$glue = $this->enclosure . $this->delimiter . $this->enclosure;
[122] Fix | Delete
$constructed = $this->enclosure . implode($glue, $labels) . $this->enclosure . $this->terminator;
[123] Fix | Delete
fwrite($file, $constructed);
[124] Fix | Delete
fclose($file);
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Function to run any setup steps necessary to begin processing for steps after the first.
[129] Fix | Delete
*
[130] Fix | Delete
* @since 3.5.0
[131] Fix | Delete
* @return void
[132] Fix | Delete
*/
[133] Fix | Delete
public function restart()
[134] Fix | Delete
{
[135] Fix | Delete
//TODO: Read back in our unfinished data.
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Function to loop over the batch.
[140] Fix | Delete
*
[141] Fix | Delete
* @since 3.5.0
[142] Fix | Delete
* @return void
[143] Fix | Delete
*/
[144] Fix | Delete
public function process()
[145] Fix | Delete
{
[146] Fix | Delete
if($this->currentPosition >= $this->sub_count-1){
[147] Fix | Delete
$this->batch_complete();
[148] Fix | Delete
return;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
$this->writeBatch();
[152] Fix | Delete
[153] Fix | Delete
// Continue looping until end
[154] Fix | Delete
$this->process();
[155] Fix | Delete
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Delete temp file before calling parent method
[160] Fix | Delete
* @inheritDoc
[161] Fix | Delete
*/
[162] Fix | Delete
public function batch_complete( ): void
[163] Fix | Delete
{
[164] Fix | Delete
parent::batch_complete();
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
public function writeBatch( ): void
[168] Fix | Delete
{
[169] Fix | Delete
if (!$file = fopen($this->file_path, 'a')) {
[170] Fix | Delete
$this->add_error('write_failure', esc_html__('Unable to write file.', 'ninja-forms'), 'fatal');
[171] Fix | Delete
$this->batch_complete();
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
$glue = $this->enclosure . $this->delimiter . $this->enclosure;
[175] Fix | Delete
[176] Fix | Delete
// for each submission within the step
[177] Fix | Delete
for ($i = 0; $i < $this->subs_per_step; $i++) {
[178] Fix | Delete
if (!isset($this->indexedLookup[$this->currentPosition])) {
[179] Fix | Delete
continue;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
$aggregatedKey = $this->indexedLookup[$this->currentPosition];
[183] Fix | Delete
$row = $this->csvObject->constructRow($aggregatedKey);
[184] Fix | Delete
[185] Fix | Delete
//Catch reference to an array or repeated fieldsets of repeater field to display each entry as a row
[186] Fix | Delete
if( array_key_exists('repeater', $row) && is_array($row['repeater']) ){
[187] Fix | Delete
foreach($row['repeater'] as $eachRow){
[188] Fix | Delete
$constructed = $this->enclosure . implode($glue, $eachRow) . $this->enclosure . $this->terminator;
[189] Fix | Delete
fwrite($file, $constructed);
[190] Fix | Delete
}
[191] Fix | Delete
$this->currentPosition++;
[192] Fix | Delete
} else {
[193] Fix | Delete
$constructed = $this->enclosure . implode($glue, $row) . $this->enclosure . $this->terminator;
[194] Fix | Delete
fwrite($file, $constructed);
[195] Fix | Delete
[196] Fix | Delete
$this->currentPosition++;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
[200] Fix | Delete
}
[201] Fix | Delete
}
[202] Fix | Delete
/**
[203] Fix | Delete
* Method that encodes $this->response and sends the data to the front-end.
[204] Fix | Delete
*
[205] Fix | Delete
* @since 3.4.0
[206] Fix | Delete
* @updated 3.4.11
[207] Fix | Delete
* @return void
[208] Fix | Delete
*/
[209] Fix | Delete
public function respond()
[210] Fix | Delete
{
[211] Fix | Delete
if (!empty($this->response['errors'])) {
[212] Fix | Delete
$this->response['errors'] = array_unique($this->response['errors']);
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
return wp_json_encode($this->response);
[216] Fix | Delete
}
[217] Fix | Delete
/**
[218] Fix | Delete
* Function to cleanup any lingering temporary elements of a batch process after completion.
[219] Fix | Delete
*
[220] Fix | Delete
* @since 3.5.0
[221] Fix | Delete
* @return void
[222] Fix | Delete
*/
[223] Fix | Delete
public function cleanup()
[224] Fix | Delete
{
[225] Fix | Delete
//TODO: Get rid of our data option.
[226] Fix | Delete
/**
[227] Fix | Delete
* We shouldn't need to delete our csv file,
[228] Fix | Delete
* as that will be overwritten the next time this process is called.
[229] Fix | Delete
*/
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Get Steps
[234] Fix | Delete
* Determines the amount of steps needed for the step processors.
[235] Fix | Delete
*
[236] Fix | Delete
* @since 3.5.0
[237] Fix | Delete
* @return int of the number of steps.
[238] Fix | Delete
*/
[239] Fix | Delete
public function get_steps()
[240] Fix | Delete
{
[241] Fix | Delete
//TODO: Refactor this when multiple form IDs are introduced.
[242] Fix | Delete
[243] Fix | Delete
// Ensure we convent our numbers from int to float to ensure that ceil works.
[244] Fix | Delete
// Get the amount of steps and return.
[245] Fix | Delete
$steps = ceil(floatval($this->sub_count) / floatval($this->subs_per_step));
[246] Fix | Delete
return $steps;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* Get the filepath of our constructed csv.
[251] Fix | Delete
*
[252] Fix | Delete
* @return string
[253] Fix | Delete
*/
[254] Fix | Delete
protected function constructFilepath()
[255] Fix | Delete
{
[256] Fix | Delete
$filename = time() . base64_encode( 'form-' . $this->form . '-all-subs' );
[257] Fix | Delete
$upload_dir = wp_upload_dir();
[258] Fix | Delete
if (!file_exists($upload_dir['basedir'] . '/ninja-forms-tmp')) {
[259] Fix | Delete
mkdir($upload_dir['basedir'] . '/ninja-forms-tmp', 0777, true);
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$file_path = trailingslashit($upload_dir['basedir']) . 'ninja-forms-tmp/' . $filename . '.' . $this->format;
[263] Fix | Delete
$this->fileUrl = trailingslashit($upload_dir['baseurl']) . 'ninja-forms-tmp/' . $filename . '.' . $this->format;
[264] Fix | Delete
return $file_path;
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
/**
[268] Fix | Delete
* Overwrites the default flag method to use user options
[269] Fix | Delete
* instead of using the default options.
[270] Fix | Delete
*
[271] Fix | Delete
* @since 3.5.0
[272] Fix | Delete
* @param $flag (String) The flag to check
[273] Fix | Delete
* @param $action (String) The type of interaction to be performed
[274] Fix | Delete
* @return Mixed
[275] Fix | Delete
*/
[276] Fix | Delete
public function flag($flag, $action)
[277] Fix | Delete
{
[278] Fix | Delete
switch ($action) {
[279] Fix | Delete
case 'add':
[280] Fix | Delete
return update_user_option(get_current_user_id(), $flag, true);
[281] Fix | Delete
case 'remove':
[282] Fix | Delete
return delete_user_option(get_current_user_id(), $flag);
[283] Fix | Delete
default:
[284] Fix | Delete
// Default to 'check'.
[285] Fix | Delete
return get_user_option(get_current_user_id(), $flag);
[286] Fix | Delete
}
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* Get file URL of downloaded file in default directory
[291] Fix | Delete
*/
[292] Fix | Delete
public function getFileUrl(): string
[293] Fix | Delete
{
[294] Fix | Delete
return $this->fileUrl;
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Get filepath of downloaded file in default directory
[299] Fix | Delete
*/
[300] Fix | Delete
public function getFilePath(): string
[301] Fix | Delete
{
[302] Fix | Delete
return $this->file_path;
[303] Fix | Delete
}
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function