PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.6
JetFormBuilder — Dynamic Blocks Form Builder v3.1.6
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 2 years ago file-tools.php 2 years ago file.php 2 years ago has-error-file.php 2 years ago media-block-value.php 2 years ago sanitize-file-exception.php 2 years ago upload-dir.php 2 years ago upload-exception.php 2 years ago upload-permission-exception.php 2 years ago uploaded-collection.php 2 years ago uploaded-file-path.php 2 years ago uploaded-file.php 2 years ago
uploaded-file.php
205 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Classes\Resources;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 class Uploaded_File implements Media_Block_Value, Uploaded_File_Path {
12
13 protected $file = '';
14 protected $url = '';
15 protected $type = '';
16 protected $attachment_id = '';
17
18 /**
19 * @param File $file
20 *
21 * @throws Upload_Exception
22 */
23 public function upload( File $file ) {
24 if ( ! function_exists( 'wp_handle_upload' ) ) {
25 include_once ABSPATH . 'wp-admin/includes/file.php';
26 include_once ABSPATH . 'wp-admin/includes/media.php';
27 }
28
29 add_filter( 'upload_dir', array( Upload_Dir::class, 'apply_upload_dir' ) );
30
31 $file_array = $file->to_array();
32 $this->upload_file( $file_array );
33
34 remove_filter( 'upload_dir', array( Upload_Dir::class, 'apply_upload_dir' ) );
35 }
36
37 /**
38 * @param array $file
39 *
40 * @throws Upload_Exception
41 */
42 protected function upload_file( array $file ) {
43 $upload = wp_handle_upload(
44 $file,
45 array(
46 'test_form' => false,
47 )
48 );
49
50 if ( ! empty( $upload['error'] ) ) {
51 throw new Upload_Exception( esc_html( $upload['error'] ) );
52 }
53
54 $this->set_from_array( $upload );
55 }
56
57 /**
58 * @throws Upload_Exception
59 */
60 public function add_attachment() {
61 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
62 require_once ABSPATH . 'wp-admin/includes/image.php';
63 }
64
65 if ( ! function_exists( 'wp_read_video_metadata' ) ) {
66 require_once ABSPATH . 'wp-admin/includes/media.php';
67 }
68
69 $attachment = wp_insert_attachment(
70 array(
71 'guid' => $this->get_url(),
72 'post_mime_type' => $this->get_type(),
73 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $this->get_file() ) ),
74 'post_content' => '',
75 'post_status' => 'publish',
76 ),
77 $this->get_file(),
78 0,
79 true
80 );
81
82 if ( is_wp_error( $attachment ) ) {
83 throw new Upload_Exception( esc_html( $attachment->get_error_message() ) );
84 }
85
86 wp_update_attachment_metadata(
87 $attachment,
88 wp_generate_attachment_metadata( $attachment, $this->get_file() )
89 );
90
91 $this->set_attachment_id( (string) $attachment );
92
93 // Update file url for performance plugins compatibility
94 $this->set_url( wp_get_attachment_url( $attachment ) );
95 }
96
97 public function set_from_array( array $upload ): Uploaded_File {
98 if ( isset( $upload['file'] ) ) {
99 $this->file = $upload['file'];
100 }
101 if ( isset( $upload['url'] ) ) {
102 $this->url = $upload['url'];
103 }
104 if ( isset( $upload['type'] ) ) {
105 $this->type = $upload['type'];
106 }
107 if ( isset( $upload['id'] ) ) {
108 $this->set_attachment_id( (string) $upload['id'] );
109 }
110
111 return $this;
112 }
113
114 public function set_attachment_id( string $attachment_id ): Uploaded_File {
115 $this->attachment_id = $attachment_id;
116
117 return $this;
118 }
119
120 /**
121 * @return string
122 */
123 public function get_type(): string {
124 return $this->type;
125 }
126
127 /**
128 * @return string
129 */
130 public function get_file(): string {
131 return $this->file;
132 }
133
134 /**
135 * @return string
136 */
137 public function get_url(): string {
138 return $this->url;
139 }
140
141 /*
142 * Realisation of
143 * \Jet_Form_Builder\Classes\Resources\Media_Block_Value
144 */
145
146 /**
147 * @return string
148 */
149 public function get_attachment_id(): string {
150 return $this->attachment_id;
151 }
152
153
154 /**
155 * @return string
156 */
157 public function get_attachment_url(): string {
158 return $this->get_url();
159 }
160
161 /**
162 * @return array
163 */
164 public function get_attachment_both(): array {
165 return array(
166 'id' => $this->get_attachment_id(),
167 'url' => $this->get_attachment_url(),
168 );
169 }
170
171 /*
172 * Realisation of
173 * \Jet_Form_Builder\Classes\Resources\Uploaded_File_Path
174 */
175
176 /**
177 * @return string
178 */
179 public function get_attachment_file(): string {
180 $file = $this->get_file();
181
182 if ( $file ) {
183 return $file;
184 }
185
186 $id = $this->get_attachment_id();
187 $url = $this->get_attachment_url();
188
189 if ( ( empty( $id ) || ! is_numeric( $id ) ) && ! empty( $url ) ) {
190 $id = attachment_url_to_postid( $url );
191 }
192
193 $file = get_attached_file( $id );
194
195 return is_string( $file ) ? $file : '';
196 }
197
198 /**
199 * @param string $url
200 */
201 public function set_url( string $url ) {
202 $this->url = $url;
203 }
204 }
205