PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.3
JetFormBuilder — Dynamic Blocks Form Builder v3.0.3
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
193 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 // Update file url for performance plugins compatibility
89 $this->set_url( wp_get_attachment_url( $attachment ) );
90 }
91
92 public function set_from_array( array $upload ): Uploaded_File {
93 if ( isset( $upload['file'] ) ) {
94 $this->file = $upload['file'];
95 }
96 if ( isset( $upload['url'] ) ) {
97 $this->url = $upload['url'];
98 }
99 if ( isset( $upload['type'] ) ) {
100 $this->type = $upload['type'];
101 }
102 if ( isset( $upload['id'] ) ) {
103 $this->set_attachment_id( (string) $upload['id'] );
104 }
105
106 return $this;
107 }
108
109 public function set_attachment_id( string $attachment_id ): Uploaded_File {
110 $this->attachment_id = $attachment_id;
111
112 return $this;
113 }
114
115 /**
116 * @return string
117 */
118 public function get_type(): string {
119 return $this->type;
120 }
121
122 /**
123 * @return string
124 */
125 public function get_file(): string {
126 return $this->file;
127 }
128
129 /**
130 * @return string
131 */
132 public function get_url(): string {
133 return $this->url;
134 }
135
136 /*
137 * Realisation of
138 * \Jet_Form_Builder\Classes\Resources\Media_Block_Value
139 */
140
141 /**
142 * @return string
143 */
144 public function get_attachment_id(): string {
145 return $this->attachment_id;
146 }
147
148
149 /**
150 * @return string
151 */
152 public function get_attachment_url(): string {
153 return $this->get_url();
154 }
155
156 /**
157 * @return array
158 */
159 public function get_attachment_both(): array {
160 return array(
161 'id' => $this->get_attachment_id(),
162 'url' => $this->get_attachment_url(),
163 );
164 }
165
166 /*
167 * Realisation of
168 * \Jet_Form_Builder\Classes\Resources\Uploaded_File_Path
169 */
170
171 /**
172 * @return string
173 */
174 public function get_attachment_file(): string {
175 $file = $this->get_file();
176
177 if ( $file ) {
178 return $file;
179 }
180
181 $file = get_attached_file( $this->get_attachment_id() );
182
183 return is_string( $file ) ? $file : '';
184 }
185
186 /**
187 * @param string $url
188 */
189 public function set_url( string $url ) {
190 $this->url = $url;
191 }
192 }
193