diff --git a/cf2/Fields/Handlers/FileUpload.php b/cf2/Fields/Handlers/FileUpload.php new file mode 100644 index 000000000..6d275deb3 --- /dev/null +++ b/cf2/Fields/Handlers/FileUpload.php @@ -0,0 +1,85 @@ +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; + } +} \ No newline at end of file diff --git a/cf2/RestApi/File/CreateFile.php b/cf2/RestApi/File/CreateFile.php index 1c347ad77..6070e333f 100644 --- a/cf2/RestApi/File/CreateFile.php +++ b/cf2/RestApi/File/CreateFile.php @@ -5,6 +5,8 @@ use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType; +use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; +use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; class CreateFile extends File { @@ -86,48 +88,21 @@ public function createItem(\WP_REST_Request $request) $uploader = $this->getUploader($field); $hashes = $request->get_param( 'hashes'); $controlCode = $request->get_param( 'control' ); - $transdata = \Caldera_Forms_Transient::get_transient($controlCode); - if( ! is_array( $transdata ) ){ - $transdata = []; - } - - $i = 0; - foreach ($files as $file) { - if (!\Caldera_Forms_Files::is_private($field)) { - $uploadArgs = array( - 'private' => false, - 'field_id' => $field['ID'], - 'form_id' => $this->getForm()['ID'] - ); - } else { - $uploadArgs = array( - 'private' => true, - 'field_id' => $field['ID'], - 'form_id' => $this->getForm()['ID'] - ); - } - - $expected = $hashes[$i]; - $actual = md5_file( $file['tmp_name'] ); - - if ( $expected !== $actual ) { - return new \WP_Error( 'rest_upload_hash_mismatch', __( 'Content hash did not match expected.' ), array( 'status' => 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++; + $handler = new FileUpload( + $field, + $this->getForm(), + new Cf1TransientsApi() + ); + try{ + $controlCode = $handler->processFiles($files,$hashes,$controlCode); + }catch( \Exception $e ){ + return new \WP_REST_Response(['message' => $e->getMessage() ],$e->getCode() ); } - \Caldera_Forms_Transient::set_transient($controlCode, array_merge( $transdata, $uploads ), DAY_IN_SECONDS); - + if( is_wp_error( $controlCode ) ){ + return $controlCode; + } $response = rest_ensure_response([ 'control' => $controlCode ]); @@ -151,4 +126,5 @@ protected function getUploader($field) } + } \ No newline at end of file diff --git a/cf2/Transients/Cf1TransientsApi.php b/cf2/Transients/Cf1TransientsApi.php new file mode 100644 index 000000000..018362f5b --- /dev/null +++ b/cf2/Transients/Cf1TransientsApi.php @@ -0,0 +1,37 @@ + '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 ); + } +}