PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.5
JetFormBuilder — Dynamic Blocks Form Builder v2.1.5
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
179 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 include_once ABSPATH . 'wp-admin/includes/image.php';
58 }
59
60 $attachment = wp_insert_attachment(
61 array(
62 'guid' => $this->get_url(),
63 'post_mime_type' => $this->get_type(),
64 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $this->get_file() ) ),
65 'post_content' => '',
66 'post_status' => 'publish',
67 ),
68 $this->get_file(),
69 0,
70 true
71 );
72
73 if ( is_wp_error( $attachment ) ) {
74 throw new Upload_Exception( $attachment->get_error_message() );
75 }
76
77 wp_update_attachment_metadata(
78 $attachment,
79 wp_generate_attachment_metadata( $attachment, $this->get_file() )
80 );
81
82 $this->set_attachment_id( (string) $attachment );
83 }
84
85 public function set_from_array( array $upload ): Uploaded_File {
86 if ( isset( $upload['file'] ) ) {
87 $this->file = wp_normalize_path( $upload['file'] );
88 }
89 if ( isset( $upload['url'] ) ) {
90 $this->url = $upload['url'];
91 }
92 if ( isset( $upload['type'] ) ) {
93 $this->type = $upload['type'];
94 }
95 if ( isset( $upload['id'] ) ) {
96 $this->set_attachment_id( (string) $upload['id'] );
97 }
98
99 return $this;
100 }
101
102 public function set_attachment_id( string $attachment_id ): Uploaded_File {
103 $this->attachment_id = $attachment_id;
104
105 return $this;
106 }
107
108 /**
109 * @return string
110 */
111 public function get_type(): string {
112 return $this->type;
113 }
114
115 /**
116 * @return string
117 */
118 public function get_file(): string {
119 return $this->file;
120 }
121
122 /**
123 * @return string
124 */
125 public function get_url(): string {
126 return $this->url;
127 }
128
129 /*
130 * Realisation of
131 * \Jet_Form_Builder\Classes\Resources\Media_Block_Value
132 */
133
134 /**
135 * @return string
136 */
137 public function get_attachment_id(): string {
138 return $this->attachment_id;
139 }
140
141
142 /**
143 * @return string
144 */
145 public function get_attachment_url(): string {
146 return $this->get_url();
147 }
148
149 /**
150 * @return array
151 */
152 public function get_attachment_both(): array {
153 return array(
154 'id' => $this->get_attachment_id(),
155 'url' => $this->get_attachment_url(),
156 );
157 }
158
159 /*
160 * Realisation of
161 * \Jet_Form_Builder\Classes\Resources\Uploaded_File_Path
162 */
163
164 /**
165 * @return string
166 */
167 public function get_attachment_file(): string {
168 $file = $this->get_file();
169
170 if ( $file ) {
171 return $file;
172 }
173
174 $file = get_attached_file( $this->get_attachment_id() );
175
176 return is_string( $file ) ? $file : '';
177 }
178 }
179