-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seperate file upload logic from rest api handler #2766
- Loading branch information
Josh Pollock
committed
Oct 25, 2018
1 parent
626b610
commit f371307
Showing
7 changed files
with
216 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Fields\Handlers; | ||
use calderawp\calderaforms\cf2\Transients\TransientApiContract; | ||
|
||
|
||
class FileUpload | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $field; | ||
/** | ||
* @var array | ||
*/ | ||
protected $form; | ||
|
||
/** | ||
* @var TransientApiContract | ||
*/ | ||
protected $transientApi; | ||
public function __construct(array $field, array $form, TransientApiContract $transientApi ) | ||
{ | ||
$this->field = $field; | ||
$this->form = $form; | ||
$this->transientApi = $transientApi; | ||
} | ||
|
||
|
||
/** | ||
* @param array $files | ||
* @param array $hashes | ||
* @param $controlCode | ||
* @return mixed | ||
* @throws \Exception | ||
*/ | ||
public function processFiles(array $files,array $hashes, $controlCode ){ | ||
$transdata = call_user_func([$this->transientApi, 'getTransient'],$controlCode); | ||
if( ! is_array( $transdata ) ){ | ||
$transdata = []; | ||
} | ||
|
||
$i = 0; | ||
foreach ($files as $file) { | ||
if (!\Caldera_Forms_Files::is_private($this->field)) { | ||
$uploadArgs = array( | ||
'private' => false, | ||
'field_id' => $this->field['ID'], | ||
'form_id' => $this->form['ID'] | ||
); | ||
} else { | ||
$uploadArgs = array( | ||
'private' => true, | ||
'field_id' => $this->field['ID'], | ||
'form_id' => $this->form['ID'] | ||
); | ||
} | ||
|
||
$expected = $hashes[$i]; | ||
$actual = md5_file( $file['tmp_name'] ); | ||
|
||
if ( $expected !== $actual ) { | ||
throw new \Exception(__( 'Content hash did not match expected.' ), 412 ); | ||
} | ||
|
||
|
||
$upload = wp_handle_upload($file, array( 'test_form' => false, 'action' => 'foo' ) ); | ||
if( !empty( $field['config']['media_lib'] ) ){ | ||
\Caldera_Forms_Files::add_to_media_library( $upload, $field ); | ||
} | ||
|
||
|
||
$uploads[] = $upload['url']; | ||
$i++; | ||
|
||
} | ||
|
||
call_user_func([$this->transientApi, 'setTransient'],$controlCode, array_merge( $transdata, $uploads ), 24 * 60 * 60 ); | ||
|
||
|
||
return $controlCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Transients; | ||
|
||
/** | ||
* Class Cf1TransientsApi | ||
* | ||
* Thin wrapper over CF 1.5+ Transients API wrapper | ||
* | ||
* @package calderawp\calderaforms\cf2\Transients | ||
*/ | ||
class Cf1TransientsApi implements TransientApiContract | ||
{ /** | ||
* @inheritdoc | ||
* @since 1.8.0 | ||
*/ | ||
public function getTransient($id){ | ||
return \Caldera_Forms_Transient::get_transient($id); | ||
} | ||
/** | ||
* @inheritdoc | ||
* @since 1.8.0 | ||
*/ | ||
public function setTransient($id, $data, $expires = null) | ||
{ | ||
return \Caldera_Forms_Transient::set_transient($id,$data,$expires); | ||
} | ||
/** | ||
* @inheritdoc | ||
* @since 1.8.0 | ||
*/ | ||
public function deleteTransient($id){ | ||
return \Caldera_Forms_Transient::delete_transient($id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Transients; | ||
|
||
|
||
interface TransientApiContract | ||
{ | ||
|
||
/** | ||
* Get stored transient | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param string $id Transient ID | ||
* @param mixed $data Data | ||
* @param null|int $expires Optional. Expiration time. Default is nul, which becomes 1 hour | ||
* | ||
* @return bool | ||
*/ | ||
public function setTransient($id, $data, $expires = null); | ||
|
||
/** | ||
* Delete transient | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param string $id Transient ID | ||
* | ||
* @return bool | ||
*/ | ||
public function deleteTransient( $id ); | ||
|
||
/** | ||
* Get a transient | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param string $id Transient ID | ||
* | ||
* @return mixed | ||
*/ | ||
public function getTransient( $id ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace calderawp\calderaforms\Tests\Unit\Fields\Handlers\FileUpload; | ||
|
||
use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; | ||
use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; | ||
use calderawp\calderaforms\Tests\Unit\TestCase; | ||
|
||
class FileUploadTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::__construct() | ||
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$field | ||
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$form | ||
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$transientApi | ||
*/ | ||
public function test__construct() | ||
{ | ||
$field = [ 'ID' => 'fld1']; | ||
$form = ['ID' => 'cd1' ]; | ||
$transients = new Cf1TransientsApi(); | ||
$handler = new FileUpload($field,$form, $transients); | ||
$this->assertAttributeEquals( $field,'field', $handler ); | ||
$this->assertAttributeEquals( $form,'form', $handler ); | ||
$this->assertAttributeEquals( $transients,'transientApi', $handler ); | ||
} | ||
} |