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