PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.0
JetFormBuilder — Dynamic Blocks Form Builder v3.0.0
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / classes / resources / uploaded-file.php
jetformbuilder / includes / classes / resources Last commit date
file-collection.php 3 years ago file-tools.php 3 years ago file.php 3 years ago media-block-value.php 3 years ago sanitize-file-exception.php 3 years ago upload-dir.php 3 years ago upload-exception.php 3 years ago upload-permission-exception.php 3 years ago uploaded-collection.php 3 years ago uploaded-file-path.php 3 years ago uploaded-file.php 3 years ago
uploaded-file.php
183 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Classes\Resources;
5
6 class Uploaded_File implements Media_Block_Value, Uploaded_File_Path {
7
8 protected $file = '';
9 protected $url = '';
10 protected $type = '';
11 protected $attachment_id = '';
12
13 /**
14 * @param File $file
15 *
16 * @throws Upload_Exception
17 */
18 public function upload( File $file ) {
19 if ( ! function_exists( 'wp_handle_upload' ) ) {
20 include_once ABSPATH . 'wp-admin/includes/file.php';
21 include_once ABSPATH . 'wp-admin/includes/media.php';
22 }
23
24 add_filter( 'upload_dir', array( Upload_Dir::class, 'apply_upload_dir' ) );
25
26 $file_array = $file->to_array();
27 $this->upload_file( $file_array );
28
29 remove_filter( 'upload_dir', array( Upload_Dir::class, 'apply_upload_dir' ) );
30 }
31
32 /**
33 * @param array $file
34 *
35 * @throws Upload_Exception
36 */
37 protected function upload_file( array $file ) {
38 $upload = wp_handle_upload(
39 $file,
40 array(
41 'test_form' => false,
42 )
43 );
44
45 if ( ! empty( $upload['error'] ) ) {
46 throw new Upload_Exception( $upload['error'] );
47 }
48
49 $this->set_from_array( $upload );
50 }
51
52 /**
53 * @throws Upload_Exception
54 */
55 public function add_attachment() {
56 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
57 require_once ABSPATH . 'wp-admin/includes/image.php';
58 }
59
60 if ( ! function_exists( 'wp_read_video_metadata' ) ) {
61 require_once ABSPATH . 'wp-admin/includes/media.php';
62 }
63
64 $attachment = wp_insert_attachment(
65 array(
66 'guid' => $this->get_url(),
67 'post_mime_type' => $this->get_type(),
68 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $this->get_file() ) ),
69 'post_content' => '',
70 'post_status' => 'publish',
71 ),
72 $this->get_file(),
73 0,
74 true
75 );
76
77 if ( is_wp_error( $attachment ) ) {
78 throw new Upload_Exception( $attachment->get_error_message() );
79 }
80
81 wp_update_attachment_metadata(
82 $attachment,
83 wp_generate_attachment_metadata( $attachment, $this->get_file() )
84 );
85
86 $this->set_attachment_id( (string) $attachment );
87 }
88
89 public function set_from_array( array $upload ): Uploaded_File {
90 if ( isset( $upload['file'] ) ) {
91 $this->file = $upload['file'];
92 }
93 if ( isset( $upload['url'] ) ) {
94 $this->url = $upload['url'];
95 }
96 if ( isset( $upload['type'] ) ) {
97 $this->type = $upload['type'];
98 }
99 if ( isset( $upload['id'] ) ) {
100 $this->set_attachment_id( (string) $upload['id'] );
101 }
102
103 return $this;
104 }
105
106 public function set_attachment_id( string $attachment_id ): Uploaded_File {
107 $this->attachment_id = $attachment_id;
108
109 return $this;
110 }
111
112 /**
113 * @return string
114 */
115 public function get_type(): string {
116 return $this->type;
117 }
118
119 /**
120 * @return string
121 */
122 public function get_file(): string {
123 return $this->file;
124 }
125
126 /**
127 * @return string
128 */
129 public function get_url(): string {
130 return $this->url;
131 }
132
133 /*
134 * Realisation of
135 * \Jet_Form_Builder\Classes\Resources\Media_Block_Value
136 */
137
138 /**
139 * @return string
140 */
141 public function get_attachment_id(): string {
142 return $this->attachment_id;
143 }
144
145
146 /**
147 * @return string
148 */
149 public function get_attachment_url(): string {
150 return $this->get_url();
151 }
152
153 /**
154 * @return array
155 */
156 public function get_attachment_both(): array {
157 return array(
158 'id' => $this->get_attachment_id(),
159 'url' => $this->get_attachment_url(),
160 );
161 }
162
163 /*
164 * Realisation of
165 * \Jet_Form_Builder\Classes\Resources\Uploaded_File_Path
166 */
167
168 /**
169 * @return string
170 */
171 public function get_attachment_file(): string {
172 $file = $this->get_file();
173
174 if ( $file ) {
175 return $file;
176 }
177
178 $file = get_attached_file( $this->get_attachment_id() );
179
180 return is_string( $file ) ? $file : '';
181 }
182 }
183