date-field-parser.php
3 years ago
datetime-field-parser.php
3 years ago
datetime-trait.php
3 years ago
hidden-field-parser.php
3 years ago
media-field-parser.php
3 years ago
repeater-field-parser.php
3 years ago
text-field-parser.php
3 years ago
wysiwyg-field-parser.php
3 years ago
media-field-parser.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request\Fields; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Resources\Media_Block_Value; |
| 7 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 8 | use Jet_Form_Builder\Request\Exceptions\Sanitize_Value_Exception; |
| 9 | use Jet_Form_Builder\Classes\Resources\Upload_Exception; |
| 10 | use Jet_Form_Builder\Request\Field_Data_Parser; |
| 11 | use Jet_Form_Builder\Request\File_Uploader; |
| 12 | |
| 13 | class Media_Field_Parser extends Field_Data_Parser { |
| 14 | |
| 15 | public function type() { |
| 16 | return 'media-field'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return array|false|int|string|null |
| 21 | * @throws Sanitize_Value_Exception |
| 22 | */ |
| 23 | public function get_response() { |
| 24 | if ( empty( $this->context->get_file() ) ) { |
| 25 | return false; |
| 26 | } |
| 27 | $uploader = ( new File_Uploader() )->set_context( $this->context ); |
| 28 | |
| 29 | try { |
| 30 | /** @var Media_Block_Value $uploads */ |
| 31 | $uploads = $uploader->upload(); |
| 32 | } catch ( Upload_Exception $exception ) { |
| 33 | throw new Sanitize_Value_Exception( $exception->getMessage() ); |
| 34 | } |
| 35 | |
| 36 | jet_fb_request_handler()->update_file( $this->name, $uploads ); |
| 37 | |
| 38 | switch ( $this->get_value_format() ) { |
| 39 | case 'id': |
| 40 | return $uploads->get_attachment_id(); |
| 41 | case 'both': |
| 42 | return $uploads->get_attachment_both(); |
| 43 | default: |
| 44 | return $uploads->get_attachment_url(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | protected function get_value_format(): string { |
| 49 | if ( empty( $this->settings['insert_attachment'] ) ) { |
| 50 | return 'url'; |
| 51 | } |
| 52 | |
| 53 | return $this->settings['value_format'] ?? 'url'; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 |