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