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 |