PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.6
Elementor Website Builder – more than just a page builder v3.5.6
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
← All changes | includes/template-library/classes/class-import-images.php +15 -84 4.1.0-beta33.5.6 View file →
@@ -1,11 +1,9 @@
1 1 <?php
2 2 namespace Elementor\TemplateLibrary\Classes;
3 3
4 -use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
5 4 use Elementor\Core\Files\Uploads_Manager;
6 5 use Elementor\Plugin;
7 -use Elementor\Utils;
8 6
9 7 if ( ! defined( 'ABSPATH' ) ) {
10 8 exit; // Exit if accessed directly.
11 9 }
@@ -102,45 +100,33 @@
102 100 * @since 3.2.0 New `$parent_post_id` option added
103 101 * @access public
104 102 *
105 103 * @param array $attachment The attachment.
106 - * @param int $parent_post_id Optional.
104 + * @param int $parent_post_id Optional
107 105 *
108 106 * @return false|array Imported image data, or false.
109 107 */
110 108 public function import( $attachment, $parent_post_id = null ) {
111 - if ( isset( $attachment['tmp_name'] ) ) {
112 - // Used when called to import a directly-uploaded file.
113 - $filename = $attachment['name'];
114 - $file_content = false;
109 + if ( ! empty( $attachment['id'] ) ) {
110 + $saved_image = $this->get_saved_image( $attachment );
115 111
116 - // security validation in case the tmp_name has been tampered with
117 - if ( is_uploaded_file( $attachment['tmp_name'] ) ) {
118 - $file_content = Utils::file_get_contents( $attachment['tmp_name'] );
112 + if ( $saved_image ) {
113 + return $saved_image;
119 114 }
120 - } else {
121 - // Used when attachment information is passed to this method.
122 - if ( ! empty( $attachment['id'] ) ) {
123 - $saved_image = $this->get_saved_image( $attachment );
115 + }
124 116
125 - if ( $saved_image ) {
126 - return $saved_image;
127 - }
128 - }
117 + // Extract the file name and extension from the url.
118 + $filename = basename( $attachment['url'] );
129 119
130 - // Extract the file name and extension from the url.
131 - $filename = basename( $attachment['url'] );
120 + $request = wp_safe_remote_get( $attachment['url'] );
132 121
133 - $request = wp_safe_remote_get( $attachment['url'] );
122 + // Make sure the request returns a valid result.
123 + if ( is_wp_error( $request ) || ( ! empty( $request['response']['code'] ) && 200 !== (int) $request['response']['code'] ) ) {
124 + return false;
125 + }
134 126
135 - // Make sure the request returns a valid result.
136 - if ( is_wp_error( $request ) || ( ! empty( $request['response']['code'] ) && 200 !== (int) $request['response']['code'] ) ) {
137 - return false;
138 - }
127 + $file_content = wp_remote_retrieve_body( $request );
139 128
140 - $file_content = wp_remote_retrieve_body( $request );
141 - }
142 -
143 129 if ( empty( $file_content ) ) {
144 130 return false;
145 131 }
146 132
@@ -145,13 +131,8 @@
145 131 }
146 132
147 133 $filetype = wp_check_filetype( $filename );
148 134
149 - // If the file type is not recognized by WordPress, exit here to avoid creation of an empty attachment document.
150 - if ( ! $filetype['ext'] ) {
151 - return false;
152 - }
153 -
154 135 if ( 'svg' === $filetype['ext'] ) {
155 136 // In case that unfiltered-files upload is not enabled, SVG images should not be imported.
156 137 if ( ! Uploads_Manager::are_unfiltered_uploads_enabled() ) {
157 138 return false;
@@ -159,9 +140,9 @@
159 140
160 141 $svg_handler = Plugin::$instance->uploads_manager->get_file_type_handlers( 'svg' );
161 142
162 143 $file_content = $svg_handler->sanitizer( $file_content );
163 - }
144 + };
164 145
165 146 $upload = wp_upload_bits(
166 147 $filename,
167 148 null,
@@ -184,10 +165,8 @@
184 165 }
185 166
186 167 $post_id = wp_insert_attachment( $post, $upload['file'], $parent_post_id );
187 168
188 - apply_filters( 'elementor/template_library/import_images/new_attachment', $post_id );
189 -
190 169 // On REST requests.
191 170 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
192 171 require_once ABSPATH . '/wp-admin/includes/image.php';
193 172 }
@@ -211,56 +190,8 @@
211 190 $this->_replace_image_ids[ $attachment['id'] ] = $new_attachment;
212 191 }
213 192
214 193 return $new_attachment;
215 - }
216 -
217 - /**
218 - * Import local file.
219 - *
220 - * Import a local file directly to WordPress media library.
221 - * Used for importing files that are already downloaded locally (e.g., from extracted ZIP).
222 - *
223 - * @since 3.33.0
224 - * @access public
225 - *
226 - * @param string $local_file_path The local file path.
227 - * @param int $parent_post_id Optional. Parent post ID.
228 - *
229 - * @return false|array Imported image data, or false on failure.
230 - */
231 - public function import_local_file( $local_file_path, $parent_post_id = null ) {
232 - global $wp_filesystem;
233 -
234 - if ( ! $wp_filesystem->exists( $local_file_path ) ) {
235 - return false;
236 - }
237 -
238 - if ( ! function_exists( 'media_handle_sideload' ) ) {
239 - require_once ABSPATH . 'wp-admin/includes/media.php';
240 - }
241 -
242 - if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
243 - require_once ABSPATH . 'wp-admin/includes/image.php';
244 - }
245 -
246 - $file_array = [
247 - 'name' => basename( $local_file_path ),
248 - 'tmp_name' => $local_file_path,
249 - ];
250 -
251 - $attachment_id = media_handle_sideload( $file_array, $parent_post_id );
252 -
253 - if ( is_wp_error( $attachment_id ) ) {
254 - return false;
255 - }
256 -
257 - apply_filters( 'elementor/template_library/import_images/new_attachment', $attachment_id );
258 -
259 - return [
260 - 'id' => $attachment_id,
261 - 'url' => wp_get_attachment_url( $attachment_id ),
262 - ];
263 194 }
264 195
265 196 /**
266 197 * Template library import images constructor.