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