date-field-parser.php
2 years ago
datetime-field-parser.php
2 years ago
datetime-trait.php
2 years ago
default-parser.php
2 years ago
hidden-field-parser.php
2 years ago
media-field-parser.php
1 year ago
repeater-field-parser.php
2 years ago
text-field-parser.php
2 years ago
wysiwyg-field-parser.php
2 years ago
media-field-parser.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Block_Parsers\Fields; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Resources\Has_Error_File; |
| 7 | use Jet_Form_Builder\Classes\Resources\Media_Block_Value; |
| 8 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 9 | use Jet_Form_Builder\Request\Exceptions\Sanitize_Value_Exception; |
| 10 | use Jet_Form_Builder\Classes\Resources\Upload_Exception; |
| 11 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 12 | use JFB_Modules\Block_Parsers\File_Uploader; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | class Media_Field_Parser extends Field_Data_Parser { |
| 20 | |
| 21 | public function type() { |
| 22 | return 'media-field'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @since 3.0.4 Added `jet-form-builder/media-field/before-upload` hook |
| 27 | * |
| 28 | * @return array|false|int|string|null |
| 29 | * @throws Sanitize_Value_Exception |
| 30 | */ |
| 31 | public function get_response() { |
| 32 | if ( |
| 33 | empty( $this->get_file() ) || |
| 34 | ( |
| 35 | is_object( $this->get_file() ) && |
| 36 | is_a( $this->get_file(), Has_Error_File::class ) && |
| 37 | $this->get_file()->has_error() |
| 38 | ) |
| 39 | ) { |
| 40 | /** |
| 41 | * We should leave here $this->value for case, while this field exporting |
| 42 | * with values from Form Record |
| 43 | * |
| 44 | * @see https://github.com/Crocoblock/issues-tracker/issues/4422 |
| 45 | */ |
| 46 | return $this->value; |
| 47 | } |
| 48 | do_action( 'jet-form-builder/media-field/before-upload', $this ); |
| 49 | |
| 50 | $uploader = ( new File_Uploader() )->set_context( $this ); |
| 51 | |
| 52 | try { |
| 53 | /** @var Media_Block_Value $uploads */ |
| 54 | $uploads = $uploader->upload(); |
| 55 | } catch ( Upload_Exception $exception ) { |
| 56 | $this->collect_error( $exception->getMessage() ); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | $this->set_file( $uploads ); |
| 61 | |
| 62 | switch ( $this->get_value_format() ) { |
| 63 | case 'id': |
| 64 | return $uploads->get_attachment_id(); |
| 65 | case 'both': |
| 66 | return $uploads->get_attachment_both(); |
| 67 | case 'ids': |
| 68 | return $uploads->get_attachment_ids(); |
| 69 | default: |
| 70 | return $uploads->get_attachment_url(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | protected function get_value_format(): string { |
| 75 | return empty( $this->settings['insert_attachment'] ) |
| 76 | ? 'url' |
| 77 | : ( $this->settings['value_format'] ?? 'url' ); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 |