PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.5
Elementor Website Builder – more than just a page builder v3.5.5
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 4.0.7 All 451 releases
elementor / includes / template-library / classes / class-import-images.php

class-import-images.php in Elementor Website Builder – more than just a page builder 3.5.5, at includes/template-library/classes/class-import-images.php

213 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\TemplateLibrary\Classes;
3
4 use Elementor\Core\Files\Uploads_Manager;
5 use Elementor\Plugin;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor template library import images.
13 *
14 * Elementor template library import images handler class is responsible for
15 * importing remote images used by the template library.
16 *
17 * @since 1.0.0
18 */
19 class Import_Images {
20
21 /**
22 * Replaced images IDs.
23 *
24 * The IDs of all the new imported images. An array containing the old
25 * attachment ID and the new attachment ID generated after the import.
26 *
27 * @since 1.0.0
28 * @access private
29 *
30 * @var array
31 */
32 private $_replace_image_ids = [];
33
34 /**
35 * Get image hash.
36 *
37 * Retrieve the sha1 hash of the image URL.
38 *
39 * @since 2.0.0
40 * @access private
41 *
42 * @param string $attachment_url The attachment URL.
43 *
44 * @return string Image hash.
45 */
46 private function get_hash_image( $attachment_url ) {
47 return sha1( $attachment_url );
48 }
49
50 /**
51 * Get saved image.
52 *
53 * Retrieve new image ID, if the image has a new ID after the import.
54 *
55 * @since 2.0.0
56 * @access private
57 *
58 * @param array $attachment The attachment.
59 *
60 * @return false|array New image ID or false.
61 */
62 private function get_saved_image( $attachment ) {
63 global $wpdb;
64
65 if ( isset( $this->_replace_image_ids[ $attachment['id'] ] ) ) {
66 return $this->_replace_image_ids[ $attachment['id'] ];
67 }
68
69 $post_id = $wpdb->get_var(
70 $wpdb->prepare(
71 'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
72 WHERE `meta_key` = \'_elementor_source_image_hash\'
73 AND `meta_value` = %s
74 ;',
75 $this->get_hash_image( $attachment['url'] )
76 )
77 );
78
79 if ( $post_id ) {
80 $new_attachment = [
81 'id' => $post_id,
82 'url' => wp_get_attachment_url( $post_id ),
83 ];
84 $this->_replace_image_ids[ $attachment['id'] ] = $new_attachment;
85
86 return $new_attachment;
87 }
88
89 return false;
90 }
91
92 /**
93 * Import image.
94 *
95 * Import a single image from a remote server, upload the image WordPress
96 * uploads folder, create a new attachment in the database and updates the
97 * attachment metadata.
98 *
99 * @since 1.0.0
100 * @since 3.2.0 New `$parent_post_id` option added
101 * @access public
102 *
103 * @param array $attachment The attachment.
104 * @param int $parent_post_id Optional
105 *
106 * @return false|array Imported image data, or false.
107 */
108 public function import( $attachment, $parent_post_id = null ) {
109 if ( ! empty( $attachment['id'] ) ) {
110 $saved_image = $this->get_saved_image( $attachment );
111
112 if ( $saved_image ) {
113 return $saved_image;
114 }
115 }
116
117 // Extract the file name and extension from the url.
118 $filename = basename( $attachment['url'] );
119
120 $request = wp_safe_remote_get( $attachment['url'] );
121
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 }
126
127 $file_content = wp_remote_retrieve_body( $request );
128
129 if ( empty( $file_content ) ) {
130 return false;
131 }
132
133 $filetype = wp_check_filetype( $filename );
134
135 if ( 'svg' === $filetype['ext'] ) {
136 // In case that unfiltered-files upload is not enabled, SVG images should not be imported.
137 if ( ! Uploads_Manager::are_unfiltered_uploads_enabled() ) {
138 return false;
139 }
140
141 $svg_handler = Plugin::$instance->uploads_manager->get_file_type_handlers( 'svg' );
142
143 $file_content = $svg_handler->sanitizer( $file_content );
144 };
145
146 $upload = wp_upload_bits(
147 $filename,
148 null,
149 $file_content
150 );
151
152 $post = [
153 'post_title' => $filename,
154 'guid' => $upload['url'],
155 ];
156
157 $info = wp_check_filetype( $upload['file'] );
158
159 if ( $info ) {
160 $post['post_mime_type'] = $info['type'];
161 } else {
162 // For now just return the origin attachment
163 return $attachment;
164 // return new \WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type.', 'elementor' ) );
165 }
166
167 $post_id = wp_insert_attachment( $post, $upload['file'], $parent_post_id );
168
169 // On REST requests.
170 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
171 require_once ABSPATH . '/wp-admin/includes/image.php';
172 }
173
174 if ( ! function_exists( 'wp_read_video_metadata' ) ) {
175 require_once ABSPATH . '/wp-admin/includes/media.php';
176 }
177
178 wp_update_attachment_metadata(
179 $post_id,
180 wp_generate_attachment_metadata( $post_id, $upload['file'] )
181 );
182 update_post_meta( $post_id, '_elementor_source_image_hash', $this->get_hash_image( $attachment['url'] ) );
183
184 $new_attachment = [
185 'id' => $post_id,
186 'url' => $upload['url'],
187 ];
188
189 if ( ! empty( $attachment['id'] ) ) {
190 $this->_replace_image_ids[ $attachment['id'] ] = $new_attachment;
191 }
192
193 return $new_attachment;
194 }
195
196 /**
197 * Template library import images constructor.
198 *
199 * Initializing the images import class used by the template library through
200 * the WordPress Filesystem API.
201 *
202 * @since 1.0.0
203 * @access public
204 */
205 public function __construct() {
206 if ( ! function_exists( 'WP_Filesystem' ) ) {
207 require_once ABSPATH . 'wp-admin/includes/file.php';
208 }
209
210 WP_Filesystem();
211 }
212 }
213