Skip to content

Commit

Permalink
#2814 schedule deletes in cf2 fiel fields and cleanup that handler a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Nov 21, 2018
1 parent 4802c92 commit 0db2fb0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
13 changes: 10 additions & 3 deletions cf2/Fields/Handlers/Cf1FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ public function upload($file, array $args = array())
return \Caldera_Forms_Files::upload($file,$args);
}

public function addFilter($fieldId, $formId, $private)
/** @inheritdoc */
public function addFilter($fieldId, $formId, $private,$transientId= null )
{
\Caldera_Forms_Files::add_upload_filter($fieldId,$formId,$private);
\Caldera_Forms_Files::add_upload_filter($fieldId,$formId,$private,$transientId);
}

/** @inheritdoc */
public function removeFilter()
{
\Caldera_Forms_Files::remove_upload_filter();
}

/** @inheritdoc */
public function scheduleFileDelete($fieldId,$formId,$file)
{
return \Caldera_Forms_Files::schedule_delete($fieldId,$formId,$file);
}
}
32 changes: 20 additions & 12 deletions cf2/Fields/Handlers/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,45 @@ public function processFiles(array $files, array $hashes)
$uploads = [];
$i = 0;
foreach ($files as $file) {
$isPrivate = \Caldera_Forms_Files::is_private($this->field);
if (!$this->isAllowedType($file)) {
throw new Exception(__('This file type is not allowed. Please try another.', 'caldera-forms'), 415);
}

$expected = $hashes[$i];
$actual = md5_file($file['tmp_name']);
$hashFromRequest = $hashes[$i];
$actualHash = md5_file($file['tmp_name']);

if ($expected !== $actual ) {
if (! hash_equals($actualHash, $hashFromRequest) ) {
throw new Exception(__( 'Content hash did not match expected.' ), 412 );
}

$this->uploader
$isPrivate = \Caldera_Forms_Files::is_private($this->field);

$this->uploader
->addFilter(
$this->field['ID'],
$this->form['ID'],
$isPrivate
$isPrivate,
$actualHash
);

if (!$this->isAllowedType($file)) {
throw new Exception(__('This file type is not allowed. Please try another.', 'caldera-forms'), 415);
}

require_once(ABSPATH . 'wp-admin/includes/file.php');
$upload = wp_handle_upload($file, array('test_form' => false, 'action' => 'foo'));


$this->uploader->removeFilter();
if (\Caldera_Forms_Files::should_attach($this->field, $this->form)) {
\Caldera_Forms_Files::add_to_media_library($upload, $this->field);
}

if( $isPrivate ){
$this->uploader->scheduleFileDelete(
$this->field['ID'],
$this->form['ID'],
$upload['file']
);
}


$uploads[] = $upload['url'];
$uploads[] = $upload['url'];
$i++;

}
Expand Down
15 changes: 14 additions & 1 deletion cf2/Fields/Handlers/UploaderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function upload($file, array $args = array());
* @param boolean $private
* @return void
*/
public function addFilter($fieldId, $formId, $private );
public function addFilter($fieldId, $formId, $private, $transientId);

/**
* Remove upload related filters
Expand All @@ -38,4 +38,17 @@ public function addFilter($fieldId, $formId, $private );
* @return void
*/
public function removeFilter();

/**
* Schedule file to be deleted as soon as possible
*
* @since 1.8.0
*
* @param string $fieldId ID of field
* @param string $formId ID of form
* @param string $file Path to file to delete.
*
* @return bool
*/
public function scheduleFileDelete($fieldId,$formId,$file);
}
36 changes: 27 additions & 9 deletions classes/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ public static function upload( $file, array $args = array() ){
self::remove_upload_filter();
}

if( $private ){
$form = Caldera_Forms_Forms::get_form($args[ 'form_id' ]);
if (is_array( $form ) ) {
$field = Caldera_Forms_Field_Util::get_field($args[ 'field_id' ], $form);
if (! self::is_persistent($field)) {
caldera_forms_schedule_job(new \calderawp\calderaforms\cf2\Jobs\DeleteFileJob($upload['file']));
}
}
}
self::schedule_delete($args, $private, $upload);

return $upload;

Expand Down Expand Up @@ -375,4 +367,30 @@ public static function types(){
);
}

/**
* Schedule file to be deleted as soon as possible
*
* @since 1.8.0
*
* @param string $field_id ID of field
* @param string $form_id ID of form
* @param string $file Path to file to delete.
*
* @return bool
*/
public static function schedule_delete($field_id, $form_id, $file )
{
$form = Caldera_Forms_Forms::get_form($form_id);
if ( is_array($form) ) {
$field = Caldera_Forms_Field_Util::get_field($field_id, $form);
if ( is_array($field) && !self::is_persistent($field) ) {
caldera_forms_schedule_job(new \calderawp\calderaforms\cf2\Jobs\DeleteFileJob($file));
return true;
}
}

return false;

}

}
4 changes: 2 additions & 2 deletions tests/Integration/Features/WpMailAttachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testDoesAttachOneFileIfItShould()
$data = $this->getTestData($file);

$mail = \Caldera_Forms_Email_Filters::mail_attachment_check( $mail, $data, $form );
$this->assertEquals( ['/tmp/small-cat.jpeg'],$mail['attachments'] );
$this->assertEquals( ['/tmp/cats/small-cat.jpeg'],$mail['attachments'] );

}

Expand Down Expand Up @@ -76,7 +76,7 @@ public function testDoesAttachTwoFileIfItShould()
];

$mail = \Caldera_Forms_Email_Filters::mail_attachment_check( $mail, $data, $form );
$this->assertEquals( ['/tmp/small-cat.jpeg', '/tmp/tiny-cat.jpeg'],$mail['attachments'] );
$this->assertEquals( ['/tmp/cats/small-cat.jpeg', '/tmp/cats/tiny-cat.jpeg'],$mail['attachments'] );

}

Expand Down
7 changes: 6 additions & 1 deletion tests/Util/Mocks/MockUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ public function removeFilter()
// TODO: Implement removeFilter() method.
}

public function addFilter($fieldId, $formId, $private)
public function addFilter($fieldId, $formId, $private,$controlCode = null)
{
// TODO: Implement addFilter() method.
}

public function scheduleFileDelete($fieldId, $formId, $file)
{
// TODO: Implement scheduleFileDelete() method.
}
}

0 comments on commit 0db2fb0

Please sign in to comment.