Skip to content

Commit

Permalink
Merge branch 'develop' into feature/2830 #2830
Browse files Browse the repository at this point in the history
* develop:
  During form submission, start buffer, then flush it. #2820 and #2448 and #2788
  Set window.cf2 if not an object #2821
  #2814 schedule deletes in cf2 fiel fields and cleanup that handler a bit
  #2814 schedule deletes
  #2814 handle deleting directory if empty
  Only try to delete file, if it exists #2814
  Test to prove that the delete file job will make an uncaught exception in some cases #2814
  Make new job for deleting tests work #2814
  New job for deleting files #2814 - failing tests
  Make new utility methods needed for #2814 and #2794 work
  Failing tests for utility methods needed for #2814  and #2794
  #2814 rm dead code and add inline docs to existing delete job

# Conflicts:
#	cf2/functions.php
  • Loading branch information
Josh Pollock committed Dec 14, 2018
2 parents cf52558 + 6f45245 commit e8dae2e
Show file tree
Hide file tree
Showing 1,773 changed files with 160,591 additions and 138 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);
}
80 changes: 80 additions & 0 deletions cf2/Jobs/DeleteFileJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php


namespace calderawp\calderaforms\cf2\Jobs;


/**
* Class DeleteFileJob
*
* Delete a file at a later time
*
* @since 1.8.0
*/
class DeleteFileJob extends Job
{

/**
* Path file is stored in
*
* @since 1.8.0
*
* @var string
*/
protected $path;

/**
* DeleteFileJob constructor.
*
* @since 1.8.0
*
* @param string $path Path file is stored in
*/
public function __construct($path)
{
$this->path = $path;
}

/** @inheritdoc */
public function handle()
{
if ( file_exists($this->path) ) {
unlink($this->path);
}

if( file_exists( $this->dirName() ) && $this->isEmptyDir() ){
rmdir(dirname($this->path));
}

}

/**
* Check if is empty directory
*
* @since 1.8.0
*
* @return bool
*/
protected function isEmptyDir()
{
foreach ( new \DirectoryIterator($this->dirName()) as $fileInfo ) {
if ( $fileInfo->isDot() ) {
continue;
};
return false;
}
return true;
}

/**
* Get name of directory file is in
*
* @since 1.8.0
*
* @return string
*/
protected function dirName()
{
return dirname($this->path);
}
}
17 changes: 16 additions & 1 deletion cf2/Jobs/DeleteTransientJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@
class DeleteTransientJob extends Job
{

/**
* ID of transient to delete
*
* @since 1.8.0
*
* @var string
*/
protected $transientId;

/**
* DeleteTransientJob constructor.
*
* @since 1.8.0
*
* @param $transientId
*/
public function __construct($transientId)
{
$this->transientId = $transientId;
}


/** @inheritdoc */
public function handle()
{
\Caldera_Forms_Transient::delete_transient($this->transientId);
Expand Down
20 changes: 0 additions & 20 deletions cf2/Jobs/XJob.php

This file was deleted.

25 changes: 22 additions & 3 deletions cf2/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
*
* @return \calderawp\calderaforms\cf2\CalderaFormsV2Contract
*/
function caldera_forms_get_v2_container(){
function caldera_forms_get_v2_container()
{

static $container;
if( ! $container ){
if ( !$container ) {
$container = new \calderawp\calderaforms\cf2\CalderaFormsV2();
do_action( 'caldera_forms_v2_init', $container );
do_action('caldera_forms_v2_init', $container);
}

return $container;
}

/**
<<<<<<< HEAD
* Setup Cf2 container
*
* @since 1.8.0
Expand Down Expand Up @@ -69,3 +71,20 @@ function caldera_forms_v2_field_upgrades($field, $form){
}
return $field;
}

/**
* Schedule delete with job manager
*
* @since 1.8.0
*
* @param \calderawp\calderaforms\cf2\Jobs\Job $job Job to schedule
* @param int $delay Optional. Minimum delay before job is run. Default is 0.
*/
function caldera_forms_schedule_job(\calderawp\calderaforms\cf2\Jobs\Job $job, $delay = 0)
{

caldera_forms_get_v2_container()
->getService(\calderawp\calderaforms\cf2\Services\QueueSchedulerService::class)
->schedule($job, $delay);
}

Loading

0 comments on commit e8dae2e

Please sign in to comment.